blob: 8e7802811654b2a9b8f667bd3ee301a81633173d [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001/*jshint node:true */
2
3var assert = require('assert');
4
5exports.HTTPParser = HTTPParser;
6function HTTPParser(type) {
7 assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE || type === undefined);
8 if (type === undefined) {
9 // Node v12+
10 } else {
11 this.initialize(type);
12 }
13}
14HTTPParser.prototype.initialize = function (type, async_resource) {
15 assert.ok(type === HTTPParser.REQUEST || type === HTTPParser.RESPONSE);
16 this.type = type;
17 this.state = type + '_LINE';
18 this.info = {
19 headers: [],
20 upgrade: false
21 };
22 this.trailers = [];
23 this.line = '';
24 this.isChunked = false;
25 this.connection = '';
26 this.headerSize = 0; // for preventing too big headers
27 this.body_bytes = null;
28 this.isUserCall = false;
29 this.hadError = false;
30};
31
32HTTPParser.encoding = 'ascii';
33HTTPParser.maxHeaderSize = 80 * 1024; // maxHeaderSize (in bytes) is configurable, but 80kb by default;
34HTTPParser.REQUEST = 'REQUEST';
35HTTPParser.RESPONSE = 'RESPONSE';
36var kOnHeaders = HTTPParser.kOnHeaders = 0;
37var kOnHeadersComplete = HTTPParser.kOnHeadersComplete = 1;
38var kOnBody = HTTPParser.kOnBody = 2;
39var kOnMessageComplete = HTTPParser.kOnMessageComplete = 3;
40
41// Some handler stubs, needed for compatibility
42HTTPParser.prototype[kOnHeaders] =
43HTTPParser.prototype[kOnHeadersComplete] =
44HTTPParser.prototype[kOnBody] =
45HTTPParser.prototype[kOnMessageComplete] = function () {};
46
47var compatMode0_12 = true;
48Object.defineProperty(HTTPParser, 'kOnExecute', {
49 get: function () {
50 // hack for backward compatibility
51 compatMode0_12 = false;
52 return 4;
53 }
54 });
55
56var methods = exports.methods = HTTPParser.methods = [
57 'DELETE',
58 'GET',
59 'HEAD',
60 'POST',
61 'PUT',
62 'CONNECT',
63 'OPTIONS',
64 'TRACE',
65 'COPY',
66 'LOCK',
67 'MKCOL',
68 'MOVE',
69 'PROPFIND',
70 'PROPPATCH',
71 'SEARCH',
72 'UNLOCK',
73 'BIND',
74 'REBIND',
75 'UNBIND',
76 'ACL',
77 'REPORT',
78 'MKACTIVITY',
79 'CHECKOUT',
80 'MERGE',
81 'M-SEARCH',
82 'NOTIFY',
83 'SUBSCRIBE',
84 'UNSUBSCRIBE',
85 'PATCH',
86 'PURGE',
87 'MKCALENDAR',
88 'LINK',
89 'UNLINK'
90];
91var method_connect = methods.indexOf('CONNECT');
92HTTPParser.prototype.reinitialize = HTTPParser;
93HTTPParser.prototype.close =
94HTTPParser.prototype.pause =
95HTTPParser.prototype.resume =
96HTTPParser.prototype.free = function () {};
97HTTPParser.prototype._compatMode0_11 = false;
98HTTPParser.prototype.getAsyncId = function() { return 0; };
99
100var headerState = {
101 REQUEST_LINE: true,
102 RESPONSE_LINE: true,
103 HEADER: true
104};
105HTTPParser.prototype.execute = function (chunk, start, length) {
106 if (!(this instanceof HTTPParser)) {
107 throw new TypeError('not a HTTPParser');
108 }
109
110 // backward compat to node < 0.11.4
111 // Note: the start and length params were removed in newer version
112 start = start || 0;
113 length = typeof length === 'number' ? length : chunk.length;
114
115 this.chunk = chunk;
116 this.offset = start;
117 var end = this.end = start + length;
118 try {
119 while (this.offset < end) {
120 if (this[this.state]()) {
121 break;
122 }
123 }
124 } catch (err) {
125 if (this.isUserCall) {
126 throw err;
127 }
128 this.hadError = true;
129 return err;
130 }
131 this.chunk = null;
132 length = this.offset - start;
133 if (headerState[this.state]) {
134 this.headerSize += length;
135 if (this.headerSize > HTTPParser.maxHeaderSize) {
136 return new Error('max header size exceeded');
137 }
138 }
139 return length;
140};
141
142var stateFinishAllowed = {
143 REQUEST_LINE: true,
144 RESPONSE_LINE: true,
145 BODY_RAW: true
146};
147HTTPParser.prototype.finish = function () {
148 if (this.hadError) {
149 return;
150 }
151 if (!stateFinishAllowed[this.state]) {
152 return new Error('invalid state for EOF');
153 }
154 if (this.state === 'BODY_RAW') {
155 this.userCall()(this[kOnMessageComplete]());
156 }
157};
158
159// These three methods are used for an internal speed optimization, and it also
160// works if theses are noops. Basically consume() asks us to read the bytes
161// ourselves, but if we don't do it we get them through execute().
162HTTPParser.prototype.consume =
163HTTPParser.prototype.unconsume =
164HTTPParser.prototype.getCurrentBuffer = function () {};
165
166//For correct error handling - see HTTPParser#execute
167//Usage: this.userCall()(userFunction('arg'));
168HTTPParser.prototype.userCall = function () {
169 this.isUserCall = true;
170 var self = this;
171 return function (ret) {
172 self.isUserCall = false;
173 return ret;
174 };
175};
176
177HTTPParser.prototype.nextRequest = function () {
178 this.userCall()(this[kOnMessageComplete]());
179 this.reinitialize(this.type);
180};
181
182HTTPParser.prototype.consumeLine = function () {
183 var end = this.end,
184 chunk = this.chunk;
185 for (var i = this.offset; i < end; i++) {
186 if (chunk[i] === 0x0a) { // \n
187 var line = this.line + chunk.toString(HTTPParser.encoding, this.offset, i);
188 if (line.charAt(line.length - 1) === '\r') {
189 line = line.substr(0, line.length - 1);
190 }
191 this.line = '';
192 this.offset = i + 1;
193 return line;
194 }
195 }
196 //line split over multiple chunks
197 this.line += chunk.toString(HTTPParser.encoding, this.offset, this.end);
198 this.offset = this.end;
199};
200
201var headerExp = /^([^: \t]+):[ \t]*((?:.*[^ \t])|)/;
202var headerContinueExp = /^[ \t]+(.*[^ \t])/;
203HTTPParser.prototype.parseHeader = function (line, headers) {
204 if (line.indexOf('\r') !== -1) {
205 throw parseErrorCode('HPE_LF_EXPECTED');
206 }
207
208 var match = headerExp.exec(line);
209 var k = match && match[1];
210 if (k) { // skip empty string (malformed header)
211 headers.push(k);
212 headers.push(match[2]);
213 } else {
214 var matchContinue = headerContinueExp.exec(line);
215 if (matchContinue && headers.length) {
216 if (headers[headers.length - 1]) {
217 headers[headers.length - 1] += ' ';
218 }
219 headers[headers.length - 1] += matchContinue[1];
220 }
221 }
222};
223
224var requestExp = /^([A-Z-]+) ([^ ]+) HTTP\/(\d)\.(\d)$/;
225HTTPParser.prototype.REQUEST_LINE = function () {
226 var line = this.consumeLine();
227 if (!line) {
228 return;
229 }
230 var match = requestExp.exec(line);
231 if (match === null) {
232 throw parseErrorCode('HPE_INVALID_CONSTANT');
233 }
234 this.info.method = this._compatMode0_11 ? match[1] : methods.indexOf(match[1]);
235 if (this.info.method === -1) {
236 throw new Error('invalid request method');
237 }
238 this.info.url = match[2];
239 this.info.versionMajor = +match[3];
240 this.info.versionMinor = +match[4];
241 this.body_bytes = 0;
242 this.state = 'HEADER';
243};
244
245var responseExp = /^HTTP\/(\d)\.(\d) (\d{3}) ?(.*)$/;
246HTTPParser.prototype.RESPONSE_LINE = function () {
247 var line = this.consumeLine();
248 if (!line) {
249 return;
250 }
251 var match = responseExp.exec(line);
252 if (match === null) {
253 throw parseErrorCode('HPE_INVALID_CONSTANT');
254 }
255 this.info.versionMajor = +match[1];
256 this.info.versionMinor = +match[2];
257 var statusCode = this.info.statusCode = +match[3];
258 this.info.statusMessage = match[4];
259 // Implied zero length.
260 if ((statusCode / 100 | 0) === 1 || statusCode === 204 || statusCode === 304) {
261 this.body_bytes = 0;
262 }
263 this.state = 'HEADER';
264};
265
266HTTPParser.prototype.shouldKeepAlive = function () {
267 if (this.info.versionMajor > 0 && this.info.versionMinor > 0) {
268 if (this.connection.indexOf('close') !== -1) {
269 return false;
270 }
271 } else if (this.connection.indexOf('keep-alive') === -1) {
272 return false;
273 }
274 if (this.body_bytes !== null || this.isChunked) { // || skipBody
275 return true;
276 }
277 return false;
278};
279
280HTTPParser.prototype.HEADER = function () {
281 var line = this.consumeLine();
282 if (line === undefined) {
283 return;
284 }
285 var info = this.info;
286 if (line) {
287 this.parseHeader(line, info.headers);
288 } else {
289 var headers = info.headers;
290 var hasContentLength = false;
291 var currentContentLengthValue;
292 var hasUpgradeHeader = false;
293 for (var i = 0; i < headers.length; i += 2) {
294 switch (headers[i].toLowerCase()) {
295 case 'transfer-encoding':
296 this.isChunked = headers[i + 1].toLowerCase() === 'chunked';
297 break;
298 case 'content-length':
299 currentContentLengthValue = +headers[i + 1];
300 if (hasContentLength) {
301 // Fix duplicate Content-Length header with same values.
302 // Throw error only if values are different.
303 // Known issues:
304 // https://github.com/request/request/issues/2091#issuecomment-328715113
305 // https://github.com/nodejs/node/issues/6517#issuecomment-216263771
306 if (currentContentLengthValue !== this.body_bytes) {
307 throw parseErrorCode('HPE_UNEXPECTED_CONTENT_LENGTH');
308 }
309 } else {
310 hasContentLength = true;
311 this.body_bytes = currentContentLengthValue;
312 }
313 break;
314 case 'connection':
315 this.connection += headers[i + 1].toLowerCase();
316 break;
317 case 'upgrade':
318 hasUpgradeHeader = true;
319 break;
320 }
321 }
322
323 // if both isChunked and hasContentLength, isChunked wins
324 // This is required so the body is parsed using the chunked method, and matches
325 // Chrome's behavior. We could, maybe, ignore them both (would get chunked
326 // encoding into the body), and/or disable shouldKeepAlive to be more
327 // resilient.
328 if (this.isChunked && hasContentLength) {
329 hasContentLength = false;
330 this.body_bytes = null;
331 }
332
333 // Logic from https://github.com/nodejs/http-parser/blob/921d5585515a153fa00e411cf144280c59b41f90/http_parser.c#L1727-L1737
334 // "For responses, "Upgrade: foo" and "Connection: upgrade" are
335 // mandatory only when it is a 101 Switching Protocols response,
336 // otherwise it is purely informational, to announce support.
337 if (hasUpgradeHeader && this.connection.indexOf('upgrade') != -1) {
338 info.upgrade = this.type === HTTPParser.REQUEST || info.statusCode === 101;
339 } else {
340 info.upgrade = info.method === method_connect;
341 }
342
343 if (this.isChunked && info.upgrade) {
344 this.isChunked = false;
345 }
346
347 info.shouldKeepAlive = this.shouldKeepAlive();
348 //problem which also exists in original node: we should know skipBody before calling onHeadersComplete
349 var skipBody;
350 if (compatMode0_12) {
351 skipBody = this.userCall()(this[kOnHeadersComplete](info));
352 } else {
353 skipBody = this.userCall()(this[kOnHeadersComplete](info.versionMajor,
354 info.versionMinor, info.headers, info.method, info.url, info.statusCode,
355 info.statusMessage, info.upgrade, info.shouldKeepAlive));
356 }
357 if (skipBody === 2) {
358 this.nextRequest();
359 return true;
360 } else if (this.isChunked && !skipBody) {
361 this.state = 'BODY_CHUNKHEAD';
362 } else if (skipBody || this.body_bytes === 0) {
363 this.nextRequest();
364 // For older versions of node (v6.x and older?), that return skipBody=1 or skipBody=true,
365 // need this "return true;" if it's an upgrade request.
366 return info.upgrade;
367 } else if (this.body_bytes === null) {
368 this.state = 'BODY_RAW';
369 } else {
370 this.state = 'BODY_SIZED';
371 }
372 }
373};
374
375HTTPParser.prototype.BODY_CHUNKHEAD = function () {
376 var line = this.consumeLine();
377 if (line === undefined) {
378 return;
379 }
380 this.body_bytes = parseInt(line, 16);
381 if (!this.body_bytes) {
382 this.state = 'BODY_CHUNKTRAILERS';
383 } else {
384 this.state = 'BODY_CHUNK';
385 }
386};
387
388HTTPParser.prototype.BODY_CHUNK = function () {
389 var length = Math.min(this.end - this.offset, this.body_bytes);
390 this.userCall()(this[kOnBody](this.chunk, this.offset, length));
391 this.offset += length;
392 this.body_bytes -= length;
393 if (!this.body_bytes) {
394 this.state = 'BODY_CHUNKEMPTYLINE';
395 }
396};
397
398HTTPParser.prototype.BODY_CHUNKEMPTYLINE = function () {
399 var line = this.consumeLine();
400 if (line === undefined) {
401 return;
402 }
403 assert.equal(line, '');
404 this.state = 'BODY_CHUNKHEAD';
405};
406
407HTTPParser.prototype.BODY_CHUNKTRAILERS = function () {
408 var line = this.consumeLine();
409 if (line === undefined) {
410 return;
411 }
412 if (line) {
413 this.parseHeader(line, this.trailers);
414 } else {
415 if (this.trailers.length) {
416 this.userCall()(this[kOnHeaders](this.trailers, ''));
417 }
418 this.nextRequest();
419 }
420};
421
422HTTPParser.prototype.BODY_RAW = function () {
423 var length = this.end - this.offset;
424 this.userCall()(this[kOnBody](this.chunk, this.offset, length));
425 this.offset = this.end;
426};
427
428HTTPParser.prototype.BODY_SIZED = function () {
429 var length = Math.min(this.end - this.offset, this.body_bytes);
430 this.userCall()(this[kOnBody](this.chunk, this.offset, length));
431 this.offset += length;
432 this.body_bytes -= length;
433 if (!this.body_bytes) {
434 this.nextRequest();
435 }
436};
437
438// backward compat to node < 0.11.6
439['Headers', 'HeadersComplete', 'Body', 'MessageComplete'].forEach(function (name) {
440 var k = HTTPParser['kOn' + name];
441 Object.defineProperty(HTTPParser.prototype, 'on' + name, {
442 get: function () {
443 return this[k];
444 },
445 set: function (to) {
446 // hack for backward compatibility
447 this._compatMode0_11 = true;
448 method_connect = 'CONNECT';
449 return (this[k] = to);
450 }
451 });
452});
453
454function parseErrorCode(code) {
455 var err = new Error('Parse Error');
456 err.code = code;
457 return err;
458}