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

MozReview-Commit-ID: GgyIkZSG2y3

--HG--
extra : rebase_source : 5398a29d8cb910c909ed88e1a6cbd9fd63e6b745
This commit is contained in:
Valentin Gosu 2018-02-26 20:43:47 +01:00
parent 2a78b51a84
commit ed218f0a19
11 changed files with 45 additions and 84 deletions

View File

@ -383,7 +383,9 @@ var FeedHandler = {
// https://foo.com/index.rdf -> feed:https://foo.com/index.rdf
let feedURI = NetUtil.newURI(aSpec);
if (feedURI.schemeIs("http")) {
feedURI.scheme = "feed";
feedURI = feedURI.mutate()
.setScheme("feed")
.finalize();
aSpec = feedURI.spec;
} else {
aSpec = "feed:" + aSpec;

View File

@ -430,7 +430,7 @@ Link::GetURI() const
void
Link::SetProtocol(const nsAString &aProtocol)
{
nsCOMPtr<nsIURI> uri(GetURIToMutate());
nsCOMPtr<nsIURI> uri(GetURI());
if (!uri) {
// Ignore failures to be compatible with NS4.
return;
@ -441,7 +441,12 @@ Link::SetProtocol(const nsAString &aProtocol)
aProtocol.EndReading(end);
nsAString::const_iterator iter(start);
(void)FindCharInReadable(':', iter, end);
(void)uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
nsresult rv = NS_MutateURI(uri)
.SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
.Finalize(uri);
if (NS_FAILED(rv)) {
return;
}
SetHrefAttribute(uri);
}

View File

@ -726,7 +726,7 @@ Location::SetProtocol(const nsAString& aProtocol,
}
nsCOMPtr<nsIURI> uri;
aRv = GetWritableURI(getter_AddRefs(uri));
aRv = GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(aRv.Failed()) || !uri) {
return;
}
@ -737,7 +737,9 @@ Location::SetProtocol(const nsAString& aProtocol,
nsAString::const_iterator iter(start);
Unused << FindCharInReadable(':', iter, end);
nsresult rv = uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
nsresult rv = NS_MutateURI(uri)
.SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
.Finalize(uri);
if (NS_WARN_IF(NS_FAILED(rv))) {
// Oh, I wish nsStandardURL returned NS_ERROR_MALFORMED_URI for _all_ the
// malformed cases, not just some of them!

View File

@ -250,12 +250,9 @@ URLMainThread::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv)
// implementation. In order to do this properly, we have to serialize the
// existing URL and reparse it in a new object.
nsCOMPtr<nsIURI> clone;
nsresult rv = mURI->Clone(getter_AddRefs(clone));
if (NS_WARN_IF(NS_FAILED(rv)) || !clone) {
return;
}
rv = clone->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
nsresult rv = NS_MutateURI(mURI)
.SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
.Finalize(clone);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}

View File

@ -821,7 +821,9 @@ URLWorker::SetProtocol(const nsAString& aProtocol, ErrorResult& aRv)
// the scheme is http or https.
if (mStdURL &&
(scheme.EqualsLiteral("http") || scheme.EqualsLiteral("https"))) {
mStdURL->SetScheme(scheme);
Unused << NS_MutateURI(mStdURL)
.SetScheme(scheme)
.Finalize(mStdURL);
return;
}

View File

@ -15,6 +15,7 @@
#include "mozilla/net/WebSocketEventService.h"
#include "nsIURI.h"
#include "nsIURIMutator.h"
#include "nsIChannel.h"
#include "nsICryptoHash.h"
#include "nsIRunnable.h"
@ -3203,11 +3204,15 @@ WebSocketChannel::AsyncOnChannelRedirect(
newChannel->SetNotificationCallbacks(this);
mEncrypted = newuriIsHttps;
newuri->Clone(getter_AddRefs(mURI));
if (mEncrypted)
rv = mURI->SetScheme(NS_LITERAL_CSTRING("wss"));
else
rv = mURI->SetScheme(NS_LITERAL_CSTRING("ws"));
rv = NS_MutateURI(newuri)
.SetScheme(mEncrypted ? NS_LITERAL_CSTRING("wss")
: NS_LITERAL_CSTRING("ws"))
.Finalize(mURI);
if (NS_FAILED(rv)) {
LOG(("WebSocketChannel: Could not set the proper scheme\n"));
return rv;
}
mHttpChannel = newHttpChannel;
mChannel = newUpgradeChannel;
@ -3461,11 +3466,10 @@ WebSocketChannel::AsyncOpen(nsIURI *aURI,
nsCOMPtr<nsIURI> localURI;
nsCOMPtr<nsIChannel> localChannel;
mURI->Clone(getter_AddRefs(localURI));
if (mEncrypted)
rv = localURI->SetScheme(NS_LITERAL_CSTRING("https"));
else
rv = localURI->SetScheme(NS_LITERAL_CSTRING("http"));
rv = NS_MutateURI(mURI)
.SetScheme(mEncrypted ? NS_LITERAL_CSTRING("https")
: NS_LITERAL_CSTRING("http"))
.Finalize(localURI);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIIOService> ioService;

View File

@ -519,30 +519,6 @@ function do_test_mutate_ref(aTest, aSuffix) {
}
}
// 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"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;
try {
URI[aProperty] = "anothervalue";
} catch(e) {
threw = true;
}
do_info("testing that setting '" + aProperty +
"' on immutable URI '" + aTest.spec + "' will throw");
Assert.ok(threw);
});
}
// TEST MAIN FUNCTION
// ------------------
function run_test()
@ -579,7 +555,7 @@ function run_test()
// For URIs that we couldn't mutate above due to them being immutable:
// Now we check that they're actually immutable.
if (aTest.immutable) {
do_test_immutable(aTest);
Assert.ok(aTest.immutable);
}
}
});

View File

@ -620,30 +620,6 @@ function do_test_mutate_ref(aTest, aSuffix) {
}
}
// 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"];
propertiesToCheck.forEach(function(aProperty) {
var threw = false;
try {
URI[aProperty] = "anothervalue";
} catch(e) {
threw = true;
}
do_info("testing that setting '" + aProperty +
"' on immutable URI '" + aTest.spec + "' will throw");
Assert.ok(threw);
});
}
// TEST MAIN FUNCTION
// ------------------
function run_test()
@ -680,7 +656,7 @@ function run_test()
// For URIs that we couldn't mutate above due to them being immutable:
// Now we check that they're actually immutable.
if (aTest.immutable) {
do_test_immutable(aTest);
Assert.ok(aTest.immutable);
}
}
});

View File

@ -306,13 +306,6 @@ 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"];
for (let prop of properties) {
Assert.throws(() => url[prop] = hugeString,
/NS_ERROR_MALFORMED_URI/,
`Passing a huge string to "${prop}" should throw`);
}
let setters = [
{ method: "setSpec", qi: Ci.nsIURIMutator },
{ method: "setUsername", qi: Ci.nsIURIMutator },
@ -324,6 +317,7 @@ add_test(function test_hugeStringThrows()
{ method: "setPathQueryRef", qi: Ci.nsIURIMutator },
{ method: "setQuery", qi: Ci.nsIURIMutator },
{ method: "setRef", qi: Ci.nsIURIMutator },
{ method: "setScheme", qi: Ci.nsIURIMutator },
{ method: "setFileName", qi: Ci.nsIURLMutator },
{ method: "setFileExtension", qi: Ci.nsIURLMutator },
{ method: "setFileBaseName", qi: Ci.nsIURLMutator },

View File

@ -127,15 +127,16 @@ function getURI() {
return null;
}
let mutator = uri.mutate();
if (uri.scheme == "http") {
uri.scheme = "https";
mutator.setScheme("https");
}
if (uri.port == -1) {
uri = uri.mutate().setPort(443).finalize();
mutator.setPort(443);
}
return uri;
return mutator.finalize();
}
function resetDialog() {

View File

@ -968,12 +968,14 @@ var ActivityStreamProvider = {
}
let iconData;
try {
const linkUri = Services.io.newURI(link.url);
let linkUri = Services.io.newURI(link.url);
iconData = await this._getIconData(linkUri);
// Switch the scheme to try again with the other
if (!iconData) {
linkUri.scheme = linkUri.scheme === "https" ? "http" : "https";
linkUri = linkUri.mutate()
.setScheme(linkUri.scheme === "https" ? "http" : "https")
.finalize();
iconData = await this._getIconData(linkUri);
}
} catch (e) {