Bug 1247733 part 2: Give nsIStandardURL an API to set its default port, and use it when upgrading HTTP connections to HTTPS. r=valentin

This commit is contained in:
Daniel Holbert 2016-02-17 19:24:36 -08:00
parent ba4e93c003
commit e064e82632
3 changed files with 53 additions and 11 deletions

View File

@ -65,4 +65,16 @@ interface nsIStandardURL : nsIMutable
in AUTF8String aSpec,
in string aOriginCharset,
in nsIURI aBaseURI);
/**
* Set the default port.
*
* Note: If this object is already using its default port (i.e. if it has
* mPort == -1), then it will now implicitly be using the new default port.
*
* @param aNewDefaultPort - if the URI has (or is later given) a port that
* matches this default, then we won't include a
* port number in the canonical form of the URL.
*/
void setDefaultPort(in long aNewDefaultPort);
};

View File

@ -2887,6 +2887,25 @@ nsStandardURL::Init(uint32_t urlType,
return SetSpec(buf);
}
NS_IMETHODIMP
nsStandardURL::SetDefaultPort(int32_t aNewDefaultPort)
{
ENSURE_MUTABLE();
InvalidateCache();
// If we're already using the new default-port as a custom port, then clear
// it off of our mSpec & set mPort to -1, to indicate that we'll be using
// the default from now on (which happens to match what we already had).
if (mPort == aNewDefaultPort) {
ReplacePortInSpec(-1);
mPort = -1;
}
mDefaultPort = aNewDefaultPort;
return NS_OK;
}
NS_IMETHODIMP
nsStandardURL::GetMutable(bool *value)
{

View File

@ -3190,21 +3190,32 @@ HttpBaseChannel::GetSecureUpgradedURI(nsIURI* aURI, nsIURI** aUpgradedURI)
nsresult rv = aURI->Clone(getter_AddRefs(upgradedURI));
NS_ENSURE_SUCCESS(rv,rv);
// Change the scheme to HTTPS:
upgradedURI->SetScheme(NS_LITERAL_CSTRING("https"));
int32_t oldPort = -1;
rv = aURI->GetPort(&oldPort);
if (NS_FAILED(rv)) return rv;
// Change the default port to 443:
nsCOMPtr<nsIStandardURL> upgradedStandardURL = do_QueryInterface(upgradedURI);
if (upgradedStandardURL) {
upgradedStandardURL->SetDefaultPort(443);
} else {
// If we don't have a nsStandardURL, fall back to using GetPort/SetPort.
// XXXdholbert Is this function even called with a non-nsStandardURL arg,
// in practice?
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
// 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);
if (oldPort == 80 || oldPort == -1) {
upgradedURI->SetPort(-1);
} else {
upgradedURI->SetPort(oldPort);
}
}
upgradedURI.forget(aUpgradedURI);
return NS_OK;