h2lean 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -3
- package/index.js +3 -3
- package/lib/client-session.js +36 -13
- package/lib/compat.js +1 -1
- package/lib/flat-list.js +61 -0
- package/lib/hpack.js +73 -21
- package/lib/server.js +46 -4
- package/lib/session.js +152 -37
- package/lib/stream.js +47 -0
- package/package.json +3 -1
package/README.md
CHANGED
|
@@ -90,7 +90,7 @@ Compatibility target is the **public surface** of `node:http2`: names, argument
|
|
|
90
90
|
server.listen(8080);
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
-
Same class names (`Http2Session`, `Http2Stream`, `Http2ServerRequest`, `Http2ServerResponse`), same event names, same `Http2Session` and settings semantics, all 240 protocol constants re-exported from `node:http2` itself — user code that compares `error.code !== http2.constants.NGHTTP2_ENHANCE_YOUR_CALM` keeps working unchanged. Both transports on both sides: `createServer` (h2c), `createSecureServer` (h2s), `connect()` over cleartext and TLS with node's ALPN rules.
|
|
93
|
+
Same class names (`Http2Session`, `Http2Stream`, `Http2ServerRequest`, `Http2ServerResponse`), same event names, same `Http2Session` and settings semantics, all 240 protocol constants re-exported from `node:http2` itself — user code that compares `error.code !== http2.constants.NGHTTP2_ENHANCE_YOUR_CALM` keeps working unchanged. Both transports on both sides: `createServer` (h2c), `createSecureServer` (h2s), `connect()` over cleartext and TLS with node's ALPN rules, and `performServerHandshake()` to run a server session over a socket you already hold.
|
|
94
94
|
|
|
95
95
|
Where behavior deliberately differs from node, it is **documented, measured, and pinned by tests** — never silent. Known divergences: [CONTRIBUTING.md](./CONTRIBUTING.md) §5.
|
|
96
96
|
|
|
@@ -236,10 +236,9 @@ h2lean terminates **untrusted bytes from the internet** and turns them into obje
|
|
|
236
236
|
|
|
237
237
|
## ⚠️ Not Implemented — Said Loudly
|
|
238
238
|
|
|
239
|
-
|
|
239
|
+
One API is absent, and it fails **loudly** with `ERR_H2LEAN_NOT_IMPLEMENTED` — never a silent wrong answer:
|
|
240
240
|
|
|
241
241
|
- **Server push** — removed from browsers in 2022 and considered obsolete; the safer replacement is [Early Hints (103)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/103).
|
|
242
|
-
- **`performServerHandshake`** — no known use case outside node's own tests.
|
|
243
242
|
|
|
244
243
|
## 📚 Documentation
|
|
245
244
|
|
package/index.js
CHANGED
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
'use strict';
|
|
11
11
|
|
|
12
12
|
const nodeHttp2 = require('node:http2');
|
|
13
|
-
const { createServer, createSecureServer, Http2Server, Http2SecureServer,
|
|
14
|
-
Http2ServerRequest, Http2ServerResponse
|
|
13
|
+
const { createServer, createSecureServer, performServerHandshake, Http2Server, Http2SecureServer,
|
|
14
|
+
Http2ServerRequest, Http2ServerResponse } = require('./lib/server');
|
|
15
15
|
const { Http2Session, OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID, ID_BY_SETTING } = require('./lib/session');
|
|
16
16
|
const { DATA, HEADERS, PRIORITY, RST_STREAM, SETTINGS, PUSH_PROMISE, PING, GOAWAY,
|
|
17
17
|
WINDOW_UPDATE, CONTINUATION } = require('./lib/frames');
|
|
@@ -54,7 +54,7 @@ module.exports = {
|
|
|
54
54
|
createServer,
|
|
55
55
|
createSecureServer,
|
|
56
56
|
connect,
|
|
57
|
-
performServerHandshake
|
|
57
|
+
performServerHandshake,
|
|
58
58
|
getDefaultSettings: () => ({ ...OUR_DEFAULTS }),
|
|
59
59
|
getPackedSettings,
|
|
60
60
|
getUnpackedSettings,
|
package/lib/client-session.js
CHANGED
|
@@ -66,6 +66,13 @@ const { EventEmitter } = require('node:events');
|
|
|
66
66
|
const tls = require('node:tls');
|
|
67
67
|
const f = require('./frames');
|
|
68
68
|
const { Decoder, Encoder, HPackError } = require('./hpack');
|
|
69
|
+
const FlatList = require('./flat-list');
|
|
70
|
+
// The process-wide inbound decode scratch, same shape and size as the server
|
|
71
|
+
// session's; the full why-sharing-is-safe argument lives with the server's
|
|
72
|
+
// instance (lib/session.js). A module-level instance here too, not the server's
|
|
73
|
+
// list: the two never meet the same fill, and coupling the client module to the
|
|
74
|
+
// server's scratch would buy nothing.
|
|
75
|
+
const FLAT = new FlatList(64);
|
|
69
76
|
const { nullProto, isFieldName, lowerName, CONNECTION_SPECIFIC, SINGLE_VALUE_FIELDS } = require('./headers');
|
|
70
77
|
const { ClientHttp2Stream } = require('./client-stream');
|
|
71
78
|
const { pieceByteAt, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
|
|
@@ -74,7 +81,7 @@ const { pieceByteAt, pieceSlice, pieceConcat, pieceEquals } = require('./pieces'
|
|
|
74
81
|
// SETTINGS id means, what node advertises, or where the copy/reference break-even sits.
|
|
75
82
|
const {
|
|
76
83
|
OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID, ID_BY_SETTING, BODY_MIN, MAX_OUT_BATCH,
|
|
77
|
-
STAGE_MIN, MAX_SHAPES, strictSingleValueFields, hashBlock, hashPieces
|
|
84
|
+
STAGE_MIN, MAX_SHAPES, strictSingleValueFields, hashBlock, hashPieces, kBoundSession
|
|
78
85
|
} = require('./session');
|
|
79
86
|
|
|
80
87
|
const {
|
|
@@ -117,6 +124,15 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
117
124
|
constructor(socket, options, scheme, authority) {
|
|
118
125
|
super();
|
|
119
126
|
const opts = options || {};
|
|
127
|
+
// The same ownership mark the server session takes (node's one Http2Session base
|
|
128
|
+
// constructor): a socket bound to any session -- client or server -- cannot be
|
|
129
|
+
// bound again, so performServerHandshake on a client's socket throws too.
|
|
130
|
+
if (socket[kBoundSession] !== undefined) {
|
|
131
|
+
const err = new Error('The socket is already bound to an Http2Session');
|
|
132
|
+
err.code = 'ERR_HTTP2_SOCKET_BOUND';
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
socket[kBoundSession] = this;
|
|
120
136
|
this.socket = socket;
|
|
121
137
|
this.type = 1; // NGHTTP2_SESSION_CLIENT
|
|
122
138
|
this.closed = false;
|
|
@@ -146,7 +162,6 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
146
162
|
this._encoder = new Encoder(this.remoteSettings.headerTableSize,
|
|
147
163
|
this.localSettings.headerTableSize);
|
|
148
164
|
this._blocks = new WeakMap();
|
|
149
|
-
this._flat = [];
|
|
150
165
|
// node's strictSingleValueFields option (validateSessionOptions, default true,
|
|
151
166
|
// internal/http2/core.js): gates the strict-single-value rule on the request list
|
|
152
167
|
// (_addField below). The connect() call surfaces the validation the way
|
|
@@ -1424,7 +1439,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1424
1439
|
|
|
1425
1440
|
_processBlock(sid, end, block, pieces, from, to) {
|
|
1426
1441
|
const stream = this._streams.get(sid);
|
|
1427
|
-
const flat =
|
|
1442
|
+
const flat = FLAT;
|
|
1428
1443
|
// The cache is disabled (null) when stream === undefined (we must decode for
|
|
1429
1444
|
// HPACK state but there is nothing to cache it for) and when the application
|
|
1430
1445
|
// opted out via shapeCache: false.
|
|
@@ -1432,19 +1447,26 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1432
1447
|
const slot = shapes === null ? 0
|
|
1433
1448
|
: (block !== null ? this._shapeSlot(block) : this._shapeSlotPieces(pieces, from, to));
|
|
1434
1449
|
const hit = shapes === null ? undefined : shapes.get(slot);
|
|
1435
|
-
|
|
1450
|
+
// where this response's field list comes from: the scratch's backing array
|
|
1451
|
+
// on a decode, a cached copy on a shape hit -- both read by index, so the
|
|
1452
|
+
// valid length travels next to the list (same as the server session)
|
|
1453
|
+
let fields;
|
|
1454
|
+
let fcount;
|
|
1436
1455
|
try {
|
|
1437
1456
|
if (hit !== undefined && (block !== null
|
|
1438
1457
|
? hit.block.equals(block)
|
|
1439
1458
|
: pieceEquals(hit.block, pieces, from, to))) {
|
|
1440
1459
|
this._shapeHits++;
|
|
1441
1460
|
fields = hit.flat;
|
|
1461
|
+
fcount = fields.length;
|
|
1442
1462
|
} else {
|
|
1443
1463
|
if (block !== null) {
|
|
1444
1464
|
this._decoder.decode(block, 0, block.length, flat);
|
|
1445
1465
|
} else {
|
|
1446
1466
|
this._decoder.decodePieces(pieces, from, to, flat);
|
|
1447
1467
|
}
|
|
1468
|
+
fields = flat.items;
|
|
1469
|
+
fcount = flat.count;
|
|
1448
1470
|
if (shapes !== null) {
|
|
1449
1471
|
if (++this._shapeMisses > 64 && this._shapeHits * 4 < this._shapeMisses) {
|
|
1450
1472
|
this._shapes = null;
|
|
@@ -1454,7 +1476,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1454
1476
|
}
|
|
1455
1477
|
shapes.set(slot, {
|
|
1456
1478
|
block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
|
|
1457
|
-
flat:
|
|
1479
|
+
flat: fields.slice(0, fcount)
|
|
1458
1480
|
});
|
|
1459
1481
|
}
|
|
1460
1482
|
}
|
|
@@ -1484,10 +1506,10 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1484
1506
|
}
|
|
1485
1507
|
return this._fatal(PROTOCOL_ERROR, 'response HEADERS on an unopened stream id');
|
|
1486
1508
|
}
|
|
1487
|
-
if (this._refuseHeaders(sid, stream, fields)) {
|
|
1509
|
+
if (this._refuseHeaders(sid, stream, fields, fcount)) {
|
|
1488
1510
|
return;
|
|
1489
1511
|
}
|
|
1490
|
-
const headers = this._toObject(fields);
|
|
1512
|
+
const headers = this._toObject(fields, fcount);
|
|
1491
1513
|
stream._onHeaders(headers, end);
|
|
1492
1514
|
if (end) {
|
|
1493
1515
|
this._dropStream(sid);
|
|
@@ -1504,10 +1526,10 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1504
1526
|
// Both shapes used to reach the application unvalidated. Returns true when
|
|
1505
1527
|
// the block was refused, so the caller does not build or hand over the
|
|
1506
1528
|
// header object.
|
|
1507
|
-
_refuseHeaders(sid, stream, fields) {
|
|
1529
|
+
_refuseHeaders(sid, stream, fields, count) {
|
|
1508
1530
|
let status = 0;
|
|
1509
1531
|
let bad = null;
|
|
1510
|
-
for (let i = 0; i <
|
|
1532
|
+
for (let i = 0; i < count; i += 2) {
|
|
1511
1533
|
const name = fields[i];
|
|
1512
1534
|
if (name.charCodeAt(0) !== 58) {
|
|
1513
1535
|
continue;
|
|
@@ -1541,11 +1563,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
1541
1563
|
return true;
|
|
1542
1564
|
}
|
|
1543
1565
|
|
|
1544
|
-
_toObject(
|
|
1566
|
+
_toObject(fields, count) {
|
|
1545
1567
|
const o = nullProto();
|
|
1546
|
-
for (let i = 0; i <
|
|
1547
|
-
const name =
|
|
1548
|
-
let value =
|
|
1568
|
+
for (let i = 0; i < count; i += 2) {
|
|
1569
|
+
const name = fields[i];
|
|
1570
|
+
let value = fields[i + 1];
|
|
1549
1571
|
if (name === ':status') {
|
|
1550
1572
|
value = value | 0;
|
|
1551
1573
|
}
|
|
@@ -2010,6 +2032,7 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2010
2032
|
}
|
|
2011
2033
|
|
|
2012
2034
|
_onSocketClose() {
|
|
2035
|
+
this.socket[kBoundSession] = undefined;
|
|
2013
2036
|
if (this._closeEmitted) {
|
|
2014
2037
|
return;
|
|
2015
2038
|
}
|
package/lib/compat.js
CHANGED
|
@@ -846,4 +846,4 @@ function onServerStream(stream, headers, options, rawHeaders) {
|
|
|
846
846
|
server.emit('request', request, response);
|
|
847
847
|
}
|
|
848
848
|
|
|
849
|
-
module.exports = { Http2ServerRequest, Http2ServerResponse, onServerStream };
|
|
849
|
+
module.exports = { Http2ServerRequest, Http2ServerResponse, onServerStream, describeType };
|
package/lib/flat-list.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// A preallocated field list filled through push. `new Array(capacity)` plus
|
|
4
|
+
// `items[count++] = value` never allocates per block and never right-trims the
|
|
5
|
+
// store the way `length = 0` does -- the trim makes the next fill re-grow it from
|
|
6
|
+
// scratch on V8 (OPTIMIZATION.md item 38) -- and a cursor write beats both
|
|
7
|
+
// Array#push shapes at the sizes a header block reaches (bench/micro/prealloc.js,
|
|
8
|
+
// item 39). This class keeps the cursor write, hidden inside push(); the cost is
|
|
9
|
+
// the array staying HOLEY_ELEMENTS forever, so every read carries a hole check,
|
|
10
|
+
// which the whole fill-then-read cycle still pays for.
|
|
11
|
+
//
|
|
12
|
+
// push() doubles as the overflow detector: a write past the preallocation grows
|
|
13
|
+
// the store under the caller, and the oversized store is kept for the rest of the
|
|
14
|
+
// event-loop turn (the other blocks of the same read batch reuse it) while one
|
|
15
|
+
// setImmediate later -- the check phase of the same iteration -- it is rebuilt at
|
|
16
|
+
// the preallocation again, so one oversized block cannot pin its size for the
|
|
17
|
+
// process lifetime. The re-grow a workload whose blocks all exceed the capacity
|
|
18
|
+
// pays is one small array per turn; the common workload never allocates at all.
|
|
19
|
+
//
|
|
20
|
+
// Only items[0..count) is valid after a fill. The class fixes neither a size nor
|
|
21
|
+
// an instance: the capacity is the caller's decision, made where the fill pattern
|
|
22
|
+
// is known.
|
|
23
|
+
class FlatList {
|
|
24
|
+
constructor(capacity) {
|
|
25
|
+
this.capacity = capacity;
|
|
26
|
+
this.items = new Array(capacity);
|
|
27
|
+
this.count = 0;
|
|
28
|
+
this._shrinkQueued = false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// The decoder's write: appends one item, keeping count in lockstep with the
|
|
32
|
+
// slot writes, and answers the growth question at the exact write that grows
|
|
33
|
+
// the store -- no per-block tail check is needed.
|
|
34
|
+
push(value) {
|
|
35
|
+
if (this.count === this.items.length) {
|
|
36
|
+
this.grown();
|
|
37
|
+
}
|
|
38
|
+
this.items[this.count++] = value;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// A fill starts at slot 0: move the cursor back. The store is left alone --
|
|
42
|
+
// right-trimming it would make the next fill re-grow it from scratch (item 38).
|
|
43
|
+
reset() {
|
|
44
|
+
this.count = 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
grown() {
|
|
48
|
+
if (this._shrinkQueued) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
this._shrinkQueued = true;
|
|
52
|
+
setImmediate(() => {
|
|
53
|
+
this._shrinkQueued = false;
|
|
54
|
+
if (this.items.length > this.capacity) {
|
|
55
|
+
this.items = new Array(this.capacity);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = FlatList;
|
package/lib/hpack.js
CHANGED
|
@@ -579,10 +579,22 @@ class Decoder {
|
|
|
579
579
|
}
|
|
580
580
|
}
|
|
581
581
|
|
|
582
|
-
// Refills
|
|
583
|
-
//
|
|
582
|
+
// Refills `out` with name, value, name, value, ... and returns the number of
|
|
583
|
+
// field lines. `out` is either the caller's plain array or a FlatList scratch
|
|
584
|
+
// (lib/flat-list.js). The plain array is written through a local cursor and its
|
|
585
|
+
// length moves with the fill at the tail -- a reused array carries stale slots
|
|
586
|
+
// beyond n, and a preallocated backing store must never see `length = 0`, the
|
|
587
|
+
// store would be right-trimmed and the next block would re-grow it
|
|
588
|
+
// (OPTIMIZATION.md item 38). The FlatList is filled through out.push(), which
|
|
589
|
+
// keeps `count` in lockstep and doubles as the overflow detector, so its tail
|
|
590
|
+
// has nothing to do.
|
|
584
591
|
decode(buf, start, end, out) {
|
|
585
|
-
out.
|
|
592
|
+
const scratch = out.items !== undefined;
|
|
593
|
+
const a = scratch ? out.items : out;
|
|
594
|
+
if (scratch) {
|
|
595
|
+
out.reset();
|
|
596
|
+
}
|
|
597
|
+
let n = 0;
|
|
586
598
|
let p = start;
|
|
587
599
|
while (p < end) {
|
|
588
600
|
const b = buf[p];
|
|
@@ -592,32 +604,44 @@ class Decoder {
|
|
|
592
604
|
if (e === undefined) {
|
|
593
605
|
throw new HPackError(`hpack: invalid index ${RI}`);
|
|
594
606
|
}
|
|
595
|
-
|
|
607
|
+
if (scratch) {
|
|
608
|
+
out.push(e[0]);
|
|
609
|
+
out.push(e[1]);
|
|
610
|
+
n += 2;
|
|
611
|
+
} else {
|
|
612
|
+
a[n++] = e[0];
|
|
613
|
+
a[n++] = e[1];
|
|
614
|
+
}
|
|
596
615
|
p = RP;
|
|
597
616
|
} else if ((b & 0xc0) === 0x40) { // 01xxxxxx incremental
|
|
598
|
-
p = this.literal(buf, p, end, out, 6);
|
|
599
|
-
|
|
617
|
+
p = this.literal(buf, p, end, out, scratch, n, 6);
|
|
618
|
+
n += 2;
|
|
619
|
+
this.insert(a[n - 2], a[n - 1]);
|
|
600
620
|
} else if ((b & 0xe0) === 0x20) { // 001xxxxx size update
|
|
601
621
|
// RFC 7541 6.3: a size update may only appear at the beginning of a header block --
|
|
602
|
-
// before its first field line, and `
|
|
622
|
+
// before its first field line, and `n === 0` is exactly that. Measured on node
|
|
603
623
|
// (h2spec hpack/4.2), one that follows a field line is GOAWAY(COMPRESSION_ERROR) whether
|
|
604
624
|
// or not it changes anything: the indices of the lines already decoded name a table that
|
|
605
625
|
// the update says was different, so a second decoder that reads the block in the order
|
|
606
626
|
// the octets arrive would rebuild a different context from ours.
|
|
607
|
-
if (
|
|
627
|
+
if (n !== 0) {
|
|
608
628
|
throw new HPackError('hpack: table size update after a field line');
|
|
609
629
|
}
|
|
610
630
|
readInt(buf, p, 5, end);
|
|
611
631
|
this.setMaxSize(RI);
|
|
612
632
|
p = RP;
|
|
613
633
|
} else { // 0000/0001 literal
|
|
614
|
-
p = this.literal(buf, p, end, out, 4);
|
|
634
|
+
p = this.literal(buf, p, end, out, scratch, n, 4);
|
|
635
|
+
n += 2;
|
|
615
636
|
}
|
|
616
637
|
}
|
|
617
|
-
|
|
638
|
+
if (!scratch) {
|
|
639
|
+
out.length = n;
|
|
640
|
+
}
|
|
641
|
+
return n >> 1;
|
|
618
642
|
}
|
|
619
643
|
|
|
620
|
-
literal(buf, p, end, out, bits) {
|
|
644
|
+
literal(buf, p, end, out, scratch, n, bits) {
|
|
621
645
|
readInt(buf, p, bits, end);
|
|
622
646
|
let name;
|
|
623
647
|
if (RI === 0) {
|
|
@@ -633,7 +657,13 @@ class Decoder {
|
|
|
633
657
|
p = RP;
|
|
634
658
|
}
|
|
635
659
|
const v = this.str(buf, p, end);
|
|
636
|
-
|
|
660
|
+
if (scratch) {
|
|
661
|
+
out.push(name);
|
|
662
|
+
out.push(v.value);
|
|
663
|
+
} else {
|
|
664
|
+
out[n] = name;
|
|
665
|
+
out[n + 1] = v.value;
|
|
666
|
+
}
|
|
637
667
|
return v.p;
|
|
638
668
|
}
|
|
639
669
|
|
|
@@ -674,7 +704,12 @@ class Decoder {
|
|
|
674
704
|
// Only a block that a socket read split takes this path. A block that arrived
|
|
675
705
|
// whole is decoded out of the chunk it came in, with no cursor and no copy.
|
|
676
706
|
decodePieces(pieces, start, end, out) {
|
|
677
|
-
out.
|
|
707
|
+
const scratch = out.items !== undefined;
|
|
708
|
+
const a = scratch ? out.items : out;
|
|
709
|
+
if (scratch) {
|
|
710
|
+
out.reset();
|
|
711
|
+
}
|
|
712
|
+
let n = 0;
|
|
678
713
|
const cur = new PieceCursor(pieces, start, end);
|
|
679
714
|
while (cur.left > 0) {
|
|
680
715
|
const b = cur.peek();
|
|
@@ -684,26 +719,36 @@ class Decoder {
|
|
|
684
719
|
if (e === undefined) {
|
|
685
720
|
throw new HPackError(`hpack: invalid index ${cur.value}`);
|
|
686
721
|
}
|
|
687
|
-
|
|
722
|
+
if (scratch) {
|
|
723
|
+
out.push(e[0]);
|
|
724
|
+
out.push(e[1]);
|
|
725
|
+
n += 2;
|
|
726
|
+
} else {
|
|
727
|
+
a[n++] = e[0];
|
|
728
|
+
a[n++] = e[1];
|
|
729
|
+
}
|
|
688
730
|
} else if ((b & 0xc0) === 0x40) { // 01xxxxxx incremental
|
|
689
|
-
this.literalPieces(cur, out, 6);
|
|
690
|
-
this.insert(
|
|
731
|
+
n = this.literalPieces(cur, out, scratch, n, 6);
|
|
732
|
+
this.insert(a[n - 2], a[n - 1]);
|
|
691
733
|
} else if ((b & 0xe0) === 0x20) { // 001xxxxx size update
|
|
692
734
|
// RFC 7541 6.3: a size update may only appear at the beginning of a header
|
|
693
735
|
// block -- before its first field line. See decode() for the measurement.
|
|
694
|
-
if (
|
|
736
|
+
if (n !== 0) {
|
|
695
737
|
throw new HPackError('hpack: table size update after a field line');
|
|
696
738
|
}
|
|
697
739
|
cur.readInt(5);
|
|
698
740
|
this.setMaxSize(cur.value);
|
|
699
741
|
} else { // 0000/0001 literal
|
|
700
|
-
this.literalPieces(cur, out, 4);
|
|
742
|
+
n = this.literalPieces(cur, out, scratch, n, 4);
|
|
701
743
|
}
|
|
702
744
|
}
|
|
703
|
-
|
|
745
|
+
if (!scratch) {
|
|
746
|
+
out.length = n;
|
|
747
|
+
}
|
|
748
|
+
return n >> 1;
|
|
704
749
|
}
|
|
705
750
|
|
|
706
|
-
literalPieces(cur, out, bits) {
|
|
751
|
+
literalPieces(cur, out, scratch, n, bits) {
|
|
707
752
|
cur.readInt(bits);
|
|
708
753
|
let name;
|
|
709
754
|
if (cur.value === 0) {
|
|
@@ -716,7 +761,14 @@ class Decoder {
|
|
|
716
761
|
name = e[0];
|
|
717
762
|
}
|
|
718
763
|
const v = this.strPieces(cur);
|
|
719
|
-
|
|
764
|
+
if (scratch) {
|
|
765
|
+
out.push(name);
|
|
766
|
+
out.push(v);
|
|
767
|
+
return n + 2;
|
|
768
|
+
}
|
|
769
|
+
out[n++] = name;
|
|
770
|
+
out[n++] = v;
|
|
771
|
+
return n;
|
|
720
772
|
}
|
|
721
773
|
|
|
722
774
|
// A literal string out of the views: the length prefix, then the octets. The
|
package/lib/server.js
CHANGED
|
@@ -52,12 +52,20 @@
|
|
|
52
52
|
// allowHTTP1 -- is answered with node's 403 text instead of being destroyed silently,
|
|
53
53
|
// which is what node does (core.js connectionListener) and what this file used to claim
|
|
54
54
|
// wrong: no ALPN is not h2 prior knowledge, and it never became a session on node.
|
|
55
|
+
// * performServerHandshake(socket, options) runs a server session over a socket the caller
|
|
56
|
+
// already holds, with no server object (node: initializeOptions + new ServerHttp2Session
|
|
57
|
+
// (options, socket, undefined)). One surface difference, measured on v26.12.1: node wraps
|
|
58
|
+
// a socket that is not a StreamBase (a generic Duplex, e.g. stream.duplexPair()) in a
|
|
59
|
+
// JSStreamSocket (core.js Http2Session constructor), so the second argument of 'connect'
|
|
60
|
+
// is that wrapper; h2lean is stream-based and hands over the socket itself. For a real
|
|
61
|
+
// net.Socket/TLSSocket -- the StreamBase case -- both pass the same object. The first
|
|
62
|
+
// argument (the session) and the argument count agree in every case.
|
|
55
63
|
'use strict';
|
|
56
64
|
|
|
57
65
|
const net = require('node:net');
|
|
58
66
|
const tls = require('node:tls');
|
|
59
67
|
const { Http2Session, OUR_DEFAULTS, strictSingleValueFields } = require('./session');
|
|
60
|
-
const { onServerStream, Http2ServerRequest, Http2ServerResponse } = require('./compat');
|
|
68
|
+
const { onServerStream, Http2ServerRequest, Http2ServerResponse, describeType } = require('./compat');
|
|
61
69
|
|
|
62
70
|
const kCompat = Symbol('h2lean.compatListener');
|
|
63
71
|
// The transport's own close/on, captured before the shared API overrides them:
|
|
@@ -116,7 +124,6 @@ function initHttp2Server(server, options) {
|
|
|
116
124
|
|
|
117
125
|
// Turns an accepted socket -- plain or already decrypted -- into a session.
|
|
118
126
|
function attachSession(server, socket) {
|
|
119
|
-
socket.setNoDelay(true);
|
|
120
127
|
const session = new Http2Session(socket, server._sessionOptions);
|
|
121
128
|
session.server = server;
|
|
122
129
|
server._sessions.add(session);
|
|
@@ -436,7 +443,42 @@ function createSecureServer(options, onRequestHandler) {
|
|
|
436
443
|
return server;
|
|
437
444
|
}
|
|
438
445
|
|
|
446
|
+
// node's performServerHandshake (core.js): initializeOptions(options) then a
|
|
447
|
+
// ServerHttp2Session with NO server object. The socket is one the caller already
|
|
448
|
+
// holds -- typically a TLSSocket an https server negotiated 'h2' on, or a plain
|
|
449
|
+
// net.Socket for h2c. node does NOT check alpnProtocol here (the ALPN verdict is
|
|
450
|
+
// the caller's, not the handshake's), and neither do we; a still-connecting socket
|
|
451
|
+
// is waited for by the session constructor. The options h2lean implements are
|
|
452
|
+
// validated the way initializeOptions validates them; the rest (remoteCustomSettings,
|
|
453
|
+
// maxSessionInvalidFrames, connectionWindowSize, origins) concern features h2lean
|
|
454
|
+
// does not implement and are ignored here exactly as they are at createServer. The one
|
|
455
|
+
// surface difference (node's JSStreamSocket wrap of a generic Duplex, so 'connect's second
|
|
456
|
+
// argument differs there) is written up in this file's header.
|
|
457
|
+
function performServerHandshake(socket, options) {
|
|
458
|
+
if (options !== undefined &&
|
|
459
|
+
(options === null || typeof options !== 'object' || Array.isArray(options))) {
|
|
460
|
+
const err = new TypeError(
|
|
461
|
+
`The "options" argument must be of type object. Received ${describeType(options)}`);
|
|
462
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
463
|
+
throw err;
|
|
464
|
+
}
|
|
465
|
+
const opts = { ...options };
|
|
466
|
+
if (opts.settings !== undefined &&
|
|
467
|
+
(opts.settings === null || typeof opts.settings !== 'object' || Array.isArray(opts.settings))) {
|
|
468
|
+
const err = new TypeError(
|
|
469
|
+
`The "options.settings" property must be of type object. Received ${describeType(opts.settings)}`);
|
|
470
|
+
err.code = 'ERR_INVALID_ARG_TYPE';
|
|
471
|
+
throw err;
|
|
472
|
+
}
|
|
473
|
+
strictSingleValueFields(opts);
|
|
474
|
+
// opts.settings carries only what the application asked for, and the session's
|
|
475
|
+
// _advertised defaults to Object.keys(opts.settings) -- so the first SETTINGS frame
|
|
476
|
+
// advertises exactly those, empty when nothing was asked, the way node's does.
|
|
477
|
+
// No server means no compat layer: the session emits 'stream', never 'request'.
|
|
478
|
+
return new Http2Session(socket, opts);
|
|
479
|
+
}
|
|
480
|
+
|
|
439
481
|
module.exports = {
|
|
440
|
-
Http2Server, Http2SecureServer, createServer, createSecureServer,
|
|
441
|
-
Http2ServerRequest, Http2ServerResponse
|
|
482
|
+
Http2Server, Http2SecureServer, createServer, createSecureServer, performServerHandshake,
|
|
483
|
+
notImplemented, Http2ServerRequest, Http2ServerResponse
|
|
442
484
|
};
|
package/lib/session.js
CHANGED
|
@@ -19,11 +19,31 @@
|
|
|
19
19
|
'use strict';
|
|
20
20
|
|
|
21
21
|
const { EventEmitter } = require('node:events');
|
|
22
|
+
const tls = require('node:tls');
|
|
22
23
|
const f = require('./frames');
|
|
23
24
|
const { Decoder, Encoder, HPackError } = require('./hpack');
|
|
24
25
|
const { Http2Stream, errorOf, emitNT } = require('./stream');
|
|
26
|
+
const FlatList = require('./flat-list');
|
|
27
|
+
// The process-wide inbound decode scratch: one list per module instance, shared by
|
|
28
|
+
// every session it serves, instead of one array per live session. Why sharing is
|
|
29
|
+
// safe: a header block's fill and its consumption are synchronous -- they happen
|
|
30
|
+
// inside one _processBlock call on one thread, and nothing on that path can reenter
|
|
31
|
+
// frame processing (bytes reach a decoder only from a socket's read path, which
|
|
32
|
+
// cannot run reentrantly), and every consumer that outlives the call takes a copy
|
|
33
|
+
// (the shape cache a slice, the raw list a slice, the header object fresh strings),
|
|
34
|
+
// never the store -- so no two fills can interleave, and one list serves them all.
|
|
35
|
+
// 64 items is 32 field lines, above every request the suite and the matrix benches
|
|
36
|
+
// serve; a larger block grows the store once, and end() schedules the end-of-turn
|
|
37
|
+
// shrink back to the preallocation.
|
|
38
|
+
const FLAT = new FlatList(64);
|
|
25
39
|
const { pieceCopyTo, pieceSlice, pieceConcat, pieceEquals } = require('./pieces');
|
|
26
40
|
const { nullProto, hasBadValue, SINGLE_VALUE_FIELDS } = require('./headers');
|
|
41
|
+
|
|
42
|
+
// The socket's ownership mark, shared with the client session (node keeps it as
|
|
43
|
+
// socket[kBoundSession] in the one Http2Session base constructor both kinds run
|
|
44
|
+
// through). A socket binds to exactly one session; performServerHandshake on an
|
|
45
|
+
// already-bound socket throws ERR_HTTP2_SOCKET_BOUND.
|
|
46
|
+
const kBoundSession = Symbol('h2lean.boundSession');
|
|
27
47
|
// One shared 499 ms clock for the long idle timeouts, instead of a libuv timer
|
|
28
48
|
// handle per connection. Vendored verbatim; see lib/fast-timer.js.
|
|
29
49
|
const { setTimeout: idleSetTimeout, clearTimeout: idleClearTimeout } =
|
|
@@ -306,17 +326,27 @@ class Http2Session extends EventEmitter {
|
|
|
306
326
|
constructor(socket, options) {
|
|
307
327
|
super();
|
|
308
328
|
const opts = options || {};
|
|
329
|
+
if (socket[kBoundSession] !== undefined) {
|
|
330
|
+
const err = new Error('The socket is already bound to an Http2Session');
|
|
331
|
+
err.code = 'ERR_HTTP2_SOCKET_BOUND';
|
|
332
|
+
throw err;
|
|
333
|
+
}
|
|
334
|
+
socket[kBoundSession] = this;
|
|
309
335
|
this.socket = socket;
|
|
310
|
-
|
|
336
|
+
// undefined until a server attaches itself; a performServerHandshake session has
|
|
337
|
+
// no server, and node's `session.server` getter reads undefined there (core.js).
|
|
338
|
+
this.server = undefined;
|
|
311
339
|
this.type = 0; // NGHTTP2_SESSION_SERVER
|
|
312
340
|
this.closed = false;
|
|
313
341
|
this.destroyed = false;
|
|
314
342
|
this.connecting = false;
|
|
315
|
-
|
|
316
|
-
//
|
|
317
|
-
//
|
|
318
|
-
|
|
319
|
-
|
|
343
|
+
this._connectEmitted = false;
|
|
344
|
+
// Derived from the socket in _start, once it is connected -- node's setupHandle
|
|
345
|
+
// fills them then and its getters read undefined before that. Over h2c there is no
|
|
346
|
+
// encryption and the protocol is 'h2c'; over h2s a peer that negotiated nothing
|
|
347
|
+
// reports false rather than a name, which is h2 with prior knowledge on TLS.
|
|
348
|
+
this.encrypted = undefined;
|
|
349
|
+
this.alpnProtocol = undefined;
|
|
320
350
|
// What the application sees. node keeps the received values in a private state object and
|
|
321
351
|
// exposes them through getters that answer NO_ERROR / 0 when no GOAWAY has arrived
|
|
322
352
|
// (`this[kState].goawayCode || NGHTTP2_NO_ERROR`), so `session.goawayCode` reads 0 on a live
|
|
@@ -379,7 +409,6 @@ class Http2Session extends EventEmitter {
|
|
|
379
409
|
// (OPTIMIZATION.md item 22 for both sides of the trade-off).
|
|
380
410
|
this._share = opts.shareHeaders === true;
|
|
381
411
|
this._strictSingle = strictSingleValueFields(opts);
|
|
382
|
-
this._flat = [];
|
|
383
412
|
// The compat layer needs the decoded field list as it came off the wire.
|
|
384
413
|
// Copying it is a per-request allocation, so it only happens for servers
|
|
385
414
|
// that actually listen for 'request'.
|
|
@@ -511,7 +540,6 @@ class Http2Session extends EventEmitter {
|
|
|
511
540
|
// costs no allocation per write
|
|
512
541
|
this._onWrittenCb = () => this._onWritten();
|
|
513
542
|
|
|
514
|
-
socket.on('data', (d) => this._onData(d));
|
|
515
543
|
socket.on('error', (err) => this._onSocketError(err));
|
|
516
544
|
socket.on('end', () => this.destroy(NO_ERROR));
|
|
517
545
|
// 'finish' means everything we wrote reached the OS, so the GOAWAY of a close() is never
|
|
@@ -524,8 +552,77 @@ class Http2Session extends EventEmitter {
|
|
|
524
552
|
}
|
|
525
553
|
});
|
|
526
554
|
socket.on('close', () => this._onSocketClose());
|
|
555
|
+
// node's constructor takes the socket's Nagle and renegotiation settings here
|
|
556
|
+
// (core.js), so a performServerHandshake socket gets them too -- attachSession
|
|
557
|
+
// used to be the only place setNoDelay was called.
|
|
558
|
+
if (typeof socket.setNoDelay === 'function') {
|
|
559
|
+
socket.setNoDelay();
|
|
560
|
+
}
|
|
561
|
+
if (typeof socket.disableRenegotiation === 'function') {
|
|
562
|
+
socket.disableRenegotiation();
|
|
563
|
+
}
|
|
564
|
+
// node defers setupHandle until the socket reports connected ("socket.connecting
|
|
565
|
+
// || socket.secureConnecting", core.js). An accepted server socket always is; a
|
|
566
|
+
// performServerHandshake socket may still be connecting, and under TLS the ALPN
|
|
567
|
+
// verdict is not known until 'secureConnect'.
|
|
568
|
+
if (socket.connecting || socket.secureConnecting) {
|
|
569
|
+
this.connecting = true;
|
|
570
|
+
const ev = socket instanceof tls.TLSSocket ? 'secureConnect' : 'connect';
|
|
571
|
+
socket.once(ev, () => this._start());
|
|
572
|
+
} else {
|
|
573
|
+
this._start();
|
|
574
|
+
}
|
|
575
|
+
if (socket.readableLength > 0) {
|
|
576
|
+
// A caller may hand performServerHandshake a paused socket with the peer's preface
|
|
577
|
+
// already buffered. node drains that buffer on the next tick (core.js constructor);
|
|
578
|
+
// do the same, then subscribe. 'connect' was queued by _start ahead of this drain, so
|
|
579
|
+
// it fires first. An accepted createServer socket has nothing buffered here and takes
|
|
580
|
+
// the synchronous subscription below, the read path this file always had.
|
|
581
|
+
process.nextTick(() => {
|
|
582
|
+
if (this.destroyed) {
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
let buf;
|
|
586
|
+
while (socket.readableLength > 0 && (buf = socket.read()) !== null) {
|
|
587
|
+
this._onData(buf);
|
|
588
|
+
}
|
|
589
|
+
if (!this.destroyed) {
|
|
590
|
+
socket.on('data', (d) => this._onData(d));
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
} else {
|
|
594
|
+
socket.on('data', (d) => this._onData(d));
|
|
595
|
+
}
|
|
596
|
+
}
|
|
527
597
|
|
|
598
|
+
// The server session's setupHandle (node core.js): advertise our SETTINGS, fill the
|
|
599
|
+
// encryption/ALPN getters and tell the application the session is up. 'connect' carries
|
|
600
|
+
// (session, socket) and fires on the next tick so a listener attached after the
|
|
601
|
+
// constructor still catches it -- at setup, before any peer octet, NOT when the preface
|
|
602
|
+
// arrives (what this used to do, and what the old comment wrongly claimed node did).
|
|
603
|
+
// Measured on v26.12.1 (tools/probe-handshake.js CASE1/CASE6): node fires it at setup
|
|
604
|
+
// with both arguments, for createServer and performServerHandshake alike.
|
|
605
|
+
_start() {
|
|
606
|
+
if (this.destroyed) {
|
|
607
|
+
// node still emits 'connect' for a session destroyed before its socket connected
|
|
608
|
+
// (setupHandle's destroyed branch), so the handlers at least run and clear out.
|
|
609
|
+
if (!this._connectEmitted) {
|
|
610
|
+
this._connectEmitted = true;
|
|
611
|
+
process.nextTick(() => this.emit('connect', this, this.socket));
|
|
612
|
+
}
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
this.connecting = false;
|
|
616
|
+
if (this.socket.encrypted) {
|
|
617
|
+
this.encrypted = true;
|
|
618
|
+
this.alpnProtocol = this.socket.alpnProtocol;
|
|
619
|
+
} else {
|
|
620
|
+
this.encrypted = false;
|
|
621
|
+
this.alpnProtocol = 'h2c';
|
|
622
|
+
}
|
|
528
623
|
this._sendSettings();
|
|
624
|
+
this._connectEmitted = true;
|
|
625
|
+
process.nextTick(() => this.emit('connect', this, this.socket));
|
|
529
626
|
}
|
|
530
627
|
|
|
531
628
|
// ---- outbound queue --------------------------------------------------------
|
|
@@ -852,11 +949,6 @@ class Http2Session extends EventEmitter {
|
|
|
852
949
|
return this._fatal(PROTOCOL_ERROR, 'connection preface malformed');
|
|
853
950
|
}
|
|
854
951
|
this._prefaceSeen += take;
|
|
855
|
-
// node's server-side session fires 'connect' once the peer's preface is in,
|
|
856
|
-
// before any SETTINGS have been decoded
|
|
857
|
-
if (this._prefaceSeen === PREFACE_LEN) {
|
|
858
|
-
this.emit('connect');
|
|
859
|
-
}
|
|
860
952
|
i = take;
|
|
861
953
|
// the chunk stopped inside the preface: nothing was produced, but the
|
|
862
954
|
// batch flag must not leak into whatever the peer sends next
|
|
@@ -1458,9 +1550,9 @@ class Http2Session extends EventEmitter {
|
|
|
1458
1550
|
// The frozen headers object for one shape. Object.freeze is shallow and
|
|
1459
1551
|
// node's toHeaderObject puts set-cookie in an array, so that array has to be
|
|
1460
1552
|
// frozen too -- otherwise push() would corrupt every other request sharing it.
|
|
1461
|
-
_sharedObject(fields) {
|
|
1462
|
-
const o = this._toObject(fields);
|
|
1463
|
-
for (let i = 0; i <
|
|
1553
|
+
_sharedObject(fields, count) {
|
|
1554
|
+
const o = this._toObject(fields, count);
|
|
1555
|
+
for (let i = 0; i < count; i += 2) {
|
|
1464
1556
|
const v = o[fields[i]];
|
|
1465
1557
|
if (Array.isArray(v)) {
|
|
1466
1558
|
Object.freeze(v);
|
|
@@ -1474,7 +1566,7 @@ class Http2Session extends EventEmitter {
|
|
|
1474
1566
|
// refused, or { fields } when only fields with illegal names were dropped. See the
|
|
1475
1567
|
// block comment on the constants above for why each rule exists and where the
|
|
1476
1568
|
// behaviour came from.
|
|
1477
|
-
_requestProblem(src, trailer) {
|
|
1569
|
+
_requestProblem(src, count, trailer) {
|
|
1478
1570
|
let method = null, path = null, scheme = null, authority = null, host = null;
|
|
1479
1571
|
let protocol = null, contentLength = null;
|
|
1480
1572
|
// RFC 9113 8.1: pseudo-header fields, if any, come before all regular fields. Once
|
|
@@ -1483,7 +1575,7 @@ class Http2Session extends EventEmitter {
|
|
|
1483
1575
|
let pseudoAllowed = !trailer;
|
|
1484
1576
|
let kept = null; // copied list, only if something is dropped
|
|
1485
1577
|
|
|
1486
|
-
for (let i = 0; i <
|
|
1578
|
+
for (let i = 0; i < count; i += 2) {
|
|
1487
1579
|
const name = src[i];
|
|
1488
1580
|
const value = src[i + 1];
|
|
1489
1581
|
|
|
@@ -1665,13 +1757,17 @@ class Http2Session extends EventEmitter {
|
|
|
1665
1757
|
|
|
1666
1758
|
_processBlock(sid, end, block, pieces, from, to) {
|
|
1667
1759
|
const existing = this._streams.get(sid);
|
|
1668
|
-
const flat =
|
|
1760
|
+
const flat = FLAT;
|
|
1669
1761
|
const shapes = existing === undefined ? this._shapes : null;
|
|
1670
1762
|
const slot = shapes === null ? 0
|
|
1671
1763
|
: (block !== null ? this._shapeSlot(block) : this._shapeSlotPieces(pieces, from, to));
|
|
1672
1764
|
const hit = shapes === null ? undefined : shapes.get(slot);
|
|
1673
|
-
// where this request's field list
|
|
1674
|
-
|
|
1765
|
+
// where this request's field list comes from: the scratch's backing array on
|
|
1766
|
+
// a decode, a cached copy on a shape hit. Both are read by index, so the
|
|
1767
|
+
// valid length travels next to the list (a FlatList's count, an array's
|
|
1768
|
+
// length) instead of being re-derived at every use.
|
|
1769
|
+
let fields;
|
|
1770
|
+
let fcount;
|
|
1675
1771
|
let shared = null;
|
|
1676
1772
|
let bytes = 0;
|
|
1677
1773
|
// verdict of the inbound validator: null means the fields may go to the application
|
|
@@ -1687,6 +1783,7 @@ class Http2Session extends EventEmitter {
|
|
|
1687
1783
|
this._shapeHits++;
|
|
1688
1784
|
bytes = hit.bytes;
|
|
1689
1785
|
fields = hit.flat;
|
|
1786
|
+
fcount = fields.length;
|
|
1690
1787
|
shared = hit.headers;
|
|
1691
1788
|
verdict = hit.verdict;
|
|
1692
1789
|
} else {
|
|
@@ -1695,14 +1792,17 @@ class Http2Session extends EventEmitter {
|
|
|
1695
1792
|
} else {
|
|
1696
1793
|
this._decoder.decodePieces(pieces, from, to, flat);
|
|
1697
1794
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1795
|
+
fields = flat.items;
|
|
1796
|
+
fcount = flat.count;
|
|
1797
|
+
for (let i = 0; i < fcount; i++) {
|
|
1798
|
+
bytes += fields[i].length;
|
|
1700
1799
|
}
|
|
1701
|
-
//
|
|
1702
|
-
bytes += (
|
|
1703
|
-
verdict = this._requestProblem(
|
|
1800
|
+
// 'fields' is name, value, name, value: one entry overhead per field line
|
|
1801
|
+
bytes += (fcount >> 1) * ENTRY_OVERHEAD;
|
|
1802
|
+
verdict = this._requestProblem(fields, fcount, existing !== undefined);
|
|
1704
1803
|
if (verdict !== null && verdict.fields !== undefined) {
|
|
1705
1804
|
fields = verdict.fields;
|
|
1805
|
+
fcount = fields.length;
|
|
1706
1806
|
}
|
|
1707
1807
|
if (shapes !== null) {
|
|
1708
1808
|
// A client that varies one header per request (x-request-id) makes
|
|
@@ -1717,11 +1817,11 @@ class Http2Session extends EventEmitter {
|
|
|
1717
1817
|
} // bounded: clients vary
|
|
1718
1818
|
const entry = {
|
|
1719
1819
|
block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
|
|
1720
|
-
flat: fields.slice(0), bytes, headers: null,
|
|
1820
|
+
flat: fields.slice(0, fcount), bytes, headers: null,
|
|
1721
1821
|
verdict
|
|
1722
1822
|
};
|
|
1723
1823
|
if (this._share) {
|
|
1724
|
-
entry.headers = this._sharedObject(fields);
|
|
1824
|
+
entry.headers = this._sharedObject(fields, fcount);
|
|
1725
1825
|
shared = entry.headers;
|
|
1726
1826
|
}
|
|
1727
1827
|
shapes.set(slot, entry);
|
|
@@ -1798,7 +1898,18 @@ class Http2Session extends EventEmitter {
|
|
|
1798
1898
|
// between "the handler declined the request" and "the handler was never reached",
|
|
1799
1899
|
// and only the second one denies a smuggling chain its second reader.
|
|
1800
1900
|
if (verdict !== null && verdict.problem !== undefined) {
|
|
1801
|
-
|
|
1901
|
+
// A live stream takes the stream error, not a bare reset: measured on node
|
|
1902
|
+
// (tools/probe-trailer-rst.js), a trailer block carrying a pseudo-header is
|
|
1903
|
+
// RST_STREAM(PROTOCOL_ERROR) on the wire AND ERR_HTTP2_STREAM_ERROR + 'close'
|
|
1904
|
+
// on the stream -- the same shape as the missing-END_STREAM refusal above --
|
|
1905
|
+
// and the closed stream is what keeps the unread-request auto-close
|
|
1906
|
+
// (Http2Stream[kMaybeDestroy], see lib/stream.js _maybeAutoClose) from
|
|
1907
|
+
// putting a second RST(NO_ERROR) on the wire behind the refusal's.
|
|
1908
|
+
if (existing !== undefined) {
|
|
1909
|
+
this._streamError(existing, PROTOCOL_ERROR, verdict.problem);
|
|
1910
|
+
} else {
|
|
1911
|
+
this.sendRst(sid, PROTOCOL_ERROR);
|
|
1912
|
+
}
|
|
1802
1913
|
return;
|
|
1803
1914
|
}
|
|
1804
1915
|
|
|
@@ -1811,7 +1922,8 @@ class Http2Session extends EventEmitter {
|
|
|
1811
1922
|
this._streamError(existing, PROTOCOL_ERROR, 'trailing HEADERS without END_STREAM');
|
|
1812
1923
|
return;
|
|
1813
1924
|
}
|
|
1814
|
-
const trailers = this._share
|
|
1925
|
+
const trailers = this._share
|
|
1926
|
+
? this._sharedObject(fields, fcount) : this._toObject(fields, fcount);
|
|
1815
1927
|
existing.emit('trailers', trailers);
|
|
1816
1928
|
if (end) {
|
|
1817
1929
|
existing._remoteEnded();
|
|
@@ -1869,7 +1981,8 @@ class Http2Session extends EventEmitter {
|
|
|
1869
1981
|
}
|
|
1870
1982
|
|
|
1871
1983
|
const headers = shared !== null ? shared
|
|
1872
|
-
: (this._share ? Object.freeze(this._toObject(fields
|
|
1984
|
+
: (this._share ? Object.freeze(this._toObject(fields, fcount))
|
|
1985
|
+
: this._toObject(fields, fcount));
|
|
1873
1986
|
// The one shape in which the content-length promise is already broken before a single body
|
|
1874
1987
|
// octet could have arrived: the field promises a body and the HEADERS frame ends the message.
|
|
1875
1988
|
// Refusing at the accept decision -- the same place the parse failures and the size limits are
|
|
@@ -1913,7 +2026,7 @@ class Http2Session extends EventEmitter {
|
|
|
1913
2026
|
// node's argument order; the field list is the session's shared decode array
|
|
1914
2027
|
// or a cached one, so the raw list handed out is always a copy
|
|
1915
2028
|
this.emit('stream', stream, headers, end ? OPT_END_STREAM : OPT_NO_END_STREAM,
|
|
1916
|
-
this._wantRaw ? fields.slice(0) : null);
|
|
2029
|
+
this._wantRaw ? fields.slice(0, fcount) : null);
|
|
1917
2030
|
}
|
|
1918
2031
|
|
|
1919
2032
|
// node's toHeaderObject semantics: null prototype, :status coerced to a
|
|
@@ -1924,11 +2037,11 @@ class Http2Session extends EventEmitter {
|
|
|
1924
2037
|
// { __proto__: null } literal: the literal is in V8's dictionary mode, and this
|
|
1925
2038
|
// map is built once per request and read once per request by the application --
|
|
1926
2039
|
// in the stream path and in the compat path alike (OPTIMIZATION.md item 4).
|
|
1927
|
-
_toObject(
|
|
2040
|
+
_toObject(fields, count) {
|
|
1928
2041
|
const o = nullProto();
|
|
1929
|
-
for (let i = 0; i <
|
|
1930
|
-
const name =
|
|
1931
|
-
let value =
|
|
2042
|
+
for (let i = 0; i < count; i += 2) {
|
|
2043
|
+
const name = fields[i];
|
|
2044
|
+
let value = fields[i + 1];
|
|
1932
2045
|
if (name === ':status') {
|
|
1933
2046
|
value = value | 0;
|
|
1934
2047
|
}
|
|
@@ -2649,6 +2762,8 @@ class Http2Session extends EventEmitter {
|
|
|
2649
2762
|
}
|
|
2650
2763
|
|
|
2651
2764
|
_onSocketClose() {
|
|
2765
|
+
// release the ownership mark (node cleanupSession clears socket[kBoundSession])
|
|
2766
|
+
this.socket[kBoundSession] = undefined;
|
|
2652
2767
|
this._open = false;
|
|
2653
2768
|
if (!this.destroyed) {
|
|
2654
2769
|
this.destroy(NO_ERROR);
|
|
@@ -2751,4 +2866,4 @@ class Http2Session extends EventEmitter {
|
|
|
2751
2866
|
|
|
2752
2867
|
module.exports = { Http2Session, OUR_DEFAULTS, PEER_DEFAULTS, SETTINGS_BY_ID,
|
|
2753
2868
|
ID_BY_SETTING, MAX_OUT_BATCH, MAX_SESSION_HOLD, STREAM_HOLD_CHARGE, BODY_MIN,
|
|
2754
|
-
STAGE_MIN, MAX_SHAPES, strictSingleValueFields, hashBlock, hashPieces };
|
|
2869
|
+
STAGE_MIN, MAX_SHAPES, strictSingleValueFields, hashBlock, hashPieces, kBoundSession };
|
package/lib/stream.js
CHANGED
|
@@ -54,6 +54,19 @@ function emitEndCloseNT(target) {
|
|
|
54
54
|
target.emit('close');
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
// The scheduled half of the unread-request auto-close -- see _maybeAutoClose
|
|
58
|
+
// below for the rule and its measurement. The unread request half reports 'end'
|
|
59
|
+
// here, before the 'close' _destroy schedules behind it: node's trace for the
|
|
60
|
+
// shape does the same (test/event-order.test.js, scenario eof-body) -- the
|
|
61
|
+
// readable side is finished by the close, never having been read.
|
|
62
|
+
function autoCloseNT(stream) {
|
|
63
|
+
if (stream._destroyed) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
process.nextTick(emitNT, stream, 'end');
|
|
67
|
+
stream._destroy(NO_ERROR, undefined, true);
|
|
68
|
+
}
|
|
69
|
+
|
|
57
70
|
const {
|
|
58
71
|
ST_OPEN, ST_HALF_CLOSED_LOCAL, ST_HALF_CLOSED_REMOTE, ST_CLOSED, NO_ERROR, PROTOCOL_ERROR
|
|
59
72
|
} = frames;
|
|
@@ -340,7 +353,41 @@ class Http2Stream extends EventEmitter {
|
|
|
340
353
|
}
|
|
341
354
|
if (this.state.remoteClose === 1) {
|
|
342
355
|
this._closeIfBothDone();
|
|
356
|
+
} else {
|
|
357
|
+
// the response is over and the peer's half is not: this is the shape node
|
|
358
|
+
// closes for the application (see _maybeAutoClose). A stream the peer has
|
|
359
|
+
// already ended takes the graceful close above, which is also what node's
|
|
360
|
+
// wire shows there -- no RST, because nghttp2 refuses to reset what both
|
|
361
|
+
// halves have closed (tools/probe-stream-teardown.js, 'request-ended').
|
|
362
|
+
this._maybeAutoClose();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// node's Http2Stream[kMaybeDestroy] (internal/http2/core.js): a response the
|
|
367
|
+
// server session has finished, with trailers neither requested nor sent
|
|
368
|
+
// (STREAM_FLAGS_HAS_TRAILERS) and a request side nobody ever started to consume
|
|
369
|
+
// (`!state.didRead && readableFlowing === null`), is closed by the session
|
|
370
|
+
// itself -- `setImmediate(callStreamClose)` off the writable side's 'finish',
|
|
371
|
+
// and closeStream submits RST_STREAM(NO_ERROR). Measured on v26.12.1
|
|
372
|
+
// (tools/probe-stream-teardown.js, 'answer-only'): HEADERS, DATA, then
|
|
373
|
+
// RST_STREAM(NO_ERROR) on the wire and the stream destroyed -- and the
|
|
374
|
+
// boundaries with it: 'resume-then-answer' shows that touching the readable
|
|
375
|
+
// side stops the auto-close, and 'request-ended' that a stream the peer already
|
|
376
|
+
// ended never gets one. Without it a peer that sends HEADERS without END_STREAM
|
|
377
|
+
// and no DATA holds a stream and a _liveStreams slot until the idle timeout,
|
|
378
|
+
// and a closing session waits for it forever (AGENTS.md 10, item 2).
|
|
379
|
+
// `_consumeState === 0` is this file's reading of `!didRead && flowing ===
|
|
380
|
+
// null`: no reader attached and no pause()/resume() called -- either has
|
|
381
|
+
// touched the request, and node stops there too.
|
|
382
|
+
_maybeAutoClose() {
|
|
383
|
+
if (this._destroyed || !this.headersSent || this._waitForTrailers ||
|
|
384
|
+
this._consumeState !== 0) {
|
|
385
|
+
return;
|
|
343
386
|
}
|
|
387
|
+
// node's own deferral: the RST goes out a turn after the response, and the
|
|
388
|
+
// stream's 'close' with it (autoCloseNT above guards against a close() the
|
|
389
|
+
// application made in between)
|
|
390
|
+
setImmediate(autoCloseNT, this);
|
|
344
391
|
}
|
|
345
392
|
|
|
346
393
|
_closeIfBothDone() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h2lean",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"description": "Pure-JavaScript HTTP/2 for Node.js — a drop-in node:http2 replacement (server and client, h2c and TLS) with zero dependencies",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
"keywords": [
|
|
15
15
|
"http2",
|
|
16
16
|
"h2c",
|
|
17
|
+
"h2",
|
|
18
|
+
"request",
|
|
17
19
|
"node:http2",
|
|
18
20
|
"nghttp2",
|
|
19
21
|
"server",
|