proxy_bypass_rules.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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_PROXY_RESOLUTION_PROXY_BYPASS_RULES_H_
  5. #define NET_PROXY_RESOLUTION_PROXY_BYPASS_RULES_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/strings/string_piece_forward.h"
  10. #include "net/base/net_export.h"
  11. #include "net/base/scheme_host_port_matcher.h"
  12. #include "net/base/scheme_host_port_matcher_rule.h"
  13. #include "url/gurl.h"
  14. namespace net {
  15. // ProxyBypassRules describes the set of URLs that should bypass the use of a
  16. // proxy.
  17. //
  18. // The rules are expressed as an ordered list of rules, which can be thought of
  19. // as being evaluated left-to-right. Order only matters when mixing "negative
  20. // rules" with "positive rules". For more details see the comments in
  21. // ProxyBypassRules::Matches().
  22. //
  23. // This rule list is serializable to a string (either comma or semi-colon
  24. // separated), which has similar semantics across platforms.
  25. //
  26. // When evalutating ProxyBypassRules there are some implicitly applied rules
  27. // when the URL does not match any of the explicit rules. See
  28. // MatchesImplicitRules() for details.
  29. class NET_EXPORT ProxyBypassRules {
  30. public:
  31. // Note: This class supports copy constructor and assignment.
  32. ProxyBypassRules();
  33. ProxyBypassRules(const ProxyBypassRules& rhs);
  34. ProxyBypassRules(ProxyBypassRules&& rhs);
  35. ~ProxyBypassRules();
  36. ProxyBypassRules& operator=(const ProxyBypassRules& rhs);
  37. ProxyBypassRules& operator=(ProxyBypassRules&& rhs);
  38. // Returns the current list of rules. The rules list contains pointers
  39. // which are owned by this class, callers should NOT keep references
  40. // or delete them.
  41. const SchemeHostPortMatcher::RuleList& rules() const {
  42. return matcher_.rules();
  43. }
  44. // Replace rule on |index| in the internal RuleList.
  45. void ReplaceRule(size_t index,
  46. std::unique_ptr<SchemeHostPortMatcherRule> rule);
  47. // Returns true if the bypass rules indicate that |url| should bypass the
  48. // proxy. Matching is done using both the explicit rules, as well as a
  49. // set of global implicit rules.
  50. //
  51. // If |reverse| is set to true then the bypass
  52. // rule list is inverted (this is almost equivalent to negating the result of
  53. // Matches(), except for implicit matches).
  54. bool Matches(const GURL& url, bool reverse = false) const;
  55. // Returns true if |*this| has the same serialized list of rules as |other|.
  56. bool operator==(const ProxyBypassRules& other) const;
  57. // Initializes the list of rules by parsing the string |raw|. |raw| is a
  58. // comma separated or semi-colon separated list of rules. See
  59. // AddRuleFromString() to see the specific rule grammar.
  60. void ParseFromString(const std::string& raw);
  61. // Adds a rule to the front of thelist that bypasses hostnames without a dot
  62. // in them (and is not an IP literal), which can be indicative of intranet
  63. // websites.
  64. //
  65. // On Windows this corresponds to the "Bypass proxy server for local
  66. // addresses" settings checkbox, and on macOS the "Exclude simple hostnames"
  67. // checkbox.
  68. void PrependRuleToBypassSimpleHostnames();
  69. // Adds a rule given by the string |raw|. The format of |raw| can be any of
  70. // the following:
  71. //
  72. // Returns true if the rule was successfully added.
  73. //
  74. // For the supported format of bypass rules see //net/docs/proxy.md.
  75. bool AddRuleFromString(base::StringPiece raw);
  76. // Appends rules that "cancels out" the implicit bypass rules. See
  77. // GetRulesToSubtractImplicit() for details.
  78. void AddRulesToSubtractImplicit();
  79. // Returns a list of bypass rules that "cancels out" the implicit bypass
  80. // rules.
  81. //
  82. // The current set of implicit bypass rules are localhost and link-local
  83. // addresses, and are subtracted using <-loopback> (an idiom from Windows),
  84. // however this could change.
  85. //
  86. // If using this for tests, see https://crbug.com/901896.
  87. static std::string GetRulesToSubtractImplicit();
  88. // Converts the rules to a string representation (ParseFormat::kDefault).
  89. std::string ToString() const;
  90. // Removes all the rules.
  91. void Clear();
  92. // Returns true if |url| matches one of the implicit proxy bypass rules
  93. // (localhost or link local).
  94. static bool MatchesImplicitRules(const GURL& url);
  95. // The delimiter used by |ToString()| for the string representation of the
  96. // proxy bypass rules.
  97. constexpr static char kBypassListDelimeter[] = ";";
  98. private:
  99. SchemeHostPortMatcher matcher_;
  100. };
  101. } // namespace net
  102. #endif // NET_PROXY_RESOLUTION_PROXY_BYPASS_RULES_H_