Skip to content

Commit b63f2ba

Browse files
committed
Bug 1837176 - Converting httpd.js to an ES module. r=necko-reviewers,webdriver-reviewers,valentin,whimboo
Differential Revision: https://phabricator.services.mozilla.com/D181158
1 parent 6039273 commit b63f2ba

File tree

12 files changed

+67
-184
lines changed

12 files changed

+67
-184
lines changed

layout/tools/reftest/jar.mn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ reftest.jar:
5252
res/ReftestFissionParent.jsm (ReftestFissionParent.jsm)
5353
res/ReftestFissionChild.jsm (ReftestFissionChild.jsm)
5454
res/AsyncSpellCheckTestHelper.sys.mjs (../../../editor/AsyncSpellCheckTestHelper.sys.mjs)
55-
res/httpd.jsm (../../../netwerk/test/httpserver/httpd.js)
55+
res/httpd.sys.mjs (../../../netwerk/test/httpserver/httpd.sys.mjs)
5656
res/StructuredLog.sys.mjs (../../../testing/modules/StructuredLog.sys.mjs)
5757
res/PerTestCoverageUtils.sys.mjs (../../../tools/code-coverage/PerTestCoverageUtils.sys.mjs)
5858
res/input.css (../../../editor/reftests/xul/input.css)

layout/tools/reftest/reftest.jsm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ const {
4444

4545
g,
4646
} = ChromeUtils.import("resource://reftest/globals.jsm");
47-
const { HttpServer } = ChromeUtils.import("resource://reftest/httpd.jsm");
47+
const { HttpServer } = ChromeUtils.importESModule(
48+
"resource://reftest/httpd.sys.mjs"
49+
);
4850
const { ReadTopManifest, CreateUrls } = ChromeUtils.import(
4951
"resource://reftest/manifest.jsm"
5052
);

netwerk/test/httpserver/httpd.js renamed to netwerk/test/httpserver/httpd.sys.mjs

Lines changed: 38 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,6 @@
1010
* httpd.js.
1111
*/
1212

13-
var EXPORTED_SYMBOLS = [
14-
"dumpn",
15-
"HTTP_400",
16-
"HTTP_401",
17-
"HTTP_402",
18-
"HTTP_403",
19-
"HTTP_404",
20-
"HTTP_405",
21-
"HTTP_406",
22-
"HTTP_407",
23-
"HTTP_408",
24-
"HTTP_409",
25-
"HTTP_410",
26-
"HTTP_411",
27-
"HTTP_412",
28-
"HTTP_413",
29-
"HTTP_414",
30-
"HTTP_415",
31-
"HTTP_417",
32-
"HTTP_500",
33-
"HTTP_501",
34-
"HTTP_502",
35-
"HTTP_503",
36-
"HTTP_504",
37-
"HTTP_505",
38-
"HttpError",
39-
"HttpServer",
40-
"LineData",
41-
"NodeServer",
42-
"nsHttpHeaders",
43-
"overrideBinaryStreamsForTests",
44-
"WriteThroughCopier",
45-
"setDebuggingStatus",
46-
];
47-
4813
const CC = Components.Constructor;
4914

5015
const PR_UINT32_MAX = Math.pow(2, 32) - 1;
@@ -63,14 +28,12 @@ var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
6328
* @param {boolean} debugTimestamp
6429
* Enables timestamping of the debugging output.
6530
*/
66-
function setDebuggingStatus(debug, debugTimestamp) {
31+
export function setDebuggingStatus(debug, debugTimestamp) {
6732
DEBUG = debug;
6833
DEBUG_TIMESTAMP = debugTimestamp;
6934
}
7035

71-
const { AppConstants } = ChromeUtils.importESModule(
72-
"resource://gre/modules/AppConstants.sys.mjs"
73-
);
36+
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
7437

7538
/**
7639
* Asserts that the given condition holds. If it doesn't, the given message is
@@ -98,10 +61,11 @@ function NS_ASSERT(cond, msg) {
9861
}
9962

10063
/** Constructs an HTTP error object. */
101-
function HttpError(code, description) {
64+
export function HttpError(code, description) {
10265
this.code = code;
10366
this.description = description;
10467
}
68+
10569
HttpError.prototype = {
10670
toString() {
10771
return this.code + " " + this.description;
@@ -111,30 +75,30 @@ HttpError.prototype = {
11175
/**
11276
* Errors thrown to trigger specific HTTP server responses.
11377
*/
114-
var HTTP_400 = new HttpError(400, "Bad Request");
115-
var HTTP_401 = new HttpError(401, "Unauthorized");
116-
var HTTP_402 = new HttpError(402, "Payment Required");
117-
var HTTP_403 = new HttpError(403, "Forbidden");
118-
var HTTP_404 = new HttpError(404, "Not Found");
119-
var HTTP_405 = new HttpError(405, "Method Not Allowed");
120-
var HTTP_406 = new HttpError(406, "Not Acceptable");
121-
var HTTP_407 = new HttpError(407, "Proxy Authentication Required");
122-
var HTTP_408 = new HttpError(408, "Request Timeout");
123-
var HTTP_409 = new HttpError(409, "Conflict");
124-
var HTTP_410 = new HttpError(410, "Gone");
125-
var HTTP_411 = new HttpError(411, "Length Required");
126-
var HTTP_412 = new HttpError(412, "Precondition Failed");
127-
var HTTP_413 = new HttpError(413, "Request Entity Too Large");
128-
var HTTP_414 = new HttpError(414, "Request-URI Too Long");
129-
var HTTP_415 = new HttpError(415, "Unsupported Media Type");
130-
var HTTP_417 = new HttpError(417, "Expectation Failed");
131-
132-
var HTTP_500 = new HttpError(500, "Internal Server Error");
133-
var HTTP_501 = new HttpError(501, "Not Implemented");
134-
var HTTP_502 = new HttpError(502, "Bad Gateway");
135-
var HTTP_503 = new HttpError(503, "Service Unavailable");
136-
var HTTP_504 = new HttpError(504, "Gateway Timeout");
137-
var HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
78+
export var HTTP_400 = new HttpError(400, "Bad Request");
79+
80+
export var HTTP_401 = new HttpError(401, "Unauthorized");
81+
export var HTTP_402 = new HttpError(402, "Payment Required");
82+
export var HTTP_403 = new HttpError(403, "Forbidden");
83+
export var HTTP_404 = new HttpError(404, "Not Found");
84+
export var HTTP_405 = new HttpError(405, "Method Not Allowed");
85+
export var HTTP_406 = new HttpError(406, "Not Acceptable");
86+
export var HTTP_407 = new HttpError(407, "Proxy Authentication Required");
87+
export var HTTP_408 = new HttpError(408, "Request Timeout");
88+
export var HTTP_409 = new HttpError(409, "Conflict");
89+
export var HTTP_410 = new HttpError(410, "Gone");
90+
export var HTTP_411 = new HttpError(411, "Length Required");
91+
export var HTTP_412 = new HttpError(412, "Precondition Failed");
92+
export var HTTP_413 = new HttpError(413, "Request Entity Too Large");
93+
export var HTTP_414 = new HttpError(414, "Request-URI Too Long");
94+
export var HTTP_415 = new HttpError(415, "Unsupported Media Type");
95+
export var HTTP_417 = new HttpError(417, "Expectation Failed");
96+
export var HTTP_500 = new HttpError(500, "Internal Server Error");
97+
export var HTTP_501 = new HttpError(501, "Not Implemented");
98+
export var HTTP_502 = new HttpError(502, "Bad Gateway");
99+
export var HTTP_503 = new HttpError(503, "Service Unavailable");
100+
export var HTTP_504 = new HttpError(504, "Gateway Timeout");
101+
export var HTTP_505 = new HttpError(505, "HTTP Version Not Supported");
138102

139103
/** Creates a hash with fields corresponding to the values in arr. */
140104
function array2obj(arr) {
@@ -183,7 +147,7 @@ const SJS_TYPE = "sjs";
183147
var firstStamp = 0;
184148

185149
/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
186-
function dumpn(str) {
150+
export function dumpn(str) {
187151
if (DEBUG) {
188152
var prefix = "HTTPD-INFO | ";
189153
if (DEBUG_TIMESTAMP) {
@@ -270,7 +234,7 @@ var BinaryOutputStream = CC(
270234
"setOutputStream"
271235
);
272236

273-
function overrideBinaryStreamsForTests(
237+
export function overrideBinaryStreamsForTests(
274238
inputStream,
275239
outputStream,
276240
responseSegmentSize
@@ -371,28 +335,10 @@ function toDateString(date) {
371335
return rv.replace("%date1%", toDate1(date));
372336
}
373337

374-
/**
375-
* Prints out a human-readable representation of the object o and its fields,
376-
* omitting those whose names begin with "_" if showMembers != true (to ignore
377-
* "private" properties exposed via getters/setters).
378-
*/
379-
function printObj(o, showMembers) {
380-
var s = "******************************\n";
381-
s += "o = {\n";
382-
for (var i in o) {
383-
if (typeof i != "string" || showMembers || (!!i.length && i[0] != "_")) {
384-
s += " " + i + ": " + o[i] + ",\n";
385-
}
386-
}
387-
s += " };\n";
388-
s += "******************************";
389-
dumpn(s);
390-
}
391-
392338
/**
393339
* Instantiates a new HTTP server.
394340
*/
395-
function nsHttpServer() {
341+
export function nsHttpServer() {
396342
/** The port on which this server listens. */
397343
this._port = undefined;
398344

@@ -429,6 +375,7 @@ function nsHttpServer() {
429375
*/
430376
this._connections = {};
431377
}
378+
432379
nsHttpServer.prototype = {
433380
// NSISERVERSOCKETLISTENER
434381

@@ -912,9 +859,9 @@ nsHttpServer.prototype = {
912859
},
913860
};
914861

915-
var HttpServer = nsHttpServer;
862+
export var HttpServer = nsHttpServer;
916863

917-
class NodeServer {
864+
export class NodeServer {
918865
// Executes command in the context of a node server.
919866
// See handler in moz-http2.js
920867
//
@@ -2084,7 +2031,7 @@ function findCRLF(array, start) {
20842031
* A container which provides line-by-line access to the arrays of bytes with
20852032
* which it is seeded.
20862033
*/
2087-
function LineData() {
2034+
export function LineData() {
20882035
/** An array of queued bytes from which to get line-based characters. */
20892036
this._data = [];
20902037

@@ -2928,7 +2875,7 @@ ServerHandler.prototype = {
29282875
// If you update the list of imports, please update the list in
29292876
// tools/lint/eslint/eslint-plugin-mozilla/lib/environments/sjs.js
29302877
// as well.
2931-
var s = Cu.Sandbox(globalThis);
2878+
var s = Cu.Sandbox(Cu.getGlobalForObject({}));
29322879
s.importFunction(dump, "dump");
29332880
s.importFunction(atob, "atob");
29342881
s.importFunction(btoa, "btoa");
@@ -4605,7 +4552,7 @@ function wouldBlock(e) {
46054552
* @throws NS_ERROR_NULL_POINTER
46064553
* if source, sink, or observer are null
46074554
*/
4608-
function WriteThroughCopier(source, sink, observer, context) {
4555+
export function WriteThroughCopier(source, sink, observer, context) {
46094556
if (!source || !sink || !observer) {
46104557
throw Components.Exception("", Cr.NS_ERROR_NULL_POINTER);
46114558
}
@@ -5285,7 +5232,7 @@ nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
52855232
* values returned by .enumerator may not be equal case-sensitively to the
52865233
* values passed to setHeader when adding headers to this.
52875234
*/
5288-
function nsHttpHeaders() {
5235+
export function nsHttpHeaders() {
52895236
/**
52905237
* A hash of headers, with header field names as the keys and header field
52915238
* values as the values. Header field names are case-insensitive, but upon
@@ -5613,60 +5560,3 @@ Request.prototype = {
56135560
}
56145561
},
56155562
};
5616-
5617-
/**
5618-
* Creates a new HTTP server listening for loopback traffic on the given port,
5619-
* starts it, and runs the server until the server processes a shutdown request,
5620-
* spinning an event loop so that events posted by the server's socket are
5621-
* processed.
5622-
*
5623-
* This method is primarily intended for use in running this script from within
5624-
* xpcshell and running a functional HTTP server without having to deal with
5625-
* non-essential details.
5626-
*
5627-
* Note that running multiple servers using variants of this method probably
5628-
* doesn't work, simply due to how the internal event loop is spun and stopped.
5629-
*
5630-
* @note
5631-
* This method only works with Mozilla 1.9 (i.e., Firefox 3 or trunk code);
5632-
* you should use this server as a component in Mozilla 1.8.
5633-
* @param port
5634-
* the port on which the server will run, or -1 if there exists no preference
5635-
* for a specific port; note that attempting to use some values for this
5636-
* parameter (particularly those below 1024) may cause this method to throw or
5637-
* may result in the server being prematurely shut down
5638-
* @param basePath
5639-
* a local directory from which requests will be served (i.e., if this is
5640-
* "/home/jwalden/" then a request to /index.html will load
5641-
* /home/jwalden/index.html); if this is omitted, only the default URLs in
5642-
* this server implementation will be functional
5643-
*/
5644-
function server(port, basePath) {
5645-
if (basePath) {
5646-
var lp = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
5647-
lp.initWithPath(basePath);
5648-
}
5649-
5650-
// if you're running this, you probably want to see debugging info
5651-
DEBUG = true;
5652-
5653-
var srv = new nsHttpServer();
5654-
if (lp) {
5655-
srv.registerDirectory("/", lp);
5656-
}
5657-
srv.registerContentType("sjs", SJS_TYPE);
5658-
srv.identity.setPrimary("http", "localhost", port);
5659-
srv.start(port);
5660-
5661-
var thread = Services.tm.currentThread;
5662-
while (!srv.isStopped()) {
5663-
thread.processNextEvent(true);
5664-
}
5665-
5666-
// get rid of any pending requests
5667-
while (thread.hasPendingEvents()) {
5668-
thread.processNextEvent(true);
5669-
}
5670-
5671-
DEBUG = false;
5672-
}

netwerk/test/httpserver/moz.build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ XPIDL_MODULE = "test_necko"
1313
XPCSHELL_TESTS_MANIFESTS += ["test/xpcshell.ini"]
1414

1515
EXTRA_COMPONENTS += [
16-
"httpd.js",
16+
"httpd.sys.mjs",
1717
]
1818

1919
TESTING_JS_MODULES += [
20-
"httpd.js",
20+
"httpd.sys.mjs",
2121
]

netwerk/test/httpserver/test/head_utils.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
const {
88
dumpn,
99
LineData,
10-
HttpServer,
1110
nsHttpHeaders,
12-
overrideBinaryStreamsForTests,
11+
HttpServer,
1312
WriteThroughCopier,
14-
} = ChromeUtils.import("resource://testing-common/httpd.js");
13+
overrideBinaryStreamsForTests,
14+
} = ChromeUtils.importESModule("resource://testing-common/httpd.sys.mjs");
15+
16+
// if these tests fail, we'll want the debug output
17+
var linDEBUG = true;
1518

1619
var { XPCOMUtils } = ChromeUtils.importESModule(
1720
"resource://gre/modules/XPCOMUtils.sys.mjs"
@@ -21,19 +24,11 @@ var { NetUtil } = ChromeUtils.importESModule(
2124
);
2225

2326
const CC = Components.Constructor;
24-
25-
const ScriptableInputStream = CC(
26-
"@mozilla.org/scriptableinputstream;1",
27-
"nsIScriptableInputStream",
28-
"init"
29-
);
3027
const FileInputStream = CC(
3128
"@mozilla.org/network/file-input-stream;1",
3229
"nsIFileInputStream",
3330
"init"
3431
);
35-
36-
/* These two are non-const only so a test can overwrite them. */
3732
var BinaryInputStream = CC(
3833
"@mozilla.org/binaryinputstream;1",
3934
"nsIBinaryInputStream",
@@ -44,6 +39,11 @@ var BinaryOutputStream = CC(
4439
"nsIBinaryOutputStream",
4540
"setOutputStream"
4641
);
42+
const ScriptableInputStream = CC(
43+
"@mozilla.org/scriptableinputstream;1",
44+
"nsIScriptableInputStream",
45+
"init"
46+
);
4747

4848
/**
4949
* Constructs a new nsHttpServer instance. This function is intended to

python/mozbuild/mozbuild/action/test_archive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
{
215215
"source": buildconfig.topobjdir,
216216
"base": "dist/bin/components",
217-
"patterns": ["httpd.js"],
217+
"patterns": ["httpd.sys.mjs"],
218218
"dest": "bin/components",
219219
},
220220
{

0 commit comments

Comments
 (0)