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 // https://foo.com/index.rdf -> feed:https://foo.com/index.rdf
let feedURI = NetUtil.newURI(aSpec); let feedURI = NetUtil.newURI(aSpec);
if (feedURI.schemeIs("http")) { if (feedURI.schemeIs("http")) {
feedURI.scheme = "feed"; feedURI = feedURI.mutate()
.setScheme("feed")
.finalize();
aSpec = feedURI.spec; aSpec = feedURI.spec;
} else { } else {
aSpec = "feed:" + aSpec; aSpec = "feed:" + aSpec;

View File

@ -430,7 +430,7 @@ Link::GetURI() const
void void
Link::SetProtocol(const nsAString &aProtocol) Link::SetProtocol(const nsAString &aProtocol)
{ {
nsCOMPtr<nsIURI> uri(GetURIToMutate()); nsCOMPtr<nsIURI> uri(GetURI());
if (!uri) { if (!uri) {
// Ignore failures to be compatible with NS4. // Ignore failures to be compatible with NS4.
return; return;
@ -441,7 +441,12 @@ Link::SetProtocol(const nsAString &aProtocol)
aProtocol.EndReading(end); aProtocol.EndReading(end);
nsAString::const_iterator iter(start); nsAString::const_iterator iter(start);
(void)FindCharInReadable(':', iter, end); (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); SetHrefAttribute(uri);
} }

View File

@ -726,7 +726,7 @@ Location::SetProtocol(const nsAString& aProtocol,
} }
nsCOMPtr<nsIURI> uri; nsCOMPtr<nsIURI> uri;
aRv = GetWritableURI(getter_AddRefs(uri)); aRv = GetURI(getter_AddRefs(uri));
if (NS_WARN_IF(aRv.Failed()) || !uri) { if (NS_WARN_IF(aRv.Failed()) || !uri) {
return; return;
} }
@ -737,7 +737,9 @@ Location::SetProtocol(const nsAString& aProtocol,
nsAString::const_iterator iter(start); nsAString::const_iterator iter(start);
Unused << FindCharInReadable(':', iter, end); 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))) { if (NS_WARN_IF(NS_FAILED(rv))) {
// Oh, I wish nsStandardURL returned NS_ERROR_MALFORMED_URI for _all_ the // Oh, I wish nsStandardURL returned NS_ERROR_MALFORMED_URI for _all_ the
// malformed cases, not just some of them! // 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 // implementation. In order to do this properly, we have to serialize the
// existing URL and reparse it in a new object. // existing URL and reparse it in a new object.
nsCOMPtr<nsIURI> clone; nsCOMPtr<nsIURI> clone;
nsresult rv = mURI->Clone(getter_AddRefs(clone)); nsresult rv = NS_MutateURI(mURI)
if (NS_WARN_IF(NS_FAILED(rv)) || !clone) { .SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)))
return; .Finalize(clone);
}
rv = clone->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
if (NS_WARN_IF(NS_FAILED(rv))) { if (NS_WARN_IF(NS_FAILED(rv))) {
return; return;
} }

View File

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

View File

@ -15,6 +15,7 @@
#include "mozilla/net/WebSocketEventService.h" #include "mozilla/net/WebSocketEventService.h"
#include "nsIURI.h" #include "nsIURI.h"
#include "nsIURIMutator.h"
#include "nsIChannel.h" #include "nsIChannel.h"
#include "nsICryptoHash.h" #include "nsICryptoHash.h"
#include "nsIRunnable.h" #include "nsIRunnable.h"
@ -3203,11 +3204,15 @@ WebSocketChannel::AsyncOnChannelRedirect(
newChannel->SetNotificationCallbacks(this); newChannel->SetNotificationCallbacks(this);
mEncrypted = newuriIsHttps; mEncrypted = newuriIsHttps;
newuri->Clone(getter_AddRefs(mURI)); rv = NS_MutateURI(newuri)
if (mEncrypted) .SetScheme(mEncrypted ? NS_LITERAL_CSTRING("wss")
rv = mURI->SetScheme(NS_LITERAL_CSTRING("wss")); : NS_LITERAL_CSTRING("ws"))
else .Finalize(mURI);
rv = mURI->SetScheme(NS_LITERAL_CSTRING("ws"));
if (NS_FAILED(rv)) {
LOG(("WebSocketChannel: Could not set the proper scheme\n"));
return rv;
}
mHttpChannel = newHttpChannel; mHttpChannel = newHttpChannel;
mChannel = newUpgradeChannel; mChannel = newUpgradeChannel;
@ -3461,11 +3466,10 @@ WebSocketChannel::AsyncOpen(nsIURI *aURI,
nsCOMPtr<nsIURI> localURI; nsCOMPtr<nsIURI> localURI;
nsCOMPtr<nsIChannel> localChannel; nsCOMPtr<nsIChannel> localChannel;
mURI->Clone(getter_AddRefs(localURI)); rv = NS_MutateURI(mURI)
if (mEncrypted) .SetScheme(mEncrypted ? NS_LITERAL_CSTRING("https")
rv = localURI->SetScheme(NS_LITERAL_CSTRING("https")); : NS_LITERAL_CSTRING("http"))
else .Finalize(localURI);
rv = localURI->SetScheme(NS_LITERAL_CSTRING("http"));
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIIOService> ioService; 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 // TEST MAIN FUNCTION
// ------------------ // ------------------
function run_test() function run_test()
@ -579,7 +555,7 @@ function run_test()
// For URIs that we couldn't mutate above due to them being immutable: // For URIs that we couldn't mutate above due to them being immutable:
// Now we check that they're actually immutable. // Now we check that they're actually immutable.
if (aTest.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 // TEST MAIN FUNCTION
// ------------------ // ------------------
function run_test() function run_test()
@ -680,7 +656,7 @@ function run_test()
// For URIs that we couldn't mutate above due to them being immutable: // For URIs that we couldn't mutate above due to them being immutable:
// Now we check that they're actually immutable. // Now we check that they're actually immutable.
if (aTest.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 url = stringToURL("http://test:test@example.com");
let hugeString = new Array(maxLen + 1).fill("a").join(""); 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 = [ let setters = [
{ method: "setSpec", qi: Ci.nsIURIMutator }, { method: "setSpec", qi: Ci.nsIURIMutator },
{ method: "setUsername", qi: Ci.nsIURIMutator }, { method: "setUsername", qi: Ci.nsIURIMutator },
@ -324,6 +317,7 @@ add_test(function test_hugeStringThrows()
{ method: "setPathQueryRef", qi: Ci.nsIURIMutator }, { method: "setPathQueryRef", qi: Ci.nsIURIMutator },
{ method: "setQuery", qi: Ci.nsIURIMutator }, { method: "setQuery", qi: Ci.nsIURIMutator },
{ method: "setRef", qi: Ci.nsIURIMutator }, { method: "setRef", qi: Ci.nsIURIMutator },
{ method: "setScheme", qi: Ci.nsIURIMutator },
{ method: "setFileName", qi: Ci.nsIURLMutator }, { method: "setFileName", qi: Ci.nsIURLMutator },
{ method: "setFileExtension", qi: Ci.nsIURLMutator }, { method: "setFileExtension", qi: Ci.nsIURLMutator },
{ method: "setFileBaseName", qi: Ci.nsIURLMutator }, { method: "setFileBaseName", qi: Ci.nsIURLMutator },

View File

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

View File

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