[libsoup/carlosgc/logger: 3/4] Set the Host header before emiting SoupMessage::starting




commit 53c357cea3b65f0660ec6b589f73bf32bd090229
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Fri Nov 20 14:39:59 2020 +0100

    Set the Host header before emiting SoupMessage::starting
    
    This way we don't need to specially handle the Host header in the logger
    and the message io when writing the headers.

 libsoup/soup-logger.c            | 21 +-----------------
 libsoup/soup-message-io.c        | 22 +++----------------
 libsoup/soup-session.c           | 46 +++++++++++++++++++++++-----------------
 libsoup/soup-uri-utils-private.h |  2 ++
 libsoup/soup-uri-utils.c         | 13 ++++++++++++
 5 files changed, 46 insertions(+), 58 deletions(-)
---
diff --git a/libsoup/soup-logger.c b/libsoup/soup-logger.c
index c47f2bb1..83f138be 100644
--- a/libsoup/soup-logger.c
+++ b/libsoup/soup-logger.c
@@ -513,22 +513,6 @@ print_request (SoupLogger *logger, SoupMessage *msg,
        if (log_level == SOUP_LOGGER_LOG_MINIMAL)
                return;
 
-       if (!soup_message_headers_get_one (soup_message_get_request_headers (msg), "Host")) {
-               char *uri_host = (char*)g_uri_get_host (uri);
-
-               if (strchr (uri_host, ':'))
-                       uri_host = g_strdup_printf ("[%s]", uri_host);
-               else if (g_hostname_is_non_ascii (uri_host))
-                       uri_host = g_hostname_to_ascii (uri_host);
-
-               soup_logger_print (logger, SOUP_LOGGER_LOG_HEADERS, '>',
-                                  "Host: %s%c%u", uri_host,
-                                  soup_uri_uses_default_port (uri) ? '\0' : ':',
-                                  g_uri_get_port (uri));
-
-               if (uri_host != g_uri_get_host (uri))
-                       g_free (uri_host);
-       }
        soup_message_headers_iter_init (&iter, soup_message_get_request_headers (msg));
        while (soup_message_headers_iter_next (&iter, &name, &value)) {
                if (!g_ascii_strcasecmp (name, "Authorization") &&
@@ -687,10 +671,7 @@ soup_logger_request_unqueued (SoupSessionFeature *logger,
 {
        g_return_if_fail (SOUP_IS_MESSAGE (msg));
 
-       g_signal_handlers_disconnect_by_func (msg, starting, logger);
-       g_signal_handlers_disconnect_by_func (msg, got_informational, logger);
-       g_signal_handlers_disconnect_by_func (msg, got_body, logger);
-       g_signal_handlers_disconnect_by_func (msg, finished, logger);
+       g_signal_handlers_disconnect_by_data (msg, logger);
 }
 
 static void
diff --git a/libsoup/soup-message-io.c b/libsoup/soup-message-io.c
index 9f51ab79..fab6f130 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -258,20 +258,16 @@ write_headers (SoupMessage          *msg,
               SoupEncoding         *encoding)
 {
        GUri *uri = soup_message_get_uri (msg);
-       char *uri_host;
        char *uri_string;
        SoupMessageHeadersIter iter;
        const char *name, *value;
 
-        uri_host = (char*)g_uri_get_host (uri);
-       if (strchr (uri_host, ':'))
-               uri_host = g_strdup_printf ("[%.*s]", (int) strcspn (uri_host, "%"), uri_host);
-       else if (g_hostname_is_non_ascii (uri_host))
-               uri_host = g_hostname_to_ascii (uri_host);
-
        if (soup_message_get_method (msg) == SOUP_METHOD_CONNECT) {
+               char *uri_host = soup_uri_get_host_for_headers (uri);
+
                /* CONNECT URI is hostname:port for tunnel destination */
                uri_string = g_strdup_printf ("%s:%d", uri_host, g_uri_get_port (uri));
+               g_free (uri_host);
        } else {
                gboolean proxy = soup_connection_is_via_proxy (conn);
 
@@ -296,19 +292,7 @@ write_headers (SoupMessage          *msg,
        g_string_append_printf (header, "%s %s HTTP/1.%d\r\n",
                                soup_message_get_method (msg), uri_string,
                                (soup_message_get_http_version (msg) == SOUP_HTTP_1_0) ? 0 : 1);
-
-       if (!soup_message_headers_get_one (soup_message_get_request_headers (msg), "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, g_uri_get_port (uri));
-               }
-       }
        g_free (uri_string);
-       if (uri_host != g_uri_get_host (uri))
-               g_free (uri_host);
 
        *encoding = soup_message_headers_get_encoding (soup_message_get_request_headers (msg));
 
diff --git a/libsoup/soup-session.c b/libsoup/soup-session.c
index 608b7411..dec2524e 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -1025,35 +1025,43 @@ soup_session_send_queue_item (SoupSession *session,
                              SoupMessageIOCompletionFn completion_cb)
 {
        SoupSessionPrivate *priv = soup_session_get_instance_private (session);
+       SoupMessageHeaders *request_headers;
 
-       if (priv->user_agent) {
-               soup_message_headers_replace (soup_message_get_request_headers (item->msg),
-                                             "User-Agent", priv->user_agent);
-       }
+       request_headers = soup_message_get_request_headers (item->msg);
+       if (priv->user_agent)
+               soup_message_headers_replace (request_headers, "User-Agent", priv->user_agent);
 
-       if (priv->accept_language &&
-           !soup_message_headers_get_list (soup_message_get_request_headers (item->msg),
-                                           "Accept-Language")) {
-               soup_message_headers_append (soup_message_get_request_headers (item->msg),
-                                            "Accept-Language",
-                                            priv->accept_language);
-       }
+       if (priv->accept_language && !soup_message_headers_get_list (request_headers, "Accept-Language"))
+               soup_message_headers_append (request_headers, "Accept-Language", priv->accept_language);
 
        /* Force keep alive connections for HTTP 1.0. Performance will
         * improve when issuing multiple requests to the same host in
         * a short period of time, as we wouldn't need to establish
         * new connections. Keep alive is implicit for HTTP 1.1.
         */
-       if (!soup_message_headers_header_contains (soup_message_get_request_headers (item->msg),
-                                                  "Connection", "Keep-Alive") &&
-           !soup_message_headers_header_contains (soup_message_get_request_headers (item->msg),
-                                                  "Connection", "close") &&
-           !soup_message_headers_header_contains (soup_message_get_request_headers (item->msg),
-                                                  "Connection", "Upgrade")) {
-               soup_message_headers_append (soup_message_get_request_headers (item->msg),
-                                            "Connection", "Keep-Alive");
+       if (!soup_message_headers_header_contains (request_headers, "Connection", "Keep-Alive") &&
+           !soup_message_headers_header_contains (request_headers, "Connection", "close") &&
+           !soup_message_headers_header_contains (request_headers, "Connection", "Upgrade")) {
+               soup_message_headers_append (request_headers, "Connection", "Keep-Alive");
        }
 
+        if (!soup_message_headers_get_one (request_headers, "Host")) {
+                GUri *uri = soup_message_get_uri (item->msg);
+                char *host;
+
+                host = soup_uri_get_host_for_headers (uri);
+                if (soup_uri_uses_default_port (uri))
+                        soup_message_headers_append (request_headers, "Host", host);
+                else {
+                        char *value;
+
+                        value = g_strdup_printf ("%s:%d", host, g_uri_get_port (uri));
+                        soup_message_headers_append (request_headers, "Host", value);
+                        g_free (value);
+                }
+                g_free (host);
+        }
+
        soup_message_starting (item->msg);
        if (item->state == SOUP_MESSAGE_RUNNING)
                soup_connection_send_request (item->conn, item, completion_cb, item);
diff --git a/libsoup/soup-uri-utils-private.h b/libsoup/soup-uri-utils-private.h
index b741a6d7..6bc2b38d 100644
--- a/libsoup/soup-uri-utils-private.h
+++ b/libsoup/soup-uri-utils-private.h
@@ -28,6 +28,8 @@ gboolean     soup_uri_host_equal            (gconstpointer v1, gconstpointer v2)
 
 GUri        *soup_uri_copy_with_normalized_flags (GUri  *uri);
 
+char        *soup_uri_get_host_for_headers  (GUri       *uri);
+
 #define SOUP_URI_IS_VALID(x) (x && g_uri_get_host(x) && g_uri_get_host(x)[0])
 
 G_END_DECLS
diff --git a/libsoup/soup-uri-utils.c b/libsoup/soup-uri-utils.c
index f2e4e76d..fe596a0b 100644
--- a/libsoup/soup-uri-utils.c
+++ b/libsoup/soup-uri-utils.c
@@ -489,3 +489,16 @@ soup_uri_copy_with_normalized_flags (GUri *uri)
                 g_uri_get_fragment (uri)
         );
 }
+
+char *
+soup_uri_get_host_for_headers (GUri *uri)
+{
+        const char *host = g_uri_get_host (uri);
+
+        if (strchr (host, ':'))
+                return g_strdup_printf ("[%.*s]", (int)strcspn (host, "%"), host);
+        if (g_hostname_is_non_ascii (host))
+                return g_hostname_to_ascii (host);
+
+        return g_strdup (host);
+}


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