[libsoup] soup-cache: do not store hop-by-hop headers.



commit 023d3b5e5e0bd0f1876071687c66a5228dfe37ff
Author: Sergio Villar Senin <svillar igalia com>
Date:   Thu May 19 10:19:13 2011 +0200

    soup-cache: do not store hop-by-hop headers.
    
    Those headers are meaningful only for single transport-level connections,
    we should not store them.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=650094

 libsoup/soup-cache.c           |   33 +++++++++++++--------------------
 libsoup/soup-headers.c         |   23 ++++-------------------
 libsoup/soup-message-headers.c |   24 ++++++++++++++++++++++++
 libsoup/soup-message-headers.h |    2 ++
 4 files changed, 43 insertions(+), 39 deletions(-)
---
diff --git a/libsoup/soup-cache.c b/libsoup/soup-cache.c
index ca9108a..70d865a 100644
--- a/libsoup/soup-cache.c
+++ b/libsoup/soup-cache.c
@@ -260,13 +260,17 @@ copy_headers (const char *name, const char *value, SoupMessageHeaders *headers)
 	soup_message_headers_append (headers, name, value);
 }
 
+static char *hop_by_hop_headers[] = {"Connection", "Keep-Alive", "Proxy-Authenticate", "Proxy-Authorization", "TE", "Trailer", "Transfer-Encoding", "Upgrade"};
+
 static void
-update_headers (const char *name, const char *value, SoupMessageHeaders *headers)
+copy_end_to_end_headers (SoupMessageHeaders *source, SoupMessageHeaders *destination)
 {
-	if (soup_message_headers_get (headers, name))
-		soup_message_headers_replace (headers, name, value);
-	else
-		soup_message_headers_append (headers, name, value);
+	int i;
+
+	soup_message_headers_foreach (source, (SoupMessageHeadersForeachFunc) copy_headers, destination);
+	for (i = 0; i < G_N_ELEMENTS (hop_by_hop_headers); i++)
+		soup_message_headers_remove (destination, hop_by_hop_headers[i]);
+	soup_message_headers_clean_connection_headers (destination);
 }
 
 static guint
@@ -413,7 +417,6 @@ static SoupCacheEntry *
 soup_cache_entry_new (SoupCache *cache, SoupMessage *msg, time_t request_time, time_t response_time)
 {
 	SoupCacheEntry *entry;
-	SoupMessageHeaders *headers;
 	const char *date;
 	char *md5;
 
@@ -433,11 +436,8 @@ soup_cache_entry_new (SoupCache *cache, SoupMessage *msg, time_t request_time, t
 	g_free (md5);
 
 	/* Headers */
-	headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
-	soup_message_headers_foreach (msg->response_headers,
-				      (SoupMessageHeadersForeachFunc)copy_headers,
-				      headers);
-	entry->headers = headers;
+	entry->headers = soup_message_headers_new (SOUP_MESSAGE_HEADERS_RESPONSE);
+	copy_end_to_end_headers (msg->response_headers, entry->headers);
 
 	/* LRU list */
 	entry->hits = 0;
@@ -985,12 +985,7 @@ msg_got_headers_cb (SoupMessage *msg, gpointer user_data)
 		 */
 		if (entry) {
 			entry->being_validated = FALSE;
-
-			/* We update the headers of the existing cache item,
-			   plus its age */
-			soup_message_headers_foreach (msg->response_headers,
-						      (SoupMessageHeadersForeachFunc)update_headers,
-						      entry->headers);
+			copy_end_to_end_headers (msg->response_headers, entry->headers);
 			soup_cache_entry_set_freshness (entry, msg, cache);
 		}
 	}
@@ -1018,9 +1013,7 @@ soup_cache_send_response (SoupCache *cache, SoupMessage *msg)
 	entry->being_validated = FALSE;
 
 	/* Headers */
-	soup_message_headers_foreach (entry->headers,
-				      (SoupMessageHeadersForeachFunc)update_headers,
-				      msg->response_headers);
+	copy_end_to_end_headers (entry->headers, msg->response_headers);
 
 	/* Add 'Age' header with the current age */
 	current_age = g_strdup_printf ("%d", soup_cache_entry_get_current_age (entry));
diff --git a/libsoup/soup-headers.c b/libsoup/soup-headers.c
index 2b9c29d..607b911 100644
--- a/libsoup/soup-headers.c
+++ b/libsoup/soup-headers.c
@@ -136,23 +136,6 @@ done:
 	return success;
 }
 
-/* RFC 2616 14.10 */
-static void
-soup_headers_clean_for_10 (SoupMessageHeaders *hdrs)
-{
-	const char *connection;
-	GSList *tokens, *t;
-
-	connection = soup_message_headers_get_list (hdrs, "Connection");
-	if (!connection)
-		return;
-
-	tokens = soup_header_parse_list (connection);
-	for (t = tokens; t; t = t->next)
-		soup_message_headers_remove (hdrs, t->data);
-	soup_header_free_list (tokens);
-}
-
 /**
  * soup_headers_parse_request:
  * @str: the header string (including the trailing blank line)
@@ -251,8 +234,9 @@ soup_headers_parse_request (const char          *str,
 	if (soup_message_headers_get_expectations (req_headers) &
 	    SOUP_EXPECTATION_UNRECOGNIZED)
 		return SOUP_STATUS_EXPECTATION_FAILED;
+	/* RFC 2616 14.10 */
 	if (minor_version == 0)
-		soup_headers_clean_for_10 (req_headers);
+		soup_message_headers_clean_connection_headers (req_headers);
 
 	if (req_method)
 		*req_method = g_strndup (method, method_end - method);
@@ -392,8 +376,9 @@ soup_headers_parse_response (const char          *str,
 	if (ver)
 		*ver = version;
 
+	/* RFC 2616 14.10 */
 	if (version == SOUP_HTTP_1_0)
-		soup_headers_clean_for_10 (headers);
+		soup_message_headers_clean_connection_headers (headers);
 
 	return TRUE;
 }
diff --git a/libsoup/soup-message-headers.c b/libsoup/soup-message-headers.c
index 1d6254c..2f83add 100644
--- a/libsoup/soup-message-headers.c
+++ b/libsoup/soup-message-headers.c
@@ -141,6 +141,30 @@ soup_message_headers_clear (SoupMessageHeaders *hdrs)
 }
 
 /**
+ * soup_message_headers_clean_connection_headers:
+ * @hdrs: a #SoupMessageHeaders
+ *
+ * Removes all the headers listed in the Connection header.
+ *
+ **/
+void
+soup_message_headers_clean_connection_headers (SoupMessageHeaders *hdrs)
+{
+	/* RFC 2616 14.10 */
+	const char *connection;
+	GSList *tokens, *t;
+
+	connection = soup_message_headers_get_list (hdrs, "Connection");
+	if (!connection)
+		return;
+
+	tokens = soup_header_parse_list (connection);
+	for (t = tokens; t; t = t->next)
+		soup_message_headers_remove (hdrs, t->data);
+	soup_header_free_list (tokens);
+}
+
+/**
  * soup_message_headers_append:
  * @hdrs: a #SoupMessageHeaders
  * @name: the header name to add
diff --git a/libsoup/soup-message-headers.h b/libsoup/soup-message-headers.h
index 967b5e7..e1dec1d 100644
--- a/libsoup/soup-message-headers.h
+++ b/libsoup/soup-message-headers.h
@@ -33,6 +33,8 @@ void                soup_message_headers_remove   (SoupMessageHeaders *hdrs,
 						   const char         *name);
 void                soup_message_headers_clear    (SoupMessageHeaders *hdrs);
 
+void                soup_message_headers_clean_connection_headers (SoupMessageHeaders *hdrs);
+
 #ifndef LIBSOUP_DISABLE_DEPRECATED
 const char         *soup_message_headers_get      (SoupMessageHeaders *hdrs,
 						   const char         *name);



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