FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

http2: fix endless loop when writing empty string by addaleax · Pull Request #18924 · nodejs/node · GitHub

/ node Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cc  (1) .js  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
14 changes: 13 additions & 1 deletion src/node_http2.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2184,6 +2184,17 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,

size_t amount = 0; // amount of data being sent in this data frame.

// Remove all empty chunks from the head of the queue.
// This is done here so that .write('', cb) is still a meaningful way to
// find out when the HTTP2 stream wants to consume data, and because the
// StreamBase API allows empty input chunks.
while (!stream->queue_.empty() && stream->queue_.front().buf.len == 0) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

One question, if we remove the empty buffer and do nothing, will the header be sent?

Refs: https://github.com/nodejs/node/pull/18673/files#diff-696b2cc418addca5f3fe5020058f8b15R1622

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@XadillaX Hm – I’m not sure I can wrap my head around everything, but this code here is only ever called by nghttp2 after the headers were sent anyway… does that answer your question?

So, yes, writing an empty buffer would trigger the headers to be sent, as I understand it.

WriteWrap* finished = stream->queue_.front().req_wrap;
stream->queue_.pop();
if (finished != nullptr)
finished->Done(0);
}

if (!stream->queue_.empty()) {
DEBUG_HTTP2SESSION2(session, "stream %d has pending outbound data", id);
amount = std::min(stream->available_outbound_length_, length);
Expand All @@ -2197,7 +2208,8 @@ ssize_t Http2Stream::Provider::Stream::OnRead(nghttp2_session* handle,
}
}

if (amount == 0 && stream->IsWritable() && stream->queue_.empty()) {
if (amount == 0 && stream->IsWritable()) {
CHECK(stream->queue_.empty());
DEBUG_HTTP2SESSION2(session, "deferring stream %d", id);
return NGHTTP2_ERR_DEFERRED;
}
Expand Down
54 changes: 54 additions & 0 deletions test/parallel/test-http2-client-write-empty-string.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

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

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

for (const chunkSequence of [
[ '' ],
[ '', '' ]
]) {
const server = http2.createServer();
server.on('stream', common.mustCall((stream, headers, flags) => {
stream.respond({ 'content-type': 'text/html' });

let data = '';
stream.on('data', common.mustNotCall((chunk) => {
data += chunk.toString();
}));
stream.on('end', common.mustCall(() => {
stream.end(`"${data}"`);
}));
}));

server.listen(0, common.mustCall(() => {
const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);

const req = client.request({
':method': 'POST',
':path': '/'
});

req.on('response', common.mustCall((headers) => {
assert.strictEqual(headers[':status'], 200);
assert.strictEqual(headers['content-type'], 'text/html');
}));

let data = '';
req.setEncoding('utf8');
req.on('data', common.mustCallAtLeast((d) => data += d));
req.on('end', common.mustCall(() => {
assert.strictEqual(data, '""');
server.close();
client.close();
}));

for (const chunk of chunkSequence)
req.write(chunk);
req.end();
}));
}

Back | FazBrowse Home | New Git URL