Bug 1417827 - Pass DNS arguments as nsACString& instead of char* r=bagder

MozReview-Commit-ID: GqNYfjy9SFp

--HG--
extra : source : 190c4f057ffafa28a9abec657a0c70fe8a9489ab
This commit is contained in:
Valentin Gosu 2018-06-14 14:30:40 +02:00
parent deb1d4dff1
commit e5efdc876d
6 changed files with 64 additions and 61 deletions

View File

@ -196,9 +196,9 @@ _GetMinTTLForRequestType_Windows(DnsapiInfo * dnsapi, const char* aHost,
}
static MOZ_ALWAYS_INLINE nsresult
_GetTTLData_Windows(const char* aHost, uint32_t* aResult, uint16_t aAddressFamily)
_GetTTLData_Windows(const nsACString& aHost, uint32_t* aResult, uint16_t aAddressFamily)
{
MOZ_ASSERT(aHost);
MOZ_ASSERT(!aHost.IsEmpty());
MOZ_ASSERT(aResult);
if (aAddressFamily != PR_AF_UNSPEC &&
aAddressFamily != PR_AF_INET &&
@ -222,10 +222,10 @@ _GetTTLData_Windows(const char* aHost, uint32_t* aResult, uint16_t aAddressFamil
// and/or AAAA requests, based on the address family requested.
unsigned int ttl = (unsigned int)-1;
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET) {
_GetMinTTLForRequestType_Windows(dnsapi, aHost, DNS_TYPE_A, &ttl);
_GetMinTTLForRequestType_Windows(dnsapi, aHost.get(), DNS_TYPE_A, &ttl);
}
if (aAddressFamily == PR_AF_UNSPEC || aAddressFamily == PR_AF_INET6) {
_GetMinTTLForRequestType_Windows(dnsapi, aHost, DNS_TYPE_AAAA, &ttl);
_GetMinTTLForRequestType_Windows(dnsapi, aHost.get(), DNS_TYPE_AAAA, &ttl);
}
{
@ -249,10 +249,10 @@ _GetTTLData_Windows(const char* aHost, uint32_t* aResult, uint16_t aAddressFamil
////////////////////////////////////
static MOZ_ALWAYS_INLINE nsresult
_GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily,
_GetAddrInfo_Portable(const nsACString& aCanonHost, uint16_t aAddressFamily,
uint16_t aFlags, AddrInfo** aAddrInfo)
{
MOZ_ASSERT(aCanonHost);
MOZ_ASSERT(!aCanonHost.IsEmpty());
MOZ_ASSERT(aAddrInfo);
// We accept the same aFlags that nsHostResolver::ResolveHost accepts, but we
@ -270,20 +270,20 @@ _GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily,
aAddressFamily = PR_AF_UNSPEC;
}
PRAddrInfo* prai = PR_GetAddrInfoByName(aCanonHost, aAddressFamily, prFlags);
PRAddrInfo* prai = PR_GetAddrInfoByName(aCanonHost.BeginReading(), aAddressFamily, prFlags);
if (!prai) {
return NS_ERROR_UNKNOWN_HOST;
}
const char* canonName = nullptr;
nsAutoCString canonName;
if (aFlags & nsHostResolver::RES_CANON_NAME) {
canonName = PR_GetCanonNameFromAddrInfo(prai);
canonName.Assign(PR_GetCanonNameFromAddrInfo(prai));
}
bool filterNameCollision = !(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION);
nsAutoPtr<AddrInfo> ai(new AddrInfo(nsCString(aCanonHost), prai, disableIPv4,
filterNameCollision, nsCString(canonName)));
nsAutoPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4,
filterNameCollision, canonName));
PR_FreeAddrInfo(prai);
if (ai->mAddresses.isEmpty()) {
return NS_ERROR_UNKNOWN_HOST;
@ -320,10 +320,10 @@ GetAddrInfoShutdown() {
}
nsresult
GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily, uint16_t aFlags,
AddrInfo** aAddrInfo, bool aGetTtl)
{
if (NS_WARN_IF(!aHost) || NS_WARN_IF(!aAddrInfo)) {
if (NS_WARN_IF(aHost.IsEmpty()) || NS_WARN_IF(!aAddrInfo)) {
return NS_ERROR_NULL_POINTER;
}
@ -334,35 +334,37 @@ GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
}
#endif
nsAutoCString host(aHost);
if (gNativeIsLocalhost) {
// pretend we use the given host but use IPv4 localhost instead!
aHost = "localhost";
host = NS_LITERAL_CSTRING("localhost");
aAddressFamily = PR_AF_INET;
}
*aAddrInfo = nullptr;
nsresult rv = _GetAddrInfo_Portable(aHost, aAddressFamily, aFlags,
nsresult rv = _GetAddrInfo_Portable(host, aAddressFamily, aFlags,
aAddrInfo);
#ifdef DNSQUERY_AVAILABLE
if (aGetTtl && NS_SUCCEEDED(rv)) {
// Figure out the canonical name, or if that fails, just use the host name
// we have.
const char *name = nullptr;
nsAutoCString name;
if (*aAddrInfo != nullptr && (*aAddrInfo)->mCanonicalName) {
name = (*aAddrInfo)->mCanonicalName;
} else {
name = aHost;
name = host;
}
LOG("Getting TTL for %s (cname = %s).", aHost, name);
LOG("Getting TTL for %s (cname = %s).", host.get(), name.get());
uint32_t ttl = 0;
nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily);
if (NS_SUCCEEDED(ttlRv)) {
(*aAddrInfo)->ttl = ttl;
LOG("Got TTL %u for %s (name = %s).", ttl, aHost, name);
LOG("Got TTL %u for %s (name = %s).", ttl, host, name.get());
} else {
LOG_WARNING("Could not get TTL for %s (cname = %s).", aHost, name);
LOG_WARNING("Could not get TTL for %s (cname = %s).", host.get(), name.get());
}
}
#endif

View File

@ -37,7 +37,7 @@ class AddrInfo;
* answers..
*/
nsresult
GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
GetAddrInfo(const nsACString& aHost, uint16_t aAddressFamily, uint16_t aFlags,
AddrInfo** aAddrInfo, bool aGetTtl);
/**

View File

@ -394,7 +394,7 @@ TRR::ReceivePush(nsIHttpChannel *pushed, nsHostRecord *pushedRec)
RefPtr<nsHostRecord> hostRecord;
nsresult rv;
rv = mHostResolver->GetHostRecord(mHost.get(),
rv = mHostResolver->GetHostRecord(mHost,
pushedRec->flags, pushedRec->af,
pushedRec->pb,
pushedRec->originSuffix,

View File

@ -396,7 +396,7 @@ NS_IMETHODIMP
nsDNSAsyncRequest::Cancel(nsresult reason)
{
NS_ENSURE_ARG(NS_FAILED(reason));
mResolver->DetachCallback(mHost.get(), mOriginAttributes, mFlags, mAF,
mResolver->DetachCallback(mHost, mOriginAttributes, mFlags, mAF,
this, reason);
return NS_OK;
}
@ -877,7 +877,7 @@ nsDNSService::AsyncResolveNative(const nsACString &aHostname,
if (!req)
return NS_ERROR_OUT_OF_MEMORY;
rv = res->ResolveHost(req->mHost.get(), req->mOriginAttributes, flags, af, req);
rv = res->ResolveHost(req->mHost, req->mOriginAttributes, flags, af, req);
req.forget(result);
return rv;
}
@ -937,7 +937,7 @@ nsDNSService::CancelAsyncResolveNative(const nsACString &aHostname,
uint16_t af = GetAFForLookup(hostname, aFlags);
res->CancelAsyncRequest(hostname.get(), aOriginAttributes, aFlags, af,
res->CancelAsyncRequest(hostname, aOriginAttributes, aFlags, af,
aListener, aReason);
return NS_OK;
}
@ -1037,7 +1037,7 @@ nsDNSService::ResolveInternal(const nsACString &aHostname,
uint16_t af = GetAFForLookup(hostname, flags);
rv = res->ResolveHost(hostname.get(), aOriginAttributes, flags, af, syncReq);
rv = res->ResolveHost(hostname, aOriginAttributes, flags, af, syncReq);
if (NS_SUCCEEDED(rv)) {
// wait for result
while (!syncReq->mDone) {

View File

@ -707,13 +707,13 @@ nsHostResolver::Shutdown()
}
nsresult
nsHostResolver::GetHostRecord(const char *host,
nsHostResolver::GetHostRecord(const nsACString &host,
uint16_t flags, uint16_t af, bool pb,
const nsCString &originSuffix,
nsHostRecord **result)
{
MutexAutoLock lock(mLock);
nsHostKey key(nsCString(host), flags, af, pb, originSuffix);
nsHostKey key(host, flags, af, pb, originSuffix);
RefPtr<nsHostRecord>& entry = mRecordDB.GetOrInsert(key);
if (!entry) {
@ -732,21 +732,22 @@ nsHostResolver::GetHostRecord(const char *host,
}
nsresult
nsHostResolver::ResolveHost(const char *host,
nsHostResolver::ResolveHost(const nsACString &aHost,
const OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
nsResolveHostCallback *aCallback)
{
NS_ENSURE_TRUE(host && *host, NS_ERROR_UNEXPECTED);
nsAutoCString host(aHost);
NS_ENSURE_TRUE(!host.IsEmpty(), NS_ERROR_UNEXPECTED);
LOG(("Resolving host [%s]%s%s.\n", host,
LOG(("Resolving host [%s]%s%s.\n", host.get(),
flags & RES_BYPASS_CACHE ? " - bypassing cache" : "",
flags & RES_REFRESH_CACHE ? " - refresh cache" : ""));
// ensure that we are working with a valid hostname before proceeding. see
// bug 304904 for details.
if (!net_IsValidHostName(nsDependentCString(host)))
if (!net_IsValidHostName(host))
return NS_ERROR_UNKNOWN_HOST;
RefPtr<nsResolveHostCallback> callback(aCallback);
@ -775,7 +776,7 @@ nsHostResolver::ResolveHost(const char *host,
nsAutoCString originSuffix;
aOriginAttributes.CreateSuffix(originSuffix);
nsHostKey key(nsCString(host), flags, af,
nsHostKey key(host, flags, af,
(aOriginAttributes.mPrivateBrowsingId > 0),
originSuffix);
RefPtr<nsHostRecord>& entry = mRecordDB.GetOrInsert(key);
@ -787,7 +788,7 @@ nsHostResolver::ResolveHost(const char *host,
MOZ_ASSERT(rec, "Record should not be null");
if (!(flags & RES_BYPASS_CACHE) &&
rec->HasUsableResult(TimeStamp::NowLoRes(), flags)) {
LOG((" Using cached record for host [%s].\n", host));
LOG((" Using cached record for host [%s].\n", host.get()));
// put reference to host record on stack...
result = rec;
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
@ -798,7 +799,7 @@ nsHostResolver::ResolveHost(const char *host,
ConditionallyRefreshRecord(rec, host);
if (rec->negative) {
LOG((" Negative cache entry for host [%s].\n", host));
LOG((" Negative cache entry for host [%s].\n", host.get()));
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
METHOD_NEGATIVE_HIT);
status = NS_ERROR_UNKNOWN_HOST;
@ -806,15 +807,15 @@ nsHostResolver::ResolveHost(const char *host,
} else if (rec->addr) {
// if the host name is an IP address literal and has been parsed,
// go ahead and use it.
LOG((" Using cached address for IP Literal [%s].\n", host));
LOG((" Using cached address for IP Literal [%s].\n", host.get()));
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
METHOD_LITERAL);
result = rec;
} else if (PR_StringToNetAddr(host, &tempAddr) == PR_SUCCESS) {
} else if (PR_StringToNetAddr(host.get(), &tempAddr) == PR_SUCCESS) {
// try parsing the host name as an IP address literal to short
// circuit full host resolution. (this is necessary on some
// platforms like Win9x. see bug 219376 for more details.)
LOG((" Host is IP Literal [%s].\n", host));
LOG((" Host is IP Literal [%s].\n", host.get()));
// ok, just copy the result into the host record, and be done
// with it! ;-)
rec->addr = MakeUnique<NetAddr>();
@ -828,13 +829,13 @@ nsHostResolver::ResolveHost(const char *host,
!rec->mResolving) {
LOG((" Lookup queue full: dropping %s priority request for "
"host [%s].\n",
IsMediumPriority(flags) ? "medium" : "low", host));
IsMediumPriority(flags) ? "medium" : "low", host.get()));
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2,
METHOD_OVERFLOW);
// This is a lower priority request and we are swamped, so refuse it.
rv = NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
} else if (flags & RES_OFFLINE) {
LOG((" Offline request for host [%s]; ignoring.\n", host));
LOG((" Offline request for host [%s]; ignoring.\n", host.get()));
rv = NS_ERROR_OFFLINE;
} else if (!rec->mResolving) {
// If this is an IPV4 or IPV6 specific request, check if there is
@ -843,7 +844,7 @@ nsHostResolver::ResolveHost(const char *host,
if (!(flags & RES_BYPASS_CACHE) &&
((af == PR_AF_INET) || (af == PR_AF_INET6))) {
// First, search for an entry with AF_UNSPEC
const nsHostKey unspecKey(nsCString(host), flags, PR_AF_UNSPEC,
const nsHostKey unspecKey(host, flags, PR_AF_UNSPEC,
(aOriginAttributes.mPrivateBrowsingId > 0),
originSuffix);
RefPtr<nsHostRecord> unspecRec = mRecordDB.Get(unspecKey);
@ -854,7 +855,7 @@ nsHostResolver::ResolveHost(const char *host,
MOZ_ASSERT(unspecRec->addr_info || unspecRec->negative,
"Entry should be resolved or negative.");
LOG((" Trying AF_UNSPEC entry for host [%s] af: %s.\n", host,
LOG((" Trying AF_UNSPEC entry for host [%s] af: %s.\n", host.get(),
(af == PR_AF_INET) ? "AF_INET" : "AF_INET6"));
// We need to lock in case any other thread is reading
@ -906,7 +907,7 @@ nsHostResolver::ResolveHost(const char *host,
// AF_UNSPEC addresses, so we mark this record as
// negative.
LOG((" No AF_INET6 in AF_UNSPEC entry: "
"host [%s] unknown host.", host));
"host [%s] unknown host.", host.get()));
result = rec;
rec->negative = true;
status = NS_ERROR_UNKNOWN_HOST;
@ -918,7 +919,7 @@ nsHostResolver::ResolveHost(const char *host,
// If no valid address was found in the cache or this is an
// AF_UNSPEC request, then start a new lookup.
if (!result) {
LOG((" No usable address in cache for host [%s].", host));
LOG((" No usable address in cache for host [%s].", host.get()));
if (flags & RES_REFRESH_CACHE) {
rec->Invalidate();
@ -935,7 +936,7 @@ nsHostResolver::ResolveHost(const char *host,
} else {
LOG((" DNS lookup for host [%s] blocking "
"pending 'getaddrinfo' query: callback [%p]",
host, callback.get()));
host.get(), callback.get()));
}
}
} else if (rec->mDidCallbacks) {
@ -944,10 +945,10 @@ nsHostResolver::ResolveHost(const char *host,
result = rec;
// make it count as a hit
Telemetry::Accumulate(Telemetry::DNS_LOOKUP_METHOD2, METHOD_HIT);
LOG((" Host [%s] re-using early TRR resolve data\n", host));
LOG((" Host [%s] re-using early TRR resolve data\n", host.get()));
} else {
LOG((" Host [%s] is being resolved. Appending callback "
"[%p].", host, callback.get()));
"[%p].", host.get(), callback.get()));
rec->mCallbacks.insertBack(callback);
if (rec->onQueue) {
@ -991,7 +992,7 @@ nsHostResolver::ResolveHost(const char *host,
}
void
nsHostResolver::DetachCallback(const char *host,
nsHostResolver::DetachCallback(const nsACString &host,
const OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
@ -1007,7 +1008,7 @@ nsHostResolver::DetachCallback(const char *host,
nsAutoCString originSuffix;
aOriginAttributes.CreateSuffix(originSuffix);
nsHostKey key(nsCString(host), flags, af,
nsHostKey key(host, flags, af,
(aOriginAttributes.mPrivateBrowsingId > 0),
originSuffix);
RefPtr<nsHostRecord> entry = mRecordDB.Get(key);
@ -1266,12 +1267,12 @@ nsHostResolver::NameLookup(nsHostRecord *rec)
}
nsresult
nsHostResolver::ConditionallyRefreshRecord(nsHostRecord *rec, const char *host)
nsHostResolver::ConditionallyRefreshRecord(nsHostRecord *rec, const nsACString &host)
{
if ((rec->CheckExpiration(TimeStamp::NowLoRes()) != nsHostRecord::EXP_VALID
|| rec->negative) && !rec->mResolving) {
LOG((" Using %s cache entry for host [%s] but starting async renewal.",
rec->negative ? "negative" :"positive", host));
rec->negative ? "negative" :"positive", host.BeginReading()));
NameLookup(rec);
if (!rec->negative) {
@ -1704,7 +1705,7 @@ nsHostResolver::CompleteLookup(nsHostRecord* rec, nsresult status, AddrInfo* aNe
}
void
nsHostResolver::CancelAsyncRequest(const char *host,
nsHostResolver::CancelAsyncRequest(const nsACString &host,
const OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
@ -1719,7 +1720,7 @@ nsHostResolver::CancelAsyncRequest(const char *host,
// Lookup the host record associated with host, flags & address family
nsHostKey key(nsCString(host), flags, af,
nsHostKey key(host, flags, af,
(aOriginAttributes.mPrivateBrowsingId > 0),
originSuffix);
RefPtr<nsHostRecord> rec = mRecordDB.Get(key);
@ -1801,12 +1802,12 @@ nsHostResolver::ThreadFunc(void *arg)
TimeStamp startTime = TimeStamp::Now();
bool getTtl = rec->mGetTtl;
nsresult status = GetAddrInfo(rec->host.get(), rec->af,
nsresult status = GetAddrInfo(rec->host, rec->af,
rec->flags, &ai,
getTtl);
#if defined(RES_RETRY_ON_FAILURE)
if (NS_FAILED(status) && rs.Reset()) {
status = GetAddrInfo(rec->host.get(), rec->af,
status = GetAddrInfo(rec->host, rec->af,
rec->flags, &ai, getTtl);
}
#endif

View File

@ -277,7 +277,7 @@ public:
};
virtual LookupStatus CompleteLookup(nsHostRecord *, nsresult, mozilla::net::AddrInfo *, bool pb) = 0;
virtual nsresult GetHostRecord(const char *host,
virtual nsresult GetHostRecord(const nsACString &host,
uint16_t flags, uint16_t af, bool pb,
const nsCString &originSuffix,
nsHostRecord **result)
@ -329,7 +329,7 @@ public:
* host lookup cannot be canceled (cancelation can be layered above this by
* having the callback implementation return without doing anything).
*/
nsresult ResolveHost(const char *hostname,
nsresult ResolveHost(const nsACString &hostname,
const mozilla::OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
@ -341,7 +341,7 @@ public:
* should correspond to the parameters passed to ResolveHost. this function
* executes the callback if the callback is still pending with the given status.
*/
void DetachCallback(const char *hostname,
void DetachCallback(const nsACString &hostname,
const mozilla::OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
@ -355,7 +355,7 @@ public:
* passed to ResolveHost. If this is the last callback associated with the
* host record, it is removed from any request queues it might be on.
*/
void CancelAsyncRequest(const char *host,
void CancelAsyncRequest(const nsACString &host,
const mozilla::OriginAttributes &aOriginAttributes,
uint16_t flags,
uint16_t af,
@ -390,7 +390,7 @@ public:
void FlushCache();
LookupStatus CompleteLookup(nsHostRecord *, nsresult, mozilla::net::AddrInfo *, bool pb) override;
nsresult GetHostRecord(const char *host,
nsresult GetHostRecord(const nsACString &host,
uint16_t flags, uint16_t af, bool pb,
const nsCString &originSuffix,
nsHostRecord **result) override;
@ -425,7 +425,7 @@ private:
* Starts a new lookup in the background for entries that are in the grace
* period with a failed connect or all cached entries are negative.
*/
nsresult ConditionallyRefreshRecord(nsHostRecord *rec, const char *host);
nsresult ConditionallyRefreshRecord(nsHostRecord *rec, const nsACString &host);
static void ThreadFunc(void *);