summaryrefslogtreecommitdiff
path: root/src/node_url.cc
diff options
context:
space:
mode:
authorYagiz Nizipli <yagiz@nizipli.com>2023-02-24 17:35:05 -0500
committerGitHub <noreply@github.com>2023-02-24 22:35:05 +0000
commit80733921a4a6aba48de2975def4a0090518e4a6a (patch)
treea679a5da70b14ea34e21daa5842193feab122647 /src/node_url.cc
parent347215e99446834de8c9095d4fc91d2f73a0ffaf (diff)
downloadnode-new-80733921a4a6aba48de2975def4a0090518e4a6a.tar.gz
url: simplify and improve url formatting
PR-URL: https://github.com/nodejs/node/pull/46736 Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_url.cc')
-rw-r--r--src/node_url.cc73
1 files changed, 59 insertions, 14 deletions
diff --git a/src/node_url.cc b/src/node_url.cc
index 39905874bf..1e8f6772fb 100644
--- a/src/node_url.cc
+++ b/src/node_url.cc
@@ -11,7 +11,6 @@
namespace node {
-using v8::Boolean;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
@@ -47,7 +46,7 @@ enum url_update_action {
kHref = 9,
};
-void SetArgs(Environment* env, Local<Value> argv[12], const ada::result& url) {
+void SetArgs(Environment* env, Local<Value> argv[10], const ada::result& url) {
Isolate* isolate = env->isolate();
argv[0] = Utf8String(isolate, url->get_href());
argv[1] = Utf8String(isolate, url->get_origin());
@@ -59,8 +58,6 @@ void SetArgs(Environment* env, Local<Value> argv[12], const ada::result& url) {
argv[7] = Utf8String(isolate, url->get_password());
argv[8] = Utf8String(isolate, url->get_port());
argv[9] = Utf8String(isolate, url->get_hash());
- argv[10] = Boolean::New(isolate, url->host.has_value());
- argv[11] = Boolean::New(isolate, url->has_opaque_path);
}
void Parse(const FunctionCallbackInfo<Value>& args) {
@@ -86,8 +83,7 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
}
base_pointer = &base.value();
}
- ada::result out =
- ada::parse(std::string_view(input.out(), input.length()), base_pointer);
+ ada::result out = ada::parse(input.ToStringView(), base_pointer);
if (!out) {
return args.GetReturnValue().Set(false);
@@ -105,8 +101,6 @@ void Parse(const FunctionCallbackInfo<Value>& args) {
undef,
undef,
undef,
- undef,
- undef,
};
SetArgs(env, argv, out);
USE(success_callback_->Call(
@@ -192,10 +186,8 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
Utf8Value new_value(isolate, args[2].As<String>());
Local<Function> success_callback_ = args[3].As<Function>();
- std::string_view new_value_view =
- std::string_view(new_value.out(), new_value.length());
- std::string_view input_view = std::string_view(input.out(), input.length());
- ada::result out = ada::parse(input_view);
+ std::string_view new_value_view = new_value.ToStringView();
+ ada::result out = ada::parse(input.ToStringView());
CHECK(out);
bool result{true};
@@ -255,8 +247,6 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
undef,
undef,
undef,
- undef,
- undef,
};
SetArgs(env, argv, out);
USE(success_callback_->Call(
@@ -264,12 +254,66 @@ void UpdateUrl(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(result);
}
+void FormatUrl(const FunctionCallbackInfo<Value>& args) {
+ CHECK_GT(args.Length(), 4);
+ CHECK(args[0]->IsString()); // url href
+
+ Environment* env = Environment::GetCurrent(args);
+ Isolate* isolate = env->isolate();
+
+ Utf8Value href(isolate, args[0].As<String>());
+ const bool fragment = args[1]->IsTrue();
+ const bool unicode = args[2]->IsTrue();
+ const bool search = args[3]->IsTrue();
+ const bool auth = args[4]->IsTrue();
+
+ ada::result out = ada::parse(href.ToStringView());
+ CHECK(out);
+
+ if (!fragment) {
+ out->fragment = std::nullopt;
+ }
+
+ if (unicode) {
+#if defined(NODE_HAVE_I18N_SUPPORT)
+ std::string hostname = out->get_hostname();
+ MaybeStackBuffer<char> buf;
+ int32_t len = i18n::ToUnicode(&buf, hostname.data(), hostname.length());
+
+ if (len < 0) {
+ out->host = "";
+ } else {
+ out->host = buf.ToString();
+ }
+#else
+ out->host = "";
+#endif
+ }
+
+ if (!search) {
+ out->query = std::nullopt;
+ }
+
+ if (!auth) {
+ out->username = "";
+ out->password = "";
+ }
+
+ std::string result = out->get_href();
+ args.GetReturnValue().Set(String::NewFromUtf8(env->isolate(),
+ result.data(),
+ NewStringType::kNormal,
+ result.length())
+ .ToLocalChecked());
+}
+
void Initialize(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
SetMethod(context, target, "parse", Parse);
SetMethod(context, target, "updateUrl", UpdateUrl);
+ SetMethod(context, target, "formatUrl", FormatUrl);
SetMethodNoSideEffect(context, target, "domainToASCII", DomainToASCII);
SetMethodNoSideEffect(context, target, "domainToUnicode", DomainToUnicode);
@@ -279,6 +323,7 @@ void Initialize(Local<Object> target,
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Parse);
registry->Register(UpdateUrl);
+ registry->Register(FormatUrl);
registry->Register(DomainToASCII);
registry->Register(DomainToUnicode);