Bypass full type sniffer if only images are acceptable

svn path=/trunk/netsurf/; revision=13436
This commit is contained in:
John Mark Bell 2012-02-08 00:52:16 +00:00
parent e2bb4aa4e0
commit 3e2e110a86
3 changed files with 25 additions and 3 deletions

View File

@ -522,6 +522,7 @@ nserror hlcache_llcache_callback(llcache_handle *handle,
case LLCACHE_EVENT_HAD_HEADERS:
error = mimesniff_compute_effective_type(handle, NULL, 0,
ctx->flags & HLCACHE_RETRIEVE_SNIFF_TYPE,
ctx->accepted_types == CONTENT_IMAGE,
&effective_type);
if (error == NSERROR_OK || error == NSERROR_NOT_FOUND) {
/* If the sniffer was successful or failed to find
@ -545,6 +546,7 @@ nserror hlcache_llcache_callback(llcache_handle *handle,
error = mimesniff_compute_effective_type(handle,
event->data.data.buf, event->data.data.len,
ctx->flags & HLCACHE_RETRIEVE_SNIFF_TYPE,
ctx->accepted_types == CONTENT_IMAGE,
&effective_type);
if (error != NSERROR_OK) {
assert(0 && "MIME sniff failed with data");
@ -561,7 +563,7 @@ nserror hlcache_llcache_callback(llcache_handle *handle,
/* DONE event before we could determine the effective MIME type.
*/
error = mimesniff_compute_effective_type(handle,
NULL, 0, false, &effective_type);
NULL, 0, false, false, &effective_type);
if (error == NSERROR_OK) {
error = hlcache_migrate_ctx(ctx, effective_type);

View File

@ -62,6 +62,7 @@ static lwc_string *application_x_gzip;
static lwc_string *application_postscript;
static lwc_string *application_pdf;
static lwc_string *video_mp4;
static lwc_string *image_svg;
nserror mimesniff_init(void)
{
@ -97,6 +98,7 @@ nserror mimesniff_init(void)
SINIT(application_postscript, "application/postscript");
SINIT(application_pdf, "application/pdf");
SINIT(video_mp4, "video/mp4");
SINIT(image_svg, "image/svg+xml");
#undef SINIT
return NSERROR_OK;
@ -104,6 +106,7 @@ nserror mimesniff_init(void)
void mimesniff_fini(void)
{
lwc_string_unref(image_svg);
lwc_string_unref(video_mp4);
lwc_string_unref(application_pdf);
lwc_string_unref(application_postscript);
@ -636,7 +639,7 @@ static nserror mimesniff__compute_feed_or_html(const uint8_t *data,
/* See mimesniff.h for documentation */
nserror mimesniff_compute_effective_type(llcache_handle *handle,
const uint8_t *data, size_t len, bool sniff_allowed,
lwc_string **effective_type)
bool image_only, lwc_string **effective_type)
{
#define S(s) { s, SLEN(s) }
static const struct tt_s {
@ -683,6 +686,22 @@ nserror mimesniff_compute_effective_type(llcache_handle *handle,
return NSERROR_OK;
}
if (image_only) {
lwc_string *official_type;
if (lwc_string_caseless_isequal(ct->media_type, image_svg,
&match) == lwc_error_ok && match) {
*effective_type = lwc_string_ref(image_svg);
http_content_type_destroy(ct);
return NSERROR_OK;
}
official_type = lwc_string_ref(ct->media_type);
http_content_type_destroy(ct);
return mimesniff__compute_image(official_type,
data, len, effective_type);
}
content_type_header_len = strlen(content_type_header);
/* Look for text types */

View File

@ -38,6 +38,7 @@ struct llcache_handle;
* \param data First data chunk, or NULL
* \param len Length of \a data, in bytes
* \param sniff_allowed Whether MIME type sniffing is allowed
* \param image_only Sniff image types only
* \param effective_type Location to receive computed type
* \return NSERROR_OK on success,
* NSERROR_NEED_DATA iff \a data is NULL and data is needed
@ -46,7 +47,7 @@ struct llcache_handle;
*/
nserror mimesniff_compute_effective_type(struct llcache_handle *handle,
const uint8_t *data, size_t len, bool sniff_allowed,
lwc_string **effective_type);
bool image_only, lwc_string **effective_type);
nserror mimesniff_init(void);
void mimesniff_fini(void);