http2: add support for TypedArray to getUnpackedSettings
PR-URL: https://github.com/nodejs/node/pull/36141 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
275aa6845e
commit
ad0a01caed
@ -2544,7 +2544,7 @@ console.log(packed.toString('base64'));
|
|||||||
added: v8.4.0
|
added: v8.4.0
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `buf` {Buffer|Uint8Array} The packed settings.
|
* `buf` {Buffer|TypedArray} The packed settings.
|
||||||
* Returns: {HTTP/2 Settings Object}
|
* Returns: {HTTP/2 Settings Object}
|
||||||
|
|
||||||
Returns a [HTTP/2 Settings Object][] containing the deserialized settings from
|
Returns a [HTTP/2 Settings Object][] containing the deserialized settings from
|
||||||
|
@ -1067,5 +1067,7 @@ module.exports = {
|
|||||||
addBufferPrototypeMethods,
|
addBufferPrototypeMethods,
|
||||||
markAsUntransferable,
|
markAsUntransferable,
|
||||||
createUnsafeBuffer,
|
createUnsafeBuffer,
|
||||||
|
readUInt16BE,
|
||||||
|
readUInt32BE,
|
||||||
reconnectZeroFillToggle
|
reconnectZeroFillToggle
|
||||||
};
|
};
|
||||||
|
@ -13,6 +13,7 @@ const {
|
|||||||
ObjectDefineProperty,
|
ObjectDefineProperty,
|
||||||
ObjectPrototypeHasOwnProperty,
|
ObjectPrototypeHasOwnProperty,
|
||||||
Promise,
|
Promise,
|
||||||
|
ReflectApply,
|
||||||
ReflectGetPrototypeOf,
|
ReflectGetPrototypeOf,
|
||||||
Set,
|
Set,
|
||||||
Symbol,
|
Symbol,
|
||||||
@ -32,6 +33,7 @@ const assert = require('assert');
|
|||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
const { readUInt16BE, readUInt32BE } = require('internal/buffer');
|
||||||
const net = require('net');
|
const net = require('net');
|
||||||
const { Duplex } = require('stream');
|
const { Duplex } = require('stream');
|
||||||
const tls = require('tls');
|
const tls = require('tls');
|
||||||
@ -3208,18 +3210,18 @@ function getPackedSettings(settings) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getUnpackedSettings(buf, options = {}) {
|
function getUnpackedSettings(buf, options = {}) {
|
||||||
if (!isArrayBufferView(buf)) {
|
if (!isArrayBufferView(buf) || buf.length === undefined) {
|
||||||
throw new ERR_INVALID_ARG_TYPE('buf',
|
throw new ERR_INVALID_ARG_TYPE('buf',
|
||||||
['Buffer', 'TypedArray', 'DataView'], buf);
|
['Buffer', 'TypedArray'], buf);
|
||||||
}
|
}
|
||||||
if (buf.length % 6 !== 0)
|
if (buf.length % 6 !== 0)
|
||||||
throw new ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH();
|
throw new ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH();
|
||||||
const settings = {};
|
const settings = {};
|
||||||
let offset = 0;
|
let offset = 0;
|
||||||
while (offset < buf.length) {
|
while (offset < buf.length) {
|
||||||
const id = buf.readUInt16BE(offset);
|
const id = ReflectApply(readUInt16BE, buf, [offset]);
|
||||||
offset += 2;
|
offset += 2;
|
||||||
const value = buf.readUInt32BE(offset);
|
const value = ReflectApply(readUInt32BE, buf, [offset]);
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
||||||
settings.headerTableSize = value;
|
settings.headerTableSize = value;
|
||||||
|
@ -133,8 +133,8 @@ http2.getPackedSettings({ enablePush: false });
|
|||||||
code: 'ERR_INVALID_ARG_TYPE',
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
name: 'TypeError',
|
name: 'TypeError',
|
||||||
message:
|
message:
|
||||||
'The "buf" argument must be an instance of Buffer, TypedArray, or ' +
|
'The "buf" argument must be an instance of Buffer or TypedArray.' +
|
||||||
`DataView.${common.invalidArgTypeHelper(input)}`
|
common.invalidArgTypeHelper(input)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -159,6 +159,58 @@ http2.getPackedSettings({ enablePush: false });
|
|||||||
assert.strictEqual(settings.enableConnectProtocol, false);
|
assert.strictEqual(settings.enableConnectProtocol, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const packed = new Uint16Array([
|
||||||
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x03, 0x00, 0x00, 0x00, 0xc8,
|
||||||
|
0x00, 0x05, 0x00, 0x00, 0x4e, 0x20,
|
||||||
|
0x00, 0x04, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x06, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
|
||||||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x00]);
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
http2.getUnpackedSettings(packed.slice(5));
|
||||||
|
}, {
|
||||||
|
code: 'ERR_HTTP2_INVALID_PACKED_SETTINGS_LENGTH',
|
||||||
|
name: 'RangeError',
|
||||||
|
message: 'Packed settings length must be a multiple of six'
|
||||||
|
});
|
||||||
|
|
||||||
|
const settings = http2.getUnpackedSettings(packed);
|
||||||
|
|
||||||
|
assert(settings);
|
||||||
|
assert.strictEqual(settings.headerTableSize, 100);
|
||||||
|
assert.strictEqual(settings.initialWindowSize, 100);
|
||||||
|
assert.strictEqual(settings.maxFrameSize, 20000);
|
||||||
|
assert.strictEqual(settings.maxConcurrentStreams, 200);
|
||||||
|
assert.strictEqual(settings.maxHeaderListSize, 100);
|
||||||
|
assert.strictEqual(settings.maxHeaderSize, 100);
|
||||||
|
assert.strictEqual(settings.enablePush, true);
|
||||||
|
assert.strictEqual(settings.enableConnectProtocol, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const packed = new DataView(Buffer.from([
|
||||||
|
0x00, 0x01, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x03, 0x00, 0x00, 0x00, 0xc8,
|
||||||
|
0x00, 0x05, 0x00, 0x00, 0x4e, 0x20,
|
||||||
|
0x00, 0x04, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x06, 0x00, 0x00, 0x00, 0x64,
|
||||||
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x01,
|
||||||
|
0x00, 0x08, 0x00, 0x00, 0x00, 0x00]).buffer);
|
||||||
|
|
||||||
|
assert.throws(() => {
|
||||||
|
http2.getUnpackedSettings(packed);
|
||||||
|
}, {
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
|
name: 'TypeError',
|
||||||
|
message:
|
||||||
|
'The "buf" argument must be an instance of Buffer or TypedArray.' +
|
||||||
|
common.invalidArgTypeHelper(packed)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
const packed = Buffer.from([
|
const packed = Buffer.from([
|
||||||
0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
|
0x00, 0x02, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user