-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(http): remove allocations checking upgrade and connection header values #17727
Changes from all commits
95d83a0
5d728f3
1a2272a
235bf65
9158317
e46732b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
const core = globalThis.Deno.core; | ||
const internals = globalThis.__bootstrap.internals; | ||
const primordials = globalThis.__bootstrap.primordials; | ||
const { BadResourcePrototype, InterruptedPrototype, ops } = core; | ||
import * as webidl from "internal:deno_webidl/00_webidl.js"; | ||
|
@@ -40,18 +41,18 @@ import { | |
} from "internal:deno_web/06_streams.js"; | ||
const { | ||
ArrayPrototypeIncludes, | ||
ArrayPrototypeMap, | ||
ArrayPrototypePush, | ||
ArrayPrototypeSome, | ||
Error, | ||
ObjectPrototypeIsPrototypeOf, | ||
SafeSetIterator, | ||
Set, | ||
SetPrototypeAdd, | ||
SetPrototypeDelete, | ||
StringPrototypeCharCodeAt, | ||
StringPrototypeIncludes, | ||
StringPrototypeToLowerCase, | ||
StringPrototypeSplit, | ||
StringPrototypeTrim, | ||
Symbol, | ||
SymbolAsyncIterator, | ||
TypeError, | ||
|
@@ -389,15 +390,13 @@ function createRespondWith( | |
} | ||
|
||
const _ws = Symbol("[[associated_ws]]"); | ||
const websocketCvf = buildCaseInsensitiveCommaValueFinder("websocket"); | ||
const upgradeCvf = buildCaseInsensitiveCommaValueFinder("upgrade"); | ||
|
||
function upgradeWebSocket(request, options = {}) { | ||
const upgrade = request.headers.get("upgrade"); | ||
const upgradeHasWebSocketOption = upgrade !== null && | ||
ArrayPrototypeSome( | ||
StringPrototypeSplit(upgrade, ","), | ||
(option) => | ||
StringPrototypeToLowerCase(StringPrototypeTrim(option)) === "websocket", | ||
); | ||
websocketCvf(upgrade); | ||
if (!upgradeHasWebSocketOption) { | ||
throw new TypeError( | ||
"Invalid Header: 'upgrade' header must contain 'websocket'", | ||
|
@@ -406,11 +405,7 @@ function upgradeWebSocket(request, options = {}) { | |
|
||
const connection = request.headers.get("connection"); | ||
const connectionHasUpgradeOption = connection !== null && | ||
ArrayPrototypeSome( | ||
StringPrototypeSplit(connection, ","), | ||
(option) => | ||
StringPrototypeToLowerCase(StringPrototypeTrim(option)) === "upgrade", | ||
); | ||
upgradeCvf(connection); | ||
if (!connectionHasUpgradeOption) { | ||
throw new TypeError( | ||
"Invalid Header: 'connection' header must contain 'Upgrade'", | ||
|
@@ -471,4 +466,77 @@ function upgradeHttp(req) { | |
return req[_deferred].promise; | ||
} | ||
|
||
const spaceCharCode = StringPrototypeCharCodeAt(" ", 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel like it's reasonable to only check for plain old spaces instead of all whitespace? I couldn't find anywhere that explained exactly how this is supposed to work though. See past git history when it was checking all kinds of whitespace (and still faster). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://greenbytes.de/tech/webdav/rfc7230.html#whitespace At least that particular spec website defines that horizontal tab is also allowed as whitespace. |
||
const tabCharCode = StringPrototypeCharCodeAt("\t", 0); | ||
const commaCharCode = StringPrototypeCharCodeAt(",", 0); | ||
|
||
/** Builds a case function that can be used to find a case insensitive | ||
* value in some text that's separated by commas. | ||
* | ||
* This is done because it doesn't require any allocations. | ||
* @param checkText {string} - The text to find. (ex. "websocket") | ||
*/ | ||
function buildCaseInsensitiveCommaValueFinder(checkText) { | ||
const charCodes = ArrayPrototypeMap( | ||
StringPrototypeSplit( | ||
StringPrototypeToLowerCase(checkText), | ||
"", | ||
), | ||
(c) => [c.charCodeAt(0), c.toUpperCase().charCodeAt(0)], | ||
); | ||
/** @type {number} */ | ||
let i; | ||
/** @type {number} */ | ||
let char; | ||
|
||
/** @param value {string} */ | ||
return function (value) { | ||
for (i = 0; i < value.length; i++) { | ||
char = value.charCodeAt(i); | ||
skipWhitespace(value); | ||
|
||
if (hasWord(value)) { | ||
skipWhitespace(value); | ||
if (i === value.length || char === commaCharCode) { | ||
return true; | ||
} | ||
} else { | ||
skipUntilComma(value); | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
/** @param value {string} */ | ||
function hasWord(value) { | ||
for (const [cLower, cUpper] of charCodes) { | ||
if (cLower === char || cUpper === char) { | ||
char = StringPrototypeCharCodeAt(value, ++i); | ||
} else { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** @param value {string} */ | ||
function skipWhitespace(value) { | ||
while (char === spaceCharCode || char === tabCharCode) { | ||
char = StringPrototypeCharCodeAt(value, ++i); | ||
} | ||
} | ||
|
||
/** @param value {string} */ | ||
function skipUntilComma(value) { | ||
while (char !== commaCharCode && i < value.length) { | ||
char = StringPrototypeCharCodeAt(value, ++i); | ||
} | ||
} | ||
} | ||
|
||
// Expose this function for unit tests | ||
internals.buildCaseInsensitiveCommaValueFinder = | ||
buildCaseInsensitiveCommaValueFinder; | ||
|
||
export { _ws, HttpConn, upgradeHttp, upgradeWebSocket }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be done via a mapped type:https://discord.com/channels/684898665143206084/688040863313428503/1055570626787672165
Not relevant for this PR though.