From 69f02e926f746b31500f46572d606fdc482ff0cb Mon Sep 17 00:00:00 2001 From: John Mark Bell Date: Sun, 4 Apr 2010 17:17:24 +0000 Subject: Make the high-level cache drop contents of unacceptable type on the floor. svn path=/trunk/netsurf/; revision=10238 --- content/hlcache.c | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- content/hlcache.h | 22 +++++++++------- 2 files changed, 87 insertions(+), 13 deletions(-) (limited to 'content') diff --git a/content/hlcache.c b/content/hlcache.c index 05a416b0b..298fe8151 100644 --- a/content/hlcache.c +++ b/content/hlcache.c @@ -26,7 +26,9 @@ #include "content/content.h" #include "content/hlcache.h" +#include "utils/http.h" #include "utils/log.h" +#include "utils/messages.h" #include "utils/url.h" typedef struct hlcache_entry hlcache_entry; @@ -38,6 +40,8 @@ struct hlcache_retrieval_ctx { hlcache_handle *handle; /**< High-level handle for object */ + const content_type *accepted_types; /**< Accepted types, or NULL */ + hlcache_child_context child; /**< Child context */ }; @@ -62,6 +66,8 @@ static hlcache_entry *hlcache_content_list; static nserror hlcache_llcache_callback(llcache_handle *handle, const llcache_event *event, void *pw); +static bool hlcache_type_is_acceptable(llcache_handle *llcache, + const content_type *accepted_types); static nserror hlcache_find_content(hlcache_retrieval_ctx *ctx); static void hlcache_content_callback(struct content *c, content_msg msg, union content_msg_data data, void *pw); @@ -74,7 +80,8 @@ static void hlcache_content_callback(struct content *c, nserror hlcache_handle_retrieve(const char *url, uint32_t flags, const char *referer, llcache_post_data *post, hlcache_handle_callback cb, void *pw, - hlcache_child_context *child, hlcache_handle **result) + hlcache_child_context *child, + const content_type *accepted_types, hlcache_handle **result) { hlcache_retrieval_ctx *ctx; nserror error; @@ -97,6 +104,8 @@ nserror hlcache_handle_retrieve(const char *url, uint32_t flags, ctx->child.quirks = child->quirks; } + ctx->accepted_types = accepted_types; + ctx->handle->cb = cb; ctx->handle->pw = pw; @@ -218,9 +227,26 @@ nserror hlcache_llcache_callback(llcache_handle *handle, switch (event->type) { case LLCACHE_EVENT_HAD_HEADERS: - error = hlcache_find_content(ctx); - if (error != NSERROR_OK) - return error; + if (hlcache_type_is_acceptable(handle, ctx->accepted_types)) { + error = hlcache_find_content(ctx); + if (error != NSERROR_OK) + return error; + } else { + /* Unacceptable type: abort fetch and report error */ + llcache_handle_abort(handle); + llcache_handle_release(handle); + + if (ctx->handle->cb != NULL) { + hlcache_event hlevent; + + hlevent.type = CONTENT_MSG_ERROR; + hlevent.data.error = messages_get("BadType"); + + ctx->handle->cb(ctx->handle, &hlevent, + ctx->handle->pw); + } + } + /* No longer require retrieval context */ free(ctx); break; @@ -246,6 +272,50 @@ nserror hlcache_llcache_callback(llcache_handle *handle, return NSERROR_OK; } +/** + * Determine if the type of a low-level cache object is acceptable + * + * \param llcache Low-level cache object to consider + * \param accepted_types Array of acceptable types, or NULL for any + * \return True if the type is acceptable, false otherwise + */ +bool hlcache_type_is_acceptable(llcache_handle *llcache, + const content_type *accepted_types) +{ + const char *content_type_header; + char *mime_type; + http_parameter *params; + content_type type; + nserror error; + + if (accepted_types == NULL) + return true; + + content_type_header = + llcache_handle_get_header(llcache, "Content-Type"); + if (content_type_header == NULL) + content_type_header = "text/plain"; + + error = http_parse_content_type(content_type_header, &mime_type, + ¶ms); + if (error != NSERROR_OK) + return false; + + type = content_lookup(mime_type); + + free(mime_type); + http_parameter_list_destroy(params); + + while (*accepted_types != CONTENT_UNKNOWN) { + if (*accepted_types == type) + break; + + accepted_types++; + } + + return *accepted_types == type; +} + /** * Find a content for the high-level cache handle * diff --git a/content/hlcache.h b/content/hlcache.h index 5ef42a301..1208b15c4 100644 --- a/content/hlcache.h +++ b/content/hlcache.h @@ -56,20 +56,23 @@ typedef nserror (*hlcache_handle_callback)(hlcache_handle *handle, /** * Retrieve a high-level cache handle for an object * - * \param url URL of the object to retrieve handle for - * \param flags Object retrieval flags - * \param referer Referring URL, or NULL if none - * \param post POST data, or NULL for a GET request - * \param cb Callback to handle object events - * \param pw Pointer to client-specific data for callback - * \param child Child retrieval context, or NULL for top-level content - * \param result Pointer to location to recieve cache handle + * \param url URL of the object to retrieve handle for + * \param flags Object retrieval flags + * \param referer Referring URL, or NULL if none + * \param post POST data, or NULL for a GET request + * \param cb Callback to handle object events + * \param pw Pointer to client-specific data for callback + * \param child Child retrieval context, or NULL for top-level content + * \param accepted_types Array of acceptable content types, or NULL for any + * \param result Pointer to location to recieve cache handle * \return NSERROR_OK on success, appropriate error otherwise * * Child contents are keyed on the tuple < URL, quirks >. * The quirks field is ignored for child contents whose behaviour is not * affected by quirks mode. * + * The \a accepted_types array must be terminated with CONTENT_UNKNOWN + * * \todo The above rules should be encoded in the handler_map. * * \todo Is there any way to sensibly reduce the number of parameters here? @@ -77,7 +80,8 @@ typedef nserror (*hlcache_handle_callback)(hlcache_handle *handle, nserror hlcache_handle_retrieve(const char *url, uint32_t flags, const char *referer, llcache_post_data *post, hlcache_handle_callback cb, void *pw, - hlcache_child_context *child, hlcache_handle **result); + hlcache_child_context *child, + const content_type *accepted_types, hlcache_handle **result); /** * Release a high-level cache handle -- cgit v1.2.3