[libsoup/content-sniffing] Implement XML-based types sniffing



commit 259b9597c8ae78c12068cb63b5b13cc52f088ff3
Author: Gustavo Noronha Silva <gns gnome org>
Date:   Thu Jun 18 22:35:08 2009 -0300

    Implement XML-based types sniffing
    
    Basically, any XML type will be trusted as being correctly given by
    the server.

 libsoup/soup-content-sniffer.c |   18 ++++++++----------
 tests/sniffing-test.c          |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 10 deletions(-)
---
diff --git a/libsoup/soup-content-sniffer.c b/libsoup/soup-content-sniffer.c
index a7a51f9..835a9dd 100644
--- a/libsoup/soup-content-sniffer.c
+++ b/libsoup/soup-content-sniffer.c
@@ -365,6 +365,11 @@ sniff (SoupContentSniffer *sniffer, SoupMessage *msg, SoupBuffer *buffer)
 	    !g_ascii_strcasecmp (content_type, "application/unknown"))
 		return sniff_unknown (sniffer, msg, buffer, FALSE);
 
+	if (g_str_has_suffix (content_type, "+xml") ||
+	    !g_ascii_strcasecmp (content_type, "text/xml") ||
+	    !g_ascii_strcasecmp (content_type, "application/xml"))
+		return g_strdup (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 */
@@ -389,16 +394,9 @@ soup_content_sniffer_got_headers_cb (SoupMessage *msg, SoupContentSniffer *sniff
 {
 	SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
 	SoupContentSnifferClass *content_sniffer_class = SOUP_CONTENT_SNIFFER_GET_CLASS (sniffer);
-	const char *content_type = soup_message_headers_get_content_type (msg->response_headers, NULL);
-
-	if ((content_type == NULL)
-	    || (g_ascii_strcasecmp (content_type, "application/octet-stream") == 0)
-	    || (g_ascii_strcasecmp (content_type, "text/plain") == 0)
-	    || (g_ascii_strcasecmp (content_type, "unknown/unknown") == 0)
-	    || (g_ascii_strcasecmp (content_type, "application/unknown") == 0)) {
-		priv->should_sniff_content = TRUE;
-		priv->bytes_for_sniffing = content_sniffer_class->get_buffer_size (sniffer);
-	}
+
+	priv->should_sniff_content = TRUE;
+	priv->bytes_for_sniffing = content_sniffer_class->get_buffer_size (sniffer);
 }
 
 static void
diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c
index a23ab3a..93990bd 100644
--- a/tests/sniffing-test.c
+++ b/tests/sniffing-test.c
@@ -93,6 +93,38 @@ server_callback (SoupServer *server, SoupMessage *msg,
 					   contents,
 					   length);
 	}
+
+	if (g_str_has_prefix (path, "/xml/")) {
+		char **components = g_strsplit (path, "/", 4);
+		char *ptr;
+
+		char *base_name = g_path_get_basename (path);
+		char *file_name = g_strdup_printf ("resources/%s", base_name);
+
+		g_file_get_contents (file_name,
+				     &contents, &length,
+				     &error);
+
+		g_free (base_name);
+		g_free (file_name);
+
+		if (error) {
+			g_error ("%s", error->message);
+			g_error_free (error);
+			exit (1);
+		}
+
+		/* Hack to allow passing type in the URI */
+		ptr = g_strrstr (components[2], "_");
+		*ptr = '/';
+
+		soup_message_set_response (msg, components[2],
+					   SOUP_MEMORY_TAKE,
+					   contents,
+					   length);
+
+		g_strfreev (components);
+	}
 }
 
 static gboolean
@@ -323,6 +355,12 @@ main (int argc, char **argv)
 	test_sniffing ("/unknown/home.gif", "image/gif");
 	test_sniffing ("/unknown/mbox", "application/octet-stream");
 
+	/* 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");
+
 	soup_uri_free (base_uri);
 
 	test_cleanup ();



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