Skip to content

Commit 159ab66

Browse files
anonrigRafaelGSS
authored andcommitted
lib: reduce URL invocations on http2 origins
PR-URL: #48338 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]>
1 parent 4ec2d92 commit 159ab66

File tree

5 files changed

+43
-4
lines changed

5 files changed

+43
-4
lines changed

lib/internal/http2/core.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const EventEmitter = require('events');
4545
const fs = require('fs');
4646
const http = require('http');
4747
const { readUInt16BE, readUInt32BE } = require('internal/buffer');
48-
const { URL } = require('internal/url');
48+
const { URL, getURLOrigin } = require('internal/url');
4949
const net = require('net');
5050
const { Duplex } = require('stream');
5151
const tls = require('tls');
@@ -636,7 +636,7 @@ function initOriginSet(session) {
636636
// We have to ensure that it is a properly serialized
637637
// ASCII origin string. The socket.servername might not
638638
// be properly ASCII encoded.
639-
originSet.add((new URL(originString)).origin);
639+
originSet.add(getURLOrigin(originString));
640640
}
641641
}
642642
return originSet;
@@ -1641,7 +1641,7 @@ class ServerHttp2Session extends Http2Session {
16411641
let origin;
16421642

16431643
if (typeof originOrStream === 'string') {
1644-
origin = (new URL(originOrStream)).origin;
1644+
origin = getURLOrigin(originOrStream);
16451645
if (origin === 'null')
16461646
throw new ERR_HTTP2_ALTSVC_INVALID_ORIGIN();
16471647
} else if (typeof originOrStream === 'number') {
@@ -1693,7 +1693,7 @@ class ServerHttp2Session extends Http2Session {
16931693
for (let i = 0; i < count; i++) {
16941694
let origin = origins[i];
16951695
if (typeof origin === 'string') {
1696-
origin = (new URL(origin)).origin;
1696+
origin = getURLOrigin(origin);
16971697
} else if (origin != null && typeof origin === 'object') {
16981698
origin = origin.origin;
16991699
}

lib/internal/url.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,16 @@ function toPathIfFileURL(fileURLOrPath) {
14361436
return fileURLToPath(fileURLOrPath);
14371437
}
14381438

1439+
/**
1440+
* This util takes a string containing a URL and return the URL origin,
1441+
* its meant to avoid calls to `new URL` constructor.
1442+
* @param {string} url
1443+
* @returns {URL['origin']}
1444+
*/
1445+
function getURLOrigin(url) {
1446+
return bindingUrl.getOrigin(url);
1447+
}
1448+
14391449
module.exports = {
14401450
toUSVString,
14411451
fileURLToPath,
@@ -1451,4 +1461,5 @@ module.exports = {
14511461
isURL,
14521462

14531463
urlUpdateActions: updateActions,
1464+
getURLOrigin,
14541465
};

src/node_errors.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ void AppendExceptionLine(Environment* env,
7474
V(ERR_INVALID_STATE, Error) \
7575
V(ERR_INVALID_THIS, TypeError) \
7676
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
77+
V(ERR_INVALID_URL, TypeError) \
7778
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
7879
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
7980
V(ERR_MISSING_ARGS, TypeError) \

src/node_url.cc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,30 @@ void BindingData::DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
117117
.ToLocalChecked());
118118
}
119119

120+
void BindingData::GetOrigin(const v8::FunctionCallbackInfo<Value>& args) {
121+
CHECK_GE(args.Length(), 1);
122+
CHECK(args[0]->IsString()); // input
123+
124+
Environment* env = Environment::GetCurrent(args);
125+
HandleScope handle_scope(env->isolate());
126+
127+
Utf8Value input(env->isolate(), args[0]);
128+
std::string_view input_view = input.ToStringView();
129+
auto out = ada::parse<ada::url_aggregator>(input_view);
130+
131+
if (!out) {
132+
THROW_ERR_INVALID_URL(env, "Invalid URL");
133+
return;
134+
}
135+
136+
std::string origin = out->get_origin();
137+
args.GetReturnValue().Set(String::NewFromUtf8(env->isolate(),
138+
origin.data(),
139+
NewStringType::kNormal,
140+
origin.length())
141+
.ToLocalChecked());
142+
}
143+
120144
void BindingData::CanParse(const FunctionCallbackInfo<Value>& args) {
121145
CHECK_GE(args.Length(), 1);
122146
CHECK(args[0]->IsString()); // input
@@ -322,6 +346,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
322346
SetMethodNoSideEffect(isolate, target, "domainToASCII", DomainToASCII);
323347
SetMethodNoSideEffect(isolate, target, "domainToUnicode", DomainToUnicode);
324348
SetMethodNoSideEffect(isolate, target, "format", Format);
349+
SetMethodNoSideEffect(isolate, target, "getOrigin", GetOrigin);
325350
SetMethod(isolate, target, "parse", Parse);
326351
SetMethod(isolate, target, "update", Update);
327352
SetFastMethodNoSideEffect(
@@ -341,6 +366,7 @@ void BindingData::RegisterExternalReferences(
341366
registry->Register(DomainToASCII);
342367
registry->Register(DomainToUnicode);
343368
registry->Register(Format);
369+
registry->Register(GetOrigin);
344370
registry->Register(Parse);
345371
registry->Register(Update);
346372
registry->Register(CanParse);

src/node_url.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class BindingData : public SnapshotableObject {
5353
const v8::FastOneByteString& input);
5454

5555
static void Format(const v8::FunctionCallbackInfo<v8::Value>& args);
56+
static void GetOrigin(const v8::FunctionCallbackInfo<v8::Value>& args);
5657
static void Parse(const v8::FunctionCallbackInfo<v8::Value>& args);
5758
static void Update(const v8::FunctionCallbackInfo<v8::Value>& args);
5859

0 commit comments

Comments
 (0)