Skip to content

Truly independent web browser

RequestServer: Set CURLOPT_PIPEWAIT on https requests

Andreas Kling624241c54baa
diff --git a/Services/RequestServer/Request.cpp b/Services/RequestServer/Request.cpp
index d43f2f14267..915bbcc8acb 100644
--- a/Services/RequestServer/Request.cpp
+++ b/Services/RequestServer/Request.cpp
@@ -1521,6 +1521,15 @@ void Request::handle_fetch_state()
set_option(CURLOPT_REDIR_PROTOCOLS_STR, "http,https");
set_option(CURLOPT_CONNECTTIMEOUT, s_connect_timeout_seconds);
+#ifdef LIBCURL_WAKES_ALL_PENDING_TRANSFERS
+ // A burst of requests to a new origin would otherwise each open a connection, even if they could all multiplex over
+ // one. The TLS handshake tells curl if a connection can multiplex. A plain HTTP connection only tells curl through
+ // the response, and that response may be waiting for this very request. An unpatched curl leaves the requests
+ // waiting in the same way over TLS, so builds with a system curl go without this.
+ if (m_url.scheme() == "https"sv)
+ set_option(CURLOPT_PIPEWAIT, 1L);
+#endif
+
if (m_alternative_service.has_value()) {
// https://www.rfc-editor.org/rfc/rfc7838#section-2.4
// If the connection to the alternative service does not negotiate the expected protocol (for example, ALPN
diff --git a/Tests/LibWeb/Fixtures/http-test-server.py b/Tests/LibWeb/Fixtures/http-test-server.py
index f0ad4715603..4037acb86ab 100755
--- a/Tests/LibWeb/Fixtures/http-test-server.py
+++ b/Tests/LibWeb/Fixtures/http-test-server.py
@@ -92,6 +92,8 @@ recorded_request_counts_lock = threading.Lock()
# Named events used by tests that need deterministic delayed responses.
unblock_events: Dict[str, threading.Event] = {}
+stalled_tls_port = 0
+stalled_tls_connection_count = 0
# Per RFC 6455: appended to Sec-WebSocket-Key before hashing to compute the Sec-WebSocket-Accept value.
WEBSOCKET_ACCEPT_GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
@@ -370,6 +372,18 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
self.end_headers()
self.wfile.write(body)
+ def _serve_stalled_tls_config(self):
+ # Lets the test page discover a port that accepts connections and never answers a TLS handshake, and how many
+ # connections it has accepted so far.
+ body = json.dumps({"port": stalled_tls_port, "connections": stalled_tls_connection_count}).encode("utf-8")
+ self.send_response(200)
+ self.send_header("Content-Type", "application/json")
+ self.send_header("Access-Control-Allow-Origin", "*")
+ self.send_header("Cache-Control", "no-store")
+ self.send_header("Content-Length", str(len(body)))
+ self.end_headers()
+ self.wfile.write(body)
+
def _serve_aia_config(self):
# Lets the test page discover the per-scenario HTTPS ports (each an OS-assigned port).
config = getattr(TestHTTPRequestHandler, "aia_config", None)
@@ -404,6 +418,8 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
self._serve_aia_artifact(request_path)
elif request_path == "/aia-test/config":
self._serve_aia_config()
+ elif request_path == "/stalled-tls/config":
+ self._serve_stalled_tls_config()
else:
self._serve_static_request()
@@ -1138,6 +1154,8 @@ def start_server(port, static_directory, ca_cert_output=None, exit_when_stdin_cl
https_ports = [https_httpd.socket.getsockname()[1]] if https_httpd else []
httpd.wpt.configure_server([httpd.socket.getsockname()[1], alternate_httpd.socket.getsockname()[1]], https_ports)
+ start_stalled_tls_listener()
+
# The first line holds the HTTP port, followed by the HTTPS port if there is one.
print(" ".join(str(port) for port in [httpd.socket.getsockname()[1], *https_ports]))
sys.stdout.flush()
@@ -1164,6 +1182,31 @@ def start_server(port, static_directory, ca_cert_output=None, exit_when_stdin_cl
httpd.server_close()
+def start_stalled_tls_listener():
+ global stalled_tls_port
+
+ listener = socket.create_server(("127.0.0.1", 0))
+ stalled_tls_port = listener.getsockname()[1]
+
+ def hold_until_closed(connection):
+ with connection:
+ try:
+ while connection.recv(4096):
+ pass
+ except OSError:
+ pass
+
+ def accept_connections():
+ global stalled_tls_connection_count
+
+ while True:
+ connection = listener.accept()[0]
+ stalled_tls_connection_count += 1
+ threading.Thread(target=hold_until_closed, args=(connection,), daemon=True).start()
+
+ threading.Thread(target=accept_connections, daemon=True).start()
+
+
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run a HTTP echo server")
parser.add_argument(
diff --git a/Tests/LibWeb/Text/expected/Fetch/https-request-does-not-wait-on-busy-http1-connection.txt b/Tests/LibWeb/Text/expected/Fetch/https-request-does-not-wait-on-busy-http1-connection.txt
new file mode 100644
index 00000000000..0df9e4c5116
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Fetch/https-request-does-not-wait-on-busy-http1-connection.txt
@@ -0,0 +1,10 @@
+Unblock status: 204
+Held response: released
+Other response: other
+Other response: other
+Other response: other
+Other response: other
+Other response: other
+Other response: other
+Other response: other
+Other response: other
diff --git a/Tests/LibWeb/Text/input/Fetch/https-request-does-not-wait-on-busy-http1-connection.html b/Tests/LibWeb/Text/input/Fetch/https-request-does-not-wait-on-busy-http1-connection.html
new file mode 100644
index 00000000000..ef6d1e707ba
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Fetch/https-request-does-not-wait-on-busy-http1-connection.html
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // The server holds the first response until the last request arrives, and it only speaks HTTP/1 over TLS. So the
+ // requests can't wait for the first one's connection to turn out to be multiplexed. Requests waiting for a connection
+ // to another origin, which never finishes its TLS handshake, must not get in the way either.
+ asyncTest(async done => {
+ const stalledConfig = async () => (await fetch(`${httpTestServer().baseURL}/stalled-tls/config`)).json();
+ const { port: stalledPort, connections: connectionsBefore } = await stalledConfig();
+
+ const stalledRequests = new AbortController();
+ for (let i = 0; i < 64; ++i)
+ fetch(`https://localhost:${stalledPort}/${uniqueID()}`, { signal: stalledRequests.signal }).catch(() => {});
+ while ((await stalledConfig()).connections <= connectionsBefore) await timeout(10);
+
+ const token = `https-request-does-not-wait-${uniqueID()}`;
+ const echoHeaders = { "Access-Control-Allow-Origin": "*", "Cache-Control": "no-store" };
+ const heldURL = await httpTestServer().createEcho("GET", "/held-response", {
+ status: 200,
+ headers: echoHeaders,
+ body: "released",
+ wait_for_unblock: token,
+ });
+ const otherURL = await httpTestServer().createEcho("GET", "/other-response", {
+ status: 200,
+ headers: echoHeaders,
+ body: "other",
+ });
+
+ const httpsOrigin = `https://localhost:${internals.getHttpsEchoServerPort()}`;
+ const heldResponse = fetch(`${httpsOrigin}${new URL(heldURL).pathname}`);
+ const otherResponses = [];
+ for (let i = 0; i < 8; ++i) otherResponses.push(fetch(`${httpsOrigin}${new URL(otherURL).pathname}`));
+ const unblockResponse = fetch(`${httpsOrigin}/unblock/${token}`);
+
+ println(`Unblock status: ${(await unblockResponse).status}`);
+ println(`Held response: ${await (await heldResponse).text()}`);
+ for (const response of otherResponses) println(`Other response: ${await (await response).text()}`);
+
+ stalledRequests.abort();
+ done();
+ });
+</script>