Skip to content

Commit

Permalink
url, bugfix: allow pass number type port to url.format. (#728)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardo2016 committed Jul 11, 2022
1 parent 6db9329 commit 24bbe7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
15 changes: 10 additions & 5 deletions fibjs/src/net/Url.cpp
Expand Up @@ -448,14 +448,19 @@ result_t Url::parse(exlib::string url, bool parseQueryString, bool slashesDenote
}

bool getString(Isolate* isolate, v8::Local<v8::Object>& args,
const char* key, exlib::string& retVal)
const char* key, exlib::string& retVal, bool allowNumber = false)
{
v8::Local<v8::Context> context = isolate->context();
JSValue v = args->Get(context, isolate->NewString(key));

if (!v.IsEmpty() && (v->IsString() || v->IsStringObject())) {
retVal = isolate->toString(v);
return true;
if (!v.IsEmpty()) {
if (v->IsString() || v->IsStringObject()) {
retVal = isolate->toString(v);
return true;
} else if (allowNumber && (v->IsNumber() || v->IsNumberObject())) {
retVal = isolate->toString(v);
return true;
}
}

return false;
Expand All @@ -481,7 +486,7 @@ result_t Url::format(v8::Local<v8::Object> args)

if (getString(isolate, args, "host", str))
set__host(str);
if (getString(isolate, args, "port", str))
if (getString(isolate, args, "port", str, true))
set_port(str);

if (getString(isolate, args, "hostname", str))
Expand Down
9 changes: 9 additions & 0 deletions test/url_test.js
Expand Up @@ -630,6 +630,15 @@ describe("url", () => {
'hash': 'h',
'query': 's'
},
'https://a.com:447/a/b/c?s#h': {
'href': 'https://a.com:447/a/b/c?s#h',
'protocol': 'https',
'hostname': 'a.com',
'pathname': 'a/b/c',
'port': 447,
'hash': 'h',
'query': 's'
},
'http://atpass:foo%40bar@127.0.0.1/': {
'href': 'http://atpass:foo%40bar@127.0.0.1/',
'username': 'atpass',
Expand Down

0 comments on commit 24bbe7e

Please sign in to comment.