OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/host_mapping_rules.h" | 5 #include "net/base/host_mapping_rules.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_split.h" | 8 #include "base/strings/string_split.h" |
9 #include "base/strings/string_tokenizer.h" | 9 #include "base/strings/string_tokenizer.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "net/base/host_port_pair.h" | 11 #include "net/base/host_port_pair.h" |
12 #include "net/base/net_util.h" | 12 #include "net/base/net_util.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 struct HostMappingRules::MapRule { | 16 struct HostMappingRules::MapRule { |
17 MapRule() : replacement_port(-1) {} | 17 MapRule() : replacement_port(-1) {} |
18 | 18 |
19 std::string hostname_pattern; | 19 std::string hostname_pattern; |
20 std::string replacement_hostname; | 20 std::string replacement_hostname; |
21 int replacement_port; | 21 int replacement_port; |
22 }; | 22 }; |
23 | 23 |
24 struct HostMappingRules::ExclusionRule { | 24 struct HostMappingRules::ExclusionRule { |
25 std::string hostname_pattern; | 25 std::string hostname_pattern; |
26 }; | 26 }; |
27 | 27 |
28 HostMappingRules::HostMappingRules() {} | 28 HostMappingRules::HostMappingRules() { |
| 29 } |
29 | 30 |
30 HostMappingRules::~HostMappingRules() {} | 31 HostMappingRules::~HostMappingRules() { |
| 32 } |
31 | 33 |
32 bool HostMappingRules::RewriteHost(HostPortPair* host_port) const { | 34 bool HostMappingRules::RewriteHost(HostPortPair* host_port) const { |
33 // Check if the hostname was excluded. | 35 // Check if the hostname was excluded. |
34 for (ExclusionRuleList::const_iterator it = exclusion_rules_.begin(); | 36 for (ExclusionRuleList::const_iterator it = exclusion_rules_.begin(); |
35 it != exclusion_rules_.end(); ++it) { | 37 it != exclusion_rules_.end(); |
| 38 ++it) { |
36 const ExclusionRule& rule = *it; | 39 const ExclusionRule& rule = *it; |
37 if (MatchPattern(host_port->host(), rule.hostname_pattern)) | 40 if (MatchPattern(host_port->host(), rule.hostname_pattern)) |
38 return false; | 41 return false; |
39 } | 42 } |
40 | 43 |
41 // Check if the hostname was remapped. | 44 // Check if the hostname was remapped. |
42 for (MapRuleList::const_iterator it = map_rules_.begin(); | 45 for (MapRuleList::const_iterator it = map_rules_.begin(); |
43 it != map_rules_.end(); ++it) { | 46 it != map_rules_.end(); |
| 47 ++it) { |
44 const MapRule& rule = *it; | 48 const MapRule& rule = *it; |
45 | 49 |
46 // The rule's hostname_pattern will be something like: | 50 // The rule's hostname_pattern will be something like: |
47 // www.foo.com | 51 // www.foo.com |
48 // *.foo.com | 52 // *.foo.com |
49 // www.foo.com:1234 | 53 // www.foo.com:1234 |
50 // *.foo.com:1234 | 54 // *.foo.com:1234 |
51 // First, we'll check for a match just on hostname. | 55 // First, we'll check for a match just on hostname. |
52 // If that fails, we'll check for a match with both hostname and port. | 56 // If that fails, we'll check for a match with both hostname and port. |
53 if (!MatchPattern(host_port->host(), rule.hostname_pattern)) { | 57 if (!MatchPattern(host_port->host(), rule.hostname_pattern)) { |
(...skipping 23 matching lines...) Expand all Loading... |
77 rule.hostname_pattern = StringToLowerASCII(parts[1]); | 81 rule.hostname_pattern = StringToLowerASCII(parts[1]); |
78 exclusion_rules_.push_back(rule); | 82 exclusion_rules_.push_back(rule); |
79 return true; | 83 return true; |
80 } | 84 } |
81 | 85 |
82 // Test for MAP rule. | 86 // Test for MAP rule. |
83 if (parts.size() == 3 && LowerCaseEqualsASCII(parts[0], "map")) { | 87 if (parts.size() == 3 && LowerCaseEqualsASCII(parts[0], "map")) { |
84 MapRule rule; | 88 MapRule rule; |
85 rule.hostname_pattern = StringToLowerASCII(parts[1]); | 89 rule.hostname_pattern = StringToLowerASCII(parts[1]); |
86 | 90 |
87 if (!ParseHostAndPort(parts[2], &rule.replacement_hostname, | 91 if (!ParseHostAndPort( |
88 &rule.replacement_port)) { | 92 parts[2], &rule.replacement_hostname, &rule.replacement_port)) { |
89 return false; // Failed parsing the hostname/port. | 93 return false; // Failed parsing the hostname/port. |
90 } | 94 } |
91 | 95 |
92 map_rules_.push_back(rule); | 96 map_rules_.push_back(rule); |
93 return true; | 97 return true; |
94 } | 98 } |
95 | 99 |
96 return false; | 100 return false; |
97 } | 101 } |
98 | 102 |
99 void HostMappingRules::SetRulesFromString(const std::string& rules_string) { | 103 void HostMappingRules::SetRulesFromString(const std::string& rules_string) { |
100 exclusion_rules_.clear(); | 104 exclusion_rules_.clear(); |
101 map_rules_.clear(); | 105 map_rules_.clear(); |
102 | 106 |
103 base::StringTokenizer rules(rules_string, ","); | 107 base::StringTokenizer rules(rules_string, ","); |
104 while (rules.GetNext()) { | 108 while (rules.GetNext()) { |
105 bool ok = AddRuleFromString(rules.token()); | 109 bool ok = AddRuleFromString(rules.token()); |
106 LOG_IF(ERROR, !ok) << "Failed parsing rule: " << rules.token(); | 110 LOG_IF(ERROR, !ok) << "Failed parsing rule: " << rules.token(); |
107 } | 111 } |
108 } | 112 } |
109 | 113 |
110 } // namespace net | 114 } // namespace net |
OLD | NEW |