Skip to content

Commit 2b53b16

Browse files
committed
http2: addtl http/2 settings
Currently, node.js http/2 is limited in sending SETTINGs, that are currently implemented by nghttp2. However, nghttp2 has the ability to send arbitary SETTINGs, that are not known beforehand. This patch adds this feature including a fall back mechanism, if a SETTING is implemented in a later nghttp2 or node version. Fixes: #1337
1 parent 5f8ec99 commit 2b53b16

File tree

6 files changed

+116
-7
lines changed

6 files changed

+116
-7
lines changed

lib/internal/errors.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,8 @@ E('ERR_HTTP2_STREAM_CANCEL', function(error) {
12791279
E('ERR_HTTP2_STREAM_ERROR', 'Stream closed with error code %s', Error);
12801280
E('ERR_HTTP2_STREAM_SELF_DEPENDENCY',
12811281
'A stream cannot depend on itself', Error);
1282+
E('ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS',
1283+
'Number of custom settings exceeds MAX_ADDITIONAL_SETTINGS', Error);
12821284
E('ERR_HTTP2_TOO_MANY_INVALID_FRAMES', 'Too many invalid HTTP/2 frames', Error);
12831285
E('ERR_HTTP2_TRAILERS_ALREADY_SENT',
12841286
'Trailing headers have already been sent', Error);

lib/internal/http2/core.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,8 @@ function pingCallback(cb) {
946946
// All settings are optional and may be left undefined
947947
const validateSettings = hideStackFrames((settings) => {
948948
if (settings === undefined) return;
949+
assertIsObject.withoutStackTrace(settings.customSettings, 'customSettings', 'Number');
950+
949951
assertWithinRange.withoutStackTrace('headerTableSize',
950952
settings.headerTableSize,
951953
0, kMaxInt);
@@ -3387,6 +3389,10 @@ function getUnpackedSettings(buf, options = kEmptyObject) {
33873389
break;
33883390
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
33893391
settings.enableConnectProtocol = value !== 0;
3392+
break;
3393+
default:
3394+
if (!settings.customSettings) settings.customSettings = {};
3395+
settings.customSettings[id] = value;
33903396
}
33913397
offset += 4;
33923398
}

lib/internal/http2/util.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
Error,
99
MathMax,
1010
Number,
11+
NumberIsNaN,
1112
ObjectKeys,
1213
SafeSet,
1314
String,
@@ -24,6 +25,7 @@ const {
2425
ERR_HTTP2_INVALID_CONNECTION_HEADERS,
2526
ERR_HTTP2_INVALID_PSEUDOHEADER: { HideStackFramesError: ERR_HTTP2_INVALID_PSEUDOHEADER },
2627
ERR_HTTP2_INVALID_SETTING_VALUE,
28+
ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS,
2729
ERR_INVALID_ARG_TYPE,
2830
ERR_INVALID_HTTP_TOKEN,
2931
},
@@ -190,6 +192,9 @@ const IDX_SETTINGS_MAX_HEADER_LIST_SIZE = 5;
190192
const IDX_SETTINGS_ENABLE_CONNECT_PROTOCOL = 6;
191193
const IDX_SETTINGS_FLAGS = 7;
192194

195+
// Maximum number of allowed additional settings
196+
const MAX_ADDITIONAL_SETTINGS = 10;
197+
193198
const IDX_SESSION_STATE_EFFECTIVE_LOCAL_WINDOW_SIZE = 0;
194199
const IDX_SESSION_STATE_EFFECTIVE_RECV_DATA_LENGTH = 1;
195200
const IDX_SESSION_STATE_NEXT_STREAM_ID = 2;
@@ -348,6 +353,80 @@ function getSettings(session, remote) {
348353

349354
function updateSettingsBuffer(settings) {
350355
let flags = 0;
356+
let numCustomSettings = 0;
357+
358+
if (typeof settings.customSettings === 'object') {
359+
const customSettings = settings.customSettings;
360+
for (const setting in customSettings) {
361+
const val = customSettings[setting];
362+
if (typeof val === 'number') {
363+
let set = false;
364+
const nsetting = Number(setting);
365+
if (NumberIsNaN(nsetting) ||
366+
typeof nsetting !== 'number' ||
367+
0 >= nsetting ||
368+
nsetting > 0xffff)
369+
throw new ERR_HTTP2_INVALID_SETTING_VALUE.RangeError(
370+
'Range Error', nsetting, 0, 0xffff);
371+
if (NumberIsNaN(val) ||
372+
typeof val !== 'number' ||
373+
0 >= val ||
374+
val > 0xffffffff)
375+
throw new ERR_HTTP2_INVALID_SETTING_VALUE.RangeError(
376+
'Range Error', val, 0, 0xffffffff);
377+
if (nsetting < IDX_SETTINGS_FLAGS) {
378+
set = true;
379+
switch (nsetting) {
380+
case IDX_SETTINGS_HEADER_TABLE_SIZE:
381+
flags |= (1 << IDX_SETTINGS_HEADER_TABLE_SIZE);
382+
settingsBuffer[IDX_SETTINGS_HEADER_TABLE_SIZE] =
383+
val;
384+
break;
385+
case IDX_SETTINGS_ENABLE_PUSH:
386+
flags |= (1 << IDX_SETTINGS_ENABLE_PUSH);
387+
settingsBuffer[IDX_SETTINGS_ENABLE_PUSH] = val;
388+
break;
389+
case IDX_SETTINGS_INITIAL_WINDOW_SIZE:
390+
flags |= (1 << IDX_SETTINGS_INITIAL_WINDOW_SIZE);
391+
settingsBuffer[IDX_SETTINGS_INITIAL_WINDOW_SIZE] =
392+
val;
393+
break;
394+
case IDX_SETTINGS_MAX_FRAME_SIZE:
395+
flags |= (1 << IDX_SETTINGS_MAX_FRAME_SIZE);
396+
settingsBuffer[IDX_SETTINGS_MAX_FRAME_SIZE] =
397+
val;
398+
break;
399+
case IDX_SETTINGS_MAX_CONCURRENT_STREAMS:
400+
flags |= (1 << IDX_SETTINGS_MAX_CONCURRENT_STREAMS);
401+
settingsBuffer[IDX_SETTINGS_MAX_CONCURRENT_STREAMS] = val;
402+
break;
403+
case IDX_SETTINGS_MAX_HEADER_LIST_SIZE:
404+
flags |= (1 << IDX_SETTINGS_MAX_HEADER_LIST_SIZE);
405+
settingsBuffer[IDX_SETTINGS_MAX_HEADER_LIST_SIZE] =
406+
val;
407+
break;
408+
case IDX_SETTINGS_ENABLE_CONNECT_PROTOCOL:
409+
flags |= (1 << IDX_SETTINGS_ENABLE_CONNECT_PROTOCOL);
410+
settingsBuffer[IDX_SETTINGS_ENABLE_CONNECT_PROTOCOL] = val;
411+
break;
412+
default:
413+
set = false;
414+
break;
415+
}
416+
}
417+
if (!set) { // not supported
418+
if (numCustomSettings === MAX_ADDITIONAL_SETTINGS)
419+
throw new ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS();
420+
421+
settingsBuffer[IDX_SETTINGS_FLAGS + 1 + 2 * numCustomSettings + 1] = nsetting;
422+
settingsBuffer[IDX_SETTINGS_FLAGS + 1 + 2 * numCustomSettings + 2] = val;
423+
numCustomSettings++;
424+
}
425+
}
426+
}
427+
}
428+
settingsBuffer[IDX_SETTINGS_FLAGS + 1] = numCustomSettings;
429+
351430
if (typeof settings.headerTableSize === 'number') {
352431
flags |= (1 << IDX_SETTINGS_HEADER_TABLE_SIZE);
353432
settingsBuffer[IDX_SETTINGS_HEADER_TABLE_SIZE] =

src/node_http2.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ size_t Http2Settings::Init(
228228
HTTP2_SETTINGS(V)
229229
#undef V
230230

231+
uint32_t numAddSettings = buffer[IDX_SETTINGS_COUNT + 1];
232+
if (numAddSettings > 0) {
233+
uint32_t offset = IDX_SETTINGS_COUNT + 1 + 1;
234+
for (uint32_t i = 0; i < numAddSettings; i++) {
235+
uint32_t key = buffer[offset + i * 2 + 0];
236+
uint32_t val = buffer[offset + i * 2 + 1];
237+
entries[count++] = nghttp2_settings_entry{(int32_t)key, val};
238+
}
239+
}
240+
231241
return count;
232242
}
233243
#undef GRABSETTING
@@ -262,7 +272,7 @@ Local<Value> Http2Settings::Pack() {
262272
}
263273

264274
Local<Value> Http2Settings::Pack(Http2State* state) {
265-
nghttp2_settings_entry entries[IDX_SETTINGS_COUNT];
275+
nghttp2_settings_entry entries[IDX_SETTINGS_COUNT + MAX_ADDITIONAL_SETTINGS];
266276
size_t count = Init(state, entries);
267277
return Pack(state->env(), count, entries);
268278
}
@@ -298,6 +308,8 @@ void Http2Settings::Update(Http2Session* session, get_setting fn) {
298308
fn(session->session(), NGHTTP2_SETTINGS_ ## name);
299309
HTTP2_SETTINGS(V)
300310
#undef V
311+
buffer[IDX_SETTINGS_COUNT + 1] =
312+
0; // no additional settings are coming, clear them
301313
}
302314

303315
// Initializes the shared TypedArray with the default settings values.
@@ -314,6 +326,7 @@ void Http2Settings::RefreshDefaults(Http2State* http2_state) {
314326
#undef V
315327

316328
buffer[IDX_SETTINGS_COUNT] = flags;
329+
buffer[IDX_SETTINGS_COUNT + 1] = 0; // no additional settings
317330
}
318331

319332

src/node_http2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,7 +1035,7 @@ class Http2Settings : public AsyncWrap {
10351035
v8::Global<v8::Function> callback_;
10361036
uint64_t startTime_;
10371037
size_t count_ = 0;
1038-
nghttp2_settings_entry entries_[IDX_SETTINGS_COUNT];
1038+
nghttp2_settings_entry entries_[IDX_SETTINGS_COUNT + MAX_ADDITIONAL_SETTINGS];
10391039
};
10401040

10411041
class Origins {

src/node_http2_state.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ namespace http2 {
2121
IDX_SETTINGS_COUNT
2222
};
2323

24+
// number of max additional settings, thus settings not implemented by nghttp2
25+
const size_t MAX_ADDITIONAL_SETTINGS = 10;
26+
2427
enum Http2SessionStateIndex {
2528
IDX_SESSION_STATE_EFFECTIVE_LOCAL_WINDOW_SIZE,
2629
IDX_SESSION_STATE_EFFECTIVE_RECV_DATA_LENGTH,
@@ -108,10 +111,11 @@ class Http2State : public BaseObject {
108111
offsetof(http2_state_internal, options_buffer),
109112
IDX_OPTIONS_FLAGS + 1,
110113
root_buffer),
111-
settings_buffer(realm->isolate(),
112-
offsetof(http2_state_internal, settings_buffer),
113-
IDX_SETTINGS_COUNT + 1,
114-
root_buffer) {}
114+
settings_buffer(
115+
realm->isolate(),
116+
offsetof(http2_state_internal, settings_buffer),
117+
IDX_SETTINGS_COUNT + 1 + 1 + 2 * MAX_ADDITIONAL_SETTINGS,
118+
root_buffer) {}
115119

116120
AliasedUint8Array root_buffer;
117121
AliasedFloat64Array session_state_buffer;
@@ -135,7 +139,12 @@ class Http2State : public BaseObject {
135139
double stream_stats_buffer[IDX_STREAM_STATS_COUNT];
136140
double session_stats_buffer[IDX_SESSION_STATS_COUNT];
137141
uint32_t options_buffer[IDX_OPTIONS_FLAGS + 1];
138-
uint32_t settings_buffer[IDX_SETTINGS_COUNT + 1];
142+
// first + 1: number of actual nghttp2 supported settings
143+
// second + 1: number of additional settings not suppoted by nghttp2
144+
// 2 * MAX_ADDITIONAL_SETTINGS: settings id and value for each
145+
// additional setting
146+
uint32_t settings_buffer[IDX_SETTINGS_COUNT + 1 + 1 +
147+
2 * MAX_ADDITIONAL_SETTINGS];
139148
};
140149
};
141150

0 commit comments

Comments
 (0)