Skip to content

Commit

Permalink
ws: don't send a body on 301 redirects
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
allisonkarlitskaya authored and martinpitt committed Feb 3, 2024
1 parent b3ccaf6 commit 87483a9
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions src/common/cockpitwebserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<html><head><title>Moved</title></head>"
"<body>Please use TLS</body></html>";
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
Expand Down

0 comments on commit 87483a9

Please sign in to comment.