From 8d511b2f7ba9979bdd970cf3131e571b8390f0ce Mon Sep 17 00:00:00 2001 From: rmg-x Date: Sun, 17 Nov 2024 16:02:50 -0600 Subject: [PATCH] RequestServer: Clear "Content-Type" header when one isn't provided libcurl will automatically set the Content-Type header when using the CURLOPT_POSTFIELDS option to "application/x-www-form-urlencoded". See: https://curl.se/libcurl/c/CURLOPT_POSTFIELDS.html The following WPT cases now pass (8 tests): http://wpt.live/xhr/send-blob-with-no-mime-type.html --- Services/RequestServer/ConnectionFromClient.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Services/RequestServer/ConnectionFromClient.cpp b/Services/RequestServer/ConnectionFromClient.cpp index c5d74fddfa5..6d05b16d3cb 100644 --- a/Services/RequestServer/ConnectionFromClient.cpp +++ b/Services/RequestServer/ConnectionFromClient.cpp @@ -311,12 +311,15 @@ void ConnectionFromClient::start_request(i32 request_id, ByteString const& metho set_option(CURLOPT_PORT, url.port_or_default()); set_option(CURLOPT_CONNECTTIMEOUT, s_connect_timeout_seconds); + bool did_set_body = false; + if (method == "GET"sv) { set_option(CURLOPT_HTTPGET, 1L); } else if (method.is_one_of("POST"sv, "PUT"sv, "PATCH"sv, "DELETE"sv)) { request->body = request_body; set_option(CURLOPT_POSTFIELDSIZE, request->body.size()); set_option(CURLOPT_POSTFIELDS, request->body.data()); + did_set_body = true; } else if (method == "HEAD") { set_option(CURLOPT_NOBODY, 1L); } @@ -325,6 +328,12 @@ void ConnectionFromClient::start_request(i32 request_id, ByteString const& metho set_option(CURLOPT_FOLLOWLOCATION, 0); struct curl_slist* curl_headers = nullptr; + + // NOTE: CURLOPT_POSTFIELDS automatically sets the Content-Type header. + // Set it to empty if the headers passed in don't contain a content type. + if (did_set_body && !request_headers.contains("Content-Type")) + curl_headers = curl_slist_append(curl_headers, "Content-Type:"); + for (auto const& header : request_headers.headers()) { auto header_string = ByteString::formatted("{}: {}", header.name, header.value); curl_headers = curl_slist_append(curl_headers, header_string.characters());