From 7c9b37e2eb17535fb906c546a4ded9dda1143f4b Mon Sep 17 00:00:00 2001 From: Weijia Wang Date: Tue, 9 Oct 2018 16:38:34 +0800 Subject: [PATCH] http: reduce duplicated code for cleaning parser PR-URL: https://github.com/nodejs/node/pull/23351 Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Anna Henningsen --- lib/_http_common.js | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/lib/_http_common.js b/lib/_http_common.js index faa6fe629ae416..49fd7a59650dc6 100644 --- a/lib/_http_common.js +++ b/lib/_http_common.js @@ -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; }); @@ -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(). @@ -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,