host_mapping_rules.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2011 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_BASE_HOST_MAPPING_RULES_H_
  5. #define NET_BASE_HOST_MAPPING_RULES_H_
  6. #include <vector>
  7. #include "base/strings/string_piece.h"
  8. #include "net/base/net_export.h"
  9. class GURL;
  10. namespace net {
  11. class HostPortPair;
  12. class NET_EXPORT_PRIVATE HostMappingRules {
  13. public:
  14. enum class RewriteResult {
  15. kRewritten,
  16. kNoMatchingRule,
  17. kInvalidRewrite,
  18. };
  19. HostMappingRules();
  20. HostMappingRules(const HostMappingRules& host_mapping_rules);
  21. ~HostMappingRules();
  22. HostMappingRules& operator=(const HostMappingRules& host_mapping_rules);
  23. // Modifies `*host_port` based on the current rules. Returns true if
  24. // `*host_port` was modified, false otherwise.
  25. bool RewriteHost(HostPortPair* host_port) const;
  26. // Modifies the host and port of `url` based on current rules. May only be
  27. // called for URLs with a host and a scheme that is standard, and if the
  28. // scheme does not allow ports, only the host will be rewritten.
  29. //
  30. // If `url` is rewritten, returns `kRewritten`. If no matching rule is found,
  31. // returns `kNoMatchingRule` and `url` is not modified. If a matching rule is
  32. // found but it results in an invalid URL, e.g. if the rule maps to
  33. // "~NOTFOUND", returns `kInvalidRewrite` and `url` is not modified.
  34. RewriteResult RewriteUrl(GURL& url) const;
  35. // Adds a rule to this mapper. The format of the rule can be one of:
  36. //
  37. // "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
  38. // "EXCLUDE" <hostname_pattern>
  39. //
  40. // The <replacement_host> can be either a hostname, or an IP address literal.
  41. //
  42. // Returns true if the rule was successfully parsed and added.
  43. bool AddRuleFromString(base::StringPiece rule_string);
  44. // Sets the rules from a comma separated list of rules.
  45. void SetRulesFromString(base::StringPiece rules_string);
  46. private:
  47. struct MapRule;
  48. struct ExclusionRule;
  49. typedef std::vector<MapRule> MapRuleList;
  50. typedef std::vector<ExclusionRule> ExclusionRuleList;
  51. MapRuleList map_rules_;
  52. ExclusionRuleList exclusion_rules_;
  53. };
  54. } // namespace net
  55. #endif // NET_BASE_HOST_MAPPING_RULES_H_