2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
|
|
|
MathMin,
|
2024-04-21 16:53:08 +00:00
|
|
|
Symbol,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-05 21:56:00 +00:00
|
|
|
const { setImmediate } = require('timers');
|
2019-03-31 11:30:12 +00:00
|
|
|
|
2024-04-26 06:50:44 +00:00
|
|
|
const { methods, allMethods, HTTPParser } = internalBinding('http_parser');
|
2019-11-20 19:48:58 +00:00
|
|
|
const { getOptionValue } = require('internal/options');
|
|
|
|
const insecureHTTPParser = getOptionValue('--insecure-http-parser');
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2019-03-30 21:13:50 +00:00
|
|
|
const FreeList = require('internal/freelist');
|
2015-01-21 16:36:59 +00:00
|
|
|
const incoming = require('_http_incoming');
|
2017-10-07 14:50:42 +00:00
|
|
|
const {
|
|
|
|
IncomingMessage,
|
|
|
|
readStart,
|
2023-02-26 10:34:02 +00:00
|
|
|
readStop,
|
2017-10-07 14:50:42 +00:00
|
|
|
} = incoming;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2017-10-19 18:16:02 +00:00
|
|
|
const kIncomingMessage = Symbol('IncomingMessage');
|
2021-07-07 07:43:48 +00:00
|
|
|
const kOnMessageBegin = HTTPParser.kOnMessageBegin | 0;
|
2015-01-21 16:36:59 +00:00
|
|
|
const kOnHeaders = HTTPParser.kOnHeaders | 0;
|
|
|
|
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
|
|
|
|
const kOnBody = HTTPParser.kOnBody | 0;
|
|
|
|
const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0;
|
2016-01-19 22:16:55 +00:00
|
|
|
const kOnExecute = HTTPParser.kOnExecute | 0;
|
2019-10-30 22:33:33 +00:00
|
|
|
const kOnTimeout = HTTPParser.kOnTimeout | 0;
|
2013-08-13 21:47:17 +00:00
|
|
|
|
2018-04-18 08:17:06 +00:00
|
|
|
const MAX_HEADER_PAIRS = 2000;
|
|
|
|
|
2013-04-11 22:15:41 +00:00
|
|
|
// Only called in the slow case where slow means
|
|
|
|
// that the request headers were either fragmented
|
|
|
|
// across multiple TCP packets or too large to be
|
|
|
|
// processed in a single run. This method is also
|
|
|
|
// called to process trailing HTTP headers.
|
|
|
|
function parserOnHeaders(headers, url) {
|
|
|
|
// Once we exceeded headers limit - stop collecting them
|
|
|
|
if (this.maxHeaderPairs <= 0 ||
|
|
|
|
this._headers.length < this.maxHeaderPairs) {
|
2021-04-15 13:05:42 +00:00
|
|
|
this._headers.push(...headers);
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
this._url += url;
|
|
|
|
}
|
|
|
|
|
2015-01-13 01:32:09 +00:00
|
|
|
// `headers` and `url` are set only if .onHeaders() has not been called for
|
|
|
|
// this request.
|
|
|
|
// `url` is not set for response parsers but that's not applicable here since
|
|
|
|
// all our parsers are request parsers.
|
|
|
|
function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
|
|
|
|
url, statusCode, statusMessage, upgrade,
|
|
|
|
shouldKeepAlive) {
|
2018-04-14 12:01:13 +00:00
|
|
|
const parser = this;
|
|
|
|
const { socket } = parser;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
if (headers === undefined) {
|
2013-04-11 22:15:41 +00:00
|
|
|
headers = parser._headers;
|
|
|
|
parser._headers = [];
|
|
|
|
}
|
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
if (url === undefined) {
|
2013-04-11 22:15:41 +00:00
|
|
|
url = parser._url;
|
|
|
|
parser._url = '';
|
|
|
|
}
|
|
|
|
|
2017-10-19 18:16:02 +00:00
|
|
|
// Parser is also used by http client
|
2024-09-24 19:48:15 +00:00
|
|
|
const ParserIncomingMessage = (socket?.server?.[kIncomingMessage]) ||
|
2018-04-14 12:01:13 +00:00
|
|
|
IncomingMessage;
|
2017-10-19 18:16:02 +00:00
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
const incoming = parser.incoming = new ParserIncomingMessage(socket);
|
|
|
|
incoming.httpVersionMajor = versionMajor;
|
|
|
|
incoming.httpVersionMinor = versionMinor;
|
|
|
|
incoming.httpVersion = `${versionMajor}.${versionMinor}`;
|
2023-01-03 10:43:21 +00:00
|
|
|
incoming.joinDuplicateHeaders = socket?.server?.joinDuplicateHeaders ||
|
|
|
|
parser.joinDuplicateHeaders;
|
2018-04-18 07:26:16 +00:00
|
|
|
incoming.url = url;
|
|
|
|
incoming.upgrade = upgrade;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2019-11-06 11:19:22 +00:00
|
|
|
let n = headers.length;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2015-01-13 01:32:09 +00:00
|
|
|
// If parser.maxHeaderPairs <= 0 assume that there's no limit.
|
|
|
|
if (parser.maxHeaderPairs > 0)
|
2019-11-22 17:04:46 +00:00
|
|
|
n = MathMin(n, parser.maxHeaderPairs);
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
incoming._addHeaderLines(headers, n);
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2015-01-29 01:05:53 +00:00
|
|
|
if (typeof method === 'number') {
|
2013-04-11 22:15:41 +00:00
|
|
|
// server only
|
2024-04-26 06:50:44 +00:00
|
|
|
incoming.method = allMethods[method];
|
2013-04-11 22:15:41 +00:00
|
|
|
} else {
|
|
|
|
// client only
|
2018-04-18 07:26:16 +00:00
|
|
|
incoming.statusCode = statusCode;
|
|
|
|
incoming.statusMessage = statusMessage;
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
return parser.onIncoming(incoming, shouldKeepAlive);
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 14:25:15 +00:00
|
|
|
function parserOnBody(b) {
|
2018-04-18 07:26:16 +00:00
|
|
|
const stream = this.incoming;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// If the stream has already been removed, then drop it.
|
2018-04-18 07:26:16 +00:00
|
|
|
if (stream === null)
|
2013-04-11 22:15:41 +00:00
|
|
|
return;
|
|
|
|
|
2018-12-10 12:27:32 +00:00
|
|
|
// Pretend this was the result of a stream._read call.
|
2022-06-10 14:25:15 +00:00
|
|
|
if (!stream._dumped) {
|
|
|
|
const ret = stream.push(b);
|
2013-04-11 22:15:41 +00:00
|
|
|
if (!ret)
|
2018-04-18 07:26:16 +00:00
|
|
|
readStop(this.socket);
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parserOnMessageComplete() {
|
2018-04-18 07:26:16 +00:00
|
|
|
const parser = this;
|
|
|
|
const stream = parser.incoming;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2018-04-18 07:26:16 +00:00
|
|
|
if (stream !== null) {
|
2013-04-11 22:15:41 +00:00
|
|
|
stream.complete = true;
|
|
|
|
// Emit any trailing headers.
|
2018-04-18 07:26:16 +00:00
|
|
|
const headers = parser._headers;
|
|
|
|
if (headers.length) {
|
|
|
|
stream._addHeaderLines(headers, headers.length);
|
2013-04-11 22:15:41 +00:00
|
|
|
parser._headers = [];
|
|
|
|
parser._url = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// For emit end event
|
|
|
|
stream.push(null);
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:03:53 +00:00
|
|
|
// Force to read the next incoming message
|
2013-10-10 22:00:05 +00:00
|
|
|
readStart(parser.socket);
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-11 10:35:30 +00:00
|
|
|
const parsers = new FreeList('parsers', 1000, function parsersCb() {
|
2019-04-22 22:57:12 +00:00
|
|
|
const parser = new HTTPParser();
|
2013-04-11 22:15:41 +00:00
|
|
|
|
2018-10-09 08:38:34 +00:00
|
|
|
cleanParser(parser);
|
2018-04-18 08:17:06 +00:00
|
|
|
|
2013-08-13 21:47:17 +00:00
|
|
|
parser[kOnHeaders] = parserOnHeaders;
|
|
|
|
parser[kOnHeadersComplete] = parserOnHeadersComplete;
|
|
|
|
parser[kOnBody] = parserOnBody;
|
|
|
|
parser[kOnMessageComplete] = parserOnMessageComplete;
|
2013-04-11 22:15:41 +00:00
|
|
|
|
|
|
|
return parser;
|
|
|
|
});
|
|
|
|
|
2018-01-13 16:48:07 +00:00
|
|
|
function closeParserInstance(parser) { parser.close(); }
|
2013-04-11 22:15:41 +00:00
|
|
|
|
|
|
|
// Free the parser and also break any links that it
|
|
|
|
// might have to any other things.
|
|
|
|
// TODO: All parser data should be attached to a
|
|
|
|
// single object, so that it can be easily cleaned
|
|
|
|
// up by doing `parser.data = {}`, which should
|
|
|
|
// be done in FreeList.free. `parsers.free(parser)`
|
|
|
|
// should be all that is needed.
|
2014-08-22 22:57:45 +00:00
|
|
|
function freeParser(parser, req, socket) {
|
2013-04-11 22:15:41 +00:00
|
|
|
if (parser) {
|
2015-10-13 06:16:39 +00:00
|
|
|
if (parser._consumed)
|
|
|
|
parser.unconsume();
|
2018-10-09 08:38:34 +00:00
|
|
|
cleanParser(parser);
|
2022-07-24 14:06:24 +00:00
|
|
|
parser.remove();
|
2017-03-10 13:17:42 +00:00
|
|
|
if (parsers.free(parser) === false) {
|
2018-01-13 16:48:07 +00:00
|
|
|
// Make sure the parser's stack has unwound before deleting the
|
|
|
|
// corresponding C++ object through .close().
|
|
|
|
setImmediate(closeParserInstance, parser);
|
2017-03-10 13:17:42 +00:00
|
|
|
} else {
|
|
|
|
// Since the Parser destructor isn't going to run the destroy() callbacks
|
|
|
|
// it needs to be triggered manually.
|
2017-07-17 23:47:12 +00:00
|
|
|
parser.free();
|
2017-03-10 13:17:42 +00:00
|
|
|
}
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
|
|
|
if (req) {
|
|
|
|
req.parser = null;
|
|
|
|
}
|
2014-08-22 22:57:45 +00:00
|
|
|
if (socket) {
|
|
|
|
socket.parser = null;
|
|
|
|
}
|
2013-04-11 22:15:41 +00:00
|
|
|
}
|
2013-04-11 23:00:19 +00:00
|
|
|
|
2017-11-30 02:37:09 +00:00
|
|
|
const tokenRegExp = /^[\^_`a-zA-Z\-0-9!#$%&'*+.|~]+$/;
|
2015-08-24 18:58:48 +00:00
|
|
|
/**
|
|
|
|
* Verifies that the given val is a valid HTTP token
|
|
|
|
* per the rules defined in RFC 7230
|
2016-01-21 07:53:20 +00:00
|
|
|
* See https://tools.ietf.org/html/rfc7230#section-3.2.6
|
2018-03-25 14:27:38 +00:00
|
|
|
*/
|
2016-12-19 02:21:57 +00:00
|
|
|
function checkIsHttpToken(val) {
|
2024-07-06 08:33:16 +00:00
|
|
|
return tokenRegExp.test(val);
|
2015-08-24 18:58:48 +00:00
|
|
|
}
|
2016-02-04 01:32:23 +00:00
|
|
|
|
2018-01-25 17:48:38 +00:00
|
|
|
const headerCharRegex = /[^\t\x20-\x7e\x80-\xff]/;
|
2016-02-04 01:32:23 +00:00
|
|
|
/**
|
|
|
|
* True if val contains an invalid field-vchar
|
|
|
|
* field-value = *( field-content / obs-fold )
|
|
|
|
* field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
|
|
|
|
* field-vchar = VCHAR / obs-text
|
2018-03-25 14:27:38 +00:00
|
|
|
*/
|
2016-02-04 01:32:23 +00:00
|
|
|
function checkInvalidHeaderChar(val) {
|
2024-07-06 08:33:16 +00:00
|
|
|
return headerCharRegex.test(val);
|
2016-02-04 01:32:23 +00:00
|
|
|
}
|
2017-02-28 00:09:32 +00:00
|
|
|
|
2018-10-09 08:38:34 +00:00
|
|
|
function cleanParser(parser) {
|
|
|
|
parser._headers = [];
|
|
|
|
parser._url = '';
|
|
|
|
parser.socket = null;
|
|
|
|
parser.incoming = null;
|
|
|
|
parser.outgoing = null;
|
|
|
|
parser.maxHeaderPairs = MAX_HEADER_PAIRS;
|
2021-07-07 07:43:48 +00:00
|
|
|
parser[kOnMessageBegin] = null;
|
2018-10-09 08:38:34 +00:00
|
|
|
parser[kOnExecute] = null;
|
2020-05-01 06:14:25 +00:00
|
|
|
parser[kOnTimeout] = null;
|
2018-10-09 08:38:34 +00:00
|
|
|
parser._consumed = false;
|
2020-05-01 06:14:25 +00:00
|
|
|
parser.onIncoming = null;
|
2023-07-03 06:20:13 +00:00
|
|
|
parser.joinDuplicateHeaders = null;
|
2018-10-09 08:38:34 +00:00
|
|
|
}
|
|
|
|
|
2019-06-30 22:34:55 +00:00
|
|
|
function prepareError(err, parser, rawPacket) {
|
|
|
|
err.rawPacket = rawPacket || parser.getCurrentBuffer();
|
|
|
|
if (typeof err.reason === 'string')
|
|
|
|
err.message = `Parse Error: ${err.reason}`;
|
|
|
|
}
|
|
|
|
|
2019-11-20 19:48:58 +00:00
|
|
|
let warnedLenient = false;
|
|
|
|
|
|
|
|
function isLenient() {
|
|
|
|
if (insecureHTTPParser && !warnedLenient) {
|
|
|
|
warnedLenient = true;
|
|
|
|
process.emitWarning('Using insecure HTTP parsing');
|
|
|
|
}
|
|
|
|
return insecureHTTPParser;
|
|
|
|
}
|
|
|
|
|
2017-02-28 00:09:32 +00:00
|
|
|
module.exports = {
|
|
|
|
_checkInvalidHeaderChar: checkInvalidHeaderChar,
|
|
|
|
_checkIsHttpToken: checkIsHttpToken,
|
|
|
|
chunkExpression: /(?:^|\W)chunked(?:$|\W)/i,
|
|
|
|
continueExpression: /(?:^|\W)100-continue(?:$|\W)/i,
|
2021-09-14 03:25:35 +00:00
|
|
|
CRLF: '\r\n', // TODO: Deprecate this.
|
2017-02-28 00:09:32 +00:00
|
|
|
freeParser,
|
|
|
|
methods,
|
2017-10-19 18:16:02 +00:00
|
|
|
parsers,
|
2018-11-30 06:39:02 +00:00
|
|
|
kIncomingMessage,
|
2019-06-30 22:34:55 +00:00
|
|
|
HTTPParser,
|
2019-11-20 19:48:58 +00:00
|
|
|
isLenient,
|
2019-06-30 22:34:55 +00:00
|
|
|
prepareError,
|
2017-02-28 00:09:32 +00:00
|
|
|
};
|