summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authormbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 08:09:40 +0000
committermbelshe@chromium.org <mbelshe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-02 08:09:40 +0000
commit3836871fc5749a9f79290fa97182c45b45d39350 (patch)
tree4867eef8782c8c8f05ea24d1c3bb27246ac2add2 /net
parent9fce5f0576768090f2c3b8f751e6e7c805cc0a6e (diff)
downloadchromium_src-3836871fc5749a9f79290fa97182c45b45d39350.zip
chromium_src-3836871fc5749a9f79290fa97182c45b45d39350.tar.gz
chromium_src-3836871fc5749a9f79290fa97182c45b45d39350.tar.bz2
Add a flag to a host resolver request to request that resolution only
be done from the cache (i.e. don't do a network lookup, and return results only synchronously). BUG=none TEST=HostResolverImplTest::DisallowNonCachedResponses Review URL: http://codereview.chromium.org/6602038 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@76515 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/base/host_resolver.cc1
-rw-r--r--net/base/host_resolver.h6
-rw-r--r--net/base/host_resolver_impl.cc12
-rw-r--r--net/base/host_resolver_impl_unittest.cc37
4 files changed, 56 insertions, 0 deletions
diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc
index 49678a2..592b03c 100644
--- a/net/base/host_resolver.cc
+++ b/net/base/host_resolver.cc
@@ -15,6 +15,7 @@ HostResolver::RequestInfo::RequestInfo(const HostPortPair& host_port_pair)
address_family_(ADDRESS_FAMILY_UNSPECIFIED),
host_resolver_flags_(0),
allow_cached_response_(true),
+ only_use_cached_response_(false),
is_speculative_(false),
priority_(MEDIUM) {
}
diff --git a/net/base/host_resolver.h b/net/base/host_resolver.h
index 4fc5c89..6a97b46 100644
--- a/net/base/host_resolver.h
+++ b/net/base/host_resolver.h
@@ -63,6 +63,9 @@ class HostResolver {
bool allow_cached_response() const { return allow_cached_response_; }
void set_allow_cached_response(bool b) { allow_cached_response_ = b; }
+ bool only_use_cached_response() const { return only_use_cached_response_; }
+ void set_only_use_cached_response(bool b) { only_use_cached_response_ = b; }
+
bool is_speculative() const { return is_speculative_; }
void set_is_speculative(bool b) { is_speculative_ = b; }
@@ -85,6 +88,9 @@ class HostResolver {
// Whether it is ok to return a result from the host cache.
bool allow_cached_response_;
+ // Whether the response will only use the cache.
+ bool only_use_cached_response_;
+
// Whether this request was started by the DNS prefetcher.
bool is_speculative_;
diff --git a/net/base/host_resolver_impl.cc b/net/base/host_resolver_impl.cc
index 7df346f..b22eee8 100644
--- a/net/base/host_resolver_impl.cc
+++ b/net/base/host_resolver_impl.cc
@@ -161,6 +161,8 @@ class RequestInfoParameters : public NetLog::EventParameters {
dict->SetInteger("address_family",
static_cast<int>(info_.address_family()));
dict->SetBoolean("allow_cached_response", info_.allow_cached_response());
+ dict->SetBoolean("only_use_cached_response",
+ info_.only_use_cached_response());
dict->SetBoolean("is_speculative", info_.is_speculative());
dict->SetInteger("priority", info_.priority());
@@ -1028,6 +1030,16 @@ int HostResolverImpl::Resolve(const RequestInfo& info,
}
}
+ if (info.only_use_cached_response()) { // Not allowed to do a real lookup.
+ OnFinishRequest(source_net_log,
+ request_net_log,
+ request_id,
+ info,
+ ERR_NAME_NOT_RESOLVED,
+ 0);
+ return ERR_NAME_NOT_RESOLVED;
+ }
+
// If no callback was specified, do a synchronous resolution.
if (!callback) {
AddressList addrlist;
diff --git a/net/base/host_resolver_impl_unittest.cc b/net/base/host_resolver_impl_unittest.cc
index 8d418e9..41e8b91 100644
--- a/net/base/host_resolver_impl_unittest.cc
+++ b/net/base/host_resolver_impl_unittest.cc
@@ -1657,6 +1657,43 @@ TEST_F(HostResolverImplTest, SetDefaultAddressFamily_Synchronous) {
EXPECT_EQ("192.1.98.1", NetAddressToString(addrlist[3].head()));
}
+TEST_F(HostResolverImplTest, DisallowNonCachedResponses) {
+ AddressList addrlist;
+ const int kPortnum = 80;
+
+ scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
+ new RuleBasedHostResolverProc(NULL));
+ resolver_proc->AddRule("just.testing", "192.168.1.42");
+
+ scoped_ptr<HostResolver> host_resolver(
+ CreateHostResolverImpl(resolver_proc));
+
+ // First hit will miss the cache.
+ HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
+ info.set_only_use_cached_response(true);
+ CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
+ int err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound());
+ EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err);
+
+ // This time, we fetch normally.
+ info.set_only_use_cached_response(false);
+ err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound());
+ EXPECT_EQ(OK, err);
+
+ // Now we should be able to fetch from the cache.
+ info.set_only_use_cached_response(true);
+ err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound());
+ EXPECT_EQ(OK, err);
+
+ const struct addrinfo* ainfo = addrlist.head();
+ EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
+ EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
+
+ const struct sockaddr* sa = ainfo->ai_addr;
+ const struct sockaddr_in* sa_in = reinterpret_cast<const sockaddr_in*>(sa);
+ EXPECT_EQ(htons(kPortnum), sa_in->sin_port);
+ EXPECT_EQ(htonl(0xc0a8012a), sa_in->sin_addr.s_addr);
+}
// TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
} // namespace