Skip to content

Commit 33ef11a

Browse files
committed
http: fix handling of HTTP upgrades with bodies
Previously, we ignored all indicators of the body (content-length or transfer-encoding headers) and treated any information following the headers as part of the upgraded stream. We now fully process the requests bodies as normal instead.
1 parent b757a8f commit 33ef11a

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

lib/_http_server.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,9 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
10781078
if (req.upgrade) {
10791079
req.upgrade = req.method === 'CONNECT' ||
10801080
!!server.shouldUpgradeCallback(req);
1081-
if (req.upgrade)
1082-
return 2;
1081+
if (req.upgrade) {
1082+
return 0;
1083+
}
10831084
}
10841085

10851086
state.incoming.push(req);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
const net = require('net');
7+
8+
const server = http.createServer();
9+
10+
server.on('request', common.mustNotCall());
11+
12+
server.on('upgrade', function(req, socket, upgradeHead) {
13+
let reqBody = '';
14+
req.on('data', common.mustCall((str) => { reqBody += str; }));
15+
req.on('end', common.mustCall(() => {
16+
assert.strictEqual(reqBody, 'request body');
17+
socket.end();
18+
}));
19+
20+
assert.strictEqual(upgradeHead.toString(), 'upgrade head');
21+
});
22+
23+
server.listen(0, function() {
24+
const conn = net.createConnection(this.address().port);
25+
conn.setEncoding('utf8');
26+
27+
conn.on('connect', function() {
28+
conn.write(
29+
'POST / HTTP/1.1\r\n' +
30+
'Upgrade: WebSocket\r\n' +
31+
'Connection: Upgrade\r\n' +
32+
'Content-Length: 12\r\n' +
33+
'\r\n' +
34+
'request body' +
35+
'upgrade head'
36+
);
37+
});
38+
39+
conn.on('end', function() {
40+
conn.end();
41+
});
42+
43+
conn.on('close', function() {
44+
server.close();
45+
});
46+
});

0 commit comments

Comments
 (0)