Skip to content

Commit

Permalink
Bug 1793597 - Also update the default port when changing the scheme o…
Browse files Browse the repository at this point in the history
…f a URL r=tschuster,necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D177048
  • Loading branch information
valenting committed May 8, 2023
1 parent 8f5ec7e commit 03260b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions netwerk/base/nsStandardURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,17 @@ nsresult nsStandardURL::SetScheme(const nsACString& input) {
// XXX the string code unfortunately doesn't provide a ToLowerCase
// that operates on a substring.
net_ToLowerCase((char*)mSpec.get(), mScheme.mLen);

// If the scheme changes the default port also changes.
if (Scheme() == "http"_ns || Scheme() == "ws"_ns) {
mDefaultPort = 80;
} else if (Scheme() == "https"_ns || Scheme() == "wss"_ns) {
mDefaultPort = 443;
}
if (mPort == mDefaultPort) {
MOZ_ALWAYS_SUCCEEDS(SetPort(-1));
}

return NS_OK;
}

Expand Down
35 changes: 35 additions & 0 deletions netwerk/test/gtest/TestURIMutator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ TEST(TestURIMutator, Mutator)
rv = mutator.Finalize(uri3);
ASSERT_EQ(rv, NS_ERROR_NOT_AVAILABLE);
ASSERT_TRUE(uri3 == nullptr);

// Make sure changing scheme updates the default port
rv = NS_NewURI(getter_AddRefs(uri),
"https://example.org:80/path?query#ref"_ns);
ASSERT_EQ(rv, NS_OK);
rv = NS_MutateURI(uri).SetScheme("http"_ns).Finalize(uri);
ASSERT_EQ(rv, NS_OK);
rv = uri->GetSpec(out);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(out, "http://example.org/path?query#ref"_ns);
int32_t port;
rv = uri->GetPort(&port);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(port, -1);
rv = uri->GetFilePath(out);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(out, "/path"_ns);
rv = uri->GetQuery(out);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(out, "query"_ns);
rv = uri->GetRef(out);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(out, "ref"_ns);

// Make sure changing scheme does not change non-default port
rv = NS_NewURI(getter_AddRefs(uri), "https://example.org:123"_ns);
ASSERT_EQ(rv, NS_OK);
rv = NS_MutateURI(uri).SetScheme("http"_ns).Finalize(uri);
ASSERT_EQ(rv, NS_OK);
rv = uri->GetSpec(out);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(out, "http://example.org:123/"_ns);
rv = uri->GetPort(&port);
ASSERT_EQ(rv, NS_OK);
ASSERT_EQ(port, 123);
}

extern MOZ_THREAD_LOCAL(uint32_t) gTlsURLRecursionCount;
Expand Down

0 comments on commit 03260b3

Please sign in to comment.