Bug 1295636 - SetHostPort should reset the port if the host parameter does not have a port number. r=valentin, r=smaug
authorDragana Damjanovic <dd.mozilla@gmail.com>
Wed, 17 Aug 2016 23:25:00 -0400
changeset 310105 b1627e53676bde6e072a7d928d9d7a44f8e52658
parent 310104 af1ae450676a05584c4be462900552b4b07996c5
child 310106 6a0f2b93749a4adafba030aa0522ebd7255bbce8
push id30576
push userryanvm@gmail.com
push dateFri, 19 Aug 2016 13:53:39 +0000
treeherdermozilla-central@74f332c38a69 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin, smaug
bugs1295636
milestone51.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1295636 - SetHostPort should reset the port if the host parameter does not have a port number. r=valentin, r=smaug
caps/nsNullPrincipalURI.cpp
dom/html/nsHTMLDocument.cpp
image/decoders/icon/nsIconURI.cpp
modules/libjar/nsJARURI.cpp
netwerk/base/nsIURI.idl
netwerk/base/nsSimpleURI.cpp
netwerk/base/nsStandardURL.cpp
--- a/caps/nsNullPrincipalURI.cpp
+++ b/caps/nsNullPrincipalURI.cpp
@@ -122,16 +122,22 @@ nsNullPrincipalURI::GetHostPort(nsACStri
 
 NS_IMETHODIMP
 nsNullPrincipalURI::SetHostPort(const nsACString &aHost)
 {
   return NS_ERROR_NOT_IMPLEMENTED;
 }
 
 NS_IMETHODIMP
+nsNullPrincipalURI::SetHostAndPort(const nsACString &aHost)
+{
+  return NS_ERROR_NOT_IMPLEMENTED;
+}
+
+NS_IMETHODIMP
 nsNullPrincipalURI::GetOriginCharset(nsACString &_charset)
 {
   _charset.Truncate();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsNullPrincipalURI::GetPassword(nsACString &_password)
--- a/dom/html/nsHTMLDocument.cpp
+++ b/dom/html/nsHTMLDocument.cpp
@@ -942,22 +942,18 @@ nsHTMLDocument::SetDomain(const nsAStrin
   }
 
   rv2 = newURI->SetUserPass(EmptyCString());
   if (NS_FAILED(rv2)) {
     rv.Throw(rv2);
     return;
   }
 
-  // If the old uri had a port number and the new domain does not have,
-  // SetHostPort will not reset the port number of the old uri, it will be
-  // kept. Here we want to reset the port number, so we need to do it manually.
-  newURI->SetPort(-1);
-
-  rv2 = newURI->SetHostPort(NS_ConvertUTF16toUTF8(aDomain));
+  // We use SetHostAndPort because we want to reset the port number if needed.
+  rv2 = newURI->SetHostAndPort(NS_ConvertUTF16toUTF8(aDomain));
   if (NS_FAILED(rv2)) {
     rv.Throw(rv2);
     return;
   }
 
   // Check new domain - must be a superdomain of the current host
   // For example, a page from foo.bar.com may set domain to bar.com,
   // but not to ar.com, baz.com, or fi.foo.bar.com.
--- a/image/decoders/icon/nsIconURI.cpp
+++ b/image/decoders/icon/nsIconURI.cpp
@@ -324,16 +324,22 @@ nsMozIconURI::GetHostPort(nsACString& aH
 
 NS_IMETHODIMP
 nsMozIconURI::SetHostPort(const nsACString& aHostPort)
 {
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
+nsMozIconURI::SetHostAndPort(const nsACString& aHostPort)
+{
+  return NS_ERROR_FAILURE;
+}
+
+NS_IMETHODIMP
 nsMozIconURI::GetHost(nsACString& aHost)
 {
   return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsMozIconURI::SetHost(const nsACString& aHost)
 {
--- a/modules/libjar/nsJARURI.cpp
+++ b/modules/libjar/nsJARURI.cpp
@@ -380,16 +380,22 @@ nsJARURI::GetHostPort(nsACString &aHostP
 
 NS_IMETHODIMP
 nsJARURI::SetHostPort(const nsACString &aHostPort)
 {
     return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
+nsJARURI::SetHostAndPort(const nsACString &aHostPort)
+{
+    return NS_ERROR_FAILURE;
+}
+
+NS_IMETHODIMP
 nsJARURI::GetHost(nsACString &aHost)
 {
     return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
 nsJARURI::SetHost(const nsACString &aHost)
 {
--- a/netwerk/base/nsIURI.idl
+++ b/netwerk/base/nsIURI.idl
@@ -118,21 +118,30 @@ interface nsIURI : nsISupports
      * Some characters may be escaped.
      */
     attribute AUTF8String username;
     attribute AUTF8String password;
 
     /**
      * The host:port (or simply the host, if port == -1).
      *
+     * If this attribute is set to a value that only has a host part, the port
+     * will not be reset. To reset the port as well use setHostAndPort.
+     *
      * Characters are NOT escaped.
      */
     attribute AUTF8String hostPort;
 
     /**
+     * This function will always set a host and a port. If the port part is
+     * empty, the value of the port will be set to the default value.
+     */
+    void setHostAndPort(in AUTF8String hostport);
+
+    /**
      * The host is the internet domain name to which this URI refers.  It could
      * be an IPv4 (or IPv6) address literal.  If supported, it could be a
      * non-ASCII internationalized domain name.
      *
      * Characters are NOT escaped.
      */
     attribute AUTF8String host;
 
--- a/netwerk/base/nsSimpleURI.cpp
+++ b/netwerk/base/nsSimpleURI.cpp
@@ -311,16 +311,24 @@ NS_IMETHODIMP
 nsSimpleURI::SetHostPort(const nsACString &result)
 {
     NS_ENSURE_STATE(mMutable);
     
     return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
+nsSimpleURI::SetHostAndPort(const nsACString &result)
+{
+    NS_ENSURE_STATE(mMutable);
+
+    return NS_ERROR_FAILURE;
+}
+
+NS_IMETHODIMP
 nsSimpleURI::GetHost(nsACString &result)
 {
     // Note: Audit all callers before changing this to return an empty
     // string -- CAPS and UI code depend on this throwing.
     return NS_ERROR_FAILURE;
 }
 
 NS_IMETHODIMP
--- a/netwerk/base/nsStandardURL.cpp
+++ b/netwerk/base/nsStandardURL.cpp
@@ -1770,16 +1770,18 @@ nsStandardURL::FindHostLimit(nsACString:
   for (int32_t i = 0; gHostLimitDigits[i]; ++i) {
     nsACString::const_iterator c(aStart);
     if (FindCharInReadable(gHostLimitDigits[i], c, aEnd)) {
       aEnd = c;
     }
   }
 }
 
+// If aValue only has a host part and no port number, the port
+// will not be reset!!!
 NS_IMETHODIMP
 nsStandardURL::SetHostPort(const nsACString &aValue)
 {
     ENSURE_MUTABLE();
 
     // We cannot simply call nsIURI::SetHost because that would treat the name as
     // an IPv6 address (like http:://[server:443]/).  We also cannot call
     // nsIURI::SetHostPort because that isn't implemented.  Sadfaces.
@@ -1840,16 +1842,28 @@ nsStandardURL::SetHostPort(const nsACStr
             // port number is missing
             return NS_ERROR_MALFORMED_URI;
         }
     }
 
     return NS_OK;
 }
 
+// This function is different than SetHostPort in that the port number will be
+// reset as well if aValue parameter does not contain a port port number.
+NS_IMETHODIMP
+nsStandardURL::SetHostAndPort(const nsACString &aValue)
+{
+  // Reset the port and than call SetHostPort. SetHostPort does not reset
+  // the port number.
+  nsresult rv = SetPort(-1);
+  NS_ENSURE_SUCCESS(rv, rv);
+  return SetHostPort(aValue);
+}
+
 NS_IMETHODIMP
 nsStandardURL::SetHost(const nsACString &input)
 {
     ENSURE_MUTABLE();
 
     const nsPromiseFlatCString &hostname = PromiseFlatCString(input);
 
     nsACString::const_iterator start, end;