summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/head_utils.js
blob: 76e7ed6fd38e41d836cd96714e248b5229e099e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */
/* global __LOCATION__ */
/* import-globals-from ../httpd.js */

var _HTTPD_JS_PATH = __LOCATION__.parent;
_HTTPD_JS_PATH.append("httpd.js");
load(_HTTPD_JS_PATH.path);

// if these tests fail, we'll want the debug output
var linDEBUG = true;

var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
var { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");

/**
 * Constructs a new nsHttpServer instance.  This function is intended to
 * encapsulate construction of a server so that at some point in the future it
 * is possible to run these tests (with at most slight modifications) against
 * the server when used as an XPCOM component (not as an inline script).
 */
function createServer() {
  return new nsHttpServer();
}

/**
 * Creates a new HTTP channel.
 *
 * @param url
 *   the URL of the channel to create
 */
function makeChannel(url) {
  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

/**
 * Make a binary input stream wrapper for the given stream.
 *
 * @param stream
 *   the nsIInputStream to wrap
 */
function makeBIS(stream) {
  return new BinaryInputStream(stream);
}

/**
 * Returns the contents of the file as a string.
 *
 * @param file : nsIFile
 *   the file whose contents are to be read
 * @returns string
 *   the contents of the file
 */
function fileContents(file) {
  const PR_RDONLY = 0x01;
  var fis = new FileInputStream(
    file,
    PR_RDONLY,
    0o444,
    Ci.nsIFileInputStream.CLOSE_ON_EOF
  );
  var sis = new ScriptableInputStream(fis);
  var contents = sis.read(file.fileSize);
  sis.close();
  return contents;
}

/**
 * Iterates over the lines, delimited by CRLF, in data, returning each line
 * without the trailing line separator.
 *
 * @param data : string
 *   a string consisting of lines of data separated by CRLFs
 * @returns Iterator
 *   an Iterator which returns each line from data in turn; note that this
 *   includes a final empty line if data ended with a CRLF
 */
function* LineIterator(data) {
  var index = 0;
  do {
    index = data.indexOf("\r\n");
    if (index >= 0) {
      yield data.substring(0, index);
    } else {
      yield data;
    }

    data = data.substring(index + 2);
  } while (index >= 0);
}

/**
 * Throws if iter does not contain exactly the CRLF-separated lines in the
 * array expectedLines.
 *
 * @param iter : Iterator
 *   an Iterator which returns lines of text
 * @param expectedLines : [string]
 *   an array of the expected lines of text
 * @throws an error message if iter doesn't agree with expectedLines
 */
function expectLines(iter, expectedLines) {
  var index = 0;
  for (var line of iter) {
    if (expectedLines.length == index) {
      throw new Error(
        `Error: got more than ${expectedLines.length} expected lines!`
      );
    }

    var expected = expectedLines[index++];
    if (expected !== line) {
      throw new Error(`Error on line ${index}!
  actual: '${line}',
  expect: '${expected}'`);
    }
  }

  if (expectedLines.length !== index) {
    throw new Error(
      `Expected more lines!  Got ${index}, expected ${expectedLines.length}`
    );
  }
}

/**
 * Spew a bunch of HTTP metadata from request into the body of response.
 *
 * @param request : nsIHttpRequest
 *   the request whose metadata should be output
 * @param response : nsIHttpResponse
 *   the response to which the metadata is written
 */
function writeDetails(request, response) {
  response.write("Method:  " + request.method + "\r\n");
  response.write("Path:    " + request.path + "\r\n");
  response.write("Query:   " + request.queryString + "\r\n");
  response.write("Version: " + request.httpVersion + "\r\n");
  response.write("Scheme:  " + request.scheme + "\r\n");
  response.write("Host:    " + request.host + "\r\n");
  response.write("Port:    " + request.port);
}

/**
 * Advances iter past all non-blank lines and a single blank line, after which
 * point the body of the response will be returned next from the iterator.
 *
 * @param iter : Iterator
 *   an iterator over the CRLF-delimited lines in an HTTP response, currently
 *   just after the Request-Line
 */
function skipHeaders(iter) {
  var line = iter.next().value;
  while (line !== "") {
    line = iter.next().value;
  }
}

/**
 * Checks that the exception e (which may be an XPConnect-created exception
 * object or a raw nsresult number) is the given nsresult.
 *
 * @param e : Exception or nsresult
 *   the actual exception
 * @param code : nsresult
 *   the expected exception
 */
function isException(e, code) {
  if (e !== code && e.result !== code) {
    do_throw("unexpected error: " + e);
  }
}

/**
 * Calls the given function at least the specified number of milliseconds later.
 * The callback will not undershoot the given time, but it might overshoot --
 * don't expect precision!
 *
 * @param milliseconds : uint
 *   the number of milliseconds to delay
 * @param callback : function() : void
 *   the function to call
 */
function callLater(msecs, callback) {
  do_timeout(msecs, callback);
}

/** *****************************************************
 * SIMPLE SUPPORT FOR LOADING/TESTING A SERIES OF URLS *
 *******************************************************/

/**
 * Create a completion callback which will stop the given server and end the
 * test, assuming nothing else remains to be done at that point.
 */
function testComplete(srv) {
  return function complete() {
    do_test_pending();
    srv.stop(function quit() {
      do_test_finished();
    });
  };
}

/**
 * Represents a path to load from the tested HTTP server, along with actions to
 * take before, during, and after loading the associated page.
 *
 * @param path
 *   the URL to load from the server
 * @param initChannel
 *   a function which takes as a single parameter a channel created for path and
 *   initializes its state, or null if no additional initialization is needed
 * @param onStartRequest
 *   called during onStartRequest for the load of the URL, with the same
 *   parameters; the request parameter has been QI'd to nsIHttpChannel and
 *   nsIHttpChannelInternal for convenience; may be null if nothing needs to be
 *   done
 * @param onStopRequest
 *   called during onStopRequest for the channel, with the same parameters plus
 *   a trailing parameter containing an array of the bytes of data downloaded in
 *   the body of the channel response; the request parameter has been QI'd to
 *   nsIHttpChannel and nsIHttpChannelInternal for convenience; may be null if
 *   nothing needs to be done
 */
function Test(path, initChannel, onStartRequest, onStopRequest) {
  function nil() {}

  this.path = path;
  this.initChannel = initChannel || nil;
  this.onStartRequest = onStartRequest || nil;
  this.onStopRequest = onStopRequest || nil;
}

/**
 * Runs all the tests in testArray.
 *
 * @param testArray
 *   a non-empty array of Tests to run, in order
 * @param done
 *   function to call when all tests have run (e.g. to shut down the server)
 */
function runHttpTests(testArray, done) {
  /** Kicks off running the next test in the array. */
  function performNextTest() {
    if (++testIndex == testArray.length) {
      try {
        done();
      } catch (e) {
        do_report_unexpected_exception(e, "running test-completion callback");
      }
      return;
    }

    do_test_pending();

    var test = testArray[testIndex];
    var ch = makeChannel(test.path);
    try {
      test.initChannel(ch);
    } catch (e) {
      try {
        do_report_unexpected_exception(
          e,
          "testArray[" + testIndex + "].initChannel(ch)"
        );
      } catch (x) {
        /* swallow and let tests continue */
      }
    }

    listener._channel = ch;
    ch.asyncOpen(listener);
  }

  /** Index of the test being run. */
  var testIndex = -1;

  /** Stream listener for the channels. */
  var listener = {
    /** Current channel being observed by this. */
    _channel: null,
    /** Array of bytes of data in body of response. */
    _data: [],

    onStartRequest(request) {
      Assert.ok(request === this._channel);
      var ch = request
        .QueryInterface(Ci.nsIHttpChannel)
        .QueryInterface(Ci.nsIHttpChannelInternal);

      this._data.length = 0;
      try {
        try {
          testArray[testIndex].onStartRequest(ch);
        } catch (e) {
          do_report_unexpected_exception(
            e,
            "testArray[" + testIndex + "].onStartRequest"
          );
        }
      } catch (e) {
        do_note_exception(
          e,
          "!!! swallowing onStartRequest exception so onStopRequest is " +
            "called..."
        );
      }
    },
    onDataAvailable(request, inputStream, offset, count) {
      var quantum = 262144; // just above half the argument-count limit
      var bis = makeBIS(inputStream);
      for (var start = 0; start < count; start += quantum) {
        var newData = bis.readByteArray(Math.min(quantum, count - start));
        Array.prototype.push.apply(this._data, newData);
      }
    },
    onStopRequest(request, status) {
      this._channel = null;

      var ch = request
        .QueryInterface(Ci.nsIHttpChannel)
        .QueryInterface(Ci.nsIHttpChannelInternal);

      // NB: The onStopRequest callback must run before performNextTest here,
      //     because the latter runs the next test's initChannel callback, and
      //     we want one test to be sequentially processed before the next
      //     one.
      try {
        testArray[testIndex].onStopRequest(ch, status, this._data);
      } catch (e) {
        do_report_unexpected_exception(
          e,
          "testArray[" + testIndex + "].onStartRequest"
        );
      } finally {
        try {
          performNextTest();
        } finally {
          do_test_finished();
        }
      }
    },
    QueryInterface: ChromeUtils.generateQI([
      "nsIStreamListener",
      "nsIRequestObserver",
    ]),
  };

  performNextTest();
}

/** **************************************
 * RAW REQUEST FORMAT TESTING FUNCTIONS *
 ****************************************/

/**
 * Sends a raw string of bytes to the given host and port and checks that the
 * response is acceptable.
 *
 * @param host : string
 *   the host to which a connection should be made
 * @param port : PRUint16
 *   the port to use for the connection
 * @param data : string or [string...]
 *   either:
 *     - the raw data to send, as a string of characters with codes in the
 *       range 0-255, or
 *     - an array of such strings whose concatenation forms the raw data
 * @param responseCheck : function(string) : void
 *   a function which is provided with the data sent by the remote host which
 *   conducts whatever tests it wants on that data; useful for tweaking the test
 *   environment between tests
 */
function RawTest(host, port, data, responseCheck) {
  if (0 > port || 65535 < port || port % 1 !== 0) {
    throw new Error("bad port");
  }
  if (!(data instanceof Array)) {
    data = [data];
  }
  if (data.length <= 0) {
    throw new Error("bad data length");
  }

  if (
    !data.every(function (v) {
      // eslint-disable-next-line no-control-regex
      return /^[\x00-\xff]*$/.test(v);
    })
  ) {
    throw new Error("bad data contained non-byte-valued character");
  }

  this.host = host;
  this.port = port;
  this.data = data;
  this.responseCheck = responseCheck;
}

/**
 * Runs all the tests in testArray, an array of RawTests.
 *
 * @param testArray : [RawTest]
 *   an array of RawTests to run, in order
 * @param done
 *   function to call when all tests have run (e.g. to shut down the server)
 * @param beforeTestCallback
 *   function to call before each test is run. Gets passed testIndex when called
 */
function runRawTests(testArray, done, beforeTestCallback) {
  do_test_pending();

  var sts = Cc["@mozilla.org/network/socket-transport-service;1"].getService(
    Ci.nsISocketTransportService
  );

  var currentThread =
    Cc["@mozilla.org/thread-manager;1"].getService().currentThread;

  /** Kicks off running the next test in the array. */
  function performNextTest() {
    if (++testIndex == testArray.length) {
      do_test_finished();
      try {
        done();
      } catch (e) {
        do_report_unexpected_exception(e, "running test-completion callback");
      }
      return;
    }

    if (beforeTestCallback) {
      try {
        beforeTestCallback(testIndex);
      } catch (e) {
        /* We don't care if this call fails */
      }
    }

    var rawTest = testArray[testIndex];

    var transport = sts.createTransport(
      [],
      rawTest.host,
      rawTest.port,
      null,
      null
    );

    var inStream = transport.openInputStream(0, 0, 0);
    var outStream = transport.openOutputStream(0, 0, 0);

    // reset
    dataIndex = 0;
    received = "";

    waitForMoreInput(inStream);
    waitToWriteOutput(outStream);
  }

  function waitForMoreInput(stream) {
    reader.stream = stream;
    stream = stream.QueryInterface(Ci.nsIAsyncInputStream);
    stream.asyncWait(reader, 0, 0, currentThread);
  }

  function waitToWriteOutput(stream) {
    // Do the QueryInterface here, not earlier, because there is no
    // guarantee that 'stream' passed in here been QIed to nsIAsyncOutputStream
    // since the last GC.
    stream = stream.QueryInterface(Ci.nsIAsyncOutputStream);
    stream.asyncWait(
      writer,
      0,
      testArray[testIndex].data[dataIndex].length,
      currentThread
    );
  }

  /** Index of the test being run. */
  var testIndex = -1;

  /**
   * Index of remaining data strings to be written to the socket in current
   * test.
   */
  var dataIndex = 0;

  /** Data received so far from the server. */
  var received = "";

  /** Reads data from the socket. */
  var reader = {
    onInputStreamReady(stream) {
      Assert.ok(stream === this.stream);
      try {
        var bis = new BinaryInputStream(stream);

        var av = 0;
        try {
          av = bis.available();
        } catch (e) {
          /* default to 0 */
          do_note_exception(e);
        }

        if (av > 0) {
          var quantum = 262144;
          for (var start = 0; start < av; start += quantum) {
            var bytes = bis.readByteArray(Math.min(quantum, av - start));
            received += String.fromCharCode.apply(null, bytes);
          }
          waitForMoreInput(stream);
          return;
        }
      } catch (e) {
        do_report_unexpected_exception(e);
      }

      var rawTest = testArray[testIndex];
      try {
        rawTest.responseCheck(received);
      } catch (e) {
        do_report_unexpected_exception(e);
      } finally {
        try {
          stream.close();
          performNextTest();
        } catch (e) {
          do_report_unexpected_exception(e);
        }
      }
    },
  };

  /** Writes data to the socket. */
  var writer = {
    onOutputStreamReady(stream) {
      var str = testArray[testIndex].data[dataIndex];

      var written = 0;
      try {
        written = stream.write(str, str.length);
        if (written == str.length) {
          dataIndex++;
        } else {
          testArray[testIndex].data[dataIndex] = str.substring(written);
        }
      } catch (e) {
        do_note_exception(e);
        /* stream could have been closed, just ignore */
      }

      try {
        // Keep writing data while we can write and
        // until there's no more data to read
        if (written > 0 && dataIndex < testArray[testIndex].data.length) {
          waitToWriteOutput(stream);
        } else {
          stream.close();
        }
      } catch (e) {
        do_report_unexpected_exception(e);
      }
    },
  };

  performNextTest();
}