Skip to content

Commit

Permalink
Bug 1433958 - Change code that sets nsIURI.scheme to use nsIURIMutato…
Browse files Browse the repository at this point in the history
…r r=mayhemer

MozReview-Commit-ID: GgyIkZSG2y3
  • Loading branch information
valenting committed Feb 26, 2018
1 parent f2e7614 commit bfd0b72
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 84 deletions.
4 changes: 3 additions & 1 deletion browser/base/content/browser-feeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions dom/base/Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions dom/base/Location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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!
Expand Down
9 changes: 3 additions & 6 deletions dom/url/URLMainThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion dom/url/URLWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
24 changes: 14 additions & 10 deletions netwerk/protocol/websocket/WebSocketChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "mozilla/net/WebSocketEventService.h"

#include "nsIURI.h"
#include "nsIURIMutator.h"
#include "nsIChannel.h"
#include "nsICryptoHash.h"
#include "nsIRunnable.h"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 1 addition & 25 deletions netwerk/test/unit/test_URIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
});
Expand Down
26 changes: 1 addition & 25 deletions netwerk/test/unit/test_URIs2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
}
}
});
Expand Down
8 changes: 1 addition & 7 deletions netwerk/test/unit/test_standardurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down
7 changes: 4 additions & 3 deletions security/manager/pki/resources/content/exceptionDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 4 additions & 2 deletions toolkit/modules/NewTabUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit bfd0b72

Please sign in to comment.