1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef NET_BASE_MAPPED_HOST_RESOLVER_H_
6 #define NET_BASE_MAPPED_HOST_RESOLVER_H_
11 #include "base/ref_counted.h"
12 #include "net/base/host_resolver.h"
16 // This class wraps an existing HostResolver instance, but modifies the
17 // request before passing it off to |impl|. This is different from
18 // MockHostResolver which does the remapping at the HostResolverProc
19 // layer, so it is able to preserve the effectiveness of the cache.
20 class MappedHostResolver
: public HostResolver
{
22 // Creates a MappedHostResolver that forwards all of its requests through
24 explicit MappedHostResolver(HostResolver
* impl
);
26 // HostResolver methods:
27 virtual int Resolve(const RequestInfo
& info
,
28 AddressList
* addresses
,
29 CompletionCallback
* callback
,
30 RequestHandle
* out_req
,
32 virtual void CancelRequest(RequestHandle req
);
33 virtual void AddObserver(Observer
* observer
);
34 virtual void RemoveObserver(Observer
* observer
);
35 virtual HostResolverImpl
* GetAsHostResolverImpl();
37 // Adds a rule to this mapper. The format of the rule can be one of:
39 // "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
40 // "EXCLUDE" <hostname_pattern>
42 // The <replacement_host> can be either a hostname, or an IP address literal.
44 // Returns true if the rule was successfully parsed and added.
45 bool AddRuleFromString(const std::string
& rule_string
);
47 // Takes a comma separated list of rules, and assigns them to this resolver.
48 void SetRulesFromString(const std::string
& rules_string
);
52 MapRule() : replacement_port(-1) {}
54 std::string hostname_pattern
;
55 std::string replacement_hostname
;
59 struct ExclusionRule
{
60 std::string hostname_pattern
;
63 typedef std::vector
<MapRule
> MapRuleList
;
64 typedef std::vector
<ExclusionRule
> ExclusionRuleList
;
66 virtual ~MappedHostResolver();
68 // Modifies |*info| based on the current rules. Returns true if the
69 // RequestInfo was modified, false otherwise.
70 bool RewriteRequest(RequestInfo
* info
) const;
72 scoped_refptr
<HostResolver
> impl_
;
74 MapRuleList map_rules_
;
75 ExclusionRuleList exclusion_rules_
;
80 #endif // NET_BASE_MAPPED_HOST_RESOLVER_H_