mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
http2: handle existing socket data when creating HTTP/2 server sessions
When emitting a 'connection' event to manually inject connections into a server, it's common for the provided stream to already contain readable data, e.g. after sniffing a connection to detect HTTP/2 from the initial bytes. Previously this was supported only for outgoing HTTP/2 sessions created with http2.connect(). This change ensures that HTTP/2 over existing streams is supported on both outgoing and incoming sessions. PR-URL: https://github.com/nodejs/node/pull/41185 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
@@ -1262,6 +1262,21 @@ class Http2Session extends EventEmitter {
|
||||
this.on('newListener', sessionListenerAdded);
|
||||
this.on('removeListener', sessionListenerRemoved);
|
||||
|
||||
// Process data on the next tick - a remoteSettings handler may be attached.
|
||||
// https://github.com/nodejs/node/issues/35981
|
||||
process.nextTick(() => {
|
||||
// Socket already has some buffered data - emulate receiving it
|
||||
// https://github.com/nodejs/node/issues/35475
|
||||
// https://github.com/nodejs/node/issues/34532
|
||||
if (socket.readableLength) {
|
||||
let buf;
|
||||
while ((buf = socket.read()) !== null) {
|
||||
debugSession(type, `${buf.length} bytes already in buffer`);
|
||||
this[kHandle].receive(buf);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
debugSession(type, 'created');
|
||||
}
|
||||
|
||||
@@ -3276,21 +3291,6 @@ function connect(authority, options, listener) {
|
||||
if (typeof listener === 'function')
|
||||
session.once('connect', listener);
|
||||
|
||||
// Process data on the next tick - a remoteSettings handler may be attached.
|
||||
// https://github.com/nodejs/node/issues/35981
|
||||
process.nextTick(() => {
|
||||
debug('Http2Session connect', options.createConnection);
|
||||
// Socket already has some buffered data - emulate receiving it
|
||||
// https://github.com/nodejs/node/issues/35475
|
||||
if (socket && socket.readableLength) {
|
||||
let buf;
|
||||
while ((buf = socket.read()) !== null) {
|
||||
debug(`Http2Session connect: ${buf.length} bytes already in buffer`);
|
||||
session[kHandle].receive(buf);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
|
||||
72
test/parallel/test-http2-autoselect-protocol.js
Normal file
72
test/parallel/test-http2-autoselect-protocol.js
Normal file
@@ -0,0 +1,72 @@
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
if (!common.hasCrypto)
|
||||
common.skip('missing crypto');
|
||||
|
||||
const assert = require('assert');
|
||||
const net = require('net');
|
||||
const http = require('http');
|
||||
const http2 = require('http2');
|
||||
|
||||
// Example test for HTTP/1 vs HTTP/2 protocol autoselection.
|
||||
// Refs: https://github.com/nodejs/node/issues/34532
|
||||
|
||||
const h1Server = http.createServer(common.mustCall((req, res) => {
|
||||
res.end('HTTP/1 Response');
|
||||
}));
|
||||
|
||||
const h2Server = http2.createServer(common.mustCall((req, res) => {
|
||||
res.end('HTTP/2 Response');
|
||||
}));
|
||||
|
||||
const rawServer = net.createServer(common.mustCall(function listener(socket) {
|
||||
const data = socket.read(3);
|
||||
|
||||
if (!data) { // Repeat until data is available
|
||||
socket.once('readable', () => listener(socket));
|
||||
return;
|
||||
}
|
||||
|
||||
// Put the data back, so the real server can handle it:
|
||||
socket.unshift(data);
|
||||
|
||||
if (data.toString('ascii') === 'PRI') { // Very dumb preface check
|
||||
h2Server.emit('connection', socket);
|
||||
} else {
|
||||
h1Server.emit('connection', socket);
|
||||
}
|
||||
}, 2));
|
||||
|
||||
rawServer.listen(common.mustCall(() => {
|
||||
const { port } = rawServer.address();
|
||||
|
||||
let done = 0;
|
||||
{
|
||||
// HTTP/2 Request
|
||||
const client = http2.connect(`http://localhost:${port}`);
|
||||
const req = client.request({ ':path': '/' });
|
||||
req.end();
|
||||
|
||||
let content = '';
|
||||
req.setEncoding('utf8');
|
||||
req.on('data', (chunk) => content += chunk);
|
||||
req.on('end', common.mustCall(() => {
|
||||
assert.strictEqual(content, 'HTTP/2 Response');
|
||||
if (++done === 2) rawServer.close();
|
||||
client.close();
|
||||
}));
|
||||
}
|
||||
|
||||
{
|
||||
// HTTP/1 Request
|
||||
http.get(`http://localhost:${port}`, common.mustCall((res) => {
|
||||
let content = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', (chunk) => content += chunk);
|
||||
res.on('end', common.mustCall(() => {
|
||||
assert.strictEqual(content, 'HTTP/1 Response');
|
||||
if (++done === 2) rawServer.close();
|
||||
}));
|
||||
}));
|
||||
}
|
||||
}));
|
||||
Reference in New Issue
Block a user