diff options
| author | Gustavo Noronha Silva <gns@gnome.org> | 2009-06-18 22:48:38 -0300 |
|---|---|---|
| committer | Gustavo Noronha Silva <gns@gnome.org> | 2009-06-18 22:51:47 -0300 |
| commit | 8712d44b78926419aff6bffe64045f024b97dbd2 (patch) | |
| tree | 4434573b94c1d619ed778db9d0460c4274771f20 | |
| parent | 259b9597c8ae78c12068cb63b5b13cc52f088ff3 (diff) | |
| download | libsoup-8712d44b78926419aff6bffe64045f024b97dbd2.tar.gz | |
Implementing the images sniffing path
This part of the algorithm is basically checking against the type
table just for image types.
| -rw-r--r-- | libsoup/soup-content-sniffer.c | 43 | ||||
| -rw-r--r-- | tests/sniffing-test.c | 12 |
2 files changed, 51 insertions, 4 deletions
diff --git a/libsoup/soup-content-sniffer.c b/libsoup/soup-content-sniffer.c index 835a9dd9..28c8d48a 100644 --- a/libsoup/soup-content-sniffer.c +++ b/libsoup/soup-content-sniffer.c @@ -330,6 +330,36 @@ sniff_text_or_binary (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer } static char* +sniff_images (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer *buffer, const char *content_type) +{ + const char *resource = buffer->data; + int resource_length = MIN(512, buffer->length); + int i; + + for (i = 0; types_table[i].pattern != NULL ; i++) { + struct _type_info *type_row = &(types_table[i]); + int j; + + if (resource_length < type_row->pattern_length) + continue; + + if (!g_str_has_prefix (type_row->sniffed_type, "image/")) + continue; + + for (j = 0; j < type_row->pattern_length; j++) { + if (resource[j] != type_row->pattern[j]) + break; + } + + /* This means our comparison above matched completely */ + if (j == type_row->pattern_length) + return g_strdup (type_row->sniffed_type); + } + + return g_strdup (content_type); +} + +static char* sniff_gio (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer *buffer) { SoupURI *uri; @@ -370,6 +400,19 @@ sniff (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer *buffer) !g_ascii_strcasecmp (content_type, "application/xml")) return g_strdup (content_type); + /* 2.7.5 Content-Type sniffing: image + * The spec says: + * + * If the resource's official type is "image/svg+xml", then + * the sniffed type of the resource is its official type (an + * XML type) + * + * The XML case is handled by the if above; if you refactor + * this code, keep this in mind. + */ + if (!g_ascii_strncasecmp (content_type, "image/", 6)) + return sniff_images (sniffer, msg, buffer, content_type); + content_type_with_params = soup_message_headers_get_one (msg->response_headers, "Content-Type"); /* If we got text/plain, use text_or_binary */ diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c index 93990bd2..785b62ee 100644 --- a/tests/sniffing-test.c +++ b/tests/sniffing-test.c @@ -94,7 +94,7 @@ server_callback (SoupServer *server, SoupMessage *msg, length); } - if (g_str_has_prefix (path, "/xml/")) { + if (g_str_has_prefix (path, "/type/")) { char **components = g_strsplit (path, "/", 4); char *ptr; @@ -357,9 +357,13 @@ main (int argc, char **argv) /* Test the XML sniffing path */ - test_sniffing ("/xml/text_xml/home.gif", "text/xml"); - test_sniffing ("/xml/anice_type+xml/home.gif", "anice/type+xml"); - test_sniffing ("/xml/application_xml/home.gif", "application/xml"); + test_sniffing ("/type/text_xml/home.gif", "text/xml"); + test_sniffing ("/type/anice_type+xml/home.gif", "anice/type+xml"); + test_sniffing ("/type/application_xml/home.gif", "application/xml"); + + /* Test the image sniffing path */ + + test_sniffing ("/type/image_png/home.gif", "image/gif"); soup_uri_free (base_uri); |
