libsoup r1146 - in trunk: . libsoup tests



Author: danw
Date: Sat Jul 26 14:08:31 2008
New Revision: 1146
URL: http://svn.gnome.org/viewvc/libsoup?rev=1146&view=rev

Log:
	* libsoup/soup-message-client-io.c (get_request_headers): don't
	add a Host header to the message if the caller already added one.
	#539803, Marc Maurer.

	* libsoup/soup-logger.c (print_request): likewise

	* tests/misc-test.c: new test file for small miscellaneous test
	cases.
	(do_host_test): test Host-header overriding


Added:
   trunk/tests/misc-test.c
Modified:
   trunk/ChangeLog
   trunk/libsoup/soup-logger.c
   trunk/libsoup/soup-message-client-io.c
   trunk/tests/   (props changed)
   trunk/tests/Makefile.am

Modified: trunk/libsoup/soup-logger.c
==============================================================================
--- trunk/libsoup/soup-logger.c	(original)
+++ trunk/libsoup/soup-logger.c	Sat Jul 26 14:08:31 2008
@@ -476,8 +476,12 @@
 	if (log_level == SOUP_LOGGER_LOG_MINIMAL)
 		return;
 
-	soup_logger_print (logger, SOUP_LOGGER_LOG_HEADERS, '>',
-			   "Host: %s", uri->host);
+	if (!soup_message_headers_get (msg->request_headers, "Host")) {
+		soup_logger_print (logger, SOUP_LOGGER_LOG_HEADERS, '>',
+				   "Host: %s%c%u", uri->host,
+				   soup_uri_uses_default_port (uri) ? '\0' : ':',
+				   uri->port);
+	}
 	soup_message_headers_iter_init (&iter, msg->request_headers);
 	while (soup_message_headers_iter_next (&iter, &name, &value)) {
 		if (!g_ascii_strcasecmp (name, "Authorization") &&

Modified: trunk/libsoup/soup-message-client-io.c
==============================================================================
--- trunk/libsoup/soup-message-client-io.c	(original)
+++ trunk/libsoup/soup-message-client-io.c	Sat Jul 26 14:08:31 2008
@@ -92,12 +92,14 @@
 	} else {
 		g_string_append_printf (header, "%s %s HTTP/1.1\r\n",
 					req->method, uri_string);
-		if (soup_uri_uses_default_port (uri)) {
-			g_string_append_printf (header, "Host: %s\r\n",
-						uri_host);
-		} else {
-			g_string_append_printf (header, "Host: %s:%d\r\n",
-						uri_host, uri->port);
+		if (!soup_message_headers_get (req->request_headers, "Host")) {
+			if (soup_uri_uses_default_port (uri)) {
+				g_string_append_printf (header, "Host: %s\r\n",
+							uri_host);
+			} else {
+				g_string_append_printf (header, "Host: %s:%d\r\n",
+							uri_host, uri->port);
+			}
 		}
 	}
 	g_free (uri_string);

Modified: trunk/tests/Makefile.am
==============================================================================
--- trunk/tests/Makefile.am	(original)
+++ trunk/tests/Makefile.am	Sat Jul 26 14:08:31 2008
@@ -19,6 +19,7 @@
 	get		\
 	getbug		\
 	header-parsing  \
+	misc-test	\
 	ntlm-test	\
 	redirect-test	\
 	simple-httpd	\
@@ -40,6 +41,7 @@
 get_SOURCES = get.c
 getbug_SOURCES = getbug.c
 header_parsing_SOURCES = header-parsing.c $(TEST_SRCS)
+misc_test_SOURCES = misc-test.c $(TEST_SRCS)
 ntlm_test_SOURCES = ntlm-test.c $(TEST_SRCS)
 proxy_test_SOURCES = proxy-test.c $(TEST_SRCS)
 pull_api_SOURCES = pull-api.c $(TEST_SRCS)
@@ -72,6 +74,7 @@
 	continue-test	\
 	date		\
 	header-parsing	\
+	misc-test	\
 	ntlm-test	\
 	redirect-test	\
 	uri-parsing	\

Added: trunk/tests/misc-test.c
==============================================================================
--- (empty file)
+++ trunk/tests/misc-test.c	Sat Jul 26 14:08:31 2008
@@ -0,0 +1,112 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2007 Red Hat, Inc.
+ */
+
+#include <ctype.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <libsoup/soup.h>
+
+#include "test-utils.h"
+
+static char *base_uri;
+
+static void
+server_callback (SoupServer *server, SoupMessage *msg,
+		 const char *path, GHashTable *query,
+		 SoupClientContext *context, gpointer data)
+{
+	SoupURI *uri = soup_message_get_uri (msg);
+
+	if (msg->method != SOUP_METHOD_GET) {
+		soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
+		return;
+	}
+
+	soup_message_set_status (msg, SOUP_STATUS_OK);
+	if (!strcmp (uri->host, "foo")) {
+		soup_message_set_response (msg, "text/plain",
+					   SOUP_MEMORY_STATIC, "foo-index", 9);
+		return;
+	} else {
+		soup_message_set_response (msg, "text/plain",
+					   SOUP_MEMORY_STATIC, "index", 5);
+		return;
+	}
+}
+
+/* Host header handling: client must be able to override the default
+ * value, server must be able to recognize different Host values.
+ */
+
+static void
+do_host_test (void)
+{
+	SoupSession *session;
+	SoupMessage *one, *two;
+
+	debug_printf (1, "Host handling\n");
+
+	session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC, NULL);
+
+	one = soup_message_new ("GET", base_uri);
+	two = soup_message_new ("GET", base_uri);
+	soup_message_headers_replace (two->request_headers, "Host", "foo");
+
+	soup_session_send_message (session, one);
+	soup_session_send_message (session, two);
+
+	soup_session_abort (session);
+	g_object_unref (session);
+
+	if (!SOUP_STATUS_IS_SUCCESSFUL (one->status_code)) {
+		debug_printf (1, "  Message 1 failed: %d %s\n",
+			      one->status_code, one->reason_phrase);
+		errors++;
+	} else if (strcmp (one->response_body->data, "index") != 0) {
+		debug_printf (1, "  Unexpected response to message 1: '%s'\n",
+			      one->response_body->data);
+		errors++;
+	}
+	g_object_unref (one);
+
+	if (!SOUP_STATUS_IS_SUCCESSFUL (two->status_code)) {
+		debug_printf (1, "  Message 2 failed: %d %s\n",
+			      two->status_code, two->reason_phrase);
+		errors++;
+	} else if (strcmp (two->response_body->data, "foo-index") != 0) {
+		debug_printf (1, "  Unexpected response to message 2: '%s'\n",
+			      two->response_body->data);
+		errors++;
+	}
+	g_object_unref (two);
+}
+
+int
+main (int argc, char **argv)
+{
+	SoupServer *server;
+
+	test_init (argc, argv, NULL);
+
+	server = soup_test_server_new (TRUE);
+	soup_server_add_handler (server, NULL, server_callback, NULL, NULL);
+	base_uri = g_strdup_printf ("http://localhost:%u/";,
+				    soup_server_get_port (server));
+
+	do_host_test ();
+
+	g_free (base_uri);
+
+	test_cleanup ();
+	return errors != 0;
+}



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