Skip to content

Commit

Permalink
http2: general cleanups
Browse files Browse the repository at this point in the history
PR-URL: nodejs#17328
Fixes: nodejs#15303
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Sebastiaan Deckers <[email protected]>
  • Loading branch information
jasnell committed Nov 28, 2017
1 parent f3686f2 commit d4111d0
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const { _connectionListener: httpConnectionListener } = require('http');
const { createPromise, promiseResolve } = process.binding('util');
const debug = util.debuglog('http2');

const kMaxFrameSize = (2 ** 24) - 1;
const kMaxInt = (2 ** 32) - 1;
const kMaxStreams = (2 ** 31) - 1;

const {
Expand Down Expand Up @@ -332,9 +334,9 @@ function emitGoaway(self, code, lastStreamID, buf) {
return;
if (!state.shuttingDown && !state.shutdown) {
self.shutdown({}, self.destroy.bind(self));
} else {
self.destroy();
return;
}
self.destroy();
}

// Called by the native layer when a goaway frame has been received
Expand Down Expand Up @@ -582,14 +584,15 @@ function doShutdown(options) {
function submitShutdown(options) {
const type = this[kType];
debug(`Http2Session ${sessionName(type)}: submitting shutdown request`);
const fn = doShutdown.bind(this, options);
if (type === NGHTTP2_SESSION_SERVER && options.graceful === true) {
// first send a shutdown notice
this[kHandle].shutdownNotice();
// then, on flip of the event loop, do the actual shutdown
setImmediate(doShutdown.bind(this), options);
} else {
doShutdown.call(this, options);
setImmediate(fn);
return;
}
fn();
}

function finishSessionDestroy(socket) {
Expand Down Expand Up @@ -844,19 +847,19 @@ class Http2Session extends EventEmitter {
settings = Object.assign(Object.create(null), settings);
assertWithinRange('headerTableSize',
settings.headerTableSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('initialWindowSize',
settings.initialWindowSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('maxFrameSize',
settings.maxFrameSize,
16384, 2 ** 24 - 1);
16384, kMaxFrameSize);
assertWithinRange('maxConcurrentStreams',
settings.maxConcurrentStreams,
0, kMaxStreams);
assertWithinRange('maxHeaderListSize',
settings.maxHeaderListSize,
0, 2 ** 32 - 1);
0, kMaxInt);
if (settings.enablePush !== undefined &&
typeof settings.enablePush !== 'boolean') {
const err = new errors.TypeError('ERR_HTTP2_INVALID_SETTING_VALUE',
Expand All @@ -871,11 +874,12 @@ class Http2Session extends EventEmitter {
debug(`Http2Session ${sessionName(this[kType])}: sending settings`);

state.pendingAck++;
const fn = submitSettings.bind(this, settings);
if (state.connecting) {
this.once('connect', submitSettings.bind(this, settings));
this.once('connect', fn);
return;
}
submitSettings.call(this, settings);
fn();
}

// Destroy the Http2Session
Expand Down Expand Up @@ -961,13 +965,14 @@ class Http2Session extends EventEmitter {
this.on('shutdown', callback);
}

const fn = submitShutdown.bind(this, options);
if (state.connecting) {
this.once('connect', submitShutdown.bind(this, options));
this.once('connect', fn);
return;
}

debug(`Http2Session ${sessionName(type)}: sending shutdown`);
submitShutdown.call(this, options);
fn();
}

_onTimeout() {
Expand Down Expand Up @@ -1368,7 +1373,7 @@ class Http2Stream extends Duplex {
rstStream(code = NGHTTP2_NO_ERROR) {
if (typeof code !== 'number')
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'code', 'number');
if (code < 0 || code > 2 ** 32 - 1)
if (code < 0 || code > kMaxInt)
throw new errors.RangeError('ERR_OUT_OF_RANGE', 'code');

const fn = submitRstStream.bind(this, code);
Expand Down Expand Up @@ -2362,19 +2367,19 @@ function getPackedSettings(settings) {
settings = settings || Object.create(null);
assertWithinRange('headerTableSize',
settings.headerTableSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('initialWindowSize',
settings.initialWindowSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('maxFrameSize',
settings.maxFrameSize,
16384, 2 ** 24 - 1);
16384, kMaxFrameSize);
assertWithinRange('maxConcurrentStreams',
settings.maxConcurrentStreams,
0, kMaxStreams);
assertWithinRange('maxHeaderListSize',
settings.maxHeaderListSize,
0, 2 ** 32 - 1);
0, kMaxInt);
if (settings.enablePush !== undefined &&
typeof settings.enablePush !== 'boolean') {
const err = new errors.TypeError('ERR_HTTP2_INVALID_SETTING_VALUE',
Expand Down Expand Up @@ -2425,22 +2430,22 @@ function getUnpackedSettings(buf, options = {}) {
if (options != null && options.validate) {
assertWithinRange('headerTableSize',
settings.headerTableSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('enablePush',
settings.enablePush,
0, 1);
assertWithinRange('initialWindowSize',
settings.initialWindowSize,
0, 2 ** 32 - 1);
0, kMaxInt);
assertWithinRange('maxFrameSize',
settings.maxFrameSize,
16384, 2 ** 24 - 1);
16384, kMaxFrameSize);
assertWithinRange('maxConcurrentStreams',
settings.maxConcurrentStreams,
0, kMaxStreams);
assertWithinRange('maxHeaderListSize',
settings.maxHeaderListSize,
0, 2 ** 32 - 1);
0, kMaxInt);
}

if (settings.enablePush !== undefined) {
Expand Down

0 comments on commit d4111d0

Please sign in to comment.