http: fix parsing of binary upgrade response body · nodejs/node@3b9ef21 · GitHub
Skip to content

Commit 3b9ef21

Browse files
bnoordhuisevanlucas
authored andcommitted
http: fix parsing of binary upgrade response body
Fix a bug where a connection upgrade response with a Transfer-Encoding header and a body whose first byte is > 127 causes said byte to be dropped on the floor when passing the remainder of the message to the 'upgrade' event listeners. Fixes: #17789 PR-URL: #17806 Fixes: #17789 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7a7e130 commit 3b9ef21

4 files changed

Lines changed: 41 additions & 28 deletions

File tree

lib/_http_client.js

Lines changed: 9 additions & 15 deletions

lib/_http_common.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,10 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
106106

107107
parser.incoming.upgrade = upgrade;
108108

109-
var skipBody = 0; // response to HEAD or CONNECT
109+
if (upgrade)
110+
return 2; // Skip body and treat as Upgrade.
110111

111-
if (!upgrade) {
112-
// For upgraded connections and CONNECT method request, we'll emit this
113-
// after parser.execute so that we can capture the first part of the new
114-
// protocol.
115-
skipBody = parser.onIncoming(parser.incoming, shouldKeepAlive);
116-
}
117-
118-
if (typeof skipBody !== 'number')
119-
return skipBody ? 1 : 0;
120-
else
121-
return skipBody;
112+
return parser.onIncoming(parser.incoming, shouldKeepAlive);
122113
}
123114

124115
// XXX This is a mess.

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
617617
} else {
618618
server.emit('request', req, res);
619619
}
620-
return false; // Not a HEAD response. (Not even a response!)
620+
return 0; // No special treatment.
621621
}
622622

623623
function resetSocketTimeout(server, socket, state) {
Lines changed: 28 additions & 0 deletions

0 commit comments

Comments
 (0)