mapped_host_resolver.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2012 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.
  4. #ifndef NET_DNS_MAPPED_HOST_RESOLVER_H_
  5. #define NET_DNS_MAPPED_HOST_RESOLVER_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/strings/string_piece.h"
  9. #include "net/base/completion_once_callback.h"
  10. #include "net/base/host_mapping_rules.h"
  11. #include "net/base/net_export.h"
  12. #include "net/base/network_isolation_key.h"
  13. #include "net/dns/dns_config.h"
  14. #include "net/dns/host_resolver.h"
  15. #include "net/log/net_log_with_source.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. #include "url/scheme_host_port.h"
  18. namespace net {
  19. // This class wraps an existing HostResolver instance, but modifies the
  20. // request before passing it off to |impl|. This is different from
  21. // MockHostResolver which does the remapping at the HostResolverProc
  22. // layer, so it is able to preserve the effectiveness of the cache.
  23. class NET_EXPORT MappedHostResolver : public HostResolver {
  24. public:
  25. // Creates a MappedHostResolver that forwards all of its requests through
  26. // |impl|.
  27. explicit MappedHostResolver(std::unique_ptr<HostResolver> impl);
  28. ~MappedHostResolver() override;
  29. void OnShutdown() override;
  30. // Adds a rule to this mapper. The format of the rule can be one of:
  31. //
  32. // "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
  33. // "EXCLUDE" <hostname_pattern>
  34. //
  35. // The <replacement_host> can be either a hostname, or an IP address literal,
  36. // or "~NOTFOUND". If it is "~NOTFOUND" then all matched hostnames will fail
  37. // to be resolved with ERR_NAME_NOT_RESOLVED.
  38. //
  39. // Returns true if the rule was successfully parsed and added.
  40. bool AddRuleFromString(base::StringPiece rule_string) {
  41. return rules_.AddRuleFromString(rule_string);
  42. }
  43. // Takes a comma separated list of rules, and assigns them to this resolver.
  44. void SetRulesFromString(base::StringPiece rules_string) {
  45. rules_.SetRulesFromString(rules_string);
  46. }
  47. // HostResolver methods:
  48. std::unique_ptr<ResolveHostRequest> CreateRequest(
  49. url::SchemeHostPort host,
  50. NetworkIsolationKey network_isolation_key,
  51. NetLogWithSource net_log,
  52. absl::optional<ResolveHostParameters> optional_parameters) override;
  53. std::unique_ptr<ResolveHostRequest> CreateRequest(
  54. const HostPortPair& host,
  55. const NetworkIsolationKey& network_isolation_key,
  56. const NetLogWithSource& net_log,
  57. const absl::optional<ResolveHostParameters>& optional_parameters)
  58. override;
  59. std::unique_ptr<ProbeRequest> CreateDohProbeRequest() override;
  60. HostCache* GetHostCache() override;
  61. base::Value GetDnsConfigAsValue() const override;
  62. void SetRequestContext(URLRequestContext* request_context) override;
  63. HostResolverManager* GetManagerForTesting() override;
  64. private:
  65. std::unique_ptr<HostResolver> impl_;
  66. HostMappingRules rules_;
  67. };
  68. } // namespace net
  69. #endif // NET_DNS_MAPPED_HOST_RESOLVER_H_