Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: net/base/registry_controlled_domains/registry_controlled_domain.cc

Issue 1303973009: [DO NOT COMMIT] Re-use the dafsa code for s-w-r histograms (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase to fix patch errors on try bots. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // NB: Modelled after Mozilla's code (originally written by Pamela Greene, 5 // NB: Modelled after Mozilla's code (originally written by Pamela Greene,
6 // later modified by others), but almost entirely rewritten for Chrome. 6 // later modified by others), but almost entirely rewritten for Chrome.
7 // (netwerk/dns/src/nsEffectiveTLDService.cpp) 7 // (netwerk/dns/src/nsEffectiveTLDService.cpp)
8 /* ***** BEGIN LICENSE BLOCK ***** 8 /* ***** BEGIN LICENSE BLOCK *****
9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 9 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
10 * 10 *
(...skipping 30 matching lines...) Expand all
41 * the provisions above, a recipient may use your version of this file under 41 * the provisions above, a recipient may use your version of this file under
42 * the terms of any one of the MPL, the GPL or the LGPL. 42 * the terms of any one of the MPL, the GPL or the LGPL.
43 * 43 *
44 * ***** END LICENSE BLOCK ***** */ 44 * ***** END LICENSE BLOCK ***** */
45 45
46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 46 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
47 47
48 #include "base/logging.h" 48 #include "base/logging.h"
49 #include "base/strings/string_util.h" 49 #include "base/strings/string_util.h"
50 #include "base/strings/utf_string_conversions.h" 50 #include "base/strings/utf_string_conversions.h"
51 #include "net/base/dafsa/lookup_string.h"
51 #include "net/base/net_module.h" 52 #include "net/base/net_module.h"
52 #include "net/base/net_util.h" 53 #include "net/base/net_util.h"
53 #include "url/gurl.h" 54 #include "url/gurl.h"
54 #include "url/third_party/mozilla/url_parse.h" 55 #include "url/third_party/mozilla/url_parse.h"
55 56
56 namespace net { 57 namespace net {
57 namespace registry_controlled_domains { 58 namespace registry_controlled_domains {
58 59
59 namespace { 60 namespace {
60 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" 61 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc"
61 62
62 // See make_dafsa.py for documentation of the generated dafsa byte array. 63 // See make_dafsa.py for documentation of the generated dafsa byte array.
63 64
64 const unsigned char* g_graph = kDafsa; 65 const unsigned char* g_graph = kDafsa;
65 size_t g_graph_length = sizeof(kDafsa); 66 size_t g_graph_length = sizeof(kDafsa);
66 67
67 const int kNotFound = -1;
68 const int kExceptionRule = 1;
69 const int kWildcardRule = 2;
70 const int kPrivateRule = 4;
71
72 // Read next offset from pos.
73 // Returns true if an offset could be read, false otherwise.
74 bool GetNextOffset(const unsigned char** pos, const unsigned char* end,
75 const unsigned char** offset) {
76 if (*pos == end)
77 return false;
78
79 // When reading an offset the byte array must always contain at least
80 // three more bytes to consume. First the offset to read, then a node
81 // to skip over and finally a destination node. No object can be smaller
82 // than one byte.
83 CHECK_LT(*pos + 2, end);
84 size_t bytes_consumed;
85 switch (**pos & 0x60) {
86 case 0x60: // Read three byte offset
87 *offset += (((*pos)[0] & 0x1F) << 16) | ((*pos)[1] << 8) | (*pos)[2];
88 bytes_consumed = 3;
89 break;
90 case 0x40: // Read two byte offset
91 *offset += (((*pos)[0] & 0x1F) << 8) | (*pos)[1];
92 bytes_consumed = 2;
93 break;
94 default:
95 *offset += (*pos)[0] & 0x3F;
96 bytes_consumed = 1;
97 }
98 if ((**pos & 0x80) != 0) {
99 *pos = end;
100 } else {
101 *pos += bytes_consumed;
102 }
103 return true;
104 }
105
106 // Check if byte at offset is last in label.
107 bool IsEOL(const unsigned char* offset, const unsigned char* end) {
108 CHECK_LT(offset, end);
109 return (*offset & 0x80) != 0;
110 }
111
112 // Check if byte at offset matches first character in key.
113 // This version matches characters not last in label.
114 bool IsMatch(const unsigned char* offset, const unsigned char* end,
115 const char* key) {
116 CHECK_LT(offset, end);
117 return *offset == *key;
118 }
119
120 // Check if byte at offset matches first character in key.
121 // This version matches characters last in label.
122 bool IsEndCharMatch(const unsigned char* offset, const unsigned char* end,
123 const char* key) {
124 CHECK_LT(offset, end);
125 return *offset == (*key | 0x80);
126 }
127
128 // Read return value at offset.
129 // Returns true if a return value could be read, false otherwise.
130 bool GetReturnValue(const unsigned char* offset, const unsigned char* end,
131 int* return_value) {
132 CHECK_LT(offset, end);
133 if ((*offset & 0xE0) == 0x80) {
134 *return_value = *offset & 0x0F;
135 return true;
136 }
137 return false;
138 }
139
140 // Lookup a domain key in a byte array generated by make_dafsa.py.
141 // The rule type is returned if key is found, otherwise kNotFound is returned.
142 int LookupString(const unsigned char* graph, size_t length, const char* key,
143 size_t key_length) {
144 const unsigned char* pos = graph;
145 const unsigned char* end = graph + length;
146 const unsigned char* offset = pos;
147 const char* key_end = key + key_length;
148 while (GetNextOffset(&pos, end, &offset)) {
149 // char <char>+ end_char offsets
150 // char <char>+ return value
151 // char end_char offsets
152 // char return value
153 // end_char offsets
154 // return_value
155 bool did_consume = false;
156 if (key != key_end && !IsEOL(offset, end)) {
157 // Leading <char> is not a match. Don't dive into this child
158 if (!IsMatch(offset, end, key))
159 continue;
160 did_consume = true;
161 ++offset;
162 ++key;
163 // Possible matches at this point:
164 // <char>+ end_char offsets
165 // <char>+ return value
166 // end_char offsets
167 // return value
168 // Remove all remaining <char> nodes possible
169 while (!IsEOL(offset, end) && key != key_end) {
170 if (!IsMatch(offset, end, key))
171 return kNotFound;
172 ++key;
173 ++offset;
174 }
175 }
176 // Possible matches at this point:
177 // end_char offsets
178 // return_value
179 // If one or more <char> elements were consumed, a failure
180 // to match is terminal. Otherwise, try the next node.
181 if (key == key_end) {
182 int return_value;
183 if (GetReturnValue(offset, end, &return_value))
184 return return_value;
185 // The DAFSA guarantees that if the first char is a match, all
186 // remaining char elements MUST match if the key is truly present.
187 if (did_consume)
188 return kNotFound;
189 continue;
190 }
191 if (!IsEndCharMatch(offset, end, key)) {
192 if (did_consume)
193 return kNotFound; // Unexpected
194 continue;
195 }
196 ++key;
197 pos = ++offset; // Dive into child
198 }
199 return kNotFound; // No match
200 }
201
202 size_t GetRegistryLengthImpl( 68 size_t GetRegistryLengthImpl(
203 const std::string& host, 69 const std::string& host,
204 UnknownRegistryFilter unknown_filter, 70 UnknownRegistryFilter unknown_filter,
205 PrivateRegistryFilter private_filter) { 71 PrivateRegistryFilter private_filter) {
206 DCHECK(!host.empty()); 72 DCHECK(!host.empty());
207 73
208 // Skip leading dots. 74 // Skip leading dots.
209 const size_t host_check_begin = host.find_first_not_of('.'); 75 const size_t host_check_begin = host.find_first_not_of('.');
210 if (host_check_begin == std::string::npos) 76 if (host_check_begin == std::string::npos)
211 return 0; // Host is only dots. 77 return 0; // Host is only dots.
(...skipping 12 matching lines...) Expand all
224 // Walk up the domain tree, most specific to least specific, 90 // Walk up the domain tree, most specific to least specific,
225 // looking for matches at each level. 91 // looking for matches at each level.
226 size_t prev_start = std::string::npos; 92 size_t prev_start = std::string::npos;
227 size_t curr_start = host_check_begin; 93 size_t curr_start = host_check_begin;
228 size_t next_dot = host.find('.', curr_start); 94 size_t next_dot = host.find('.', curr_start);
229 if (next_dot >= host_check_len) // Catches std::string::npos as well. 95 if (next_dot >= host_check_len) // Catches std::string::npos as well.
230 return 0; // This can't have a registry + domain. 96 return 0; // This can't have a registry + domain.
231 while (1) { 97 while (1) {
232 const char* domain_str = host.data() + curr_start; 98 const char* domain_str = host.data() + curr_start;
233 size_t domain_length = host_check_len - curr_start; 99 size_t domain_length = host_check_len - curr_start;
234 int type = LookupString(g_graph, g_graph_length, domain_str, domain_length); 100 int type =
235 bool do_check = 101 LookupStringInDafsa(g_graph, g_graph_length, domain_str, domain_length);
236 type != kNotFound && (!(type & kPrivateRule) || 102 bool do_check = type != kDafsaNotFound &&
237 private_filter == INCLUDE_PRIVATE_REGISTRIES); 103 (!(type & kDafsaPrivateRule) ||
104 private_filter == INCLUDE_PRIVATE_REGISTRIES);
238 105
239 // If the apparent match is a private registry and we're not including 106 // If the apparent match is a private registry and we're not including
240 // those, it can't be an actual match. 107 // those, it can't be an actual match.
241 if (do_check) { 108 if (do_check) {
242 // Exception rules override wildcard rules when the domain is an exact 109 // Exception rules override wildcard rules when the domain is an exact
243 // match, but wildcards take precedence when there's a subdomain. 110 // match, but wildcards take precedence when there's a subdomain.
244 if (type & kWildcardRule && (prev_start != std::string::npos)) { 111 if (type & kDafsaWildcardRule && (prev_start != std::string::npos)) {
245 // If prev_start == host_check_begin, then the host is the registry 112 // If prev_start == host_check_begin, then the host is the registry
246 // itself, so return 0. 113 // itself, so return 0.
247 return (prev_start == host_check_begin) ? 0 114 return (prev_start == host_check_begin) ? 0
248 : (host.length() - prev_start); 115 : (host.length() - prev_start);
249 } 116 }
250 117
251 if (type & kExceptionRule) { 118 if (type & kDafsaExceptionRule) {
252 if (next_dot == std::string::npos) { 119 if (next_dot == std::string::npos) {
253 // If we get here, we had an exception rule with no dots (e.g. 120 // If we get here, we had an exception rule with no dots (e.g.
254 // "!foo"). This would only be valid if we had a corresponding 121 // "!foo"). This would only be valid if we had a corresponding
255 // wildcard rule, which would have to be "*". But we explicitly 122 // wildcard rule, which would have to be "*". But we explicitly
256 // disallow that case, so this kind of rule is invalid. 123 // disallow that case, so this kind of rule is invalid.
257 NOTREACHED() << "Invalid exception rule"; 124 NOTREACHED() << "Invalid exception rule";
258 return 0; 125 return 0;
259 } 126 }
260 return host.length() - next_dot - 1; 127 return host.length() - next_dot - 1;
261 } 128 }
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 253
387 void SetFindDomainGraph(const unsigned char* domains, size_t length) { 254 void SetFindDomainGraph(const unsigned char* domains, size_t length) {
388 CHECK(domains); 255 CHECK(domains);
389 CHECK_NE(length, 0u); 256 CHECK_NE(length, 0u);
390 g_graph = domains; 257 g_graph = domains;
391 g_graph_length = length; 258 g_graph_length = length;
392 } 259 }
393 260
394 } // namespace registry_controlled_domains 261 } // namespace registry_controlled_domains
395 } // namespace net 262 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698