Enhance DNS prefetching by resolving URIs against the document base URI

This commit is contained in:
wuggy 2026-04-23 22:19:46 -07:00
commit b913fa62d9

View file

@ -923,15 +923,15 @@ nsContentSink::PrefetchDNS(const nsAString &aHref)
{ {
nsAutoString hostname; nsAutoString hostname;
if (StringBeginsWith(aHref, NS_LITERAL_STRING("//"))) { // Resolve against the document base URI so relative and scheme-relative
hostname = Substring(aHref, 2); // Link header values can still extract a hostname for speculation.
} const nsACString& charset = mDocument->GetDocumentCharacterSet();
else { nsCOMPtr<nsIURI> uri;
nsCOMPtr<nsIURI> uri; NS_NewURI(getter_AddRefs(uri), aHref,
NS_NewURI(getter_AddRefs(uri), aHref); charset.IsEmpty() ? nullptr : PromiseFlatCString(charset).get(),
if (!uri) { mDocument->GetDocBaseURI());
return;
} if (uri) {
nsresult rv; nsresult rv;
bool isLocalResource = false; bool isLocalResource = false;
rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE, rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_LOCAL_RESOURCE,
@ -941,6 +941,9 @@ nsContentSink::PrefetchDNS(const nsAString &aHref)
uri->GetHost(host); uri->GetHost(host);
CopyUTF8toUTF16(host, hostname); CopyUTF8toUTF16(host, hostname);
} }
} else if (aHref.FindChar('/') == kNotFound && aHref.FindChar(':') == kNotFound) {
// Support a bare hostname token in Link headers.
hostname = aHref;
} }
if (!hostname.IsEmpty() && nsHTMLDNSPrefetch::IsAllowed(mDocument)) { if (!hostname.IsEmpty() && nsHTMLDNSPrefetch::IsAllowed(mDocument)) {