Skip to content

Commit 883ed4b

Browse files
Vincent Boivintargos
Vincent Boivin
authored andcommitted
http2: add updateSettings to both http2 servers
Allow the user to update the server settings after using http2.createSecureServer() or after using http2.createServer(). Fixes: #35353 PR-URL: #35383 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Ricky Zhou <[email protected]>
1 parent 55b7a6c commit 883ed4b

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

doc/api/http2.md

+26
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,19 @@ A value of `0` will disable the timeout behavior on incoming connections.
18791879
The socket timeout logic is set up on connection, so changing this
18801880
value only affects new connections to the server, not any existing connections.
18811881

1882+
#### `server.updateSettings([settings])`
1883+
<!-- YAML
1884+
added: REPLACEME
1885+
-->
1886+
1887+
* `settings` {HTTP/2 Settings Object}
1888+
1889+
Used to update the server with the provided settings.
1890+
1891+
Throws `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values.
1892+
1893+
Throws `ERR_INVALID_ARG_TYPE` for invalid `settings` argument.
1894+
18821895
### Class: `Http2SecureServer`
18831896
<!-- YAML
18841897
added: v8.4.0
@@ -2058,6 +2071,19 @@ A value of `0` will disable the timeout behavior on incoming connections.
20582071
The socket timeout logic is set up on connection, so changing this
20592072
value only affects new connections to the server, not any existing connections.
20602073

2074+
#### `server.updateSettings([settings])`
2075+
<!-- YAML
2076+
added: REPLACEME
2077+
-->
2078+
2079+
* `settings` {HTTP/2 Settings Object}
2080+
2081+
Used to update the server with the provided settings.
2082+
2083+
Throws `ERR_HTTP2_INVALID_SETTING_VALUE` for invalid `settings` values.
2084+
2085+
Throws `ERR_INVALID_ARG_TYPE` for invalid `settings` argument.
2086+
20612087
### `http2.createServer(options[, onRequestHandler])`
20622088
<!-- YAML
20632089
added: v8.4.0

lib/internal/http2/core.js

+12
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,12 @@ class Http2SecureServer extends TLSServer {
29922992
}
29932993
return this;
29942994
}
2995+
2996+
updateSettings(settings) {
2997+
assertIsObject(settings, 'settings');
2998+
validateSettings(settings);
2999+
this[kOptions].settings = { ...this[kOptions].settings, ...settings };
3000+
}
29953001
}
29963002

29973003
class Http2Server extends NETServer {
@@ -3014,6 +3020,12 @@ class Http2Server extends NETServer {
30143020
}
30153021
return this;
30163022
}
3023+
3024+
updateSettings(settings) {
3025+
assertIsObject(settings, 'settings');
3026+
validateSettings(settings);
3027+
this[kOptions].settings = { ...this[kOptions].settings, ...settings };
3028+
}
30173029
}
30183030

30193031
Http2Server.prototype[EventEmitter.captureRejectionSymbol] = function(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
// This test ensures that the Http2SecureServer and Http2Server
4+
// settings are updated when the setting object is valid.
5+
// When the setting object is invalid, this test ensures that
6+
// updateSettings throws an exception.
7+
8+
const common = require('../common');
9+
if (!common.hasCrypto) { common.skip('missing crypto'); }
10+
const assert = require('assert');
11+
const http2 = require('http2');
12+
13+
testUpdateSettingsWith({
14+
server: http2.createSecureServer(),
15+
newServerSettings: {
16+
'headerTableSize': 1,
17+
'initialWindowSize': 1,
18+
'maxConcurrentStreams': 1,
19+
'maxHeaderListSize': 1,
20+
'maxFrameSize': 16385,
21+
'enablePush': false,
22+
'enableConnectProtocol': true
23+
}
24+
});
25+
testUpdateSettingsWith({
26+
server: http2.createServer(),
27+
newServerSettings: {
28+
'enablePush': false
29+
}
30+
});
31+
32+
function testUpdateSettingsWith({ server, newServerSettings }) {
33+
const oldServerSettings = getServerSettings(server);
34+
assert.notDeepStrictEqual(oldServerSettings, newServerSettings);
35+
server.updateSettings(newServerSettings);
36+
const updatedServerSettings = getServerSettings(server);
37+
assert.deepStrictEqual(updatedServerSettings, { ...oldServerSettings,
38+
...newServerSettings });
39+
assert.throws(() => server.updateSettings(''), {
40+
message: 'The "settings" argument must be of type object. ' +
41+
'Received type string (\'\')',
42+
code: 'ERR_INVALID_ARG_TYPE',
43+
name: 'TypeError'
44+
});
45+
assert.throws(() => server.updateSettings({
46+
'maxHeaderListSize': 'foo'
47+
}), {
48+
message: 'Invalid value for setting "maxHeaderListSize": foo',
49+
code: 'ERR_HTTP2_INVALID_SETTING_VALUE',
50+
name: 'RangeError'
51+
});
52+
}
53+
54+
function getServerSettings(server) {
55+
const options = Object
56+
.getOwnPropertySymbols(server)
57+
.find((s) => s.toString() === 'Symbol(options)');
58+
return server[options].settings;
59+
}

0 commit comments

Comments
 (0)