Bug 1433958 - Change code that sets nsIURI.password to use nsIURIMutator draft
authorValentin Gosu <valentin.gosu@gmail.com>
Mon, 26 Feb 2018 20:43:46 +0100
changeset 759892 66d7ada74e088b7bdf2b43e27a64666988c32427
parent 759891 53f03a92407f7fd1da2a96ccc0f96e6be25053fc
child 759893 0712280f03b96d36d6e0dcfb1d86933347fd801f
push id100504
push uservalentin.gosu@gmail.com
push dateMon, 26 Feb 2018 19:44:44 +0000
bugs1433958
milestone60.0a1
Bug 1433958 - Change code that sets nsIURI.password to use nsIURIMutator MozReview-Commit-ID: I8igxClMM5L
dom/base/Link.cpp
dom/url/URLMainThread.cpp
netwerk/streamconv/converters/nsFTPDirListingConv.cpp
netwerk/streamconv/converters/nsIndexedToHTML.cpp
netwerk/test/unit/test_URIs.js
netwerk/test/unit/test_URIs2.js
netwerk/test/unit/test_standardurl.js
--- a/dom/base/Link.cpp
+++ b/dom/base/Link.cpp
@@ -444,24 +444,28 @@ Link::SetProtocol(const nsAString &aProt
   (void)uri->SetScheme(NS_ConvertUTF16toUTF8(Substring(start, iter)));
 
   SetHrefAttribute(uri);
 }
 
 void
 Link::SetPassword(const nsAString &aPassword)
 {
-  nsCOMPtr<nsIURI> uri(GetURIToMutate());
+  nsCOMPtr<nsIURI> uri(GetURI());
   if (!uri) {
     // Ignore failures to be compatible with NS4.
     return;
   }
 
-  uri->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
-  SetHrefAttribute(uri);
+  nsresult rv = NS_MutateURI(uri)
+                  .SetPassword(NS_ConvertUTF16toUTF8(aPassword))
+                  .Finalize(uri);
+  if (NS_SUCCEEDED(rv)) {
+    SetHrefAttribute(uri);
+  }
 }
 
 void
 Link::SetUsername(const nsAString &aUsername)
 {
   nsCOMPtr<nsIURI> uri(GetURI());
   if (!uri) {
     // Ignore failures to be compatible with NS4.
--- a/dom/url/URLMainThread.cpp
+++ b/dom/url/URLMainThread.cpp
@@ -301,17 +301,19 @@ void
 URLMainThread::GetPassword(nsAString& aPassword, ErrorResult& aRv) const
 {
   URL_GETTER(aPassword, GetPassword);
 }
 
 void
 URLMainThread::SetPassword(const nsAString& aPassword, ErrorResult& aRv)
 {
-  mURI->SetPassword(NS_ConvertUTF16toUTF8(aPassword));
+  Unused << NS_MutateURI(mURI)
+              .SetPassword(NS_ConvertUTF16toUTF8(aPassword))
+              .Finalize(mURI);
 }
 
 void
 URLMainThread::GetHost(nsAString& aHost, ErrorResult& aRv) const
 {
   URL_GETTER(aHost, GetHostPort);
 }
 
--- a/netwerk/streamconv/converters/nsFTPDirListingConv.cpp
+++ b/netwerk/streamconv/converters/nsFTPDirListingConv.cpp
@@ -9,16 +9,17 @@
 #include "mozilla/Logging.h"
 #include "nsCOMPtr.h"
 #include "nsEscape.h"
 #include "nsStringStream.h"
 #include "nsIStreamListener.h"
 #include "nsCRT.h"
 #include "nsIChannel.h"
 #include "nsIURI.h"
+#include "nsIURIMutator.h"
 
 #include "ParseFTPList.h"
 #include <algorithm>
 
 #include "mozilla/UniquePtrExtensions.h"
 #include "mozilla/Unused.h"
 
 //
@@ -186,23 +187,24 @@ nsFTPDirListingConv::GetHeaders(nsACStri
     // build up 300 line
     headers.AppendLiteral("300: ");
 
     // Bug 111117 - don't print the password
     nsAutoCString pw;
     nsAutoCString spec;
     uri->GetPassword(pw);
     if (!pw.IsEmpty()) {
-         rv = uri->SetPassword(EmptyCString());
+         nsCOMPtr<nsIURI> noPassURI;
+         rv = NS_MutateURI(uri)
+                .SetPassword(EmptyCString())
+                .Finalize(noPassURI);
          if (NS_FAILED(rv)) return rv;
-         rv = uri->GetAsciiSpec(spec);
+         rv = noPassURI->GetAsciiSpec(spec);
          if (NS_FAILED(rv)) return rv;
          headers.Append(spec);
-         rv = uri->SetPassword(pw);
-         if (NS_FAILED(rv)) return rv;
     } else {
         rv = uri->GetAsciiSpec(spec);
         if (NS_FAILED(rv)) return rv;
 
         headers.Append(spec);
     }
     headers.Append(char(nsCRT::LF));
     // END 300:
--- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp
+++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp
@@ -181,19 +181,19 @@ nsIndexedToHTML::DoOnStartRequest(nsIReq
         // This is done by the 300: line generation in ftp, but we don't use
         // that - see above
 
         nsAutoCString pw;
         rv = uri->GetPassword(pw);
         if (NS_FAILED(rv)) return rv;
         if (!pw.IsEmpty()) {
              nsCOMPtr<nsIURI> newUri;
-             rv = uri->Clone(getter_AddRefs(newUri));
-             if (NS_FAILED(rv)) return rv;
-             rv = newUri->SetPassword(EmptyCString());
+             rv = NS_MutateURI(uri)
+                    .SetPassword(EmptyCString())
+                    .Finalize(newUri);
              if (NS_FAILED(rv)) return rv;
              rv = newUri->GetAsciiSpec(titleUri);
              if (NS_FAILED(rv)) return rv;
         }
 
         nsAutoCString path;
         rv = uri->GetPathQueryRef(path);
         if (NS_FAILED(rv)) return rv;
--- a/netwerk/test/unit/test_URIs.js
+++ b/netwerk/test/unit/test_URIs.js
@@ -519,17 +519,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", "password",
+  var propertiesToCheck = ["spec", "scheme",
                            "host", "port", "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
@@ -620,17 +620,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", "password",
+  var propertiesToCheck = ["scheme",
                            "host", "port", "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
@@ -297,27 +297,28 @@ 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",
-                    "password", "host", "ref",
+                    "host", "ref",
                     "query"];
   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 },
+    { method: "setPassword", qi: Ci.nsIURIMutator },
     { method: "setFilePath", qi: Ci.nsIURIMutator },
     { method: "setHostPort", qi: Ci.nsIURIMutator },
     { method: "setUserPass", qi: Ci.nsIURIMutator },
     { method: "setPathQueryRef", qi: Ci.nsIURIMutator },
     { method: "setFileName", qi: Ci.nsIURLMutator },
     { method: "setFileExtension", qi: Ci.nsIURLMutator },
     { method: "setFileBaseName", qi: Ci.nsIURLMutator },
   ];
@@ -498,29 +499,29 @@ add_test(function test_normalize_ipv6() 
   Assert.equal(url.spec, "http://[::c009:505]/");
 
   run_next_test();
 });
 
 add_test(function test_emptyPassword() {
   var url = stringToURL("http://a:@example.com");
   Assert.equal(url.spec, "http://a@example.com/");
-  url.password = "pp";
+  url = url.mutate().setPassword("pp").finalize();
   Assert.equal(url.spec, "http://a:pp@example.com/");
-  url.password = "";
+  url = url.mutate().setPassword("").finalize();
   Assert.equal(url.spec, "http://a@example.com/");
   url = url.mutate().setUserPass("xxx:").finalize();
   Assert.equal(url.spec, "http://xxx@example.com/");
-  url.password = "zzzz";
+  url = url.mutate().setPassword("zzzz").finalize();
   Assert.equal(url.spec, "http://xxx:zzzz@example.com/");
   url = url.mutate().setUserPass("xxxxx:yyyyyy").finalize();
   Assert.equal(url.spec, "http://xxxxx:yyyyyy@example.com/");
   url = url.mutate().setUserPass("z:").finalize();
   Assert.equal(url.spec, "http://z@example.com/");
-  url.password = "ppppppppppp";
+  url = url.mutate().setPassword("ppppppppppp").finalize();
   Assert.equal(url.spec, "http://z:ppppppppppp@example.com/");
   run_next_test();
 });
 
 registerCleanupFunction(function () {
   gPrefs.clearUserPref("network.standard-url.punycode-host");
 });