1 // Copyright 2020 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_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_ 6 #define NET_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_ 7 8 #include <memory> 9 #include <string> 10 #include <string_view> 11 12 #include "net/base/cronet_buildflags.h" 13 #include "net/base/ip_address.h" 14 #include "net/base/ip_endpoint.h" 15 #include "net/base/net_export.h" 16 #include "net/base/scheme_host_port_matcher_result.h" 17 #include "url/gurl.h" 18 19 namespace net { 20 21 // Interface for an individual SchemeHostPortMatcher rule. 22 class NET_EXPORT SchemeHostPortMatcherRule { 23 public: 24 SchemeHostPortMatcherRule() = default; 25 SchemeHostPortMatcherRule(const SchemeHostPortMatcherRule&) = delete; 26 SchemeHostPortMatcherRule& operator=(const SchemeHostPortMatcherRule&) = 27 delete; 28 29 virtual ~SchemeHostPortMatcherRule() = default; 30 31 // Creates a SchemeHostPortMatcherRule by best-effort parsing the string. If 32 // it can't parse, returns a nullptr. It only parses all the rule types in 33 // this header file. Types with other serializations will need to be handled 34 // by the caller. 35 static std::unique_ptr<SchemeHostPortMatcherRule> FromUntrimmedRawString( 36 std::string_view raw_untrimmed); 37 38 // Evaluates the rule against |url|. 39 virtual SchemeHostPortMatcherResult Evaluate(const GURL& url) const = 0; 40 // Returns a string representation of this rule. The returned string will not 41 // match any distinguishable rule of any type. 42 virtual std::string ToString() const = 0; 43 // Returns true if |this| is an instance of 44 // SchemeHostPortMatcherHostnamePatternRule. 45 virtual bool IsHostnamePatternRule() const; 46 47 #if !BUILDFLAG(CRONET_BUILD) 48 // Cronet disables tracing and doesn't provide an implementation of 49 // base::trace_event::EstimateMemoryUsage. Having this conditional is 50 // preferred over a fake implementation to avoid reporting fake metrics. 51 52 // Estimates dynamic memory usage. 53 // See base/trace_event/memory_usage_estimator.h for more info. 54 virtual size_t EstimateMemoryUsage() const; 55 #endif // !BUILDFLAG(CRONET_BUILD) 56 }; 57 58 // Rule that matches URLs with wildcard hostname patterns, and 59 // scheme/port restrictions. 60 // 61 // For example: 62 // *.google.com 63 // https://*.google.com 64 // google.com:443 65 class NET_EXPORT SchemeHostPortMatcherHostnamePatternRule 66 : public SchemeHostPortMatcherRule { 67 public: 68 SchemeHostPortMatcherHostnamePatternRule(const std::string& optional_scheme, 69 const std::string& hostname_pattern, 70 int optional_port); 71 SchemeHostPortMatcherHostnamePatternRule( 72 const SchemeHostPortMatcherHostnamePatternRule&) = delete; 73 SchemeHostPortMatcherHostnamePatternRule& operator=( 74 const SchemeHostPortMatcherHostnamePatternRule&) = delete; 75 76 // SchemeHostPortMatcherRule implementation: 77 SchemeHostPortMatcherResult Evaluate(const GURL& url) const override; 78 std::string ToString() const override; 79 bool IsHostnamePatternRule() const override; 80 81 // Generates a new SchemeHostPortMatcherHostnamePatternRule based on the 82 // current rule. The new rule will do suffix matching if the current rule 83 // doesn't. For example, "google.com" would become "*google.com" and match 84 // "foogoogle.com". 85 std::unique_ptr<SchemeHostPortMatcherHostnamePatternRule> 86 GenerateSuffixMatchingRule() const; 87 88 #if !BUILDFLAG(CRONET_BUILD) 89 // Cronet disables tracing and doesn't provide an implementation of 90 // base::trace_event::EstimateMemoryUsage. Having this conditional is 91 // preferred over a fake implementation to avoid reporting fake metrics. 92 93 // Estimates dynamic memory usage. 94 // See base/trace_event/memory_usage_estimator.h for more info. 95 size_t EstimateMemoryUsage() const override; 96 #endif // !BUILDFLAG(CRONET_BUILD) 97 98 private: 99 const std::string optional_scheme_; 100 const std::string hostname_pattern_; 101 const int optional_port_; 102 }; 103 104 // Rule that matches URLs with IP address as hostname, and scheme/port 105 // restrictions. * only works in the host portion. i18n domain names must be 106 // input in punycode format. 107 // 108 // For example: 109 // 127.0.0.1, 110 // http://127.0.0.1 111 // [::1] 112 // [0:0::1] 113 // http://[::1]:99 114 class NET_EXPORT SchemeHostPortMatcherIPHostRule 115 : public SchemeHostPortMatcherRule { 116 public: 117 SchemeHostPortMatcherIPHostRule(const std::string& optional_scheme, 118 const IPEndPoint& ip_end_point); 119 SchemeHostPortMatcherIPHostRule(const SchemeHostPortMatcherIPHostRule&) = 120 delete; 121 SchemeHostPortMatcherIPHostRule& operator=( 122 const SchemeHostPortMatcherIPHostRule&) = delete; 123 124 // SchemeHostPortMatcherRule implementation: 125 SchemeHostPortMatcherResult Evaluate(const GURL& url) const override; 126 std::string ToString() const override; 127 128 #if !BUILDFLAG(CRONET_BUILD) 129 // Cronet disables tracing and doesn't provide an implementation of 130 // base::trace_event::EstimateMemoryUsage. Having this conditional is 131 // preferred over a fake implementation to avoid reporting fake metrics. 132 133 // Estimates dynamic memory usage. 134 // See base/trace_event/memory_usage_estimator.h for more info. 135 size_t EstimateMemoryUsage() const override; 136 #endif // !BUILDFLAG(CRONET_BUILD) 137 138 private: 139 const std::string optional_scheme_; 140 const std::string ip_host_; 141 const int optional_port_; 142 }; 143 144 // Rule for matching a URL that is an IP address, if that IP address falls 145 // within a certain numeric range. 146 // 147 // For example: 148 // 127.0.0.1/8. 149 // FE80::/10 150 // but not http://127.0.0.1:7/8 or http://[FE80::]/10 (IPv6 with brackets). 151 class NET_EXPORT SchemeHostPortMatcherIPBlockRule 152 : public SchemeHostPortMatcherRule { 153 public: 154 // |ip_prefix| + |prefix_length| define the IP block to match. 155 SchemeHostPortMatcherIPBlockRule(const std::string& description, 156 const std::string& optional_scheme, 157 const IPAddress& ip_prefix, 158 size_t prefix_length_in_bits); 159 SchemeHostPortMatcherIPBlockRule(const SchemeHostPortMatcherIPBlockRule&) = 160 delete; 161 SchemeHostPortMatcherIPBlockRule& operator=( 162 const SchemeHostPortMatcherIPBlockRule&) = delete; 163 164 // SchemeHostPortMatcherRule implementation: 165 SchemeHostPortMatcherResult Evaluate(const GURL& url) const override; 166 std::string ToString() const override; 167 168 #if !BUILDFLAG(CRONET_BUILD) 169 // Cronet disables tracing and doesn't provide an implementation of 170 // base::trace_event::EstimateMemoryUsage. Having this conditional is 171 // preferred over a fake implementation to avoid reporting fake metrics. 172 173 // Estimates dynamic memory usage. 174 // See base/trace_event/memory_usage_estimator.h for more info. 175 size_t EstimateMemoryUsage() const override; 176 #endif // !BUILDFLAG(CRONET_BUILD) 177 178 private: 179 const std::string description_; 180 const std::string optional_scheme_; 181 const IPAddress ip_prefix_; 182 const size_t prefix_length_in_bits_; 183 }; 184 185 } // namespace net 186 187 #endif // NET_BASE_SCHEME_HOST_PORT_MATCHER_RULE_H_ 188