Truly independent web browser
RequestServer: Send a request’s body whatever its method
sideshowbarker4158afeb5a20
diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cppindex a9c6b2631e0..618c9134c60 100644--- a/Services/RequestServer/Request.cpp+++ b/Services/RequestServer/Request.cpp@@ -1507,7 +1507,9 @@ void Request::handle_fetch_state()curl_slist* curl_headers = nullptr;- if (m_method.is_one_of("POST"sv, "PUT"sv, "PATCH"sv, "DELETE"sv)) {+ // Any method can carry a body (WebDAV's PROPFIND and REPORT, e.g.), and the request's headers already announce+ // its length, so hand curl the body whatever the method.+ if (m_method.is_one_of("POST"sv, "PUT"sv, "PATCH"sv, "DELETE"sv) || (!m_request_body.is_empty() && m_method != "HEAD"sv)) {set_option(CURLOPT_POSTFIELDSIZE, m_request_body.size());set_option(CURLOPT_POSTFIELDS, m_request_body.data());diff --git a/Tests/LibWeb/Fixtures/http-test-server.py b/Tests/LibWeb/Fixtures/http-test-server.pyindex bfc65d38352..18c22137321 100755--- a/Tests/LibWeb/Fixtures/http-test-server.py+++ b/Tests/LibWeb/Fixtures/http-test-server.py@@ -37,6 +37,7 @@ Endpoints:- Echo responses accept request_body_read_size=<bytes> and request_body_read_delay_ms=<ms> query parameters,which read the request's body that many bytes at a time, waiting that long before each read.Each named token holds the response after the corresponding chunk until GET /unblock/<token>.+ - An echo response's body may contain $BODY, which the server replaces with the request's body.- GET <any path> with an "Upgrade: websocket" header: Performs a WebSocket handshake and then echoesevery text/binary frame back to the client verbatim. With a "send-binary=<size>" query parameter, theserver first sends a binary frame of that many bytes: a 1, zeros, and then a 2.@@ -490,6 +491,12 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):def do_DELETE(self):self.do_other()+ def do_PROPFIND(self):+ self.do_other()++ def do_REPORT(self):+ self.do_other()+def do_SAFE(self):self.do_other()@@ -636,6 +643,7 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):# The request's body is read before responding, so closing the connection does not reset it.content_length = int(self.headers.get("Content-Length", 0))+ request_body = bytearray()if content_length:read_size = int(query.get("request_body_read_size", [content_length])[0])read_delay_ms = int(query.get("request_body_read_delay_ms", [0])[0])@@ -643,11 +651,12 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):while remaining > 0:if read_delay_ms:time.sleep(read_delay_ms / 1000)- read = len(self.rfile.read(min(read_size, remaining)))+ data = self.rfile.read(min(read_size, remaining))# The client closed the connection before sending the whole body.- if read == 0:+ if not data:break- remaining -= read+ request_body += data+ remaining -= len(data)is_revalidation_request = "If-Modified-Since" in self.headerssend_not_modified = is_revalidation_request and "X-Ladybird-Respond-With-Not-Modified" in self.headers@@ -716,6 +725,7 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):response_body = echo.body.replace("$HEADERS", headers) if echo.body else headerselse:response_body = echo.body or ""+ response_body = response_body.replace("$BODY", request_body.decode("utf-8", "replace"))# FIXME: This only supports "Range: bytes=start-end" and "Range: bytes=start-". There are other formats to# support if needed: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Range#syntaxdiff --git a/Tests/LibWeb/Text/expected/Fetch/request-body-with-any-method.txt b/Tests/LibWeb/Text/expected/Fetch/request-body-with-any-method.txtnew file mode 100644index 00000000000..5893db7c5c8--- /dev/null+++ b/Tests/LibWeb/Text/expected/Fetch/request-body-with-any-method.txt@@ -0,0 +1,4 @@+fetch POST: <post/>+fetch PROPFIND: <propfind/>+fetch REPORT: <report/>+XHR PROPFIND: <xhr-propfind/>diff --git a/Tests/LibWeb/Text/input/Fetch/request-body-with-any-method.html b/Tests/LibWeb/Text/input/Fetch/request-body-with-any-method.htmlnew file mode 100644index 00000000000..160b9e5e573--- /dev/null+++ b/Tests/LibWeb/Text/input/Fetch/request-body-with-any-method.html@@ -0,0 +1,28 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ const server = httpTestServer();+ for (const method of ["POST", "PROPFIND", "REPORT"]) {+ const url = await server.createEcho(method, `/request-body-${method}`, {+ status: 200,+ headers: { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" },+ body: "$BODY",+ });+ const response = await fetch(url, { method, body: `<${method.toLowerCase()}/>` });+ println(`fetch ${method}: ${await response.text()}`);+ }++ const url = await server.createEcho("PROPFIND", "/request-body-xhr", {+ status: 200,+ headers: { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" },+ body: "$BODY",+ });+ const xhr = new XMLHttpRequest();+ xhr.open("PROPFIND", url);+ xhr.send("<xhr-propfind/>");+ await new Promise(resolve => (xhr.onloadend = resolve));+ println(`XHR PROPFIND: ${xhr.responseText}`);+ done();+ });+</script>