xref: /aosp_15_r20/external/cronet/net/base/host_mapping_rules.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2011 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_BASE_HOST_MAPPING_RULES_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_BASE_HOST_MAPPING_RULES_H_
7*6777b538SAndroid Build Coastguard Worker 
8*6777b538SAndroid Build Coastguard Worker #include <string_view>
9*6777b538SAndroid Build Coastguard Worker #include <vector>
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
12*6777b538SAndroid Build Coastguard Worker 
13*6777b538SAndroid Build Coastguard Worker class GURL;
14*6777b538SAndroid Build Coastguard Worker 
15*6777b538SAndroid Build Coastguard Worker namespace net {
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker class HostPortPair;
18*6777b538SAndroid Build Coastguard Worker 
19*6777b538SAndroid Build Coastguard Worker class NET_EXPORT_PRIVATE HostMappingRules {
20*6777b538SAndroid Build Coastguard Worker  public:
21*6777b538SAndroid Build Coastguard Worker   enum class RewriteResult {
22*6777b538SAndroid Build Coastguard Worker     kRewritten,
23*6777b538SAndroid Build Coastguard Worker     kNoMatchingRule,
24*6777b538SAndroid Build Coastguard Worker     kInvalidRewrite,
25*6777b538SAndroid Build Coastguard Worker   };
26*6777b538SAndroid Build Coastguard Worker 
27*6777b538SAndroid Build Coastguard Worker   HostMappingRules();
28*6777b538SAndroid Build Coastguard Worker   HostMappingRules(const HostMappingRules& host_mapping_rules);
29*6777b538SAndroid Build Coastguard Worker   ~HostMappingRules();
30*6777b538SAndroid Build Coastguard Worker 
31*6777b538SAndroid Build Coastguard Worker   HostMappingRules& operator=(const HostMappingRules& host_mapping_rules);
32*6777b538SAndroid Build Coastguard Worker 
33*6777b538SAndroid Build Coastguard Worker   // Modifies `*host_port` based on the current rules. Returns true if
34*6777b538SAndroid Build Coastguard Worker   // `*host_port` was modified, false otherwise.
35*6777b538SAndroid Build Coastguard Worker   bool RewriteHost(HostPortPair* host_port) const;
36*6777b538SAndroid Build Coastguard Worker 
37*6777b538SAndroid Build Coastguard Worker   // Modifies the host and port of `url` based on current rules. May only be
38*6777b538SAndroid Build Coastguard Worker   // called for URLs with a host and a scheme that is standard, and if the
39*6777b538SAndroid Build Coastguard Worker   // scheme does not allow ports, only the host will be rewritten.
40*6777b538SAndroid Build Coastguard Worker   //
41*6777b538SAndroid Build Coastguard Worker   // If `url` is rewritten, returns `kRewritten`. If no matching rule is found,
42*6777b538SAndroid Build Coastguard Worker   // returns `kNoMatchingRule` and `url` is not modified. If a matching rule is
43*6777b538SAndroid Build Coastguard Worker   // found but it results in an invalid URL, e.g. if the rule maps to
44*6777b538SAndroid Build Coastguard Worker   // "^NOTFOUND", returns `kInvalidRewrite` and `url` is not modified.
45*6777b538SAndroid Build Coastguard Worker   RewriteResult RewriteUrl(GURL& url) const;
46*6777b538SAndroid Build Coastguard Worker 
47*6777b538SAndroid Build Coastguard Worker   // Adds a rule to this mapper. The format of the rule can be one of:
48*6777b538SAndroid Build Coastguard Worker   //
49*6777b538SAndroid Build Coastguard Worker   //   "MAP" <hostname_pattern> <replacement_host> [":" <replacement_port>]
50*6777b538SAndroid Build Coastguard Worker   //   "EXCLUDE" <hostname_pattern>
51*6777b538SAndroid Build Coastguard Worker   //
52*6777b538SAndroid Build Coastguard Worker   // The <replacement_host> can be either a hostname, or an IP address literal.
53*6777b538SAndroid Build Coastguard Worker   //
54*6777b538SAndroid Build Coastguard Worker   // Returns true if the rule was successfully parsed and added.
55*6777b538SAndroid Build Coastguard Worker   bool AddRuleFromString(std::string_view rule_string);
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker   // Sets the rules from a comma separated list of rules.
58*6777b538SAndroid Build Coastguard Worker   void SetRulesFromString(std::string_view rules_string);
59*6777b538SAndroid Build Coastguard Worker 
60*6777b538SAndroid Build Coastguard Worker  private:
61*6777b538SAndroid Build Coastguard Worker   struct MapRule;
62*6777b538SAndroid Build Coastguard Worker   struct ExclusionRule;
63*6777b538SAndroid Build Coastguard Worker 
64*6777b538SAndroid Build Coastguard Worker   typedef std::vector<MapRule> MapRuleList;
65*6777b538SAndroid Build Coastguard Worker   typedef std::vector<ExclusionRule> ExclusionRuleList;
66*6777b538SAndroid Build Coastguard Worker 
67*6777b538SAndroid Build Coastguard Worker   MapRuleList map_rules_;
68*6777b538SAndroid Build Coastguard Worker   ExclusionRuleList exclusion_rules_;
69*6777b538SAndroid Build Coastguard Worker };
70*6777b538SAndroid Build Coastguard Worker 
71*6777b538SAndroid Build Coastguard Worker }  // namespace net
72*6777b538SAndroid Build Coastguard Worker 
73*6777b538SAndroid Build Coastguard Worker #endif  // NET_BASE_HOST_MAPPING_RULES_H_
74