Bug 1915419 - Add nsIEffectiveTLDService getSchemelessSiteFromHost. r=bvandersloot,necko-reviewers,valentin

Differential Revision: https://phabricator.services.mozilla.com/D220604
This commit is contained in:
Paul Zuehlcke 2024-09-24 09:17:55 +00:00
parent 1698a97cf4
commit 2d8852cfd7
3 changed files with 69 additions and 1 deletions

View File

@ -206,6 +206,23 @@ nsEffectiveTLDService::GetSchemelessSite(nsIURI* aURI, nsACString& aSite) {
return rv;
}
// Variant of GetSchemelessSite which accepts a host string instead of a URI.
NS_IMETHODIMP
nsEffectiveTLDService::GetSchemelessSiteFromHost(const nsACString& aHostname,
nsACString& aSite) {
NS_ENSURE_TRUE(!aHostname.IsEmpty(), NS_ERROR_FAILURE);
nsresult rv = GetBaseDomainFromHost(aHostname, 0, aSite);
if (rv == NS_ERROR_HOST_IS_IP_ADDRESS ||
rv == NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS) {
aSite.Assign(aHostname);
nsContentUtils::MaybeFixIPv6Host(aSite);
return NS_OK;
}
return rv;
}
// External function for dealing with URIs to get site correctly.
// Calls through to GetSchemelessSite(), and serializes with the scheme and
// "://" prepended.

View File

@ -119,6 +119,28 @@ interface nsIEffectiveTLDService : nsISupports
*/
ACString getSchemelessSite(in nsIURI aURI);
/**
* Same as getSchemelessSite but takes a host string. Prefer the URI variant
* if possible.
*
* @param aHost
* The host to analyze.
*
* @return the Site.
*
* @throws NS_ERROR_UNEXPECTED
* or other error returned by nsIIDNService::normalize when
* the hostname contains characters disallowed in URIs
*
* @see getSchemelessSite()
* @see getBaseDomain()
* @see getSite()
*
* @warning This function should not be used without good reason. Please
* use getSite() or the Origin if you are not absolutely certain.
*/
ACString getSchemelessSiteFromHost(in AUTF8String aHost);
/**
* Get the Site for the origin of aURI; e.g. for
* "https://www.bbc.co.uk/index.html", this would be "https://bbc.co.uk".

View File

@ -64,7 +64,10 @@ add_task(() => {
["https://test.example.com", "example.com"],
["https://test1.test2.example.com", "example.com"],
["https://test1.test2.example.co.uk", "example.co.uk"],
["https://test1.test2.example.公司.香港", "example.xn--55qx5d.xn--j6w193g"],
[
"https://test1.test2.example.\u516c\u53f8.\u9999\u6e2f",
"example.xn--55qx5d.xn--j6w193g",
],
]) {
let origin = Services.io.newURI(originString);
let answer = Services.eTLD.getSchemelessSite(origin);
@ -75,3 +78,29 @@ add_task(() => {
);
}
});
add_task(function test_getSchemelessSiteFromHost() {
for (let [host, result] of [
["com", "com"],
["test", "test"],
["test.", "test."],
["::1", "[::1]"],
["localhost", "localhost"],
["127.0.0.1", "127.0.0.1"],
["example.com", "example.com"],
["test.example.com", "example.com"],
["test1.test2.example.com", "example.com"],
["test1.test2.example.co.uk", "example.co.uk"],
[
"test1.test2.example.\u516c\u53f8.\u9999\u6e2f",
"example.xn--55qx5d.xn--j6w193g",
],
]) {
let answer = Services.eTLD.getSchemelessSiteFromHost(host);
Assert.equal(
answer,
result,
`"${host}" should have schemeless site ${result}, got ${answer}.`
);
}
});