Skip to content

Commit 59614d7

Browse files
committed
[Squash] Updates
1 parent 957eff9 commit 59614d7

File tree

5 files changed

+39
-28
lines changed

5 files changed

+39
-28
lines changed

src/node_http2.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "node_http2_state.h"
66

77
#include <queue>
8+
#include <algorithm>
89

910
namespace node {
1011

@@ -197,7 +198,10 @@ Http2Session::Http2Session(Environment* env,
197198

198199
padding_strategy_ = opts.GetPaddingStrategy();
199200

200-
Init(type, *opts, nullptr, opts.GetMaxHeaderPairs());
201+
int32_t maxHeaderPairs = opts.GetMaxHeaderPairs();
202+
maxHeaderPairs = type == NGHTTP2_SESSION_SERVER ?
203+
std::max(maxHeaderPairs, 4) : std::max(maxHeaderPairs, 1);
204+
Init(type, *opts, nullptr, maxHeaderPairs);
201205

202206
// For every node::Http2Session instance, there is a uv_prepare_t handle
203207
// whose callback is triggered on every tick of the event loop. When

src/node_http2_core-inl.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,17 @@ inline bool Nghttp2Stream::AddHeader(nghttp2_rcbuf* name,
5454
nghttp2_rcbuf* value,
5555
uint8_t flags) {
5656
size_t length = GetBufferLength(name) + GetBufferLength(value) + 32;
57-
if (current_headers_count_ + 1 == current_headers_.length() ||
57+
if (current_headers_.size() == max_header_pairs_ ||
5858
current_headers_length_ + length > max_header_length_) {
5959
return false;
6060
}
61-
current_headers_[current_headers_count_].name = name;
62-
current_headers_[current_headers_count_].value = value;
63-
current_headers_[current_headers_count_].flags = flags;
61+
nghttp2_header header;
62+
header.name = name;
63+
header.value = value;
64+
header.flags = flags;
65+
current_headers_.push_back(header);
6466
nghttp2_rcbuf_incref(name);
6567
nghttp2_rcbuf_incref(value);
66-
current_headers_count_++;
6768
current_headers_length_ += length;
6869
return true;
6970
}
@@ -661,13 +662,13 @@ Nghttp2Stream::Nghttp2Stream(
661662
int options) : id_(id),
662663
session_(session),
663664
current_headers_category_(category) {
664-
// Allocate a fixed incoming header size based on the current local setting
665-
// for max header list size.
666-
uint32_t maxHeaderListSize = session->GetMaxHeaderPairs();
667-
if (maxHeaderListSize == 0)
668-
maxHeaderListSize = DEFAULT_MAX_HEADER_LIST_PAIRS;
669-
current_headers_.AllocateSufficientStorage(maxHeaderListSize);
665+
// Limit the number of header pairs
666+
max_header_pairs_ = session->GetMaxHeaderPairs();
667+
if (max_header_pairs_ == 0)
668+
max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
669+
current_headers_.reserve(max_header_pairs_);
670670

671+
// Limit the number of header octets
671672
max_header_length_ =
672673
std::min(
673674
nghttp2_session_get_local_settings(

src/node_http2_core.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "nghttp2/nghttp2.h"
1010

1111
#include <queue>
12+
#include <vector>
1213
#include <stdio.h>
1314
#include <unordered_map>
1415

@@ -444,22 +445,22 @@ class Nghttp2Stream {
444445
uint8_t flags);
445446

446447
inline nghttp2_header* headers() {
447-
return *current_headers_;
448+
return current_headers_.data();
448449
}
449450

450451
inline nghttp2_headers_category headers_category() const {
451452
return current_headers_category_;
452453
}
453454

454455
inline size_t headers_count() const {
455-
return current_headers_count_;
456+
return current_headers_.size();
456457
}
457458

458459
void StartHeaders(nghttp2_headers_category category) {
459460
DEBUG_HTTP2("Nghttp2Stream %d: starting headers, category: %d\n",
460461
id_, category);
461-
current_headers_count_ = 0;
462462
current_headers_length_ = 0;
463+
current_headers_.clear();
463464
current_headers_category_ = category;
464465
}
465466

@@ -472,7 +473,7 @@ class Nghttp2Stream {
472473

473474
// Internal state flags
474475
int flags_ = NGHTTP2_STREAM_FLAG_NONE;
475-
uint32_t max_header_pairs_;
476+
uint32_t max_header_pairs_ = DEFAULT_MAX_HEADER_LIST_PAIRS;
476477
uint32_t max_header_length_ = DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE;
477478

478479
// The RST_STREAM code used to close this stream
@@ -493,10 +494,8 @@ class Nghttp2Stream {
493494
// they are temporarily stored here until the OnFrameReceived is called
494495
// signalling the end of the HEADERS frame
495496
nghttp2_headers_category current_headers_category_ = NGHTTP2_HCAT_HEADERS;
496-
size_t current_headers_count_ = 0;
497-
uint32_t current_headers_length_ = 0;
498-
MaybeStackBuffer<nghttp2_header, DEFAULT_SETTINGS_MAX_HEADER_LIST_SIZE>
499-
current_headers_;
497+
uint32_t current_headers_length_ = 0; // total number of octets
498+
std::vector<nghttp2_header> current_headers_;
500499

501500
// Inbound Data... This is the data received via DATA frames for this stream.
502501
std::queue<uv_buf_t> data_chunks_;

test/parallel/test-http2-too-many-headers.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ const {
1010
} = http2.constants;
1111

1212
// By default, the maximum number of header fields allowed per
13-
// block is 1024, including the HTTP pseudo-header fields, by
14-
// setting one here, no request should be acccepted because it
15-
// does not allow for even the basic HTTP pseudo-headers
16-
const server = http2.createServer({ maxHeaderListPairs: 1 });
13+
// block is 128, including the HTTP pseudo-header fields. The
14+
// minimum value for servers is 4, setting this to any value
15+
// less than 4 will still leave the minimum to 4.
16+
const server = http2.createServer({ maxHeaderListPairs: 0 });
1717
server.on('stream', common.mustNotCall());
1818

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

22-
const req = client.request();
22+
const req = client.request({ foo: 'bar' });
2323
req.on('error', common.expectsError({
2424
code: 'ERR_HTTP2_STREAM_ERROR',
2525
type: Error,

test/parallel/test-http2-util-update-options-buffer.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,25 @@ const IDX_OPTIONS_MAX_RESERVED_REMOTE_STREAMS = 1;
1515
const IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH = 2;
1616
const IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS = 3;
1717
const IDX_OPTIONS_PADDING_STRATEGY = 4;
18-
const IDX_OPTIONS_FLAGS = 5;
18+
const IDX_OPTIONS_MAX_HEADER_LIST_PAIRS = 5;
19+
const IDX_OPTIONS_FLAGS = 6;
1920

2021
{
2122
updateOptionsBuffer({
2223
maxDeflateDynamicTableSize: 1,
2324
maxReservedRemoteStreams: 2,
2425
maxSendHeaderBlockLength: 3,
2526
peerMaxConcurrentStreams: 4,
26-
paddingStrategy: 5
27+
paddingStrategy: 5,
28+
maxHeaderListPairs: 6
2729
});
2830

2931
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_DEFLATE_DYNAMIC_TABLE_SIZE], 1);
3032
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_RESERVED_REMOTE_STREAMS], 2);
3133
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH], 3);
3234
strictEqual(optionsBuffer[IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS], 4);
3335
strictEqual(optionsBuffer[IDX_OPTIONS_PADDING_STRATEGY], 5);
36+
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_HEADER_LIST_PAIRS], 6);
3437

3538
const flags = optionsBuffer[IDX_OPTIONS_FLAGS];
3639

@@ -39,6 +42,7 @@ const IDX_OPTIONS_FLAGS = 5;
3942
ok(flags & (1 << IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH));
4043
ok(flags & (1 << IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS));
4144
ok(flags & (1 << IDX_OPTIONS_PADDING_STRATEGY));
45+
ok(flags & (1 << IDX_OPTIONS_MAX_HEADER_LIST_PAIRS));
4246
}
4347

4448
{
@@ -48,14 +52,16 @@ const IDX_OPTIONS_FLAGS = 5;
4852
maxDeflateDynamicTableSize: 1,
4953
maxReservedRemoteStreams: 2,
5054
peerMaxConcurrentStreams: 4,
51-
paddingStrategy: 5
55+
paddingStrategy: 5,
56+
maxHeaderListPairs: 6
5257
});
5358

5459
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_DEFLATE_DYNAMIC_TABLE_SIZE], 1);
5560
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_RESERVED_REMOTE_STREAMS], 2);
5661
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH], 0);
5762
strictEqual(optionsBuffer[IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS], 4);
5863
strictEqual(optionsBuffer[IDX_OPTIONS_PADDING_STRATEGY], 5);
64+
strictEqual(optionsBuffer[IDX_OPTIONS_MAX_HEADER_LIST_PAIRS], 6);
5965

6066
const flags = optionsBuffer[IDX_OPTIONS_FLAGS];
6167

@@ -64,4 +70,5 @@ const IDX_OPTIONS_FLAGS = 5;
6470
ok(!(flags & (1 << IDX_OPTIONS_MAX_SEND_HEADER_BLOCK_LENGTH)));
6571
ok(flags & (1 << IDX_OPTIONS_PEER_MAX_CONCURRENT_STREAMS));
6672
ok(flags & (1 << IDX_OPTIONS_PADDING_STRATEGY));
73+
ok(flags & (1 << IDX_OPTIONS_MAX_HEADER_LIST_PAIRS));
6774
}

0 commit comments

Comments
 (0)