From e3bddeec18f9b36c1314cdd5fbfa59119ba6d619 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Sat, 14 Apr 2018 14:01:13 +0200 Subject: [PATCH] http: fix undefined error in parser event The current check for socket.server[kIncomingMessage] does not account for the possibility of a socket.server that doesn't have that property defined. Fix it. Backport-PR-URL: https://github.com/nodejs/node/pull/22880 PR-URL: https://github.com/nodejs/node/pull/20029 Fixes: https://github.com/nodejs/node/issues/19231 Reviewed-By: Ben Noordhuis Reviewed-By: Luigi Pinca Reviewed-By: Trivikram Kamat Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Khaidi Chu --- lib/_http_common.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/_http_common.js b/lib/_http_common.js index 6fc283e607633e..cacc04eaf7364e 100644 --- a/lib/_http_common.js +++ b/lib/_http_common.js @@ -62,7 +62,8 @@ function parserOnHeaders(headers, url) { function parserOnHeadersComplete(versionMajor, versionMinor, headers, method, url, statusCode, statusMessage, upgrade, shouldKeepAlive) { - var parser = this; + const parser = this; + const { socket } = parser; if (!headers) { headers = parser._headers; @@ -75,10 +76,11 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method, } // Parser is also used by http client - var ParserIncomingMessage = parser.socket && parser.socket.server ? - parser.socket.server[kIncomingMessage] : IncomingMessage; + const ParserIncomingMessage = (socket && socket.server && + socket.server[kIncomingMessage]) || + IncomingMessage; - parser.incoming = new ParserIncomingMessage(parser.socket); + parser.incoming = new ParserIncomingMessage(socket); parser.incoming.httpVersionMajor = versionMajor; parser.incoming.httpVersionMinor = versionMinor; parser.incoming.httpVersion = `${versionMajor}.${versionMinor}`;