[libsoup] Revert "Revert "Set the Host header before emiting SoupMessage::starting""



commit 0c23f0816b51385d2784e97a276db9af8a4b626d
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Thu Dec 10 11:00:26 2020 +0100

    Revert "Revert "Set the Host header before emiting SoupMessage::starting""
    
    This reverts commit 3579711dbccf00a0b8233abd0c0b717edb756ad1.

 libsoup/soup-logger.c            | 22 +---------------------
 libsoup/soup-message-io.c        | 22 +++-------------------
 libsoup/soup-session.c           | 21 +++++++++++++++++++--
 libsoup/soup-uri-utils-private.h |  2 ++
 libsoup/soup-uri-utils.c         | 13 +++++++++++++
 5 files changed, 38 insertions(+), 42 deletions(-)
---
diff --git a/libsoup/soup-logger.c b/libsoup/soup-logger.c
index ed18fc12..86fce4dc 100644
--- a/libsoup/soup-logger.c
+++ b/libsoup/soup-logger.c
@@ -18,7 +18,6 @@
 #include "soup-connection.h"
 #include "soup-message-private.h"
 #include "soup-misc.h"
-#include "soup-uri-utils-private.h"
 #include "soup.h"
 #include "soup-session-feature-private.h"
 
@@ -515,22 +514,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") &&
@@ -689,10 +672,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 1055c099..064f3d35 100644
--- a/libsoup/soup-message-io.c
+++ b/libsoup/soup-message-io.c
@@ -259,20 +259,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);
 
@@ -297,19 +293,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 c2db87ac..ae564a89 100644
--- a/libsoup/soup-session.c
+++ b/libsoup/soup-session.c
@@ -992,9 +992,9 @@ soup_session_send_queue_item (SoupSession *session,
                              SoupMessageIOCompletionFn completion_cb)
 {
        SoupSessionPrivate *priv = soup_session_get_instance_private (session);
-        SoupMessageHeaders *request_headers;
+       SoupMessageHeaders *request_headers;
 
-        request_headers = soup_message_get_request_headers (item->msg);
+       request_headers = soup_message_get_request_headers (item->msg);
        if (priv->user_agent)
                soup_message_headers_replace (request_headers, "User-Agent", priv->user_agent);
 
@@ -1012,6 +1012,23 @@ soup_session_send_queue_item (SoupSession *session,
                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 4bc4c11c..de4a162a 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]