mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 21:19:50 +01:00
f7fbbeedc6
The http spec does not say anything about Upgrade headers making protocol switch mandatory but Node.js implements them as if they are. Relax the requirements to only destroy the socket if no upgrade listener exists on the client when status code is 101. PR-URL: https://github.com/nodejs/node/pull/19981 Fixes: https://github.com/nodejs/node/issues/11552 Refs: https://tools.ietf.org/html/rfc7230#section-6.7 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const http = require('http');
|
|
|
|
const tests = [
|
|
{ headers: {}, expected: 'regular' },
|
|
{ headers: { upgrade: 'h2c' }, expected: 'regular' },
|
|
{ headers: { connection: 'upgrade' }, expected: 'regular' },
|
|
{ headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'upgrade' },
|
|
{ headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'destroy' },
|
|
{ headers: { connection: 'upgrade', upgrade: 'h2c' }, expected: 'regular' },
|
|
];
|
|
|
|
function fire() {
|
|
if (tests.length === 0)
|
|
return server.close();
|
|
|
|
const test = tests.shift();
|
|
|
|
const done = common.mustCall(function done(result) {
|
|
assert.strictEqual(result, test.expected);
|
|
|
|
fire();
|
|
});
|
|
|
|
const req = http.request({
|
|
port: server.address().port,
|
|
path: '/',
|
|
headers: test.headers
|
|
}, function onResponse(res) {
|
|
res.resume();
|
|
done('regular');
|
|
});
|
|
|
|
if (test.expected === 'destroy') {
|
|
req.on('socket', () => req.socket.on('close', () => {
|
|
server.removeAllListeners('upgrade');
|
|
done('destroy');
|
|
}));
|
|
} else {
|
|
req.on('upgrade', function onUpgrade(res, socket) {
|
|
socket.destroy();
|
|
done('upgrade');
|
|
});
|
|
}
|
|
|
|
req.end();
|
|
}
|
|
|
|
const server = http.createServer(function(req, res) {
|
|
res.writeHead(200, {
|
|
Connection: 'upgrade, keep-alive',
|
|
Upgrade: 'h2c'
|
|
});
|
|
res.end('hello world');
|
|
}).on('upgrade', function(req, socket) {
|
|
socket.end('HTTP/1.1 101 Switching protocols\r\n' +
|
|
'Connection: upgrade\r\n' +
|
|
'Upgrade: h2c\r\n\r\n' +
|
|
'ohai');
|
|
}).listen(0, fire);
|