Bug 1082723 - Add IPv6 delimiters to nsLocation::GetHostname and Link::GetHostname r=smaug

This commit is contained in:
Valentin Gosu 2014-10-20 13:46:20 +03:00
parent 74a360e504
commit f4b976820f
5 changed files with 30 additions and 29 deletions

View File

@ -2184,6 +2184,12 @@ public:
*/ */
static uint64_t GetInnerWindowID(nsIRequest* aRequest); static uint64_t GetInnerWindowID(nsIRequest* aRequest);
/**
* If the hostname for aURI is an IPv6 it encloses it in brackets,
* otherwise it just outputs the hostname in aHost.
*/
static void GetHostOrIPv6WithBrackets(nsIURI* aURI, nsAString& aHost);
private: private:
static bool InitializeEventTable(); static bool InitializeEventTable();

View File

@ -361,13 +361,7 @@ Link::GetHostname(nsAString &_hostname, ErrorResult& aError)
return; return;
} }
nsAutoCString host; nsContentUtils::GetHostOrIPv6WithBrackets(uri, _hostname);
nsresult rv = uri->GetHost(host);
// Note that failure to get the host from the URI is not necessarily a bad
// thing. Some URIs do not have a host.
if (NS_SUCCEEDED(rv)) {
CopyUTF8toUTF16(host, _hostname);
}
} }
void void

View File

@ -6998,3 +6998,23 @@ nsContentUtils::GetInnerWindowID(nsIRequest* aRequest)
return inner ? inner->WindowID() : 0; return inner ? inner->WindowID() : 0;
} }
void
nsContentUtils::GetHostOrIPv6WithBrackets(nsIURI* aURI, nsAString& aHost)
{
aHost.Truncate();
nsAutoCString hostname;
nsresult rv = aURI->GetHost(hostname);
if (NS_FAILED(rv)) { // Some URIs do not have a host
return;
}
if (hostname.FindChar(':') != -1) { // Escape IPv6 address
MOZ_ASSERT(!hostname.Length() ||
(hostname[0] !='[' && hostname[hostname.Length() - 1] != ']'));
hostname.Insert('[', 0);
hostname.Append(']');
}
CopyUTF8toUTF16(hostname, aHost);
}

View File

@ -382,17 +382,7 @@ void
URL::GetHostname(nsString& aHostname, ErrorResult& aRv) const URL::GetHostname(nsString& aHostname, ErrorResult& aRv) const
{ {
aHostname.Truncate(); aHostname.Truncate();
nsAutoCString tmp; nsContentUtils::GetHostOrIPv6WithBrackets(mURI, aHostname);
nsresult rv = mURI->GetHost(tmp);
if (NS_SUCCEEDED(rv)) {
if (tmp.FindChar(':') != -1) { // Escape IPv6 address
MOZ_ASSERT(!tmp.Length() ||
(tmp[0] !='[' && tmp[tmp.Length() - 1] != ']'));
tmp.Insert('[', 0);
tmp.Append(']');
}
CopyUTF8toUTF16(tmp, aHostname);
}
} }
void void

View File

@ -407,18 +407,9 @@ nsLocation::GetHostname(nsAString& aHostname)
aHostname.Truncate(); aHostname.Truncate();
nsCOMPtr<nsIURI> uri; nsCOMPtr<nsIURI> uri;
nsresult result; GetURI(getter_AddRefs(uri), true);
result = GetURI(getter_AddRefs(uri), true);
if (uri) { if (uri) {
nsAutoCString host; nsContentUtils::GetHostOrIPv6WithBrackets(uri, aHostname);
result = uri->GetHost(host);
if (NS_SUCCEEDED(result)) {
AppendUTF8toUTF16(host, aHostname);
}
} }
return NS_OK; return NS_OK;