Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit c372471

Browse files
committed
Backed out changeset 2a40bdf17bea (bug 1584397) for causing test_http2-proxy.js to permafail CLOSED TREE
1 parent 99449af commit c372471

6 files changed

Lines changed: 169 additions & 390 deletions

File tree

netwerk/test/httpserver/httpd.js

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -880,43 +880,25 @@ nsHttpServer.prototype = {
880880
var HttpServer = nsHttpServer;
881881

882882
class NodeServer {
883-
// Executes command in the context of a node server.
883+
// Executes command on the already running node server
884884
// See handler in moz-http2.js
885885
//
886886
// Example use:
887-
// let id = NodeServer.fork(); // id is a random string
888-
// await NodeServer.execute(id, `"hello"`)
887+
// await NodeServer.execute(`"hello"`)
889888
// > "hello"
890-
// await NodeServer.execute(id, `(() => "hello")()`)
889+
// await NodeServer.execute(`(() => "hello")()`)
891890
// > "hello"
892-
// await NodeServer.execute(id, `(() => var_defined_on_server)()`)
891+
// await NodeServer.execute(`(() => var_defined_on_server)()`)
893892
// > "0"
894-
// await NodeServer.execute(id, `var_defined_on_server`)
893+
// await NodeServer.execute(`var_defined_on_server`)
895894
// > "0"
896895
// function f(param) { if (param) return param; return "bla"; }
897-
// await NodeServer.execute(id, f); // Defines the function on the server
898-
// await NodeServer.execute(id, `f()`) // executes defined function
896+
// await NodeServer.execute(f); // Defines the function on the server
897+
// await NodeServer.execute(`f()`) // executes defined function
899898
// > "bla"
900-
// let result = await NodeServer.execute(id, `f("test")`);
899+
// let result = await NodeServer.execute(`f("test")`);
901900
// > "test"
902-
// await NodeServer.kill(id); // shuts down the server
903-
904-
// Forks a new node server using moz-http2-child.js as a starting point
905-
static fork() {
906-
return this.sendCommand("", "/fork");
907-
}
908-
// Executes command in the context of the node server indicated by `id`
909-
static execute(id, command) {
910-
return this.sendCommand(command, `/execute/${id}`);
911-
}
912-
// Shuts down the server
913-
static kill(id) {
914-
return this.sendCommand("", `/kill/${id}`);
915-
}
916-
917-
// Issues a request to the node server (handler defined in moz-http2.js)
918-
// This method should not be called directly.
919-
static sendCommand(command, path) {
901+
static execute(command) {
920902
let env = Cc["@mozilla.org/process/environment;1"].getService(
921903
Ci.nsIEnvironment
922904
);
@@ -926,7 +908,7 @@ class NodeServer {
926908
}
927909

928910
let req = new XMLHttpRequest();
929-
req.open("POST", `http://127.0.0.1:${h2Port}${path}`);
911+
req.open("POST", `http://127.0.0.1:${h2Port}/execute`);
930912

931913
// Passing a function to NodeServer.execute will define that function
932914
// in node. It can be called in a later execute command.

netwerk/test/unit/test_http2-proxy.js

Lines changed: 7 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const { NodeServer } = ChromeUtils.import("resource://testing-common/httpd.js");
2626

2727
let proxy_port;
2828
let server_port;
29+
let proxy_id;
2930
let filter;
3031

3132
// See moz-http2
@@ -41,11 +42,7 @@ class ProxyFilter {
4142
this.QueryInterface = ChromeUtils.generateQI([Ci.nsIProtocolProxyFilter]);
4243
}
4344
applyFilter(pps, uri, pi, cb) {
44-
if (
45-
uri.pathQueryRef.startsWith("/execute") ||
46-
uri.pathQueryRef.startsWith("/fork") ||
47-
uri.pathQueryRef.startsWith("/kill")
48-
) {
45+
if (uri.pathQueryRef == "/execute") {
4946
// So we allow NodeServer.execute to work
5047
cb.onProxyFilterResult(pi);
5148
return;
@@ -118,154 +115,13 @@ function get_response(channel, flags = CL_ALLOW_UNKNOWN_CL) {
118115

119116
let initial_session_count = 0;
120117

121-
class http2ProxyCode {
122-
static listen(server, envport) {
123-
if (!server) {
124-
return Promise.resolve(0);
125-
}
126-
127-
let portSelection = 0;
128-
if (envport !== undefined) {
129-
try {
130-
portSelection = parseInt(envport, 10);
131-
} catch (e) {
132-
portSelection = -1;
133-
}
134-
}
135-
return new Promise(resolve => {
136-
server.listen(portSelection, "0.0.0.0", 2000, () => {
137-
resolve(server.address().port);
138-
});
139-
});
140-
}
141-
142-
static startNewProxy() {
143-
const fs = require("fs");
144-
const options = {
145-
key: fs.readFileSync(__dirname + "/http2-cert.key"),
146-
cert: fs.readFileSync(__dirname + "/http2-cert.pem"),
147-
};
148-
const http2 = require("http2");
149-
global.proxy = http2.createSecureServer(options);
150-
this.setupProxy();
151-
return http2ProxyCode.listen(proxy).then(port => {
152-
return { port, success: true };
153-
});
154-
}
155-
156-
static closeProxy() {
157-
proxy.closeSockets();
158-
return new Promise(resolve => {
159-
proxy.close(resolve);
160-
});
161-
}
162-
163-
static proxySessionCount() {
164-
if (!proxy) {
165-
return 0;
166-
}
167-
return proxy.proxy_session_count;
168-
}
169-
170-
static setupProxy() {
171-
if (!proxy) {
172-
throw new Error("proxy is null");
173-
}
174-
proxy.proxy_session_count = 0;
175-
proxy.on("session", () => {
176-
++proxy.proxy_session_count;
177-
});
178-
179-
// We need to track active connections so we can forcefully close keep-alive
180-
// connections when shutting down the proxy.
181-
proxy.socketIndex = 0;
182-
proxy.socketMap = {};
183-
proxy.on("connection", function(socket) {
184-
let index = proxy.socketIndex++;
185-
proxy.socketMap[index] = socket;
186-
socket.on("close", function() {
187-
delete proxy.socketMap[index];
188-
});
189-
});
190-
proxy.closeSockets = function() {
191-
for (let i in proxy.socketMap) {
192-
proxy.socketMap[i].destroy();
193-
}
194-
};
195-
196-
proxy.on("stream", (stream, headers) => {
197-
if (headers[":method"] !== "CONNECT") {
198-
// Only accept CONNECT requests
199-
stream.respond({ ":status": 405 });
200-
stream.end();
201-
return;
202-
}
203-
204-
const target = headers[":authority"];
205-
206-
const authorization_token = headers["proxy-authorization"];
207-
if (
208-
"authorization-token" != authorization_token ||
209-
target == "407.example.com:443"
210-
) {
211-
stream.respond({ ":status": 407 });
212-
// Deliberately send no Proxy-Authenticate header
213-
stream.end();
214-
return;
215-
}
216-
if (target == "404.example.com:443") {
217-
// 404 Not Found, a response code that a proxy should return when the host can't be found
218-
stream.respond({ ":status": 404 });
219-
stream.end();
220-
return;
221-
}
222-
if (target == "429.example.com:443") {
223-
// 429 Too Many Requests, a response code that a proxy should return when receiving too many requests
224-
stream.respond({ ":status": 429 });
225-
stream.end();
226-
return;
227-
}
228-
if (target == "502.example.com:443") {
229-
// 502 Bad Gateway, a response code mostly resembling immediate connection error
230-
stream.respond({ ":status": 502 });
231-
stream.end();
232-
return;
233-
}
234-
if (target == "504.example.com:443") {
235-
// 504 Gateway Timeout, did not receive a timely response from an upstream server
236-
stream.respond({ ":status": 504 });
237-
stream.end();
238-
return;
239-
}
240-
241-
const net = require("net");
242-
const socket = net.connect(serverPort, "127.0.0.1", () => {
243-
try {
244-
stream.respond({ ":status": 200 });
245-
socket.pipe(stream);
246-
stream.pipe(socket);
247-
} catch (exception) {
248-
console.log(exception);
249-
stream.close();
250-
}
251-
});
252-
socket.on("error", error => {
253-
throw `Unxpected error when conneting the HTTP/2 server from the HTTP/2 proxy during CONNECT handling: '${error}'`;
254-
});
255-
});
256-
}
257-
}
258-
259118
function proxy_session_counter() {
260119
return new Promise(async resolve => {
261-
let data = await NodeServer.execute(
262-
processId,
263-
`http2ProxyCode.proxySessionCount()`
264-
);
120+
let data = await NodeServer.execute(`proxySessionCount("${proxy_id}")`);
265121
resolve(parseInt(data) - initial_session_count);
266122
});
267123
}
268-
let processId;
124+
269125
add_task(async function setup() {
270126
// Set to allow the cert presented by our H2 server
271127
do_get_profile();
@@ -282,14 +138,9 @@ add_task(async function setup() {
282138
);
283139
server_port = env.get("MOZHTTP2_PORT");
284140
Assert.notEqual(server_port, null);
285-
processId = await NodeServer.fork();
286-
await NodeServer.execute(processId, `serverPort = ${server_port}`);
287-
await NodeServer.execute(processId, http2ProxyCode);
288-
let proxy = await NodeServer.execute(
289-
processId,
290-
`http2ProxyCode.startNewProxy()`
291-
);
141+
let proxy = await NodeServer.execute(`startNewProxy()`);
292142
proxy_port = proxy.port;
143+
proxy_id = proxy.name;
293144
Assert.notEqual(proxy_port, null);
294145

295146
Services.prefs.setStringPref(
@@ -318,8 +169,7 @@ registerCleanupFunction(async () => {
318169

319170
pps.unregisterFilter(filter);
320171

321-
await NodeServer.execute(processId, `http2ProxyCode.closeProxy()`);
322-
await NodeServer.kill(processId);
172+
await NodeServer.execute(`closeProxy("${proxy_id}")`);
323173
});
324174

325175
/**

netwerk/test/unit/test_node_execute.js

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,79 +10,27 @@ add_task(async function test_execute() {
1010
function f() {
1111
return "bla";
1212
}
13-
let id = await NodeServer.fork();
14-
equal(await NodeServer.execute(id, `"hello"`), "hello");
15-
equal(await NodeServer.execute(id, `(() => "hello")()`), "hello");
16-
equal(await NodeServer.execute(id, `my_defined_var = 1;`), 1);
17-
equal(await NodeServer.execute(id, `(() => my_defined_var)()`), 1);
18-
equal(await NodeServer.execute(id, `my_defined_var`), 1);
13+
equal(await NodeServer.execute(`"hello"`), "hello");
14+
equal(await NodeServer.execute(`(() => "hello")()`), "hello");
15+
equal(await NodeServer.execute(`my_defined_var = 0;`), 0);
16+
equal(await NodeServer.execute(`(() => my_defined_var)()`), 0);
17+
equal(await NodeServer.execute(`my_defined_var`), 0);
1918

20-
await NodeServer.execute(id, `not_defined_var`)
19+
await NodeServer.execute(`not_defined_var`)
2120
.then(() => {
2221
ok(false, "should have thrown");
2322
})
2423
.catch(e => {
2524
equal(e.message, "ReferenceError: not_defined_var is not defined");
26-
ok(
27-
e.stack.includes("moz-http2-child.js"),
28-
`stack should be coming from moz-http2-child.js - ${e.stack}`
29-
);
30-
});
31-
await NodeServer.execute("definitely_wrong_id", `"hello"`)
32-
.then(() => {
33-
ok(false, "should have thrown");
34-
})
35-
.catch(e => {
36-
equal(e.message, "Error: could not find id");
3725
ok(
3826
e.stack.includes("moz-http2.js"),
39-
`stack should be coming from moz-http2.js - ${e.stack}`
27+
`stack should ve coming from moz-http2.js - ${e.stack}`
4028
);
4129
});
42-
43-
// Defines f in the context of the node server.
44-
// The implementation of NodeServer.execute prepends `functionName =` to the
45-
// body of the function we pass so it gets attached to the global context
46-
// in the server.
47-
equal(await NodeServer.execute(id, f), undefined);
48-
equal(await NodeServer.execute(id, `f()`), "bla");
49-
50-
class myClass {
51-
static doStuff() {
52-
return my_defined_var;
53-
}
54-
}
55-
56-
equal(await NodeServer.execute(id, myClass), undefined);
57-
equal(await NodeServer.execute(id, `myClass.doStuff()`), 1);
58-
59-
equal(await NodeServer.kill(id), undefined);
60-
await NodeServer.execute(id, `f()`)
61-
.then(() => ok(false, "should throw"))
62-
.catch(e => equal(e.message, "Error: could not find id"));
63-
id = await NodeServer.fork();
64-
// Check that a child process dying during a call throws an error.
65-
await NodeServer.execute(id, `process.exit()`)
66-
.then(() => ok(false, "should throw"))
67-
.catch(e =>
68-
equal(e.message, "child process exit closing code: 0 signal: null")
69-
);
70-
71-
id = await NodeServer.fork();
72-
equal(
73-
await NodeServer.execute(
74-
id,
75-
`setTimeout(function() { sendBackResponse(undefined) }, 0); 2`
76-
),
77-
2
78-
);
79-
await new Promise(resolve => do_timeout(10, resolve));
80-
await NodeServer.kill(id)
81-
.then(() => ok(false, "should throw"))
82-
.catch(e =>
83-
equal(
84-
e.message,
85-
`forked process without handler sent: {"error":"","errorStack":""}\n`
86-
)
87-
);
30+
equal(await NodeServer.execute(f), undefined);
31+
equal(await NodeServer.execute(`f()`), "bla");
32+
let result = await NodeServer.execute(`startNewProxy()`);
33+
equal(result.success, true);
34+
info(JSON.stringify(result));
35+
equal(await NodeServer.execute(`closeProxy("${result.name}")`), undefined);
8836
});

netwerk/test/unit/test_trr.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,15 @@ class DNSListener {
145145
}
146146
}
147147

148-
Cu.importGlobalProperties(["fetch"]);
149-
150148
add_task(async function test0_nodeExecute() {
151149
// This test checks that moz-http2.js running in node is working.
152150
// This should always be the first test in this file (except for setup)
153151
// otherwise we may encounter random failures when the http2 server is down.
154-
155-
let env = Cc["@mozilla.org/process/environment;1"].getService(
156-
Ci.nsIEnvironment
152+
equal(
153+
await NodeServer.execute(`"hello"`),
154+
"hello",
155+
"Check that moz-http2.js is running"
157156
);
158-
let execPort = env.get("MOZNODE_EXEC_PORT");
159-
await fetch(`http://127.0.0.1:${execPort}/test`)
160-
.then(response => {
161-
ok(true, "NodeServer is working");
162-
})
163-
.catch(e => {
164-
ok(false, `There was an error ${e}`);
165-
});
166157
});
167158

168159
// verify basic A record

0 commit comments

Comments
 (0)