10
10
* httpd.js.
11
11
*/
12
12
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
-
48
13
const CC = Components . Constructor ;
49
14
50
15
const PR_UINT32_MAX = Math . pow ( 2 , 32 ) - 1 ;
@@ -63,14 +28,12 @@ var DEBUG_TIMESTAMP = false; // non-const so tweakable in server tests
63
28
* @param {boolean } debugTimestamp
64
29
* Enables timestamping of the debugging output.
65
30
*/
66
- function setDebuggingStatus ( debug , debugTimestamp ) {
31
+ export function setDebuggingStatus ( debug , debugTimestamp ) {
67
32
DEBUG = debug ;
68
33
DEBUG_TIMESTAMP = debugTimestamp ;
69
34
}
70
35
71
- const { AppConstants } = ChromeUtils . importESModule (
72
- "resource://gre/modules/AppConstants.sys.mjs"
73
- ) ;
36
+ import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs" ;
74
37
75
38
/**
76
39
* Asserts that the given condition holds. If it doesn't, the given message is
@@ -98,10 +61,11 @@ function NS_ASSERT(cond, msg) {
98
61
}
99
62
100
63
/** Constructs an HTTP error object. */
101
- function HttpError ( code , description ) {
64
+ export function HttpError ( code , description ) {
102
65
this . code = code ;
103
66
this . description = description ;
104
67
}
68
+
105
69
HttpError . prototype = {
106
70
toString ( ) {
107
71
return this . code + " " + this . description ;
@@ -111,30 +75,30 @@ HttpError.prototype = {
111
75
/**
112
76
* Errors thrown to trigger specific HTTP server responses.
113
77
*/
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" ) ;
138
102
139
103
/** Creates a hash with fields corresponding to the values in arr. */
140
104
function array2obj ( arr ) {
@@ -183,7 +147,7 @@ const SJS_TYPE = "sjs";
183
147
var firstStamp = 0 ;
184
148
185
149
/** dump(str) with a trailing "\n" -- only outputs if DEBUG. */
186
- function dumpn ( str ) {
150
+ export function dumpn ( str ) {
187
151
if ( DEBUG ) {
188
152
var prefix = "HTTPD-INFO | " ;
189
153
if ( DEBUG_TIMESTAMP ) {
@@ -270,7 +234,7 @@ var BinaryOutputStream = CC(
270
234
"setOutputStream"
271
235
) ;
272
236
273
- function overrideBinaryStreamsForTests (
237
+ export function overrideBinaryStreamsForTests (
274
238
inputStream ,
275
239
outputStream ,
276
240
responseSegmentSize
@@ -371,28 +335,10 @@ function toDateString(date) {
371
335
return rv . replace ( "%date1%" , toDate1 ( date ) ) ;
372
336
}
373
337
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
-
392
338
/**
393
339
* Instantiates a new HTTP server.
394
340
*/
395
- function nsHttpServer ( ) {
341
+ export function nsHttpServer ( ) {
396
342
/** The port on which this server listens. */
397
343
this . _port = undefined ;
398
344
@@ -429,6 +375,7 @@ function nsHttpServer() {
429
375
*/
430
376
this . _connections = { } ;
431
377
}
378
+
432
379
nsHttpServer . prototype = {
433
380
// NSISERVERSOCKETLISTENER
434
381
@@ -912,9 +859,9 @@ nsHttpServer.prototype = {
912
859
} ,
913
860
} ;
914
861
915
- var HttpServer = nsHttpServer ;
862
+ export var HttpServer = nsHttpServer ;
916
863
917
- class NodeServer {
864
+ export class NodeServer {
918
865
// Executes command in the context of a node server.
919
866
// See handler in moz-http2.js
920
867
//
@@ -2084,7 +2031,7 @@ function findCRLF(array, start) {
2084
2031
* A container which provides line-by-line access to the arrays of bytes with
2085
2032
* which it is seeded.
2086
2033
*/
2087
- function LineData ( ) {
2034
+ export function LineData ( ) {
2088
2035
/** An array of queued bytes from which to get line-based characters. */
2089
2036
this . _data = [ ] ;
2090
2037
@@ -2928,7 +2875,7 @@ ServerHandler.prototype = {
2928
2875
// If you update the list of imports, please update the list in
2929
2876
// tools/lint/eslint/eslint-plugin-mozilla/lib/environments/sjs.js
2930
2877
// as well.
2931
- var s = Cu . Sandbox ( globalThis ) ;
2878
+ var s = Cu . Sandbox ( Cu . getGlobalForObject ( { } ) ) ;
2932
2879
s . importFunction ( dump , "dump" ) ;
2933
2880
s . importFunction ( atob , "atob" ) ;
2934
2881
s . importFunction ( btoa , "btoa" ) ;
@@ -4605,7 +4552,7 @@ function wouldBlock(e) {
4605
4552
* @throws NS_ERROR_NULL_POINTER
4606
4553
* if source, sink, or observer are null
4607
4554
*/
4608
- function WriteThroughCopier ( source , sink , observer , context ) {
4555
+ export function WriteThroughCopier ( source , sink , observer , context ) {
4609
4556
if ( ! source || ! sink || ! observer ) {
4610
4557
throw Components . Exception ( "" , Cr . NS_ERROR_NULL_POINTER ) ;
4611
4558
}
@@ -5285,7 +5232,7 @@ nsHttpVersion.HTTP_1_1 = new nsHttpVersion("1.1");
5285
5232
* values returned by .enumerator may not be equal case-sensitively to the
5286
5233
* values passed to setHeader when adding headers to this.
5287
5234
*/
5288
- function nsHttpHeaders ( ) {
5235
+ export function nsHttpHeaders ( ) {
5289
5236
/**
5290
5237
* A hash of headers, with header field names as the keys and header field
5291
5238
* values as the values. Header field names are case-insensitive, but upon
@@ -5613,60 +5560,3 @@ Request.prototype = {
5613
5560
}
5614
5561
} ,
5615
5562
} ;
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
- }
0 commit comments