Make SetPort actually update the internal offsets correctly. Bug 388281, r+sr=biesi

This commit is contained in:
bzbarsky@mit.edu 2007-07-16 21:48:47 -07:00
Родитель c659851b49
Коммит 8b31383627
2 изменённых файлов: 36 добавлений и 4 удалений

Просмотреть файл

@ -1511,13 +1511,16 @@ nsStandardURL::SetPort(PRInt32 port)
buf.Assign(':');
buf.AppendInt(port);
mSpec.Insert(buf, mHost.mPos + mHost.mLen);
mAuthority.mLen += buf.Length();
ShiftFromPath(buf.Length());
}
else if (port == -1) {
else if (port == -1 || port == mDefaultPort) {
// need to remove the port number from the URL spec
PRUint32 start = mHost.mPos + mHost.mLen;
mSpec.Cut(start, mPath.mPos - start);
ShiftFromPath(start - mPath.mPos);
PRUint32 lengthToCut = mPath.mPos - start;
mSpec.Cut(start, lengthToCut);
mAuthority.mLen -= lengthToCut;
ShiftFromPath(-lengthToCut);
}
else {
// need to replace the existing port
@ -1526,8 +1529,10 @@ nsStandardURL::SetPort(PRInt32 port)
PRUint32 start = mHost.mPos + mHost.mLen + 1;
PRUint32 length = mPath.mPos - start;
mSpec.Replace(start, length, buf);
if (buf.Length() != length)
if (buf.Length() != length) {
mAuthority.mLen += buf.Length() - length;
ShiftFromPath(buf.Length() - length);
}
}
mPort = port;

Просмотреть файл

@ -0,0 +1,27 @@
const Cc = Components.classes;
const Ci = Components.interfaces;
function run_test() {
const ios = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var uri = ios.newURI("http://foo.com/file.txt", null, null);
uri.port = 90;
do_check_eq(uri.hostPort, "foo.com:90");
uri = ios.newURI("http://foo.com:10/file.txt", null, null);
uri.port = 500;
do_check_eq(uri.hostPort, "foo.com:500");
uri = ios.newURI("http://foo.com:5000/file.txt", null, null);
uri.port = 20;
do_check_eq(uri.hostPort, "foo.com:20");
uri = ios.newURI("http://foo.com:5000/file.txt", null, null);
uri.port = -1;
do_check_eq(uri.hostPort, "foo.com");
uri = ios.newURI("http://foo.com:5000/file.txt", null, null);
uri.port = 80;
do_check_eq(uri.hostPort, "foo.com");
}