From 87483a98d16752fd41e1a13e050e50c9e0951359 Mon Sep 17 00:00:00 2001 From: Allison Karlitskaya Date: Fri, 2 Feb 2024 22:05:24 +0100 Subject: [PATCH] ws: don't send a body on 301 redirects As with the previous commit doing the same for cockpit-tls, we don't need to send the body in the GET case and we must not send it in the HEAD case, so just never send it. Take some time to do a bit of autoptr cleanups while we're in here. --- src/common/cockpitwebserver.c | 30 +++++------------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/src/common/cockpitwebserver.c b/src/common/cockpitwebserver.c index 7b3e0c52ddd2..c6a7492198a3 100644 --- a/src/common/cockpitwebserver.c +++ b/src/common/cockpitwebserver.c @@ -742,43 +742,23 @@ cockpit_web_request_process_delayed_reply (CockpitWebRequest *self, const gchar *path, GHashTable *headers) { - CockpitWebResponse *response; - const gchar *host; - const gchar *body; - GBytes *bytes; - gsize length; - gchar *url; - g_assert (self->delayed_reply > 299); - response = cockpit_web_request_respond (self); + g_autoptr(CockpitWebResponse) response = cockpit_web_request_respond (self); g_signal_connect_data (response, "done", G_CALLBACK (on_web_response_done), g_object_ref (self->web_server), (GClosureNotify)g_object_unref, 0); if (self->delayed_reply == 301) { - body = "Moved" - "Please use TLS"; - host = g_hash_table_lookup (headers, "Host"); - url = g_strdup_printf ("https://%s%s", - host != NULL ? host : "", path); - length = strlen (body); - cockpit_web_response_headers (response, 301, "Moved Permanently", length, - "Content-Type", "text/html", - "Location", url, - NULL); - g_free (url); - bytes = g_bytes_new_static (body, length); - if (cockpit_web_response_queue (response, bytes)) - cockpit_web_response_complete (response); - g_bytes_unref (bytes); + const gchar *host = g_hash_table_lookup (headers, "Host"); + g_autofree gchar *url = g_strdup_printf ("https://%s%s", host != NULL ? host : "", path); + cockpit_web_response_headers (response, 301, "Moved Permanently", 0, "Location", url, NULL); + cockpit_web_response_complete (response); } else { cockpit_web_response_error (response, self->delayed_reply, NULL, NULL); } - - g_object_unref (response); } static gboolean