treat ipv4 addresses as a single lookup key in the url classifier. b=395376, r=tony, a=mconnor

This commit is contained in:
dcamp@mozilla.com 2007-10-01 17:21:45 -07:00
Родитель 5ee9239f75
Коммит 1463a9325b
1 изменённых файлов: 62 добавлений и 31 удалений

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

@ -489,9 +489,12 @@ private:
nsresult GetLookupFragments(const nsCSubstring& spec,
nsTArray<nsUrlClassifierHash>& fragments);
// Check for a canonicalized IP address.
PRBool IsCanonicalizedIP(const nsACString& host);
// Get the database key for a given URI. This is the top three
// domain components if they exist, otherwise the top two.
// hostname.com/foo/bar -> hostname
// hostname.com/foo/bar -> hostname.com
// mail.hostname.com/foo/bar -> mail.hostname.com
// www.mail.hostname.com/foo/bar -> mail.hostname.com
nsresult GetKey(const nsACString& spec, nsUrlClassifierHash& hash);
@ -812,6 +815,15 @@ nsUrlClassifierDBServiceWorker::DoLookup(const nsACString& spec,
}
const nsCSubstring& host = Substring(begin, iter++);
nsTArray<PRUint32> resultTables;
nsUrlClassifierHash hash;
if (IsCanonicalizedIP(host)) {
// Don't break up the host into components
hash.FromPlaintext(host, mCryptoHash);
CheckKey(spec, hash, resultTables);
} else {
nsCStringArray hostComponents;
hostComponents.ParseString(PromiseFlatCString(host).get(), ".");
@ -828,12 +840,10 @@ nsUrlClassifierDBServiceWorker::DoLookup(const nsACString& spec,
lookupHost.Append(".");
lookupHost.Append(*hostComponents[last]);
lookupHost.Append("/");
nsUrlClassifierHash hash;
hash.FromPlaintext(lookupHost, mCryptoHash);
// we ignore failures from CheckKey because we'd rather try to find
// more results than fail.
nsTArray<PRUint32> resultTables;
CheckKey(spec, hash, resultTables);
// Now check with three domain components
@ -846,6 +856,7 @@ nsUrlClassifierDBServiceWorker::DoLookup(const nsACString& spec,
CheckKey(spec, hash, resultTables);
}
}
nsCAutoString result;
for (PRUint32 i = 0; i < resultTables.Length(); i++) {
@ -1151,6 +1162,21 @@ nsUrlClassifierDBServiceWorker::WriteEntry(nsUrlClassifierEntry& entry)
return NS_OK;
}
PRBool
nsUrlClassifierDBServiceWorker::IsCanonicalizedIP(const nsACString& host)
{
// The canonicalization process will have left IP addresses in dotted
// decimal with no surprises.
PRUint32 i1, i2, i3, i4;
char c;
if (PR_sscanf(PromiseFlatCString(host).get(), "%u.%u.%u.%u%c",
&i1, &i2, &i3, &i4, &c) == 4) {
return (i1 <= 0xFF && i1 <= 0xFF && i1 <= 0xFF && i1 <= 0xFF);
}
return PR_FALSE;
}
nsresult
nsUrlClassifierDBServiceWorker::GetKey(const nsACString& spec,
nsUrlClassifierHash& hash)
@ -1164,7 +1190,12 @@ nsUrlClassifierDBServiceWorker::GetKey(const nsACString& spec,
return NS_OK;
}
const nsCSubstring& host = Substring(begin, iter++);
const nsCSubstring& host = Substring(begin, iter);
if (IsCanonicalizedIP(host)) {
return hash.FromPlaintext(host, mCryptoHash);
}
nsCStringArray hostComponents;
hostComponents.ParseString(PromiseFlatCString(host).get(), ".");