Bug 1433958 - Change code that sets nsIURI.host to use nsIURIMutator r=mayhemer

MozReview-Commit-ID: 7T5gCu8WOfa

--HG--
extra : rebase_source : 752c34e5f9dde2e16fc9243c11860b987acbcb93
This commit is contained in:
Valentin Gosu 2018-02-26 20:43:47 +01:00
parent 7f3b09b694
commit 27e9e0be9f
12 changed files with 55 additions and 42 deletions

View file

@ -228,7 +228,9 @@ DomainSet::ContainsSuperDomain(nsIURI* aDomain, bool* aContains)
if (index == kNotFound)
break;
domain.Assign(Substring(domain, index + 1));
rv = clone->SetHost(domain);
rv = NS_MutateURI(clone)
.SetHost(domain)
.Finalize(clone);
NS_ENSURE_SUCCESS(rv, rv);
}

View file

@ -16,6 +16,7 @@
#include "nsIScriptObjectPrincipal.h"
#include "nsIScriptContext.h"
#include "nsIURL.h"
#include "nsIURIMutator.h"
#include "nsINestedURI.h"
#include "nspr.h"
#include "nsJSPrincipals.h"
@ -533,10 +534,8 @@ DenyAccessIfURIHasFlags(nsIURI* aURI, uint32_t aURIFlags)
static bool
EqualOrSubdomain(nsIURI* aProbeArg, nsIURI* aBase)
{
// Make a clone of the incoming URI, because we're going to mutate it.
nsCOMPtr<nsIURI> probe;
nsresult rv = aProbeArg->Clone(getter_AddRefs(probe));
NS_ENSURE_SUCCESS(rv, false);
nsresult rv;
nsCOMPtr<nsIURI> probe = aProbeArg;
nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService(NS_EFFECTIVETLDSERVICE_CONTRACTID);
NS_ENSURE_TRUE(tldService, false);
@ -554,7 +553,9 @@ EqualOrSubdomain(nsIURI* aProbeArg, nsIURI* aBase)
return false;
}
NS_ENSURE_SUCCESS(rv, false);
rv = probe->SetHost(newHost);
rv = NS_MutateURI(probe)
.SetHost(newHost)
.Finalize(probe);
NS_ENSURE_SUCCESS(rv, false);
}
}

View file

@ -512,7 +512,7 @@ nsDefaultURIFixup::TryKeywordFixupForURIInfo(const nsACString& aURIString,
}
bool
nsDefaultURIFixup::MakeAlternateURI(nsIURI* aURI)
nsDefaultURIFixup::MakeAlternateURI(nsCOMPtr<nsIURI>& aURI)
{
if (!Preferences::GetRootBranch()) {
return false;
@ -607,7 +607,10 @@ nsDefaultURIFixup::MakeAlternateURI(nsIURI* aURI)
}
// Assign the new host string over the old one
aURI->SetHost(newHost);
Unused << NS_MutateURI(aURI)
.SetHost(newHost)
.Finalize(aURI);
return true;
}

View file

@ -37,7 +37,7 @@ private:
nsDefaultURIFixupInfo* aFixupInfo,
nsIInputStream** aPostData);
bool PossiblyHostPortUrl(const nsACString& aUrl);
bool MakeAlternateURI(nsIURI* aURI);
bool MakeAlternateURI(nsCOMPtr<nsIURI>& aURI);
bool IsDomainWhitelisted(const nsACString& aAsciiHost,
const uint32_t aDotLoc);
};

View file

@ -501,13 +501,18 @@ Link::SetHost(const nsAString &aHost)
void
Link::SetHostname(const nsAString &aHostname)
{
nsCOMPtr<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURI> uri(GetURI());
if (!uri) {
// Ignore failures to be compatible with NS4.
return;
}
(void)uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
nsresult rv = NS_MutateURI(uri)
.SetHost(NS_ConvertUTF16toUTF8(aHostname))
.Finalize(uri);
if (NS_FAILED(rv)) {
return;
}
SetHrefAttribute(uri);
}

View file

@ -407,12 +407,14 @@ Location::SetHostname(const nsAString& aHostname,
}
nsCOMPtr<nsIURI> uri;
aRv = GetWritableURI(getter_AddRefs(uri));
aRv = GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(aRv.Failed()) || !uri) {
return;
}
aRv = uri->SetHost(NS_ConvertUTF16toUTF8(aHostname));
aRv = NS_MutateURI(uri)
.SetHost(NS_ConvertUTF16toUTF8(aHostname))
.Finalize(uri);
if (NS_WARN_IF(aRv.Failed())) {
return;
}

View file

@ -353,7 +353,9 @@ URLMainThread::SetHostname(const nsAString& aHostname, ErrorResult& aRv)
{
// nsStandardURL returns NS_ERROR_UNEXPECTED for an empty hostname
// The return code is silently ignored
mURI->SetHost(NS_ConvertUTF16toUTF8(aHostname));
mozilla::Unused << NS_MutateURI(mURI)
.SetHost(NS_ConvertUTF16toUTF8(aHostname))
.Finalize(mURI);
}
void

View file

@ -28,6 +28,7 @@
#include "mozilla/Attributes.h"
#include "nsXULAppAPI.h"
#include "nsIPrincipal.h"
#include "nsIURIMutator.h"
#include "nsContentUtils.h"
#include "nsIScriptSecurityManager.h"
#include "nsIEffectiveTLDService.h"
@ -296,16 +297,13 @@ GetNextSubDomainURI(nsIURI* aURI)
}
nsCOMPtr<nsIURI> uri;
rv = aURI->Clone(getter_AddRefs(uri));
rv = NS_MutateURI(aURI)
.SetHost(domain)
.Finalize(uri);
if (NS_FAILED(rv) || !uri) {
return nullptr;
}
rv = uri->SetHost(domain);
if (NS_FAILED(rv)) {
return nullptr;
}
return uri.forget();
}
@ -664,7 +662,9 @@ UpgradeHostToOriginAndInsert(const nsACString& aHost, const nsCString& aType,
if (NS_WARN_IF(NS_FAILED(rv))) continue;
// Use the provided host - this URI may be for a subdomain, rather than the host we care about.
rv = uri->SetHost(aHost);
rv = NS_MutateURI(uri)
.SetHost(aHost)
.Finalize(uri);
if (NS_WARN_IF(NS_FAILED(rv))) continue;
// We now have a URI which we can make a nsIPrincipal out of

View file

@ -526,8 +526,7 @@ function do_test_immutable(aTest) {
var URI = NetUtil.newURI(aTest.spec);
// All the non-readonly attributes on nsIURI.idl:
var propertiesToCheck = ["spec", "scheme",
"host"];
var propertiesToCheck = ["spec", "scheme"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;

View file

@ -627,8 +627,7 @@ function do_test_immutable(aTest) {
var URI = NetUtil.newURI(aTest.spec);
// All the non-readonly attributes on nsIURI.idl:
var propertiesToCheck = ["scheme",
"host"];
var propertiesToCheck = ["scheme"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;

View file

@ -16,7 +16,7 @@ function run_test() {
success = false;
try {
newURI.host = " foo.com";
newURI = newURI.mutate().setHost(" foo.com").finalize();
}
catch (e) {
success = e.result == Cr.NS_ERROR_MALFORMED_URI;

View file

@ -169,7 +169,7 @@ add_test(function test_setRef()
add_test(function test_ipv6()
{
var url = stringToURL("http://example.com");
url.host = "[2001::1]";
url = url.mutate().setHost("[2001::1]").finalize();
Assert.equal(url.host, "2001::1");
url = stringToURL("http://example.com");
@ -190,14 +190,14 @@ add_test(function test_ipv6_fail()
{
var url = stringToURL("http://example.com");
Assert.throws(() => { url.host = "2001::1"; }, "missing brackets");
Assert.throws(() => { url.host = "[2001::1]:20"; }, "url.host with port");
Assert.throws(() => { url.host = "[2001::1"; }, "missing last bracket");
Assert.throws(() => { url.host = "2001::1]"; }, "missing first bracket");
Assert.throws(() => { url.host = "2001[::1]"; }, "bad bracket position");
Assert.throws(() => { url.host = "[]"; }, "empty IPv6 address");
Assert.throws(() => { url.host = "[hello]"; }, "bad IPv6 address");
Assert.throws(() => { url.host = "[192.168.1.1]"; }, "bad IPv6 address");
Assert.throws(() => { url = url.mutate().setHost("2001::1").finalize(); }, "missing brackets");
Assert.throws(() => { url = url.mutate().setHost("[2001::1]:20").finalize(); }, "url.host with port");
Assert.throws(() => { url = url.mutate().setHost("[2001::1").finalize(); }, "missing last bracket");
Assert.throws(() => { url = url.mutate().setHost("2001::1]").finalize(); }, "missing first bracket");
Assert.throws(() => { url = url.mutate().setHost("2001[::1]").finalize(); }, "bad bracket position");
Assert.throws(() => { url = url.mutate().setHost("[]").finalize(); }, "empty IPv6 address");
Assert.throws(() => { url = url.mutate().setHost("[hello]").finalize(); }, "bad IPv6 address");
Assert.throws(() => { url = url.mutate().setHost("[192.168.1.1]").finalize(); }, "bad IPv6 address");
Assert.throws(() => { url = url.mutate().setHostPort("2001::1").finalize(); }, "missing brackets");
Assert.throws(() => { url = url.mutate().setHostPort("[2001::1]30").finalize(); }, "missing : after IP");
Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]").finalize(); }, "bad IPv6 address");
@ -219,7 +219,7 @@ add_test(function test_clearedSpec()
Assert.throws(() => { url = url.mutate().setSpec("http: example").finalize(); }, "set bad spec");
Assert.throws(() => { url = url.mutate().setSpec("").finalize(); }, "set empty spec");
Assert.equal(url.spec, "http://example.com/path");
url.host = "allizom.org";
url = url.mutate().setHost("allizom.org").finalize().QueryInterface(Ci.nsIURL);
var ref = stringToURL("http://allizom.org/path");
symmetricEquality(true, url, ref);
@ -301,8 +301,7 @@ add_test(function test_hugeStringThrows()
let url = stringToURL("http://test:test@example.com");
let hugeString = new Array(maxLen + 1).fill("a").join("");
let properties = ["scheme",
"host"];
let properties = ["scheme"];
for (let prop of properties) {
Assert.throws(() => url[prop] = hugeString,
/NS_ERROR_MALFORMED_URI/,
@ -315,6 +314,7 @@ add_test(function test_hugeStringThrows()
{ method: "setPassword", qi: Ci.nsIURIMutator },
{ method: "setFilePath", qi: Ci.nsIURIMutator },
{ method: "setHostPort", qi: Ci.nsIURIMutator },
{ method: "setHost", qi: Ci.nsIURIMutator },
{ method: "setUserPass", qi: Ci.nsIURIMutator },
{ method: "setPathQueryRef", qi: Ci.nsIURIMutator },
{ method: "setQuery", qi: Ci.nsIURIMutator },
@ -474,7 +474,7 @@ add_test(function test_ipv4Normalize()
}
var url = stringToURL("resource://path/to/resource/");
url.host = "123";
url = url.mutate().setHost("123").finalize();
Assert.equal(url.host, "123");
run_next_test();
@ -483,10 +483,10 @@ add_test(function test_ipv4Normalize()
add_test(function test_invalidHostChars() {
var url = stringToURL("http://example.org/");
for (let i = 0; i <= 0x20; i++) {
Assert.throws(() => { url.host = "a" + String.fromCharCode(i) + "b"; }, "Trying to set hostname containing char code: " + i);
Assert.throws(() => { url = url.mutate().setHost("a" + String.fromCharCode(i) + "b").finalize(); }, "Trying to set hostname containing char code: " + i);
}
for (let c of "@[]*<>|:\"") {
Assert.throws(() => { url.host = "a" + c; }, "Trying to set hostname containing char: " + c);
Assert.throws(() => { url = url.mutate().setHost("a" + c).finalize(); }, "Trying to set hostname containing char: " + c);
}
// It also can't contain /, \, #, ?, but we treat these characters as
@ -496,7 +496,7 @@ add_test(function test_invalidHostChars() {
add_test(function test_normalize_ipv6() {
var url = stringToURL("http://example.com");
url.host = "[::192.9.5.5]";
url = url.mutate().setHost("[::192.9.5.5]").finalize();
Assert.equal(url.spec, "http://[::c009:505]/");
run_next_test();