Skip to content

Commit

Permalink
http: reduce duplicated code for cleaning parser
Browse files Browse the repository at this point in the history
PR-URL: #23351
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
starkwang authored and jasnell committed Oct 17, 2018
1 parent 7d07e16 commit 37e40e3
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions lib/_http_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,13 @@ function parserOnMessageComplete() {
const parsers = new FreeList('parsers', 1000, function parsersCb() {
const parser = new HTTPParser(HTTPParser.REQUEST);

parser._headers = [];
parser._url = '';
parser._consumed = false;

parser.socket = null;
parser.incoming = null;
parser.outgoing = null;

parser.maxHeaderPairs = MAX_HEADER_PAIRS;
cleanParser(parser);

parser.onIncoming = null;
parser[kOnHeaders] = parserOnHeaders;
parser[kOnHeadersComplete] = parserOnHeadersComplete;
parser[kOnBody] = parserOnBody;
parser[kOnMessageComplete] = parserOnMessageComplete;
parser[kOnExecute] = null;

return parser;
});
Expand All @@ -182,17 +173,9 @@ function closeParserInstance(parser) { parser.close(); }
// should be all that is needed.
function freeParser(parser, req, socket) {
if (parser) {
parser._headers = [];
parser._url = '';
parser.maxHeaderPairs = MAX_HEADER_PAIRS;
parser.onIncoming = null;
if (parser._consumed)
parser.unconsume();
parser._consumed = false;
parser.socket = null;
parser.incoming = null;
parser.outgoing = null;
parser[kOnExecute] = null;
cleanParser(parser);
if (parsers.free(parser) === false) {
// Make sure the parser's stack has unwound before deleting the
// corresponding C++ object through .close().
Expand Down Expand Up @@ -238,6 +221,17 @@ function checkInvalidHeaderChar(val) {
return headerCharRegex.test(val);
}

function cleanParser(parser) {
parser._headers = [];
parser._url = '';
parser.socket = null;
parser.incoming = null;
parser.outgoing = null;
parser.maxHeaderPairs = MAX_HEADER_PAIRS;
parser[kOnExecute] = null;
parser._consumed = false;
}

module.exports = {
_checkInvalidHeaderChar: checkInvalidHeaderChar,
_checkIsHttpToken: checkIsHttpToken,
Expand Down

0 comments on commit 37e40e3

Please sign in to comment.