Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1479,7 +1479,8 @@ int Http2Session::OnDataChunkReceived(nghttp2_session* handle,
// into those buffers.
// The typical case is actually the exception here; Http2StreamListeners
// know about the HTTP2 session associated with this stream, so they know
// about the larger from-socket read buffer, so they do not require copying.
// about the larger from-socket read buffer, so they usually do not require
// copying.
do {
uv_buf_t buf = stream->EmitAlloc(len);
ssize_t avail = len;
Expand Down Expand Up @@ -1580,6 +1581,13 @@ int Http2Session::OnNghttpError(nghttp2_session* handle,
return 0;
}

// A DATA chunk is passed to JavaScript as a slice of the socket read buffer
// only if that buffer is at most this many times larger than the chunk.
// Smaller chunks are copied, so that a queued Buffer cannot keep alive a
// socket read made mostly of other frames or padding. With the default
// limits, full-size DATA frames in a full socket read are still sliced.
static constexpr size_t kMaxDataChunkSliceRatio = 4;

uv_buf_t Http2StreamListener::OnStreamAlloc(size_t size) {
// See the comments in Http2Session::OnDataChunkReceived
// (which is the only possible call site for this method).
Expand All @@ -1598,6 +1606,21 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
return;
}

// Verify that the data is inside the current read buffer.
size_t offset = buf.base - session->stream_buf_.base;
CHECK_LE(offset, session->stream_buf_.len);
CHECK_LE(offset + buf.len, session->stream_buf_.len);

if (static_cast<size_t>(nread) * kMaxDataChunkSliceRatio <
session->stream_buf_.len) {
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
env->isolate(), nread, BackingStoreInitializationMode::kUninitialized);
memcpy(bs->Data(), buf.base, nread);
stream->CallJSOnreadMethod(
nread, ArrayBuffer::New(env->isolate(), std::move(bs)), 0);
return;
}

Local<ArrayBuffer> ab;
if (session->stream_buf_ab_.IsEmpty()) {
ab = ArrayBuffer::New(env->isolate(),
Expand All @@ -1610,12 +1633,6 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
// There is a single large array buffer for the entire data read from the
// network; create a slice of that array buffer and emit it as the
// received data buffer.
size_t offset = buf.base - session->stream_buf_.base;

// Verify that the data offset is inside the current read buffer.
CHECK_LE(offset, session->stream_buf_.len);
CHECK_LE(offset + buf.len, session->stream_buf_.len);

stream->CallJSOnreadMethod(nread, ab, offset);
}

Expand Down Expand Up @@ -2336,8 +2353,8 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
static_cast<unsigned int>(nread));

// Store this so we can create an ArrayBuffer for read data from it.
// DATA frames will be emitted as slices of that ArrayBuffer to avoid having
// to copy memory.
// Large DATA frames will be emitted as slices of that ArrayBuffer to avoid
// having to copy memory.
stream_buf_allocation_ = std::move(bs);

ConsumeHTTP2Data();
Expand Down
90 changes: 90 additions & 0 deletions test/parallel/test-http2-data-chunk-backing-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');

const assert = require('assert');
const http2 = require('http2');
const net = require('net');

// A DATA chunk queued in a paused request body must not keep alive the
// whole socket read it arrived in. Each batch below is an ignored extension
// frame followed by a one-byte DATA frame.

const batchCount = 64;

function frame(type, flags, id, payload) {
const header = Buffer.alloc(9);
header.writeUIntBE(payload.length, 0, 3);
header[3] = type;
header[4] = flags;
header.writeUInt32BE(id, 5);
return Buffer.concat([header, payload]);
}

const filler = frame(0xb, 0, 0, Buffer.alloc(16_365, 0x5a));
const data = frame(0, 0, 1, Buffer.from('a'));
const batch = Buffer.concat([filler, data]);
const pingPayload = Buffer.from('datachnk');

let requestStream;

const server = http2.createServer();
server.on('stream', common.mustCall((stream) => {
requestStream = stream;
// Leave the request body unconsumed so the chunks stay queued.
}));

server.listen(0, common.mustCall(() => {
const peer = net.connect(server.address().port);
let received = Buffer.alloc(0);
let settingsSeen = false;

peer.on('connect', () => {
peer.write(Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'));
peer.write(frame(4, 0, 0, Buffer.alloc(0)));
});

peer.on('data', (chunk) => {
received = Buffer.concat([received, chunk]);
while (received.length >= 9) {
const length = received.readUIntBE(0, 3);
if (received.length < 9 + length) break;
const type = received[3];
const flags = received[4];
const payload = received.subarray(9, 9 + length);
received = received.subarray(9 + length);

if (type === 4 && !(flags & 1) && !settingsSeen) {
settingsSeen = true;
peer.write(frame(4, 1, 0, Buffer.alloc(0)));
const hpack = Buffer.from([0x83, 0x86, 0x84, 0x01, 0x01, 0x61]);
peer.write(frame(1, 4, 1, hpack));
for (let i = 0; i < batchCount; i++)
peer.write(batch);
// The PING is answered only after everything before it was parsed.
peer.write(frame(6, 0, 0, pingPayload));
} else if (type === 6 && (flags & 1) && payload.equals(pingPayload)) {
check();
peer.destroy();
server.close();
}
}
});
}));

function check() {
const buffers = [];
let buffer;
while ((buffer = requestStream.read(1)) !== null)
buffers.push(buffer);

assert.strictEqual(buffers.length, batchCount);
for (const chunk of buffers) {
assert.strictEqual(chunk.toString(), 'a');
assert.ok(chunk.buffer.byteLength < 1024,
`a ${chunk.length} byte chunk retains ` +
`${chunk.buffer.byteLength} bytes`);
}
}
Loading