Skip to content

Commit

Permalink
Bug 1775244 - Make HTTP2ProxyCode speak Http/2, r=necko-reviewers,dra…
Browse files Browse the repository at this point in the history
  • Loading branch information
KershawChang committed Jul 27, 2022
1 parent 0d6c077 commit 57cf34b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
67 changes: 64 additions & 3 deletions netwerk/test/unit/head_servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,76 @@ class HTTP2ProxyCode {
key: fs.readFileSync(__dirname + "/http2-cert.key"),
cert: fs.readFileSync(__dirname + "/http2-cert.pem"),
};
const https = require("https");
global.proxy = https.createServer(options, BaseProxyCode.proxyHandler);
global.proxy.on("connect", BaseProxyCode.onConnect);
const http2 = require("http2");
global.proxy = http2.createSecureServer(options);
this.setupProxy();

await global.proxy.listen(port);
let proxyPort = global.proxy.address().port;
await ADB.forwardPort(proxyPort);
return proxyPort;
}

static setupProxy() {
if (!global.proxy) {
throw new Error("proxy is null");
}

global.proxy.on("stream", (stream, headers) => {
if (headers[":scheme"] === "http") {
const http = require("http");
let url = `${headers[":scheme"]}://${headers[":authority"]}${headers[":path"]}`;
http
.get(url, { method: headers[":method"] }, proxyresp => {
let headers = Object.assign({}, proxyresp.headers);
// Filter out some prohibited headers.
["connection", "transfer-encoding", "keep-alive"].forEach(prop => {
delete headers[prop];
});
stream.respond(
Object.assign({ ":status": proxyresp.statusCode }, headers)
);
proxyresp.on("data", chunk => {
stream.write(chunk);
});
proxyresp.on("end", () => {
stream.end();
});
})
.on("error", e => {
console.log(`sock err: ${e}`);
});
return;
}
if (headers[":method"] !== "CONNECT") {
// Only accept CONNECT requests
stream.respond({ ":status": 405 });
stream.end();
return;
}

const target = headers[":authority"];
const { port } = new URL(`https://${target}`);
const net = require("net");
const socket = net.connect(port, "127.0.0.1", () => {
try {
stream.respond({ ":status": 200 });
socket.pipe(stream);
stream.pipe(socket);
} catch (exception) {
console.log(exception);
stream.close();
}
});
socket.on("error", error => {
// On windows platform, the sockeet is closed with ECONNRESET in the end.
if (error.errno == "ECONNRESET") {
return;
}
throw `Unxpected error when conneting the HTTP/2 server from the HTTP/2 proxy during CONNECT handling: '${error}'`;
});
});
}
}

class NodeHTTP2ProxyServer extends BaseHTTPProxy {
Expand Down
8 changes: 0 additions & 8 deletions netwerk/test/unit/test_servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,6 @@ add_task(async function test_https_proxy() {
equal(req.status, Cr.NS_OK);
equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
equal(buff, server.constructor.name);
equal(
req.QueryInterface(Ci.nsIHttpChannel).protocolVersion,
server.constructor.name == "NodeHTTP2Server" ? "h2" : "http/1.1"
);
}
);
});
Expand Down Expand Up @@ -275,10 +271,6 @@ add_task(async function test_http2_proxy() {
equal(req.status, Cr.NS_OK);
equal(req.QueryInterface(Ci.nsIHttpChannel).responseStatus, 200);
equal(buff, server.constructor.name);
equal(
req.QueryInterface(Ci.nsIHttpChannel).protocolVersion,
server.constructor.name == "NodeHTTP2Server" ? "h2" : "http/1.1"
);
}
);
});

0 comments on commit 57cf34b

Please sign in to comment.