Bug 1214305 - Part 2: Refactor the logic for obtaining the secure upgraded URI into HttpBaseChannel; r=mcmanus
☠☠ backed out by 50b5e58fd4cd ☠ ☠
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 02 Nov 2015 11:27:00 -0500
changeset 308945 be072bba15fc640071e76c99df7aee5e8b90e07d
parent 308944 3b85acbe7be7111f06f44ad36a324c9e10415483
child 308946 347cd0f8f1e3cad2ef816d600310dd8100820ece
push id7543
push userpehrsons@gmail.com
push dateMon, 16 Nov 2015 02:58:32 +0000
reviewersmcmanus
bugs1214305
milestone45.0a1
Bug 1214305 - Part 2: Refactor the logic for obtaining the secure upgraded URI into HttpBaseChannel; r=mcmanus
netwerk/protocol/http/HttpBaseChannel.cpp
netwerk/protocol/http/HttpBaseChannel.h
netwerk/protocol/http/nsHttpChannel.cpp
--- a/netwerk/protocol/http/HttpBaseChannel.cpp
+++ b/netwerk/protocol/http/HttpBaseChannel.cpp
@@ -3120,11 +3120,40 @@ HttpBaseChannel::SetCorsPreflightParamet
 
   mRequireCORSPreflight = true;
   mUnsafeHeaders = aUnsafeHeaders;
   mWithCredentials = aWithCredentials;
   mPreflightPrincipal = aPrincipal;
   return NS_OK;
 }
 
+// static
+nsresult
+HttpBaseChannel::GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI)
+{
+  nsCOMPtr<nsIURI> upgradedURI;
+
+  nsresult rv = aURI->Clone(getter_AddRefs(upgradedURI));
+  NS_ENSURE_SUCCESS(rv,rv);
+
+  upgradedURI->SetScheme(NS_LITERAL_CSTRING("https"));
+
+  int32_t oldPort = -1;
+  rv = aURI->GetPort(&oldPort);
+  if (NS_FAILED(rv)) return rv;
+
+  // Keep any nonstandard ports so only the scheme is changed.
+  // For example:
+  //  http://foo.com:80 -> https://foo.com:443
+  //  http://foo.com:81 -> https://foo.com:81
+
+  if (oldPort == 80 || oldPort == -1)
+      upgradedURI->SetPort(-1);
+  else
+      upgradedURI->SetPort(oldPort);
+
+  upgradedURI.forget(aUpgradedURI);
+  return NS_OK;
+}
+
 } // namespace net
 } // namespace mozilla
 
--- a/netwerk/protocol/http/HttpBaseChannel.h
+++ b/netwerk/protocol/http/HttpBaseChannel.h
@@ -295,16 +295,20 @@ public: /* Necko internal use only... */
     // mListenerContext.
     nsresult DoApplyContentConversions(nsIStreamListener *aNextListener,
                                        nsIStreamListener **aNewNextListener);
 
     // Callback on main thread when NS_AsyncCopy() is finished populating
     // the new mUploadStream.
     void EnsureUploadStreamIsCloneableComplete(nsresult aStatus);
 
+    // Returns an https URI for channels that need to go through secure
+    // upgrades.
+    static nsresult GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI);
+
 protected:
   nsCOMArray<nsISecurityConsoleMessage> mSecurityConsoleMessages;
 
   // Handle notifying listener, removing from loadgroup if request failed.
   void     DoNotifyListener();
   virtual void DoNotifyListenerCleanup() = 0;
 
   // drop reference to listener, its callbacks, and the progress sink
--- a/netwerk/protocol/http/nsHttpChannel.cpp
+++ b/netwerk/protocol/http/nsHttpChannel.cpp
@@ -1863,40 +1863,22 @@ nsHttpChannel::HandleAsyncRedirectChanne
     nsresult rv = StartRedirectChannelToHttps();
     if (NS_FAILED(rv))
         ContinueAsyncRedirectChannelToURI(rv);
 }
 
 nsresult
 nsHttpChannel::StartRedirectChannelToHttps()
 {
-    nsresult rv = NS_OK;
     LOG(("nsHttpChannel::HandleAsyncRedirectChannelToHttps() [STS]\n"));
 
     nsCOMPtr<nsIURI> upgradedURI;
-
-    rv = mURI->Clone(getter_AddRefs(upgradedURI));
+    nsresult rv = GetSecureUpgradedURI(mURI, getter_AddRefs(upgradedURI));
     NS_ENSURE_SUCCESS(rv,rv);
 
-    upgradedURI->SetScheme(NS_LITERAL_CSTRING("https"));
-
-    int32_t oldPort = -1;
-    rv = mURI->GetPort(&oldPort);
-    if (NS_FAILED(rv)) return rv;
-
-    // Keep any nonstandard ports so only the scheme is changed.
-    // For example:
-    //  http://foo.com:80 -> https://foo.com:443
-    //  http://foo.com:81 -> https://foo.com:81
-
-    if (oldPort == 80 || oldPort == -1)
-        upgradedURI->SetPort(-1);
-    else
-        upgradedURI->SetPort(oldPort);
-
     return StartRedirectChannelToURI(upgradedURI,
                                      nsIChannelEventSink::REDIRECT_PERMANENT |
                                      nsIChannelEventSink::REDIRECT_STS_UPGRADE);
 }
 
 void
 nsHttpChannel::HandleAsyncAPIRedirect()
 {