Skip to content

Commit e31fd7a

Browse files
committed
url: warn when operation requires ICU
Continuation of: #35099 Signed-off-by: Darshan Sen <raisinten@gmail.com>
1 parent 7afa7b9 commit e31fd7a

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/node_url.cc

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "node_errors.h"
44
#include "node_external_reference.h"
55
#include "node_i18n.h"
6+
#include "node_process.h" // TODO: Replace with node_process-inl.h
67
#include "util-inl.h"
78

89
#include <cmath>
@@ -56,7 +57,8 @@ class URLHost {
5657
void ParseIPv4Host(const char* input, size_t length, bool* is_ipv4);
5758
void ParseIPv6Host(const char* input, size_t length);
5859
void ParseOpaqueHost(const char* input, size_t length);
59-
void ParseHost(const char* input,
60+
void ParseHost(Environment* env,
61+
const char* input,
6062
size_t length,
6163
bool is_special,
6264
bool unicode = false);
@@ -767,15 +769,19 @@ bool StartsWithWindowsDriveLetter(const char* p, const char* end) {
767769
}
768770

769771
#if defined(NODE_HAVE_I18N_SUPPORT)
770-
bool ToUnicode(const std::string& input, std::string* output) {
772+
bool ToUnicode(Environment* env,
773+
const std::string& input,
774+
std::string* output) {
771775
MaybeStackBuffer<char> buf;
772776
if (i18n::ToUnicode(&buf, input.c_str(), input.length()) < 0)
773777
return false;
774778
output->assign(*buf, buf.length());
775779
return true;
776780
}
777781

778-
bool ToASCII(const std::string& input, std::string* output) {
782+
bool ToASCII(Environment* env,
783+
const std::string& input,
784+
std::string* output) {
779785
MaybeStackBuffer<char> buf;
780786
if (i18n::ToASCII(&buf, input.c_str(), input.length()) < 0)
781787
return false;
@@ -786,12 +792,20 @@ bool ToASCII(const std::string& input, std::string* output) {
786792
}
787793
#else
788794
// Intentional non-ops if ICU is not present.
789-
bool ToUnicode(const std::string& input, std::string* output) {
795+
bool ToUnicode(Environment* env,
796+
const std::string& input,
797+
std::string* output) {
798+
ProcessEmitWarning(env,
799+
"Cannot convert to unicode when intl is disabled");
790800
*output = input;
791801
return true;
792802
}
793803

794-
bool ToASCII(const std::string& input, std::string* output) {
804+
bool ToASCII(Environment* env,
805+
const std::string& input,
806+
std::string* output) {
807+
ProcessEmitWarning(env,
808+
"Cannot convert to ASCII when intl is disabled");
795809
*output = input;
796810
return true;
797811
}
@@ -1022,7 +1036,8 @@ void URLHost::ParseOpaqueHost(const char* input, size_t length) {
10221036
SetOpaque(std::move(output));
10231037
}
10241038

1025-
void URLHost::ParseHost(const char* input,
1039+
void URLHost::ParseHost(Environment* env,
1040+
const char* input,
10261041
size_t length,
10271042
bool is_special,
10281043
bool unicode) {
@@ -1045,7 +1060,7 @@ void URLHost::ParseHost(const char* input,
10451060
std::string decoded = PercentDecode(input, length);
10461061

10471062
// Then we have to punycode toASCII
1048-
if (!ToASCII(decoded, &decoded))
1063+
if (!ToASCII(env, decoded, &decoded))
10491064
return;
10501065

10511066
// If any of the following characters are still present, we have to fail
@@ -1063,7 +1078,7 @@ void URLHost::ParseHost(const char* input,
10631078
return;
10641079

10651080
// If the unicode flag is set, run the result through punycode ToUnicode
1066-
if (unicode && !ToUnicode(decoded, &decoded))
1081+
if (unicode && !ToUnicode(env, decoded, &decoded))
10671082
return;
10681083

10691084
// It's not an IPv4 or IPv6 address, it must be a domain
@@ -1169,7 +1184,8 @@ std::string URLHost::ToString() const {
11691184
return dest;
11701185
}
11711186

1172-
bool ParseHost(const std::string& input,
1187+
bool ParseHost(Environment* env,
1188+
const std::string& input,
11731189
std::string* output,
11741190
bool is_special,
11751191
bool unicode = false) {
@@ -1178,7 +1194,7 @@ bool ParseHost(const std::string& input,
11781194
return true;
11791195
}
11801196
URLHost host;
1181-
host.ParseHost(input.c_str(), input.length(), is_special, unicode);
1197+
host.ParseHost(env, input.c_str(), input.length(), is_special, unicode);
11821198
if (host.ParsingFailed())
11831199
return false;
11841200
*output = host.ToStringMove();
@@ -1767,7 +1783,7 @@ void URL::Parse(const char* input,
17671783
return;
17681784
}
17691785
url->flags |= URL_FLAGS_HAS_HOST;
1770-
if (!ParseHost(buffer, &url->host, special)) {
1786+
if (!ParseHost(env_, buffer, &url->host, special)) {
17711787
url->flags |= URL_FLAGS_FAILED;
17721788
return;
17731789
}
@@ -1794,7 +1810,7 @@ void URL::Parse(const char* input,
17941810
return;
17951811
}
17961812
url->flags |= URL_FLAGS_HAS_HOST;
1797-
if (!ParseHost(buffer, &url->host, special)) {
1813+
if (!ParseHost(env_, buffer, &url->host, special)) {
17981814
url->flags |= URL_FLAGS_FAILED;
17991815
return;
18001816
}
@@ -1962,7 +1978,7 @@ void URL::Parse(const char* input,
19621978
state = kPathStart;
19631979
} else {
19641980
std::string host;
1965-
if (!ParseHost(buffer, &host, special)) {
1981+
if (!ParseHost(env_, buffer, &host, special)) {
19661982
url->flags |= URL_FLAGS_FAILED;
19671983
return;
19681984
}
@@ -2298,7 +2314,7 @@ void DomainToASCII(const FunctionCallbackInfo<Value>& args) {
22982314

22992315
URLHost host;
23002316
// Assuming the host is used for a special scheme.
2301-
host.ParseHost(*value, value.length(), true);
2317+
host.ParseHost(env, *value, value.length(), true);
23022318
if (host.ParsingFailed()) {
23032319
args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
23042320
return;
@@ -2316,7 +2332,7 @@ void DomainToUnicode(const FunctionCallbackInfo<Value>& args) {
23162332

23172333
URLHost host;
23182334
// Assuming the host is used for a special scheme.
2319-
host.ParseHost(*value, value.length(), true, true);
2335+
host.ParseHost(env, *value, value.length(), true, true);
23202336
if (host.ParsingFailed()) {
23212337
args.GetReturnValue().Set(FIXED_ONE_BYTE_STRING(env->isolate(), ""));
23222338
return;
@@ -2406,7 +2422,7 @@ std::string URL::ToFilePath() const {
24062422
if ((context_.flags & URL_FLAGS_HAS_HOST) &&
24072423
context_.host.length() > 0) {
24082424
std::string unicode_host;
2409-
if (!ToUnicode(context_.host, &unicode_host)) {
2425+
if (!ToUnicode(env_, context_.host, &unicode_host)) {
24102426
return "";
24112427
}
24122428
return "\\\\" + unicode_host + decoded_path;
@@ -2442,7 +2458,8 @@ URL URL::FromFilePath(const std::string& file_path) {
24422458
// This function works by calling out to a JS function that creates and
24432459
// returns the JS URL object. Be mindful of the JS<->Native boundary
24442460
// crossing that is required.
2445-
MaybeLocal<Value> URL::ToObject(Environment* env) const {
2461+
MaybeLocal<Value> URL::ToObject(Environment* env) {
2462+
env_ = env;
24462463
Isolate* isolate = env->isolate();
24472464
Local<Context> context = env->context();
24482465
Context::Scope context_scope(context);

src/node_url.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class URL {
173173
// Get the file URL from native file system path.
174174
static URL FromFilePath(const std::string& file_path);
175175

176-
v8::MaybeLocal<v8::Value> ToObject(Environment* env) const;
176+
v8::MaybeLocal<v8::Value> ToObject(Environment* env);
177177

178178
URL(const URL&) = default;
179179
URL& operator=(const URL&) = default;
@@ -183,6 +183,7 @@ class URL {
183183
URL() : URL("") {}
184184

185185
private:
186+
Environment* env_ = nullptr;
186187
struct url_data context_;
187188
};
188189

0 commit comments

Comments
 (0)