GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — http,http2: speed up header validation and write coalescing · nodejs/node@62f65f6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal
    • http2
      • util.js
    • http
      • client.js
      • common.js
      • incoming.js
      • outgoing.js
      • server.js
  • src
    • node_http2.cc
    • node_http_parser.cc
  • test/parallel
    • test-http-chunked-end-coalesce.js
    • test-http-common.js
    • test-http-native-headers.js
    • test-http-outgoing-message-write-callback.js
    • test-http-parser.js
    • test-http-response-cork.js
  • typings/internalBinding
    • http_parser.d.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/http/client.js‎

Copy file name to clipboardExpand all lines: lib/internal/http/client.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
942942
// we need to rewrite it to the first one and skip all the pending IncomingMessage
943943
socket.parser.incoming = req.res;
944944
socket.parser.incoming[kSkipPendingData] = true;
945+
socket.parser.setSkipBody?.(true);
945946
}
946947
return 0;
947948
}
Collapse file

‎lib/internal/http/common.js‎

Copy file name to clipboardExpand all lines: lib/internal/http/common.js
+21-12Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@
2222
'use strict';
2323

2424
const {
25+
ArrayIsArray,
2526
MathMin,
2627
Symbol,
2728
Uint8Array,
2829
} = primordials;
2930
const { setImmediate } = require('timers');
3031

31-
const { methods, allMethods, HTTPParser } = internalBinding('http_parser');
32+
const {
33+
methods,
34+
allMethods,
35+
HTTPParser,
36+
} = internalBinding('http_parser');
3237
const { getOptionValue } = require('internal/options');
3338
const insecureHTTPParser = getOptionValue('--insecure-http-parser');
3439

@@ -105,13 +110,17 @@ function parserOnHeadersComplete(versionMajor, versionMinor, headers, method,
105110
incoming.url = url;
106111
incoming.upgrade = upgrade;
107112

108-
let n = headers.length;
113+
if (headers !== undefined && !ArrayIsArray(headers)) {
114+
incoming._setNativeHeaders(headers, parser.maxHeaderPairs);
115+
} else {
116+
let n = headers.length;
109117

110-
// If parser.maxHeaderPairs <= 0 assume that there's no limit.
111-
if (parser.maxHeaderPairs > 0)
112-
n = MathMin(n, parser.maxHeaderPairs);
118+
// If parser.maxHeaderPairs <= 0 assume that there's no limit.
119+
if (parser.maxHeaderPairs > 0)
120+
n = MathMin(n, parser.maxHeaderPairs);
113121

114-
incoming._addHeaderLines(headers, n);
122+
incoming._addHeaderLines(headers, n);
123+
}
115124

116125
if (typeof method === 'number') {
117126
// server only
@@ -241,14 +250,14 @@ const validTokenChars = new Uint8Array([
241250
* @returns {boolean}
242251
*/
243252
function checkIsHttpToken(val) {
244-
if (val.length >= 10) {
253+
const len = val.length;
254+
if (len === 0) return false;
255+
// Table is faster for short names (Connection, Keep-Alive). V8's regex
256+
// wins once the name is longer than ~10 bytes (Content-Length, etc.).
257+
if (len > 10)
245258
return tokenRegExp.test(val);
246-
}
247-
248-
if (val.length === 0) return false;
249259

250-
// Use lookup table for short strings, regex for longer ones
251-
for (let i = 0; i < val.length; i++) {
260+
for (let i = 0; i < len; i++) {
252261
if (!validTokenChars[val.charCodeAt(i)]) {
253262
return false;
254263
}
Collapse file

‎lib/internal/http/incoming.js‎

Copy file name to clipboardExpand all lines: lib/internal/http/incoming.js
+147Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
MathMin,
2526
ObjectDefineProperty,
2627
ObjectSetPrototypeOf,
2728
Symbol,
@@ -31,12 +32,29 @@ const { Readable, finished } = require('stream');
3132
const {
3233
deprecateInstantiation,
3334
} = require('internal/util');
35+
const {
36+
HTTPParser,
37+
nativeHeadersHas,
38+
nativeHeadersGet,
39+
nativeHeadersToArray,
40+
} = internalBinding('http_parser');
41+
42+
const kNativeFlagHost = HTTPParser.kNativeHeaderFlagHost;
43+
const kNativeFlagExpect = HTTPParser.kNativeHeaderFlagExpect;
44+
const kNativeFlagContentLength = HTTPParser.kNativeHeaderFlagContentLength;
45+
const kNativeFlagTransferEncoding =
46+
HTTPParser.kNativeHeaderFlagTransferEncoding;
47+
const kNativeFlagTE = HTTPParser.kNativeHeaderFlagTE;
48+
const kNativeHeadersCountOffset = HTTPParser.kNativeHeadersCountOffset;
49+
const kNativeHeadersFlagsOffset = HTTPParser.kNativeHeadersFlagsOffset;
3450

3551
const { AbortController } = require('internal/abort_controller');
3652

3753
const kHeaders = Symbol('kHeaders');
3854
const kHeadersDistinct = Symbol('kHeadersDistinct');
3955
const kHeadersCount = Symbol('kHeadersCount');
56+
const kNativeHeaders = Symbol('kNativeHeaders');
57+
const kNativeFlags = Symbol('kNativeFlags');
4058
const kTrailers = Symbol('kTrailers');
4159
const kTrailersDistinct = Symbol('kTrailersDistinct');
4260
const kTrailersCount = Symbol('kTrailersCount');
@@ -82,6 +100,8 @@ function IncomingMessage(socket) {
82100
this.complete = false;
83101
this[kHeaders] = null;
84102
this[kHeadersCount] = 0;
103+
this[kNativeHeaders] = null;
104+
this[kNativeFlags] = 0;
85105
this.rawHeaders = [];
86106
this[kTrailers] = null;
87107
this[kTrailersCount] = 0;
@@ -122,6 +142,44 @@ ObjectDefineProperty(IncomingMessage.prototype, 'connection', {
122142
},
123143
});
124144

145+
function materializeRawHeaders() {
146+
const headers = this[kNativeHeaders] !== null ?
147+
nativeHeadersToArray(this[kNativeHeaders], this[kHeadersCount]) :
148+
[];
149+
this[kNativeHeaders] = null;
150+
this[kNativeFlags] = 0;
151+
ObjectDefineProperty(this, 'rawHeaders', {
152+
__proto__: null,
153+
configurable: true,
154+
enumerable: true,
155+
writable: true,
156+
value: headers,
157+
});
158+
return headers;
159+
}
160+
161+
function setRawHeaders(val) {
162+
this[kNativeHeaders] = null;
163+
this[kNativeFlags] = 0;
164+
ObjectDefineProperty(this, 'rawHeaders', {
165+
__proto__: null,
166+
configurable: true,
167+
enumerable: true,
168+
writable: true,
169+
value: val,
170+
});
171+
}
172+
173+
function defineLazyRawHeaders(self) {
174+
ObjectDefineProperty(self, 'rawHeaders', {
175+
__proto__: null,
176+
configurable: true,
177+
enumerable: true,
178+
get: materializeRawHeaders,
179+
set: setRawHeaders,
180+
});
181+
}
182+
125183
ObjectDefineProperty(IncomingMessage.prototype, 'headers', {
126184
__proto__: null,
127185
get: function() {
@@ -317,6 +375,63 @@ function abortSignal(self) {
317375
}
318376
}
319377

378+
IncomingMessage.prototype._setNativeHeaders = function _setNativeHeaders(native, maxPairs) {
379+
this[kNativeHeaders] = native;
380+
this[kNativeFlags] = native.readUInt32LE(kNativeHeadersFlagsOffset);
381+
const pairs = native.readUInt32LE(kNativeHeadersCountOffset);
382+
const len = pairs * 2;
383+
this[kHeadersCount] = maxPairs > 0 ? MathMin(len, maxPairs) : len;
384+
defineLazyRawHeaders(this);
385+
};
386+
387+
IncomingMessage.prototype._hasHeader = function _hasHeader(name) {
388+
if (this[kNativeHeaders] !== null) {
389+
const flags = this[kNativeFlags];
390+
switch (name) {
391+
case 'host':
392+
return (flags & kNativeFlagHost) !== 0;
393+
case 'expect':
394+
return (flags & kNativeFlagExpect) !== 0;
395+
case 'content-length':
396+
return (flags & kNativeFlagContentLength) !== 0;
397+
case 'transfer-encoding':
398+
return (flags & kNativeFlagTransferEncoding) !== 0;
399+
case 'te':
400+
return (flags & kNativeFlagTE) !== 0;
401+
default:
402+
return nativeHeadersHas(this[kNativeHeaders], name);
403+
}
404+
}
405+
const headers = this.rawHeaders;
406+
const n = this[kHeadersCount];
407+
const want = name.length;
408+
for (let i = 0; i < n; i += 2) {
409+
const field = headers[i];
410+
if (field.length === want && field.toLowerCase() === name)
411+
return true;
412+
}
413+
return false;
414+
};
415+
416+
IncomingMessage.prototype._getHeader = function _getHeader(name) {
417+
if (this[kNativeHeaders] !== null)
418+
return nativeHeadersGet(this[kNativeHeaders], name);
419+
const headers = this.rawHeaders;
420+
const n = this[kHeadersCount];
421+
const want = name.length;
422+
let value;
423+
for (let i = 0; i < n; i += 2) {
424+
const field = headers[i];
425+
if (field.length === want && field.toLowerCase() === name) {
426+
if (value === undefined)
427+
value = headers[i + 1];
428+
else
429+
value += ', ' + headers[i + 1];
430+
}
431+
}
432+
return value;
433+
};
434+
320435
IncomingMessage.prototype._addHeaderLines = _addHeaderLines;
321436
function _addHeaderLines(headers, n) {
322437
if (headers?.length) {
@@ -556,13 +671,40 @@ function _addHeaderLineDistinct(field, value, dest) {
556671
}
557672
}
558673

674+
// Scan C++ headers (or rawHeaders) so callers do not force req.headers.
675+
IncomingMessage.prototype._hasBodyHeaders = function _hasBodyHeaders() {
676+
if (this[kNativeHeaders] !== null) {
677+
return (this[kNativeFlags] &
678+
(kNativeFlagContentLength | kNativeFlagTransferEncoding)) !== 0;
679+
}
680+
const headers = this.rawHeaders;
681+
const n = this[kHeadersCount];
682+
for (let i = 0; i < n; i += 2) {
683+
const name = headers[i];
684+
const len = name.length;
685+
if (len === 14) {
686+
if (name === 'Content-Length' || name === 'content-length' ||
687+
name.toLowerCase() === 'content-length') {
688+
return true;
689+
}
690+
} else if (len === 17) {
691+
if (name === 'Transfer-Encoding' || name === 'transfer-encoding' ||
692+
name.toLowerCase() === 'transfer-encoding') {
693+
return true;
694+
}
695+
}
696+
}
697+
return false;
698+
};
699+
559700
IncomingMessage.prototype._dumpAndCloseReadable = function _dumpAndCloseReadable() {
560701
this._dumped = true;
561702
this._readableState.ended = true;
562703
this._readableState.endEmitted = true;
563704
this._readableState.destroyed = true;
564705
this._readableState.closed = true;
565706
this._readableState.closeEmitted = true;
707+
this.socket?.parser?.setSkipBody?.(true);
566708
};
567709

568710

@@ -571,6 +713,7 @@ IncomingMessage.prototype._dumpAndCloseReadable = function _dumpAndCloseReadable
571713
IncomingMessage.prototype._dump = function _dump() {
572714
if (!this._dumped) {
573715
this._dumped = true;
716+
this.socket?.parser?.setSkipBody?.(true);
574717
// If there is buffered data, it may trigger 'data' events.
575718
// Remove 'data' event listeners explicitly.
576719
this.removeAllListeners('data');
@@ -604,6 +747,10 @@ function asciiEqualIgnoreCase(a, lower) {
604747
}
605748

606749
function getRawHeader(msg, lowerName, joinDuplicates) {
750+
// Packed headers stay in the native buffer until something reads them.
751+
// Joined lookups can be answered without materializing rawHeaders.
752+
if (joinDuplicates && msg[kNativeHeaders] !== null)
753+
return msg._getHeader(lowerName);
607754
const rawHeaders = msg.rawHeaders;
608755
const count = msg[kHeadersCount];
609756
let result;

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.