GeezerVison is showing github.com — http: avoid toLowerCase on the server hot path · nodejs/node@6aab8e1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_http_incoming.js +44-6Lines changed: 44 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,16 +56,15 @@ function readStop(socket) { | 56 | 56 |
| 57 | 57 | /* Abstract base class for ServerRequest and ClientResponse. */ | 58 | 58 | function IncomingMessage(socket) { | 59 | | - let streamOptions; | | 59 | + Readable.call(this); | 60 | 60 |
| 61 | 61 | if (socket) { | 62 | | - streamOptions = { | 63 | | - highWaterMark: socket.readableHighWaterMark, | 64 | | - }; | | 62 | + const hwm = socket.readableHighWaterMark; | | 63 | + if (this._readableState.highWaterMark !== hwm) { | | 64 | + this._readableState.highWaterMark = hwm; | | 65 | + } | 65 | 66 | } | 66 | 67 |
| 67 | | - Readable.call(this, streamOptions); | 68 | | - | 69 | 68 | this._readableState.readingMore = true; | 70 | 69 |
| 71 | 70 | this.socket = socket; | @@ -579,6 +578,43 @@ IncomingMessage.prototype._dump = function _dump() { | 579 | 578 | } | 580 | 579 | }; | 581 | 580 |
| | 581 | +// Case-insensitive ASCII compare against an already-lowercased name. | | 582 | +// Avoids allocating a lowercased copy of every header name. | | 583 | +function asciiEqualIgnoreCase(a, lower) { | | 584 | + const len = lower.length; | | 585 | + if (a.length !== len) | | 586 | + return false; | | 587 | + if (a === lower) | | 588 | + return true; | | 589 | + for (let i = 0; i < len; i++) { | | 590 | + let c = a.charCodeAt(i); | | 591 | + if (c >= 65 && c <= 90) | | 592 | + c += 32; | | 593 | + if (c !== lower.charCodeAt(i)) | | 594 | + return false; | | 595 | + } | | 596 | + return true; | | 597 | +} | | 598 | + | | 599 | +function getRawHeader(msg, lowerName, joinDuplicates) { | | 600 | + const rawHeaders = msg.rawHeaders; | | 601 | + const count = msg[kHeadersCount]; | | 602 | + let result; | | 603 | + for (let i = 0; i < count; i += 2) { | | 604 | + if (!asciiEqualIgnoreCase(rawHeaders[i], lowerName)) | | 605 | + continue; | | 606 | + const value = rawHeaders[i + 1]; | | 607 | + if (result === undefined) { | | 608 | + result = value; | | 609 | + if (!joinDuplicates) | | 610 | + return result; | | 611 | + } else { | | 612 | + result += ', ' + value; | | 613 | + } | | 614 | + } | | 615 | + return result; | | 616 | +} | | 617 | + | 582 | 618 | function onError(self, error, cb) { | 583 | 619 | // This is to keep backward compatible behavior. | 584 | 620 | // An error is emitted only if there are listeners attached to the event. | @@ -593,6 +629,8 @@ module.exports = { | 593 | 629 | IncomingMessage, | 594 | 630 | kDetachAbortSignal, | 595 | 631 | kHeadersCount, | | 632 | + asciiEqualIgnoreCase, | | 633 | + getRawHeader, | 596 | 634 | readStart, | 597 | 635 | readStop, | 598 | 636 | }; |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_outgoing.js +29-1Lines changed: 29 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -652,10 +652,38 @@ function storeHeader(self, state, key, value, validate, lenient) { | 652 | 652 | matchHeader(self, state, key, value); | 653 | 653 | } | 654 | 654 |
| | 655 | +function lowerOutgoingHeaderName(field) { | | 656 | + switch (field) { | | 657 | + case 'Connection': | | 658 | + case 'connection': | | 659 | + return 'connection'; | | 660 | + case 'Content-Length': | | 661 | + case 'content-length': | | 662 | + return 'content-length'; | | 663 | + case 'Transfer-Encoding': | | 664 | + case 'transfer-encoding': | | 665 | + return 'transfer-encoding'; | | 666 | + case 'Date': | | 667 | + case 'date': | | 668 | + return 'date'; | | 669 | + case 'Expect': | | 670 | + case 'expect': | | 671 | + return 'expect'; | | 672 | + case 'Trailer': | | 673 | + case 'trailer': | | 674 | + return 'trailer'; | | 675 | + case 'Keep-Alive': | | 676 | + case 'keep-alive': | | 677 | + return 'keep-alive'; | | 678 | + default: | | 679 | + return field.toLowerCase(); | | 680 | + } | | 681 | +} | | 682 | + | 655 | 683 | function matchHeader(self, state, field, value) { | 656 | 684 | if (field.length < 4 || field.length > 17) | 657 | 685 | return; | 658 | | - field = field.toLowerCase(); | | 686 | + field = lowerOutgoingHeaderName(field); | 659 | 687 | switch (field) { | 660 | 688 | case 'connection': | 661 | 689 | state.connection = true; | |
Collapse file Copy file name to clipboardExpand all lines: lib/_http_server.js +29-23Lines changed: 29 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,8 @@ const { | 72 | 72 | IncomingMessage, | 73 | 73 | kDetachAbortSignal, | 74 | 74 | kHeadersCount, | | 75 | + asciiEqualIgnoreCase, | | 76 | + getRawHeader, | 75 | 77 | } = require('_http_incoming'); | 76 | 78 | const { | 77 | 79 | ConnResetException, | @@ -217,7 +219,8 @@ function ServerResponse(req, options) { | 217 | 219 | this._expect_continue = false; | 218 | 220 |
| 219 | 221 | if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) { | 220 | | - this.useChunkedEncodingByDefault = chunkExpression.test(req.headers.te); | | 222 | + this.useChunkedEncodingByDefault = | | 223 | + chunkExpression.test(getRawHeader(req, 'te', true)); | 221 | 224 | this.shouldKeepAlive = false; | 222 | 225 | } | 223 | 226 |
| @@ -1182,10 +1185,8 @@ function hasRequestHeader(req, length, lowerName) { | 1182 | 1185 | const count = req[kHeadersCount]; | 1183 | 1186 | for (let i = 0; i < count; i += 2) { | 1184 | 1187 | const key = rawHeaders[i]; | 1185 | | - if (key.length === length && | 1186 | | - (key === lowerName || key.toLowerCase() === lowerName)) { | | 1188 | + if (key.length === length && asciiEqualIgnoreCase(key, lowerName)) | 1187 | 1189 | return true; | 1188 | | - } | 1189 | 1190 | } | 1190 | 1191 | return false; | 1191 | 1192 | } | @@ -1194,12 +1195,14 @@ function hasBodyHeaders(req) { | 1194 | 1195 | const rawHeaders = req.rawHeaders; | 1195 | 1196 | const count = req[kHeadersCount]; | 1196 | 1197 | for (let i = 0; i < count; i += 2) { | 1197 | | - const length = rawHeaders[i].length; | 1198 | | - if (length === 14 || length === 17) { | 1199 | | - const key = rawHeaders[i].toLowerCase(); | 1200 | | - if (key === 'content-length' || key === 'transfer-encoding') { | | 1198 | + const key = rawHeaders[i]; | | 1199 | + const length = key.length; | | 1200 | + if (length === 14) { | | 1201 | + if (asciiEqualIgnoreCase(key, 'content-length')) | | 1202 | + return true; | | 1203 | + } else if (length === 17) { | | 1204 | + if (asciiEqualIgnoreCase(key, 'transfer-encoding')) | 1201 | 1205 | return true; | 1202 | | - } | 1203 | 1206 | } | 1204 | 1207 | } | 1205 | 1208 | return false; | @@ -1333,22 +1336,25 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | 1333 | 1336 | server.emit('dropRequest', req, socket); | 1334 | 1337 | res.writeHead(503); | 1335 | 1338 | res.end(); | 1336 | | - } else if (hasRequestHeader(req, 6, 'expect')) { | 1337 | | - handled = true; | 1338 | | - | 1339 | | - if (continueExpression.test(req.headers.expect)) { | 1340 | | - res._expect_continue = true; | 1341 | | - if (server.listenerCount('checkContinue') > 0) { | 1342 | | - server.emit('checkContinue', req, res); | | 1339 | + } else { | | 1340 | + const expect = getRawHeader(req, 'expect', true); | | 1341 | + if (expect !== undefined) { | | 1342 | + handled = true; | | 1343 | + | | 1344 | + if (continueExpression.test(expect)) { | | 1345 | + res._expect_continue = true; | | 1346 | + if (server.listenerCount('checkContinue') > 0) { | | 1347 | + server.emit('checkContinue', req, res); | | 1348 | + } else { | | 1349 | + res.writeContinue(); | | 1350 | + server.emit('request', req, res); | | 1351 | + } | | 1352 | + } else if (server.listenerCount('checkExpectation') > 0) { | | 1353 | + server.emit('checkExpectation', req, res); | 1343 | 1354 | } else { | 1344 | | - res.writeContinue(); | 1345 | | - server.emit('request', req, res); | | 1355 | + res.writeHead(417); | | 1356 | + res.end(); | 1346 | 1357 | } | 1347 | | - } else if (server.listenerCount('checkExpectation') > 0) { | 1348 | | - server.emit('checkExpectation', req, res); | 1349 | | - } else { | 1350 | | - res.writeHead(417); | 1351 | | - res.end(); | 1352 | 1358 | } | 1353 | 1359 | } | 1354 | 1360 | } | |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const http = require('http'); | | 6 | +const net = require('net'); | | 7 | + | | 8 | +const server = http.createServer({ | | 9 | + optimizeEmptyRequests: true, | | 10 | +}, common.mustCall((req, res) => { | | 11 | + // Host / Expect lookups must not force materialization of req.headers. | | 12 | + // Accessing headers afterwards still works, including unusual casing. | | 13 | + assert.strictEqual(req.headers.host, 'example.test'); | | 14 | + res.writeHead(200, { 'Content-Type': 'text/plain' }); | | 15 | + res.end('ok'); | | 16 | +}, 3)); | | 17 | + | | 18 | +server.listen(0, common.mustCall(() => { | | 19 | + const port = server.address().port; | | 20 | + | | 21 | + function request(headers) { | | 22 | + return new Promise((resolve, reject) => { | | 23 | + const socket = net.connect(port, '127.0.0.1', () => { | | 24 | + socket.write(headers); | | 25 | + }); | | 26 | + let data = ''; | | 27 | + socket.setEncoding('utf8'); | | 28 | + socket.on('data', (chunk) => { data += chunk; }); | | 29 | + socket.on('end', () => resolve(data)); | | 30 | + socket.on('error', reject); | | 31 | + }); | | 32 | + } | | 33 | + | | 34 | + (async () => { | | 35 | + // Mixed-case Host must satisfy requireHostHeader. | | 36 | + const mixedHost = await request( | | 37 | + 'GET / HTTP/1.1\r\nHOST: example.test\r\nConnection: close\r\n\r\n', | | 38 | + ); | | 39 | + assert.match(mixedHost, /^HTTP\/1\.1 200 /); | | 40 | + | | 41 | + // Unusual Host casing. | | 42 | + const oddHost = await request( | | 43 | + 'GET / HTTP/1.1\r\nhOsT: example.test\r\nConnection: close\r\n\r\n', | | 44 | + ); | | 45 | + assert.match(oddHost, /^HTTP\/1\.1 200 /); | | 46 | + | | 47 | + // Mixed-case Expect: 100-continue still triggers the continue path. | | 48 | + const expectContinue = await request( | | 49 | + 'POST / HTTP/1.1\r\nHost: example.test\r\n' + | | 50 | + 'EXPECT: 100-continue\r\nContent-Length: 0\r\nConnection: close\r\n\r\n', | | 51 | + ); | | 52 | + assert.match(expectContinue, /^HTTP\/1\.1 100 Continue\r\n/); | | 53 | + assert.match(expectContinue, /HTTP\/1\.1 200 /); | | 54 | + | | 55 | + // Missing Host is still rejected. | | 56 | + const missingHost = await request( | | 57 | + 'GET / HTTP/1.1\r\nConnection: close\r\n\r\n', | | 58 | + ); | | 59 | + assert.match(missingHost, /^HTTP\/1\.1 400 /); | | 60 | + | | 61 | + server.close(); | | 62 | + })().then(common.mustCall()); | | 63 | +})); |
|