Pārlūkot izejas kodu

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
rmg-x 8 mēneši atpakaļ
vecāks
revīzija
8d511b2f7b
1 mainītis faili ar 9 papildinājumiem un 0 dzēšanām
  1. 9 0
      Services/RequestServer/ConnectionFromClient.cpp

+ 9 - 0
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());