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