From 0231675abf3b467cff49d1976df882931d91019d Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 23 Feb 2011 22:27:55 +0000 Subject: add resource handling move gtk and framebuffer to use generic resource handling svn path=/trunk/netsurf/; revision=11772 --- content/fetch.c | 2 + content/fetchers/about.c | 44 +------ content/fetchers/resource.c | 277 ++++++++++++++++++++++++++++++++++++++++++++ content/fetchers/resource.h | 38 ++++++ content/llcache.c | 25 +++- 5 files changed, 341 insertions(+), 45 deletions(-) create mode 100644 content/fetchers/resource.c create mode 100644 content/fetchers/resource.h (limited to 'content') diff --git a/content/fetch.c b/content/fetch.c index a848e17fa..e521ca2fc 100644 --- a/content/fetch.c +++ b/content/fetch.c @@ -38,6 +38,7 @@ #include "utils/config.h" #include "content/fetch.h" +#include "content/fetchers/resource.h" #include "content/fetchers/about.h" #include "content/fetchers/curl.h" #include "content/fetchers/data.h" @@ -112,6 +113,7 @@ void fetch_init(void) fetch_curl_register(); fetch_data_register(); fetch_file_register(); + fetch_resource_register(); fetch_about_register(); fetch_active = false; } diff --git a/content/fetchers/about.c b/content/fetchers/about.c index 8ed112200..c4a376a29 100644 --- a/content/fetchers/about.c +++ b/content/fetchers/about.c @@ -127,52 +127,16 @@ fetch_about_blank_handler_aborted: return false; } -static const char *authors[] = { - "John-Mark Bell", "James Bursa", "Michael Drake", - "Rob Kendrick", "Adrian Lees", "Vincent Sanders", - "Daniel Silverstone", "Richard Wilson", NULL -}; static bool fetch_about_credits_handler(struct fetch_about_context *ctx) { - char buffer[4096]; - int code = 200; - int slen; - int auth_loop = 0; - - /* content is going to return ok */ - fetch_set_http_code(ctx->fetchh, code); + /* content is going to return redirect */ + fetch_set_http_code(ctx->fetchh, 302); - /* content type */ - if (fetch_about_send_header(ctx, "Content-Type: text/html")) - goto fetch_about_credits_handler_aborted; - - slen = snprintf(buffer, sizeof buffer, - "NetSurf Browser Credits" - "

NetSurf Browser Credits

" - "

Authors

" - ""); - - if (fetch_about_send_callback(FETCH_DATA, ctx, buffer, slen, - FETCH_ERROR_NO_ERROR)) - goto fetch_about_credits_handler_aborted; - - fetch_about_send_callback(FETCH_FINISHED, ctx, 0, 0, - FETCH_ERROR_NO_ERROR); + fetch_about_send_callback(FETCH_REDIRECT, ctx, "resource:credits.html", + 0, FETCH_ERROR_NO_ERROR); return true; - -fetch_about_credits_handler_aborted: - return false; } static bool fetch_about_config_handler(struct fetch_about_context *ctx) diff --git a/content/fetchers/resource.c b/content/fetchers/resource.c new file mode 100644 index 000000000..2818305d0 --- /dev/null +++ b/content/fetchers/resource.c @@ -0,0 +1,277 @@ +/* + * Copyright 2011 Vincent Sanders + * + * This file is part of NetSurf. + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/* resource: URL handling. Based on the data fetcher by Rob Kendrick */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "utils/config.h" +#include "content/dirlist.h" +#include "content/fetch.h" +#include "content/fetchers/resource.h" +#include "content/urldb.h" +#include "desktop/netsurf.h" +#include "desktop/options.h" +#include "utils/log.h" +#include "utils/messages.h" +#include "utils/url.h" +#include "utils/utils.h" +#include "utils/ring.h" + +struct fetch_resource_context; + +typedef bool (*fetch_resource_handler)(struct fetch_resource_context *); + +/** Context for an resource fetch */ +struct fetch_resource_context { + struct fetch_resource_context *r_next, *r_prev; + + struct fetch *fetchh; /**< Handle for this fetch */ + + bool aborted; /**< Flag indicating fetch has been aborted */ + bool locked; /**< Flag indicating entry is already entered */ + + char *url; + char *redirect_url; /**< The url the fetch redirects to */ + + fetch_resource_handler handler; +}; + +static struct fetch_resource_context *ring = NULL; + +/** issue fetch callbacks with locking */ +static inline bool fetch_resource_send_callback(fetch_msg msg, + struct fetch_resource_context *ctx, const void *data, + unsigned long size, fetch_error_code errorcode) +{ + ctx->locked = true; + fetch_send_callback(msg, ctx->fetchh, data, size, errorcode); + ctx->locked = false; + + return ctx->aborted; +} + +static bool fetch_resource_send_header(struct fetch_resource_context *ctx, + const char *fmt, ...) +{ + char header[64]; + va_list ap; + + va_start(ap, fmt); + + vsnprintf(header, sizeof header, fmt, ap); + + va_end(ap); + + fetch_resource_send_callback(FETCH_HEADER, ctx, header, strlen(header), + FETCH_ERROR_NO_ERROR); + + return ctx->aborted; +} + + + + +static bool fetch_resource_redirect_handler(struct fetch_resource_context *ctx) +{ + /* content is going to return redirect */ + fetch_set_http_code(ctx->fetchh, 302); + + fetch_resource_send_callback(FETCH_REDIRECT, ctx, ctx->redirect_url, 0, + FETCH_ERROR_NO_ERROR); + + return true; +} + + +static bool fetch_resource_notfound_handler(struct fetch_resource_context *ctx) +{ + int code = 404; + char buffer[1024]; + const char *title; + char key[8]; + + /* content is going to return error code */ + fetch_set_http_code(ctx->fetchh, code); + + /* content type */ + if (fetch_resource_send_header(ctx, "Content-Type: text/html")) + goto fetch_resource_notfound_handler_aborted; + + snprintf(key, sizeof key, "HTTP%03d", code); + title = messages_get(key); + + snprintf(buffer, sizeof buffer, "%s" + "

%s

" + "

Error %d while fetching file %s

", + title, title, code, ctx->url); + + if (fetch_resource_send_callback(FETCH_DATA, ctx, buffer, strlen(buffer), + FETCH_ERROR_NO_ERROR)) + goto fetch_resource_notfound_handler_aborted; + + fetch_resource_send_callback(FETCH_FINISHED, ctx, 0, 0, + FETCH_ERROR_NO_ERROR); + +fetch_resource_notfound_handler_aborted: + return false; +} + + + +/** callback to initialise the resource fetcher. */ +static bool fetch_resource_initialise(const char *scheme) +{ + return true; +} + +/** callback to initialise the resource fetcher. */ +static void fetch_resource_finalise(const char *scheme) +{ +} + +/** callback to set up a resource fetch context. */ +static void * +fetch_resource_setup(struct fetch *fetchh, + const char *url, + bool only_2xx, + const char *post_urlenc, + const struct fetch_multipart_data *post_multipart, + const char **headers) +{ + struct fetch_resource_context *ctx; + struct url_components urlcomp; + + ctx = calloc(1, sizeof(*ctx)); + if (ctx == NULL) + return NULL; + + url_get_components(url, &urlcomp); + + ctx->redirect_url = gui_find_resource(urlcomp.path); + if (ctx->redirect_url == NULL) { + ctx->handler = fetch_resource_notfound_handler; + } else { + ctx->handler = fetch_resource_redirect_handler; + } + + ctx->url = strdup(url); + + url_destroy_components(&urlcomp); + + ctx->fetchh = fetchh; + + RING_INSERT(ring, ctx); + + return ctx; +} + +/** callback to free a resource fetch */ +static void fetch_resource_free(void *ctx) +{ + struct fetch_resource_context *c = ctx; + free(c->redirect_url); + free(c->url); + RING_REMOVE(ring, c); + free(ctx); +} + +/** callback to start a resource fetch */ +static bool fetch_resource_start(void *ctx) +{ + return true; +} + +/** callback to abort a resource fetch */ +static void fetch_resource_abort(void *ctx) +{ + struct fetch_resource_context *c = ctx; + + /* To avoid the poll loop having to deal with the fetch context + * disappearing from under it, we simply flag the abort here. + * The poll loop itself will perform the appropriate cleanup. + */ + c->aborted = true; +} + + +/** callback to poll for additional resource fetch contents */ +static void fetch_resource_poll(const char *scheme) +{ + struct fetch_resource_context *c, *next; + + if (ring == NULL) return; + + /* Iterate over ring, processing each pending fetch */ + c = ring; + do { + /* Take a copy of the next pointer as we may destroy + * the ring item we're currently processing */ + next = c->r_next; + + /* Ignore fetches that have been flagged as locked. + * This allows safe re-entrant calls to this function. + * Re-entrancy can occur if, as a result of a callback, + * the interested party causes fetch_poll() to be called + * again. + */ + if (c->locked == true) { + continue; + } + + /* Only process non-aborted fetches */ + if (c->aborted == false) { + /* resource fetches can be processed in one go */ + c->handler(c); + } + + + fetch_remove_from_queues(c->fetchh); + fetch_free(c->fetchh); + + /* Advance to next ring entry, exiting if we've reached + * the start of the ring or the ring has become empty + */ + } while ( (c = next) != ring && ring != NULL); +} + +void fetch_resource_register(void) +{ + fetch_add_fetcher("resource", + fetch_resource_initialise, + fetch_resource_setup, + fetch_resource_start, + fetch_resource_abort, + fetch_resource_free, + fetch_resource_poll, + fetch_resource_finalise); +} diff --git a/content/fetchers/resource.h b/content/fetchers/resource.h new file mode 100644 index 000000000..6c590d390 --- /dev/null +++ b/content/fetchers/resource.h @@ -0,0 +1,38 @@ +/* + * Copyright 2011 Vincent Sanders + * + * This file is part of NetSurf. + * + * NetSurf is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * NetSurf is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** \file + * resource: URL method handler + */ + +#ifndef NETSURF_CONTENT_FETCHERS_FETCH_RESOURCE_H +#define NETSURF_CONTENT_FETCHERS_FETCH_RESOURCE_H + +/** + * register the resource scheme. + * + * should only be called from the fetch initialise + */ +void fetch_resource_register(void); + +/** + * callback to translate resource to full url + */ +char* gui_find_resource(const char *filename); + +#endif diff --git a/content/llcache.c b/content/llcache.c index 64e37915a..a7c8cbe06 100644 --- a/content/llcache.c +++ b/content/llcache.c @@ -1952,6 +1952,7 @@ nserror llcache_fetch_redirect(llcache_object *object, const char *target, const llcache_post_data *post = object->fetch.post; char *url, *absurl; char *scheme; + char *object_scheme; url_func_result result; /* Extract HTTP response code from the fetch object */ long http_code = fetch_http_code(object->fetch.fetch); @@ -1999,20 +2000,34 @@ nserror llcache_fetch_redirect(llcache_object *object, const char *target, return NSERROR_NOMEM; } - /* Ensure that redirects to file:/// don't happen */ - result = url_scheme(url, &scheme); + /* Ensure that redirects to file:/// only happen for valid schemes */ + result = url_scheme(object->url, &object_scheme); if (result != URL_FUNC_OK) { free(url); return NSERROR_NOMEM; } - if (strcasecmp(scheme, "file") == 0) { - free(scheme); + result = url_scheme(url, &scheme); + if (result != URL_FUNC_OK) { free(url); - return NSERROR_OK; + return NSERROR_NOMEM; + } + + /* resource is allowed to redirect anywhere */ + if ((strcasecmp(object_scheme, "resource") != 0) && + (strcasecmp(object_scheme, "about") != 0)) { + /* file, about and resource are not valid redirect targets */ + if ((strcasecmp(scheme, "file") == 0) || + (strcasecmp(scheme, "about") == 0) || + (strcasecmp(scheme, "resource") == 0)) { + free(scheme); + free(url); + return NSERROR_OK; + } } free(scheme); + free(object_scheme); /* Bail out if we've no way of handling this URL */ if (fetch_can_fetch(url) == false) { -- cgit v1.2.3