summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/test_request_line_split_in_two_packets.js
blob: b1d7a7d071a0d3e32769cb72a6f66039bcc5bb71 (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
/* -*- 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/. */

/**
 * Tests that even when an incoming request's data for the Request-Line doesn't
 * all fit in a single onInputStreamReady notification, the request is handled
 * properly.
 */

var srv = createServer();
srv.start(-1);
const PORT = srv.identity.primaryPort;

function run_test() {
  srv.registerPathHandler(
    "/lots-of-leading-blank-lines",
    lotsOfLeadingBlankLines
  );
  srv.registerPathHandler("/very-long-request-line", veryLongRequestLine);

  runRawTests(tests, testComplete(srv));
}

/** *************
 * BEGIN TESTS *
 ***************/

var test, gData, str;
var tests = [];

function veryLongRequestLine(request, response) {
  writeDetails(request, response);
  response.setStatusLine(request.httpVersion, 200, "TEST PASSED");
}

var reallyLong = "0123456789ABCDEF0123456789ABCDEF"; // 32
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 128
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 512
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 2048
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 8192
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 32768
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 131072
reallyLong = reallyLong + reallyLong + reallyLong + reallyLong; // 524288
if (reallyLong.length !== 524288) {
  throw new TypeError("generated length not as long as expected");
}
str =
  "GET /very-long-request-line?" +
  reallyLong +
  " HTTP/1.1\r\n" +
  "Host: localhost:" +
  PORT +
  "\r\n" +
  "\r\n";
gData = [];
for (let i = 0; i < str.length; i += 16384) {
  gData.push(str.substr(i, 16384));
}

function checkVeryLongRequestLine(data) {
  var iter = LineIterator(data);

  print("data length: " + data.length);
  print("iter object: " + iter);

  // Status-Line
  Assert.equal(iter.next().value, "HTTP/1.1 200 TEST PASSED");

  skipHeaders(iter);

  // Okay, next line must be the data we expected to be written
  var body = [
    "Method:  GET",
    "Path:    /very-long-request-line",
    "Query:   " + reallyLong,
    "Version: 1.1",
    "Scheme:  http",
    "Host:    localhost",
    "Port:    " + PORT,
  ];

  expectLines(iter, body);
}
test = new RawTest("localhost", PORT, gData, checkVeryLongRequestLine);
tests.push(test);

function lotsOfLeadingBlankLines(request, response) {
  writeDetails(request, response);
  response.setStatusLine(request.httpVersion, 200, "TEST PASSED");
}

var blankLines = "\r\n";
for (let i = 0; i < 14; i++) {
  blankLines += blankLines;
}
str =
  blankLines +
  "GET /lots-of-leading-blank-lines HTTP/1.1\r\n" +
  "Host: localhost:" +
  PORT +
  "\r\n" +
  "\r\n";
gData = [];
for (let i = 0; i < str.length; i += 100) {
  gData.push(str.substr(i, 100));
}

function checkLotsOfLeadingBlankLines(data) {
  var iter = LineIterator(data);

  // Status-Line
  print("data length: " + data.length);
  print("iter object: " + iter);

  Assert.equal(iter.next().value, "HTTP/1.1 200 TEST PASSED");

  skipHeaders(iter);

  // Okay, next line must be the data we expected to be written
  var body = [
    "Method:  GET",
    "Path:    /lots-of-leading-blank-lines",
    "Query:   ",
    "Version: 1.1",
    "Scheme:  http",
    "Host:    localhost",
    "Port:    " + PORT,
  ];

  expectLines(iter, body);
}

test = new RawTest("localhost", PORT, gData, checkLotsOfLeadingBlankLines);
tests.push(test);