[libsoup/content-sniffing] Implementing the images sniffing path



commit 8712d44b78926419aff6bffe64045f024b97dbd2
Author: Gustavo Noronha Silva <gns gnome org>
Date:   Thu Jun 18 22:48:38 2009 -0300

    Implementing the images sniffing path
    
    This part of the algorithm is basically checking against the type
    table just for image types.

 libsoup/soup-content-sniffer.c |   43 ++++++++++++++++++++++++++++++++++++++++
 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 835a9dd..28c8d48 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 93990bd..785b62e 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);
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]