Bug 991471 - Fix offset when setting host on URL. r=mcmanus, a=abillings
authorValentin Gosu <valentin.gosu@gmail.com>
Thu, 10 Apr 2014 10:23:04 -0400
changeset 183742 1be8ef9bf661db27b69e68ccbd9a66934c65c1d0
parent 183741 05c933823ad8fed6c3c6dde3ff8a6dea026cc008
child 183743 7872e02410a7012d625de794a319591540a1a38e
push id3466
push userryanvm@gmail.com
push dateMon, 14 Apr 2014 14:00:12 +0000
treeherdermozilla-beta@1be8ef9bf661 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcmanus, abillings
bugs991471
milestone29.0
Bug 991471 - Fix offset when setting host on URL. r=mcmanus, a=abillings
netwerk/base/src/nsStandardURL.cpp
--- a/netwerk/base/src/nsStandardURL.cpp
+++ b/netwerk/base/src/nsStandardURL.cpp
@@ -1502,17 +1502,24 @@ nsStandardURL::SetHost(const nsACString 
     else if (NormalizeIDN(flat, hostBuf)) {
         host = hostBuf.get();
         len = hostBuf.Length();
     }
     else
         len = flat.Length();
 
     if (mHost.mLen < 0) {
-        mHost.mPos = mAuthority.mPos;
+        int port_length = 0;
+        if (mPort != -1) {
+            nsAutoCString buf;
+            buf.Assign(':');
+            buf.AppendInt(mPort);
+            port_length = buf.Length();
+        }
+        mHost.mPos = mAuthority.mPos + mAuthority.mLen - port_length;
         mHost.mLen = 0;
     }
 
     int32_t shift = ReplaceSegment(mHost.mPos, mHost.mLen, host, len);
 
     if (shift) {
         mHost.mLen = len;
         mAuthority.mLen += shift;
@@ -1546,37 +1553,46 @@ nsStandardURL::SetPort(int32_t port)
 
     InvalidateCache();
 
     if (mPort == -1) {
         // need to insert the port number in the URL spec
         nsAutoCString buf;
         buf.Assign(':');
         buf.AppendInt(port);
-        mSpec.Insert(buf, mHost.mPos + mHost.mLen);
+        mSpec.Insert(buf, mAuthority.mPos + mAuthority.mLen);
         mAuthority.mLen += buf.Length();
         ShiftFromPath(buf.Length());
     }
     else if (port == -1 || port == mDefaultPort) {
         // Don't allow mPort == mDefaultPort
         port = -1;
 
+        // compute length of the current port
+        nsAutoCString buf;
+        buf.Assign(':');
+        buf.AppendInt(mPort);
+
         // need to remove the port number from the URL spec
-        uint32_t start = mHost.mPos + mHost.mLen;
-        int32_t lengthToCut = mPath.mPos - start;
+        uint32_t start = mAuthority.mPos + mAuthority.mLen - buf.Length();
+        int32_t lengthToCut = buf.Length();
         mSpec.Cut(start, lengthToCut);
         mAuthority.mLen -= lengthToCut;
         ShiftFromPath(-lengthToCut);
     }
     else {
         // need to replace the existing port
         nsAutoCString buf;
+        buf.Assign(':');
+        buf.AppendInt(mPort);
+        uint32_t start = mAuthority.mPos + mAuthority.mLen - buf.Length();
+        uint32_t length = buf.Length();
+
+        buf.Assign(':');
         buf.AppendInt(port);
-        uint32_t start = mHost.mPos + mHost.mLen + 1;
-        uint32_t length = mPath.mPos - start;
         mSpec.Replace(start, length, buf);
         if (buf.Length() != length) {
             mAuthority.mLen += buf.Length() - length;
             ShiftFromPath(buf.Length() - length);
         }
     }
 
     mPort = port;