Bug 1433958 - Change code that uses nsIURI.hostPort to use nsIURIMutator
MozReview-Commit-ID: IuUurzUP5Rw
--- a/dom/base/Link.cpp
+++ b/dom/base/Link.cpp
@@ -10,16 +10,17 @@
#include "mozilla/MemoryReporting.h"
#include "mozilla/dom/Element.h"
#ifdef ANDROID
#include "mozilla/IHistory.h"
#else
#include "mozilla/places/History.h"
#endif
#include "nsIURL.h"
+#include "nsIURIMutator.h"
#include "nsISizeOf.h"
#include "nsIDocShell.h"
#include "nsIPrefetchService.h"
#include "nsCPrefetchService.h"
#include "nsStyleLinkElement.h"
#include "nsEscape.h"
#include "nsGkAtoms.h"
@@ -469,23 +470,28 @@ Link::SetUsername(const nsAString &aUser
uri->SetUsername(NS_ConvertUTF16toUTF8(aUsername));
SetHrefAttribute(uri);
}
void
Link::SetHost(const nsAString &aHost)
{
- nsCOMPtr<nsIURI> uri(GetURIToMutate());
+ nsCOMPtr<nsIURI> uri(GetURI());
if (!uri) {
// Ignore failures to be compatible with NS4.
return;
}
- (void)uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
+ nsresult rv = NS_MutateURI(uri)
+ .SetHostPort(NS_ConvertUTF16toUTF8(aHost))
+ .Finalize(uri);
+ if (NS_FAILED(rv)) {
+ return;
+ }
SetHrefAttribute(uri);
}
void
Link::SetHostname(const nsAString &aHostname)
{
nsCOMPtr<nsIURI> uri(GetURIToMutate());
if (!uri) {
--- a/dom/base/Location.cpp
+++ b/dom/base/Location.cpp
@@ -9,16 +9,17 @@
#include "nsIScriptObjectPrincipal.h"
#include "nsIScriptContext.h"
#include "nsIDocShell.h"
#include "nsIDocShellLoadInfo.h"
#include "nsIWebNavigation.h"
#include "nsCDefaultURIFixup.h"
#include "nsIURIFixup.h"
#include "nsIURL.h"
+#include "nsIURIMutator.h"
#include "nsIJARURI.h"
#include "nsNetUtil.h"
#include "nsCOMPtr.h"
#include "nsEscape.h"
#include "nsIDOMWindow.h"
#include "nsIDocument.h"
#include "nsIPresShell.h"
#include "nsPresContext.h"
@@ -356,22 +357,24 @@ Location::SetHost(const nsAString& aHost
ErrorResult& aRv)
{
if (!CallerSubsumes(&aSubjectPrincipal)) {
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}
nsCOMPtr<nsIURI> uri;
- aRv = GetWritableURI(getter_AddRefs(uri));
+ aRv = GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(aRv.Failed()) || !uri) {
return;
}
- aRv = uri->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
+ aRv = NS_MutateURI(uri)
+ .SetHostPort(NS_ConvertUTF16toUTF8(aHost))
+ .Finalize(uri);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
aRv = SetURI(uri);
}
void
--- a/dom/url/URLMainThread.cpp
+++ b/dom/url/URLMainThread.cpp
@@ -3,19 +3,21 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "URLMainThread.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Blob.h"
+#include "mozilla/Unused.h"
#include "nsContentUtils.h"
#include "nsHostObjectProtocolHandler.h"
#include "nsIURL.h"
+#include "nsIURIMutator.h"
#include "nsNetUtil.h"
namespace mozilla {
namespace dom {
namespace {
template<typename T>
@@ -309,17 +311,19 @@ void
URLMainThread::GetHost(nsAString& aHost, ErrorResult& aRv) const
{
URL_GETTER(aHost, GetHostPort);
}
void
URLMainThread::SetHost(const nsAString& aHost, ErrorResult& aRv)
{
- mURI->SetHostPort(NS_ConvertUTF16toUTF8(aHost));
+ Unused << NS_MutateURI(mURI)
+ .SetHostPort(NS_ConvertUTF16toUTF8(aHost))
+ .Finalize(mURI);
}
void
URLMainThread::UpdateURLSearchParams()
{
if (!mSearchParams) {
return;
}
--- a/netwerk/test/unit/test_URIs.js
+++ b/netwerk/test/unit/test_URIs.js
@@ -520,17 +520,17 @@ function do_test_mutate_ref(aTest, aSuff
// Tests that normally-mutable properties can't be modified on
// special URIs that are known to be immutable.
function do_test_immutable(aTest) {
Assert.ok(aTest.immutable);
var URI = NetUtil.newURI(aTest.spec);
// All the non-readonly attributes on nsIURI.idl:
var propertiesToCheck = ["spec", "scheme", "userPass", "username", "password",
- "hostPort", "host", "port", "pathQueryRef", "query", "ref"];
+ "host", "port", "pathQueryRef", "query", "ref"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;
try {
URI[aProperty] = "anothervalue";
} catch(e) {
threw = true;
}
--- a/netwerk/test/unit/test_URIs2.js
+++ b/netwerk/test/unit/test_URIs2.js
@@ -621,17 +621,17 @@ function do_test_mutate_ref(aTest, aSuff
// Tests that normally-mutable properties can't be modified on
// special URIs that are known to be immutable.
function do_test_immutable(aTest) {
Assert.ok(aTest.immutable);
var URI = NetUtil.newURI(aTest.spec);
// All the non-readonly attributes on nsIURI.idl:
var propertiesToCheck = ["scheme", "userPass", "username", "password",
- "hostPort", "host", "port", "pathQueryRef", "query", "ref"];
+ "host", "port", "pathQueryRef", "query", "ref"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;
try {
URI[aProperty] = "anothervalue";
} catch(e) {
threw = true;
}
--- a/netwerk/test/unit/test_standardurl.js
+++ b/netwerk/test/unit/test_standardurl.js
@@ -168,23 +168,23 @@ add_test(function test_setRef()
// Bug 960014 - Make nsStandardURL::SetHost less magical around IPv6
add_test(function test_ipv6()
{
var url = stringToURL("http://example.com");
url.host = "[2001::1]";
Assert.equal(url.host, "2001::1");
url = stringToURL("http://example.com");
- url.hostPort = "[2001::1]:30";
+ url = url.mutate().setHostPort("[2001::1]:30").finalize();
Assert.equal(url.host, "2001::1");
Assert.equal(url.port, 30);
Assert.equal(url.hostPort, "[2001::1]:30");
url = stringToURL("http://example.com");
- url.hostPort = "2001:1";
+ url = url.mutate().setHostPort("2001:1").finalize();
Assert.equal(url.host, "0.0.7.209");
Assert.equal(url.port, 1);
Assert.equal(url.hostPort, "0.0.7.209:1");
run_next_test();
});
add_test(function test_ipv6_fail()
{
@@ -193,28 +193,28 @@ add_test(function test_ipv6_fail()
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.hostPort = "2001::1"; }, "missing brackets");
- Assert.throws(() => { url.hostPort = "[2001::1]30"; }, "missing : after IP");
- Assert.throws(() => { url.hostPort = "[2001:1]"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "[2001:1]10"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "[2001:1]10:20"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "[2001:1]:10:20"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "[2001:1"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "2001]:1"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = "2001:1]"; }, "bad IPv6 address");
- Assert.throws(() => { url.hostPort = ""; }, "Empty hostPort should fail");
- Assert.throws(() => { url.hostPort = "[2001::1]:"; }, "missing port number");
- Assert.throws(() => { url.hostPort = "[2001::1]:bad"; }, "bad port number");
+ 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");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]10:20").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001:1]:10:20").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001:1").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("2001]:1").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("2001:1]").finalize(); }, "bad IPv6 address");
+ Assert.throws(() => { url = url.mutate().setHostPort("").finalize(); }, "Empty hostPort should fail");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001::1]:").finalize(); }, "missing port number");
+ Assert.throws(() => { url = url.mutate().setHostPort("[2001::1]:bad").finalize(); }, "bad port number");
run_next_test();
});
add_test(function test_clearedSpec()
{
var url = stringToURL("http://example.com/path");
Assert.throws(() => { url = url.mutate().setSpec("http: example").finalize(); }, "set bad spec");
Assert.throws(() => { url = url.mutate().setSpec("").finalize(); }, "set empty spec");
@@ -297,29 +297,27 @@ add_test(function test_hugeStringThrows(
{
let prefs = Cc["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService);
let maxLen = prefs.getIntPref("network.standard-url.max-length");
let url = stringToURL("http://test:test@example.com");
let hugeString = new Array(maxLen + 1).fill("a").join("");
let properties = ["scheme", "userPass", "username",
- "password", "hostPort", "host", "pathQueryRef", "ref",
+ "password", "host", "pathQueryRef", "ref",
"query", "filePath"];
for (let prop of properties) {
Assert.throws(() => url[prop] = hugeString,
/NS_ERROR_MALFORMED_URI/,
`Passing a huge string to "${prop}" should throw`);
}
- Assert.throws(() => { url = url.mutate().setSpec(hugeString).finalize(); },
- /NS_ERROR_MALFORMED_URI/,
- "Passing a huge string to setSpec should throw");
-
let setters = [
+ { method: "setSpec", qi: Ci.nsIURIMutator },
+ { method: "setHostPort", qi: Ci.nsIURIMutator },
{ method: "setFileName", qi: Ci.nsIURLMutator },
{ method: "setFileExtension", qi: Ci.nsIURLMutator },
{ method: "setFileBaseName", qi: Ci.nsIURLMutator },
];
for (let prop of setters) {
Assert.throws(() => url = url.mutate().QueryInterface(prop.qi)[prop.method](hugeString).finalize(),
/NS_ERROR_MALFORMED_URI/,