From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- testing/mochitest/Http2Server/http2_server.js | 166 +++++++++++++++++++++ .../mochitest/Http2Server/mochitest-cert.key.pem | 28 ++++ testing/mochitest/Http2Server/mochitest-cert.pem | 23 +++ 3 files changed, 217 insertions(+) create mode 100644 testing/mochitest/Http2Server/http2_server.js create mode 100644 testing/mochitest/Http2Server/mochitest-cert.key.pem create mode 100644 testing/mochitest/Http2Server/mochitest-cert.pem (limited to 'testing/mochitest/Http2Server') diff --git a/testing/mochitest/Http2Server/http2_server.js b/testing/mochitest/Http2Server/http2_server.js new file mode 100644 index 0000000000..798693e6f0 --- /dev/null +++ b/testing/mochitest/Http2Server/http2_server.js @@ -0,0 +1,166 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +/* globals require, __dirname, global, Buffer, process */ + +const fs = require("fs"); +const options = { + key: fs.readFileSync(__dirname + "/mochitest-cert.key.pem"), + cert: fs.readFileSync(__dirname + "/mochitest-cert.pem"), + settings: { + enableConnectProtocol: true, + }, +}; +const http2 = require("http2"); +const http = require("http"); +const url = require("url"); +const path = require("path"); + +// This is the path of node-ws when running mochitest locally. +let node_ws_root = path.join(__dirname, "../../xpcshell/node-ws"); +if (!fs.existsSync(node_ws_root)) { + // This path is for running mochitest on try. + node_ws_root = path.join(__dirname, "./node_ws"); +} + +const WebSocket = require(`${node_ws_root}/lib/websocket`); + +let listeningPort = parseInt(process.argv[3].split("=")[1]); +let log = function () {}; + +function handle_h2_non_connect(stream, headers) { + const session = stream.session; + const uri = new URL( + `${headers[":scheme"]}://${headers[":authority"]}${headers[":path"]}` + ); + const url = uri.toString(); + + log("REQUEST:", url); + log("REQUEST HEADER:", JSON.stringify(headers)); + + stream.on("close", () => { + log("REQUEST STREAM CLOSED:", stream.rstCode); + }); + stream.on("error", error => { + log("RESPONSE STREAM ERROR", error, url, "on session:", session.__id); + }); + + let newHeaders = {}; + for (let key in headers) { + if (!key.startsWith(":")) { + newHeaders[key] = headers[key]; + } + } + + const options = { + protocol: "http:", + hostname: "127.0.0.1", + port: 8888, + path: headers[":path"], + method: headers[":method"], + headers: newHeaders, + }; + + log("OPTION:", JSON.stringify(options)); + const request = http.request(options); + + stream.pipe(request); + + request.on("response", response => { + const headers = Object.fromEntries( + Object.entries(response.headers).filter( + ([key]) => + !["connection", "transfer-encoding", "keep-alive"].includes(key) + ) + ); + headers[":status"] = response.statusCode; + log("RESPONSE BEGIN", url, headers, "on session:", session.__id); + + try { + stream.respond(headers); + + response.on("data", data => { + log("RESPONSE DATA", data.length, url); + stream.write(data); + }); + response.on("error", error => { + log("RESPONSE ERROR", error, url, "on session:", session.__id); + stream.close(http2.constants.NGHTTP2_REFUSED_STREAM); + }); + response.on("end", () => { + log("RESPONSE END", url, "on session:", session.__id); + stream.end(); + }); + } catch (exception) { + log("RESPONSE EXCEPTION", exception, url, "on session:", session.__id); + stream.close(); + } + }); + request.on("error", error => { + console.error("REQUEST ERROR", error, url, "on session:", session.__id); + try { + stream.respond({ + ":status": 502, + "content-type": "application/proxy-explanation+json", + }); + stream.end( + JSON.stringify({ + title: "request error", + description: error.toString(), + }) + ); + } catch (exception) { + stream.close(); + } + }); +} + +let server = http2.createSecureServer(options); + +let session_count = 0; +let session_id = 0; + +server.on("session", session => { + session.__id = ++session_id; + session.__tunnel_count = 0; + + ++session_count; + if (session_count === 1) { + log(`\n\n>>> FIRST SESSION OPENING\n`); + } + log(`*** NEW SESSION`, session.__id, "( sessions:", session_count, ")"); + + session.on("error", error => { + console.error("SESSION ERROR", session.__id, error); + }); + session.on("close", () => { + --session_count; + log(`*** CLOSED SESSION`, session.__id, "( sessions:", session_count, ")"); + if (!session_count) { + log(`\n\n<<< LAST SESSION CLOSED\n`); + } + }); +}); + +server.on("stream", (stream, headers) => { + if (headers[":method"] === "CONNECT") { + stream.respond(); + + const ws = new WebSocket(null); + stream.setNoDelay = () => {}; + ws.setSocket(stream, Buffer.from(""), 100 * 1024 * 1024); + + ws.on("message", data => { + ws.send(data); + }); + } else { + handle_h2_non_connect(stream, headers); + } +}); + +server.listen(listeningPort); + +console.log(`Http2 server listening on ports ${server.address().port}`); diff --git a/testing/mochitest/Http2Server/mochitest-cert.key.pem b/testing/mochitest/Http2Server/mochitest-cert.key.pem new file mode 100644 index 0000000000..09e044f5e0 --- /dev/null +++ b/testing/mochitest/Http2Server/mochitest-cert.key.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC6iFGoRI4W1kH9 +braIBjYQPTwT2erkNUq07PVoV2wke8HHJajg2B+9sZwGm24ahvJr4q9adWtqZHEI +eqVap0WH9xzVJJwCfs1D/B5p0DggKZOrIMNJ5Nu5TMJrbA7tFYIP8X6taRqx0wI6 +iypB7qdw4A8Njf1mCyuwJJKkfbmIYXmQsVeQPdI7xeC4SB+oN9OIQ+8nFthVt2Za +qn4CkC86exCABiTMHGyXrZZhW7filhLAdTGjDJHdtMr3/K0dJdMJ77kXDqdo4bN7 +LyJvaeO0ipVhHe4m1iWdq5EITjbLHCQELL8Wiy/l8Y+ZFzG4s/5JI/pyUcQx1QOs +2hgKNe2NAgMBAAECggEBAJ7LzjhhpFTsseD+j4XdQ8kvWCXOLpl4hNDhqUnaosWs +VZskBFDlrJ/gw+McDu+mUlpl8MIhlABO4atGPd6e6CKHzJPnRqkZKcXmrD2IdT9s +JbpZeec+XY+yOREaPNq4pLDN9fnKsF8SM6ODNcZLVWBSXn47kq18dQTPHcfLAFeI +r8vh6Pld90AqFRUw1YCDRoZOs3CqeZVqWHhiy1M3kTB/cNkcltItABppAJuSPGgz +iMnzbLm16+ZDAgQceNkIIGuHAJy4yrrK09vbJ5L7kRss9NtmA1hb6a4Mo7jmQXqg +SwbkcOoaO1gcoDpngckxW2KzDmAR8iRyWUbuxXxtlEECgYEA3W4dT//r9o2InE0R +TNqqnKpjpZN0KGyKXCmnF7umA3VkTVyqZ0xLi8cyY1hkYiDkVQ12CKwn1Vttt0+N +gSfvj6CQmLaRR94GVXNEfhg9Iv59iFrOtRPZWB3V4HwakPXOCHneExNx7O/JznLp +xD3BJ9I4GQ3oEXc8pdGTAfSMdCsCgYEA16dz2evDgKdn0v7Ak0rU6LVmckB3Gs3r +ta15b0eP7E1FmF77yVMpaCicjYkQL63yHzTi3UlA66jAnW0fFtzClyl3TEMnXpJR +3b5JCeH9O/Hkvt9Go5uLODMo70rjuVuS8gcK8myefFybWH/t3gXo59hspXiG+xZY +EKd7mEW8MScCgYEAlkcrQaYQwK3hryJmwWAONnE1W6QtS1oOtOnX6zWBQAul3RMs +2xpekyjHu8C7sBVeoZKXLt+X0SdR2Pz2rlcqMLHqMJqHEt1OMyQdse5FX8CT9byb +WS11bmYhR08ywHryL7J100B5KzK6JZC7smGu+5WiWO6lN2VTFb6cJNGRmS0CgYAo +tFCnp1qFZBOyvab3pj49lk+57PUOOCPvbMjo+ibuQT+LnRIFVA8Su+egx2got7pl +rYPMpND+KiIBFOGzXQPVqFv+Jwa9UPzmz83VcbRspiG47UfWBbvnZbCqSgZlrCU2 +TaIBVAMuEgS4VZ0+NPtbF3yaVv+TUQpaSmKHwVHeLQKBgCgGe5NVgB0u9S36ltit +tYlnPPjuipxv9yruq+nva+WKT0q/BfeIlH3IUf2qNFQhR6caJGv7BU7naqNGq80m +ks/J5ExR5vBpxzXgc7oBn2pyFJYckbJoccrqv48GRBigJpDjmo1f8wZ7fNt/ULH1 +NBinA5ZsT8d0v3QCr2xDJH9D +-----END PRIVATE KEY----- diff --git a/testing/mochitest/Http2Server/mochitest-cert.pem b/testing/mochitest/Http2Server/mochitest-cert.pem new file mode 100644 index 0000000000..fd968257a1 --- /dev/null +++ b/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