Skip to content

Commit

Permalink
http2: account for buffered data before creating HTTP2 socket wrap
Browse files Browse the repository at this point in the history
If there is data stored in the socket, create a `JSStreamSocket`
wrapper so that no data is lost when passing the socket to the
C++ layer (which is unaware of data buffered in JS land).

Fixes: nodejs#34532
  • Loading branch information
addaleax committed Oct 6, 2020
1 parent a6ca822 commit 54fc9a1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,11 @@ class Http2Session extends EventEmitter {
constructor(type, options, socket) {
super();

if (!socket._handle || !socket._handle.isStreamBase) {
// Ideally, the readableLength check would not be necessary, and we would
// have a way to handle the buffered data at the C++ StreamBase level.
if (!socket._handle ||
!socket._handle.isStreamBase ||
socket.readableLength > 0) {
socket = new JSStreamSocket(socket);
}

Expand Down
72 changes: 72 additions & 0 deletions test/parallel/test-http2-autoselect-protocol.js
Original file line number Diff line number Diff line change
@@ -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();
}));
}));
}
}));

0 comments on commit 54fc9a1

Please sign in to comment.