From 7bd0336d01e0626d94e55bc4a3dac74baf2f5be3 Mon Sep 17 00:00:00 2001 From: Valentin Gosu Date: Mon, 26 Feb 2018 20:43:45 +0100 Subject: [PATCH] Bug 1433958 - Change code that sets nsIURI.pathQueryRef to use nsIURIMutator r=mayhemer MozReview-Commit-ID: HVyZ3E1XuLN --HG-- extra : rebase_source : fd122b3122e0f3eb371d429bd86ad35d09dcfbf6 --- caps/DomainPolicy.cpp | 10 +++--- chrome/nsChromeProtocolHandler.cpp | 16 ++++----- chrome/nsChromeRegistry.cpp | 9 +++-- chrome/nsChromeRegistry.h | 7 ++-- dom/base/FormData.cpp | 3 +- dom/base/FormData.h | 2 +- dom/base/nsDocument.cpp | 15 +++++--- dom/base/nsGlobalWindowOuter.cpp | 8 +++-- dom/html/HTMLFormElement.cpp | 3 +- dom/html/HTMLFormSubmission.cpp | 35 +++++++++++++------ dom/html/HTMLFormSubmission.h | 8 +++-- dom/webbrowserpersist/nsWebBrowserPersist.cpp | 15 ++++---- dom/webbrowserpersist/nsWebBrowserPersist.h | 2 +- netwerk/protocol/ftp/nsFTPChannel.h | 5 +++ .../protocol/ftp/nsFtpConnectionThread.cpp | 13 +++++-- netwerk/protocol/http/HttpBaseChannel.cpp | 15 +++++--- .../streamconv/converters/nsIndexedToHTML.cpp | 6 +++- netwerk/test/unit/test_URIs.js | 8 ++--- netwerk/test/unit/test_URIs2.js | 8 ++--- netwerk/test/unit/test_standardurl.js | 7 ++-- .../test/xpcshell/test_mapURIToAddonID.js | 5 +-- 21 files changed, 128 insertions(+), 72 deletions(-) diff --git a/caps/DomainPolicy.cpp b/caps/DomainPolicy.cpp index f89fa8cd28fb..f40a5432d0c0 100644 --- a/caps/DomainPolicy.cpp +++ b/caps/DomainPolicy.cpp @@ -9,6 +9,7 @@ #include "mozilla/ipc/URIUtils.h" #include "mozilla/Unused.h" #include "nsIMessageManager.h" +#include "nsIURIMutator.h" #include "nsScriptSecurityManager.h" namespace mozilla { @@ -151,11 +152,10 @@ static already_AddRefed GetCanonicalClone(nsIURI* aURI) { nsCOMPtr clone; - nsresult rv = aURI->Clone(getter_AddRefs(clone)); - NS_ENSURE_SUCCESS(rv, nullptr); - rv = clone->SetUserPass(EmptyCString()); - NS_ENSURE_SUCCESS(rv, nullptr); - rv = clone->SetPathQueryRef(EmptyCString()); + nsresult rv = NS_MutateURI(aURI) + .SetUserPass(EmptyCString()) + .SetPathQueryRef(EmptyCString()) + .Finalize(clone); NS_ENSURE_SUCCESS(rv, nullptr); return clone.forget(); } diff --git a/chrome/nsChromeProtocolHandler.cpp b/chrome/nsChromeProtocolHandler.cpp index f998b1966a77..e42744ae4f75 100644 --- a/chrome/nsChromeProtocolHandler.cpp +++ b/chrome/nsChromeProtocolHandler.cpp @@ -77,7 +77,7 @@ nsChromeProtocolHandler::NewURI(const nsACString &aSpec, // Chrome: URLs (currently) have no additional structure beyond that provided // by standard URLs, so there is no "outer" given to CreateInstance nsresult rv; - nsCOMPtr surl; + nsCOMPtr surl; nsCOMPtr base(aBaseURI); rv = NS_MutateURI(new mozilla::net::nsStandardURL::Mutator()) .Apply(NS_MutatorMethod(&nsIStandardURLMutator::Init, @@ -118,17 +118,13 @@ nsChromeProtocolHandler::NewChannel2(nsIURI* aURI, #ifdef DEBUG // Check that the uri we got is already canonified nsresult debug_rv; - nsCOMPtr debugClone; - debug_rv = aURI->Clone(getter_AddRefs(debugClone)); + nsCOMPtr debugURL = aURI; + debug_rv = nsChromeRegistry::Canonify(debugURL); if (NS_SUCCEEDED(debug_rv)) { - nsCOMPtr debugURL (do_QueryInterface(debugClone)); - debug_rv = nsChromeRegistry::Canonify(debugURL); + bool same; + debug_rv = aURI->Equals(debugURL, &same); if (NS_SUCCEEDED(debug_rv)) { - bool same; - debug_rv = aURI->Equals(debugURL, &same); - if (NS_SUCCEEDED(debug_rv)) { - NS_ASSERTION(same, "Non-canonified chrome uri passed to nsChromeProtocolHandler::NewChannel!"); - } + NS_ASSERTION(same, "Non-canonified chrome uri passed to nsChromeProtocolHandler::NewChannel!"); } } #endif diff --git a/chrome/nsChromeRegistry.cpp b/chrome/nsChromeRegistry.cpp index 8e81c531c4cf..f193636bfd61 100644 --- a/chrome/nsChromeRegistry.cpp +++ b/chrome/nsChromeRegistry.cpp @@ -31,6 +31,7 @@ #include "mozilla/StyleSheet.h" #include "mozilla/StyleSheetInlines.h" #include "mozilla/dom/Location.h" +#include "nsIURIMutator.h" #include "unicode/uloc.h" @@ -152,7 +153,7 @@ nsChromeRegistry::Init() } nsresult -nsChromeRegistry::GetProviderAndPath(nsIURL* aChromeURL, +nsChromeRegistry::GetProviderAndPath(nsIURI* aChromeURL, nsACString& aProvider, nsACString& aPath) { nsresult rv; @@ -199,7 +200,7 @@ nsChromeRegistry::GetProviderAndPath(nsIURL* aChromeURL, nsresult -nsChromeRegistry::Canonify(nsIURL* aChromeURL) +nsChromeRegistry::Canonify(nsCOMPtr& aChromeURL) { NS_NAMED_LITERAL_CSTRING(kSlash, "/"); @@ -228,7 +229,9 @@ nsChromeRegistry::Canonify(nsIURL* aChromeURL) else { return NS_ERROR_INVALID_ARG; } - aChromeURL->SetPathQueryRef(path); + return NS_MutateURI(aChromeURL) + .SetPathQueryRef(path) + .Finalize(aChromeURL); } else { // prevent directory traversals ("..") diff --git a/chrome/nsChromeRegistry.h b/chrome/nsChromeRegistry.h index 3fd33aad306a..795a4988d3f5 100644 --- a/chrome/nsChromeRegistry.h +++ b/chrome/nsChromeRegistry.h @@ -70,7 +70,10 @@ public: static nsChromeRegistry* gChromeRegistry; - static nsresult Canonify(nsIURL* aChromeURL); + // This method can change its parameter, so due to thread safety issues + // it should only be called for nsCOMPtr that is on the stack, + // unless you know what you are doing. + static nsresult Canonify(nsCOMPtr& aChromeURL); protected: virtual ~nsChromeRegistry(); @@ -91,7 +94,7 @@ protected: uint32_t* aFlags) = 0; static nsresult RefreshWindow(nsPIDOMWindowOuter* aWindow); - static nsresult GetProviderAndPath(nsIURL* aChromeURL, + static nsresult GetProviderAndPath(nsIURI* aChromeURL, nsACString& aProvider, nsACString& aPath); bool GetDirectionForLocale(const nsACString& aLocale); diff --git a/dom/base/FormData.cpp b/dom/base/FormData.cpp index 5a78314665de..2e85927fa94f 100644 --- a/dom/base/FormData.cpp +++ b/dom/base/FormData.cpp @@ -101,7 +101,8 @@ NS_INTERFACE_MAP_END nsresult FormData::GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) + int64_t* aPostDataStreamLength, + nsCOMPtr& aOutURI) { NS_NOTREACHED("Shouldn't call FormData::GetEncodedSubmission"); return NS_OK; diff --git a/dom/base/FormData.h b/dom/base/FormData.h index 3ca02ac20f23..44fbc0539a53 100644 --- a/dom/base/FormData.h +++ b/dom/base/FormData.h @@ -107,7 +107,7 @@ public: // HTMLFormSubmission virtual nsresult GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) override; + int64_t* aPostDataStreamLength, nsCOMPtr& aOutURI) override; virtual nsresult AddNameValuePair(const nsAString& aName, const nsAString& aValue) override diff --git a/dom/base/nsDocument.cpp b/dom/base/nsDocument.cpp index a20f614f6217..e848a8162202 100644 --- a/dom/base/nsDocument.cpp +++ b/dom/base/nsDocument.cpp @@ -273,6 +273,7 @@ #include "mozilla/MediaManager.h" #include "nsIURIClassifier.h" +#include "nsIURIMutator.h" #include "mozilla/DocumentStyleRootIterator.h" #include "mozilla/ServoRestyleManager.h" #include "mozilla/ClearOnShutdown.h" @@ -9121,8 +9122,8 @@ nsDocument::MaybePreLoadImage(nsIURI* uri, const nsAString &aCrossOriginAttr, void nsDocument::MaybePreconnect(nsIURI* aOrigURI, mozilla::CORSMode aCORSMode) { - nsCOMPtr uri; - if (NS_FAILED(aOrigURI->Clone(getter_AddRefs(uri)))) { + NS_MutateURI mutator(aOrigURI); + if (NS_FAILED(mutator.GetStatus())) { return; } @@ -9134,9 +9135,15 @@ nsDocument::MaybePreconnect(nsIURI* aOrigURI, mozilla::CORSMode aCORSMode) // normalize the path before putting it in the hash to accomplish that. if (aCORSMode == CORS_ANONYMOUS) { - uri->SetPathQueryRef(NS_LITERAL_CSTRING("/anonymous")); + mutator.SetPathQueryRef(NS_LITERAL_CSTRING("/anonymous")); } else { - uri->SetPathQueryRef(NS_LITERAL_CSTRING("/")); + mutator.SetPathQueryRef(NS_LITERAL_CSTRING("/")); + } + + nsCOMPtr uri; + nsresult rv = mutator.Finalize(uri); + if (NS_FAILED(rv)) { + return; } auto entry = mPreloadedPreconnects.LookupForAdd(uri); diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 0295ddafd738..126d20be60ff 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -146,6 +146,7 @@ #include "nsCSSProps.h" #include "nsIDOMFileList.h" #include "nsIURIFixup.h" +#include "nsIURIMutator.h" #ifndef DEBUG #include "nsIAppStartup.h" #include "nsToolkitCompsCID.h" @@ -5793,8 +5794,11 @@ nsGlobalWindowOuter::PostMessageMozOuter(JSContext* aCx, JS::Handle a return; } - if (NS_FAILED(originURI->SetUserPass(EmptyCString())) || - NS_FAILED(originURI->SetPathQueryRef(EmptyCString()))) { + nsresult rv = NS_MutateURI(originURI) + .SetUserPass(EmptyCString()) + .SetPathQueryRef(EmptyCString()) + .Finalize(originURI); + if (NS_FAILED(rv)) { return; } diff --git a/dom/html/HTMLFormElement.cpp b/dom/html/HTMLFormElement.cpp index 016b78a8c477..02b3553c8a14 100644 --- a/dom/html/HTMLFormElement.cpp +++ b/dom/html/HTMLFormElement.cpp @@ -777,7 +777,8 @@ HTMLFormElement::SubmitSubmission(HTMLFormSubmission* aFormSubmission) int64_t postDataStreamLength = -1; rv = aFormSubmission->GetEncodedSubmission(actionURI, getter_AddRefs(postDataStream), - &postDataStreamLength); + &postDataStreamLength, + actionURI); NS_ENSURE_SUBMIT_SUCCESS(rv); rv = linkHandler->OnLinkClickSync(this, actionURI, diff --git a/dom/html/HTMLFormSubmission.cpp b/dom/html/HTMLFormSubmission.cpp index 1ac39b5c621a..7ddd2905d7a9 100644 --- a/dom/html/HTMLFormSubmission.cpp +++ b/dom/html/HTMLFormSubmission.cpp @@ -19,6 +19,7 @@ #include "nsDirectoryServiceDefs.h" #include "nsStringStream.h" #include "nsIURI.h" +#include "nsIURIMutator.h" #include "nsIURL.h" #include "nsNetUtil.h" #include "nsLinebreakConverter.h" @@ -113,7 +114,7 @@ public: virtual nsresult GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) override; + int64_t* aPostDataStreamLength, nsCOMPtr& aOutURI) override; protected: @@ -268,9 +269,11 @@ HandleMailtoSubject(nsCString& aPath) nsresult FSURLEncoded::GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) + int64_t* aPostDataStreamLength, + nsCOMPtr& aOutURI) { nsresult rv = NS_OK; + aOutURI = aURI; *aPostDataStream = nullptr; *aPostDataStreamLength = -1; @@ -295,8 +298,9 @@ FSURLEncoded::GetEncodedSubmission(nsIURI* aURI, path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody; - rv = aURI->SetPathQueryRef(path); - + return NS_MutateURI(aURI) + .SetPathQueryRef(path) + .Finalize(aOutURI); } else { nsCOMPtr dataStream; @@ -331,7 +335,9 @@ FSURLEncoded::GetEncodedSubmission(nsIURI* aURI, nsCOMPtr url = do_QueryInterface(aURI); if (url) { - url->SetQuery(mQueryString); + rv = NS_MutateURI(aURI) + .SetQuery(mQueryString) + .Finalize(aOutURI); } else { nsAutoCString path; @@ -356,7 +362,9 @@ FSURLEncoded::GetEncodedSubmission(nsIURI* aURI, // Bug 42616: Add named anchor to end after query string path.Append(mQueryString + namedAnchor); - aURI->SetPathQueryRef(path); + rv = NS_MutateURI(aURI) + .SetPathQueryRef(path) + .Finalize(aOutURI); } } @@ -621,9 +629,11 @@ FSMultipartFormData::AddDataChunk(const nsACString& aName, nsresult FSMultipartFormData::GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) + int64_t* aPostDataStreamLength, + nsCOMPtr& aOutURI) { nsresult rv; + aOutURI = aURI; // Make header nsCOMPtr mimeStream @@ -686,7 +696,7 @@ public: virtual nsresult GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreaLength) override; + int64_t* aPostDataStreaLength, nsCOMPtr& aOutURI) override; private: nsString mBody; @@ -726,9 +736,11 @@ FSTextPlain::AddNameDirectoryPair(const nsAString& aName, nsresult FSTextPlain::GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) + int64_t* aPostDataStreamLength, + nsCOMPtr& aOutURI) { nsresult rv = NS_OK; + aOutURI = aURI; *aPostDataStream = nullptr; *aPostDataStreamLength = -1; @@ -754,8 +766,9 @@ FSTextPlain::GetEncodedSubmission(nsIURI* aURI, path += NS_LITERAL_CSTRING("&force-plain-text=Y&body=") + escapedBody; - rv = aURI->SetPathQueryRef(path); - + rv = NS_MutateURI(aURI) + .SetPathQueryRef(path) + .Finalize(aOutURI); } else { // Create data stream. // We do want to send the data through the charset encoder and we want to diff --git a/dom/html/HTMLFormSubmission.h b/dom/html/HTMLFormSubmission.h index 5c4c930657ab..b9f1fd2a1ccb 100644 --- a/dom/html/HTMLFormSubmission.h +++ b/dom/html/HTMLFormSubmission.h @@ -82,13 +82,15 @@ public: * Given a URI and the current submission, create the final URI and data * stream that will be submitted. Subclasses *must* implement this. * - * @param aURI the URI being submitted to [INOUT] + * @param aURI the URI being submitted to [IN] * @param aPostDataStream a data stream for POST data [OUT] * @param aPostDataStreamLength a data stream for POST data length [OUT] + * @param aOutURI the resulting URI. May be the same as aURI [OUT] */ virtual nsresult GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) = 0; + int64_t* aPostDataStreamLength, + nsCOMPtr& aOutURI) = 0; /** * Get the charset that will be used for submission. @@ -168,7 +170,7 @@ public: virtual nsresult GetEncodedSubmission(nsIURI* aURI, nsIInputStream** aPostDataStream, - int64_t* aPostDataStreamLength) override; + int64_t* aPostDataStreamLength, nsCOMPtr& aOutURI) override; void GetContentType(nsACString& aContentType) { diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.cpp b/dom/webbrowserpersist/nsWebBrowserPersist.cpp index 922155db2fc2..d37c22ac7e18 100644 --- a/dom/webbrowserpersist/nsWebBrowserPersist.cpp +++ b/dom/webbrowserpersist/nsWebBrowserPersist.cpp @@ -625,7 +625,7 @@ nsWebBrowserPersist::SerializeNextFile() if (NS_WARN_IF(NS_FAILED(rv))) { break; } - rv = AppendPathToURI(fileAsURI, data->mFilename); + rv = AppendPathToURI(fileAsURI, data->mFilename, fileAsURI); if (NS_WARN_IF(NS_FAILED(rv))) { break; } @@ -1305,7 +1305,7 @@ nsWebBrowserPersist::GetLocalFileFromURI(nsIURI *aURI, nsIFile **aLocalFile) } /* static */ nsresult -nsWebBrowserPersist::AppendPathToURI(nsIURI *aURI, const nsAString & aPath) +nsWebBrowserPersist::AppendPathToURI(nsIURI *aURI, const nsAString & aPath, nsCOMPtr& aOutURI) { NS_ENSURE_ARG_POINTER(aURI); @@ -1322,9 +1322,10 @@ nsWebBrowserPersist::AppendPathToURI(nsIURI *aURI, const nsAString & aPath) // Store the path back on the URI AppendUTF16toUTF8(aPath, newPath); - aURI->SetPathQueryRef(newPath); - return NS_OK; + return NS_MutateURI(aURI) + .SetPathQueryRef(newPath) + .Finalize(aOutURI); } nsresult nsWebBrowserPersist::SaveURIInternal( @@ -2576,7 +2577,7 @@ nsWebBrowserPersist::URIData::GetLocalURI(nsIURI *targetBaseURI, nsCString& aSpe } else { rv = mDataPath->Clone(getter_AddRefs(fileAsURI)); NS_ENSURE_SUCCESS(rv, rv); - rv = AppendPathToURI(fileAsURI, mFilename); + rv = AppendPathToURI(fileAsURI, mFilename, fileAsURI); NS_ENSURE_SUCCESS(rv, rv); } @@ -2699,7 +2700,7 @@ nsWebBrowserPersist::SaveSubframeContent( nsCOMPtr frameURI; rv = mCurrentDataPath->Clone(getter_AddRefs(frameURI)); NS_ENSURE_SUCCESS(rv, rv); - rv = AppendPathToURI(frameURI, filenameWithExt); + rv = AppendPathToURI(frameURI, filenameWithExt, frameURI); NS_ENSURE_SUCCESS(rv, rv); // Work out the path for the subframe data @@ -2710,7 +2711,7 @@ nsWebBrowserPersist::SaveSubframeContent( // Append _data newFrameDataPath.AppendLiteral("_data"); - rv = AppendPathToURI(frameDataURI, newFrameDataPath); + rv = AppendPathToURI(frameDataURI, newFrameDataPath, frameDataURI); NS_ENSURE_SUCCESS(rv, rv); // Make frame document & data path conformant and unique diff --git a/dom/webbrowserpersist/nsWebBrowserPersist.h b/dom/webbrowserpersist/nsWebBrowserPersist.h index eca26c63a596..17b570d783e7 100644 --- a/dom/webbrowserpersist/nsWebBrowserPersist.h +++ b/dom/webbrowserpersist/nsWebBrowserPersist.h @@ -91,7 +91,7 @@ private: void CleanupLocalFiles(); nsresult GetValidURIFromObject(nsISupports *aObject, nsIURI **aURI) const; static nsresult GetLocalFileFromURI(nsIURI *aURI, nsIFile **aLocalFile); - static nsresult AppendPathToURI(nsIURI *aURI, const nsAString & aPath); + static nsresult AppendPathToURI(nsIURI *aURI, const nsAString & aPath, nsCOMPtr& aOutURI); nsresult MakeAndStoreLocalFilenameInURIMap( nsIURI *aURI, bool aNeedsPersisting, URIData **aData); nsresult MakeOutputStream( diff --git a/netwerk/protocol/ftp/nsFTPChannel.h b/netwerk/protocol/ftp/nsFTPChannel.h index a9feab5da701..3a695e1f2328 100644 --- a/netwerk/protocol/ftp/nsFTPChannel.h +++ b/netwerk/protocol/ftp/nsFTPChannel.h @@ -47,6 +47,11 @@ public: SetURI(uri); } + void UpdateURI(nsIURI *aURI) { + MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread(), "Not thread-safe."); + mURI = aURI; + } + nsIProxyInfo *ProxyInfo() { return mProxyInfo; } diff --git a/netwerk/protocol/ftp/nsFtpConnectionThread.cpp b/netwerk/protocol/ftp/nsFtpConnectionThread.cpp index d028641909f0..653af01bf248 100644 --- a/netwerk/protocol/ftp/nsFtpConnectionThread.cpp +++ b/netwerk/protocol/ftp/nsFtpConnectionThread.cpp @@ -40,6 +40,7 @@ #include "nsIRunnable.h" #include "nsISocketTransportService.h" #include "nsIURI.h" +#include "nsIURIMutator.h" #include "nsILoadInfo.h" #include "NullPrincipal.h" #include "nsIAuthPrompt2.h" @@ -1645,11 +1646,19 @@ nsFtpState::Init(nsFtpChannel *channel) removeParamsFromPath(path); + nsCOMPtr outURI; // FTP parameters such as type=i are ignored if (url) { - url->SetFilePath(path); + rv = NS_MutateURI(url) + .SetFilePath(path) + .Finalize(outURI); } else { - mChannel->URI()->SetPathQueryRef(path); + rv = NS_MutateURI(mChannel->URI()) + .SetPathQueryRef(path) + .Finalize(outURI); + } + if (NS_SUCCEEDED(rv)) { + mChannel->UpdateURI(outURI); } // Skip leading slash diff --git a/netwerk/protocol/http/HttpBaseChannel.cpp b/netwerk/protocol/http/HttpBaseChannel.cpp index ee2772f0f360..f3ba32dd357b 100644 --- a/netwerk/protocol/http/HttpBaseChannel.cpp +++ b/netwerk/protocol/http/HttpBaseChannel.cpp @@ -1793,7 +1793,9 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer, // strip away any userpass; we don't want to be giving out passwords ;-) // This is required by Referrer Policy stripping algorithm. - rv = clone->SetUserPass(EmptyCString()); + rv = NS_MutateURI(clone) + .SetUserPass(EmptyCString()) + .Finalize(clone); if (NS_FAILED(rv)) return rv; // 0: full URI @@ -1871,9 +1873,10 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer, rv = url->GetFilePath(path); if (NS_FAILED(rv)) return rv; spec.Append(path); - rv = url->SetQuery(EmptyCString()); - if (NS_FAILED(rv)) return rv; - rv = url->SetRef(EmptyCString()); + rv = NS_MutateURI(url) + .SetQuery(EmptyCString()) + .SetRef(EmptyCString()) + .Finalize(clone); if (NS_FAILED(rv)) return rv; break; } @@ -1885,7 +1888,9 @@ HttpBaseChannel::SetReferrerWithPolicy(nsIURI *referrer, case 2: // scheme+host+port+/ spec.AppendLiteral("/"); // This nukes any query/ref present as well in the case of nsStandardURL - rv = clone->SetPathQueryRef(EmptyCString()); + rv = NS_MutateURI(clone) + .SetPathQueryRef(EmptyCString()) + .Finalize(clone); if (NS_FAILED(rv)) return rv; break; } diff --git a/netwerk/streamconv/converters/nsIndexedToHTML.cpp b/netwerk/streamconv/converters/nsIndexedToHTML.cpp index 91372de3a617..def11afe483a 100644 --- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp +++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp @@ -13,6 +13,7 @@ #include "nsStringStream.h" #include "nsIFile.h" #include "nsIFileURL.h" +#include "nsIURIMutator.h" #include "nsEscape.h" #include "nsIDirIndex.h" #include "nsURLHelper.h" @@ -25,6 +26,7 @@ #include "nsString.h" #include #include "nsIChannel.h" +#include "mozilla/Unused.h" using mozilla::intl::LocaleService; @@ -249,7 +251,9 @@ nsIndexedToHTML::DoOnStartRequest(nsIRequest* request, nsISupports *aContext, if (baseUri.Last() != '/') { baseUri.Append('/'); path.Append('/'); - uri->SetPathQueryRef(path); + mozilla::Unused << NS_MutateURI(uri) + .SetPathQueryRef(path) + .Finalize(uri); } if (!path.EqualsLiteral("/")) { rv = uri->Resolve(NS_LITERAL_CSTRING(".."), parentStr); diff --git a/netwerk/test/unit/test_URIs.js b/netwerk/test/unit/test_URIs.js index 948053414387..04c8fdb0e4c7 100644 --- a/netwerk/test/unit/test_URIs.js +++ b/netwerk/test/unit/test_URIs.js @@ -502,16 +502,16 @@ function do_test_mutate_ref(aTest, aSuffix) { var pathWithSuffix = aTest.pathQueryRef + aSuffix; do_info("testing that setting path to " + pathWithSuffix + " and then clearing ref does what we expect"); - testURI.pathQueryRef = pathWithSuffix; + testURI = testURI.mutate().setPathQueryRef(pathWithSuffix).finalize(); testURI.ref = ""; do_check_uri_eq(testURI, refURIWithoutSuffix); do_check_uri_eqExceptRef(testURI, refURIWithSuffix); // Also: make sure that clearing .pathQueryRef also clears .ref - testURI.pathQueryRef = pathWithSuffix; + testURI = testURI.mutate().setPathQueryRef(pathWithSuffix).finalize(); do_info("testing that clearing path from " + pathWithSuffix + " also clears .ref"); - testURI.pathQueryRef = ""; + testURI = testURI.mutate().setPathQueryRef("").finalize(); Assert.equal(testURI.ref, ""); } } @@ -525,7 +525,7 @@ function do_test_immutable(aTest) { var URI = NetUtil.newURI(aTest.spec); // All the non-readonly attributes on nsIURI.idl: var propertiesToCheck = ["spec", "scheme", "userPass", "username", "password", - "host", "port", "pathQueryRef", "query", "ref"]; + "host", "port", "query", "ref"]; propertiesToCheck.forEach(function(aProperty) { var threw = false; diff --git a/netwerk/test/unit/test_URIs2.js b/netwerk/test/unit/test_URIs2.js index 0cc5740135cd..d4aa13b14305 100644 --- a/netwerk/test/unit/test_URIs2.js +++ b/netwerk/test/unit/test_URIs2.js @@ -603,16 +603,16 @@ function do_test_mutate_ref(aTest, aSuffix) { var pathWithSuffix = aTest.pathQueryRef + aSuffix; do_info("testing that setting path to " + pathWithSuffix + " and then clearing ref does what we expect"); - testURI.pathQueryRef = pathWithSuffix; + testURI = testURI.mutate().setPathQueryRef(pathWithSuffix).finalize(); testURI.ref = ""; do_check_uri_eq(testURI, refURIWithoutSuffix); do_check_uri_eqExceptRef(testURI, refURIWithSuffix); // Also: make sure that clearing .pathQueryRef also clears .ref - testURI.pathQueryRef = pathWithSuffix; + testURI = testURI.mutate().setPathQueryRef(pathWithSuffix).finalize(); do_info("testing that clearing path from " + pathWithSuffix + " also clears .ref"); - testURI.pathQueryRef = ""; + testURI = testURI.mutate().setPathQueryRef("").finalize(); Assert.equal(testURI.ref, ""); } } @@ -626,7 +626,7 @@ function do_test_immutable(aTest) { var URI = NetUtil.newURI(aTest.spec); // All the non-readonly attributes on nsIURI.idl: var propertiesToCheck = ["scheme", "userPass", "username", "password", - "host", "port", "pathQueryRef", "query", "ref"]; + "host", "port", "query", "ref"]; propertiesToCheck.forEach(function(aProperty) { var threw = false; diff --git a/netwerk/test/unit/test_standardurl.js b/netwerk/test/unit/test_standardurl.js index 7415b3b90370..990f93849ca3 100644 --- a/netwerk/test/unit/test_standardurl.js +++ b/netwerk/test/unit/test_standardurl.js @@ -55,8 +55,8 @@ add_test(function test_setEmptyPath() { symmetricEquality(false, target, provided); - provided.pathQueryRef = ""; - target.pathQueryRef = ""; + provided = provided.mutate().setPathQueryRef("").finalize(); + target = target.mutate().setPathQueryRef("").finalize(); Assert.equal(provided.spec, target.spec); symmetricEquality(true, target, provided); @@ -302,7 +302,7 @@ add_test(function test_hugeStringThrows() let hugeString = new Array(maxLen + 1).fill("a").join(""); let properties = ["scheme", "userPass", "username", - "password", "host", "pathQueryRef", "ref", + "password", "host", "ref", "query", "filePath"]; for (let prop of properties) { Assert.throws(() => url[prop] = hugeString, @@ -313,6 +313,7 @@ add_test(function test_hugeStringThrows() let setters = [ { method: "setSpec", qi: Ci.nsIURIMutator }, { method: "setHostPort", qi: Ci.nsIURIMutator }, + { method: "setPathQueryRef", qi: Ci.nsIURIMutator }, { method: "setFileName", qi: Ci.nsIURLMutator }, { method: "setFileExtension", qi: Ci.nsIURLMutator }, { method: "setFileBaseName", qi: Ci.nsIURLMutator }, diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_mapURIToAddonID.js b/toolkit/mozapps/extensions/test/xpcshell/test_mapURIToAddonID.js index bc5269b7510c..1016891a8b01 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_mapURIToAddonID.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_mapURIToAddonID.js @@ -314,8 +314,9 @@ function run_test_provider() { check_mapping(provider.uri, provider.id); - let u2 = provider.uri.clone(); - u2.pathQueryRef = "notmapped"; + let u2 = provider.uri.mutate() + .setPathQueryRef("notmapped") + .finalize(); Assert.equal(AddonManager.mapURIToAddonID(u2), null); AddonManagerPrivate.unregisterProvider(provider);