| OLD | NEW |
| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 const unsigned char* g_graph = kDafsa; | 64 const unsigned char* g_graph = kDafsa; |
| 65 size_t g_graph_length = sizeof(kDafsa); | 65 size_t g_graph_length = sizeof(kDafsa); |
| 66 | 66 |
| 67 const int kNotFound = -1; | 67 const int kNotFound = -1; |
| 68 const int kExceptionRule = 1; | 68 const int kExceptionRule = 1; |
| 69 const int kWildcardRule = 2; | 69 const int kWildcardRule = 2; |
| 70 const int kPrivateRule = 4; | 70 const int kPrivateRule = 4; |
| 71 | 71 |
| 72 // Read next offset from pos. | 72 // Read next offset from pos. |
| 73 // Returns true if an offset could be read, false otherwise. | 73 // Returns true if an offset could be read, false otherwise. |
| 74 bool GetNextOffset(const unsigned char** pos, const unsigned char* end, | 74 bool GetNextOffset(const unsigned char** pos, |
| 75 const unsigned char* end, |
| 75 const unsigned char** offset) { | 76 const unsigned char** offset) { |
| 76 if (*pos == end) | 77 if (*pos == end) |
| 77 return false; | 78 return false; |
| 78 | 79 |
| 79 // When reading an offset the byte array must always contain at least | 80 // 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 // 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 // to skip over and finally a destination node. No object can be smaller |
| 82 // than one byte. | 83 // than one byte. |
| 83 CHECK_LT(*pos + 2, end); | 84 CHECK_LT(*pos + 2, end); |
| 84 size_t bytes_consumed; | 85 size_t bytes_consumed; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 104 } | 105 } |
| 105 | 106 |
| 106 // Check if byte at offset is last in label. | 107 // Check if byte at offset is last in label. |
| 107 bool IsEOL(const unsigned char* offset, const unsigned char* end) { | 108 bool IsEOL(const unsigned char* offset, const unsigned char* end) { |
| 108 CHECK_LT(offset, end); | 109 CHECK_LT(offset, end); |
| 109 return (*offset & 0x80) != 0; | 110 return (*offset & 0x80) != 0; |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Check if byte at offset matches first character in key. | 113 // Check if byte at offset matches first character in key. |
| 113 // This version matches characters not last in label. | 114 // This version matches characters not last in label. |
| 114 bool IsMatch(const unsigned char* offset, const unsigned char* end, | 115 bool IsMatch(const unsigned char* offset, |
| 116 const unsigned char* end, |
| 115 const char* key) { | 117 const char* key) { |
| 116 CHECK_LT(offset, end); | 118 CHECK_LT(offset, end); |
| 117 return *offset == *key; | 119 return *offset == *key; |
| 118 } | 120 } |
| 119 | 121 |
| 120 // Check if byte at offset matches first character in key. | 122 // Check if byte at offset matches first character in key. |
| 121 // This version matches characters last in label. | 123 // This version matches characters last in label. |
| 122 bool IsEndCharMatch(const unsigned char* offset, const unsigned char* end, | 124 bool IsEndCharMatch(const unsigned char* offset, |
| 125 const unsigned char* end, |
| 123 const char* key) { | 126 const char* key) { |
| 124 CHECK_LT(offset, end); | 127 CHECK_LT(offset, end); |
| 125 return *offset == (*key | 0x80); | 128 return *offset == (*key | 0x80); |
| 126 } | 129 } |
| 127 | 130 |
| 128 // Read return value at offset. | 131 // Read return value at offset. |
| 129 // Returns true if a return value could be read, false otherwise. | 132 // Returns true if a return value could be read, false otherwise. |
| 130 bool GetReturnValue(const unsigned char* offset, const unsigned char* end, | 133 bool GetReturnValue(const unsigned char* offset, |
| 134 const unsigned char* end, |
| 131 int* return_value) { | 135 int* return_value) { |
| 132 CHECK_LT(offset, end); | 136 CHECK_LT(offset, end); |
| 133 if ((*offset & 0xE0) == 0x80) { | 137 if ((*offset & 0xE0) == 0x80) { |
| 134 *return_value = *offset & 0x0F; | 138 *return_value = *offset & 0x0F; |
| 135 return true; | 139 return true; |
| 136 } | 140 } |
| 137 return false; | 141 return false; |
| 138 } | 142 } |
| 139 | 143 |
| 140 // Lookup a domain key in a byte array generated by make_dafsa.py. | 144 // 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. | 145 // 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, | 146 int LookupString(const unsigned char* graph, |
| 147 size_t length, |
| 148 const char* key, |
| 143 size_t key_length) { | 149 size_t key_length) { |
| 144 const unsigned char* pos = graph; | 150 const unsigned char* pos = graph; |
| 145 const unsigned char* end = graph + length; | 151 const unsigned char* end = graph + length; |
| 146 const unsigned char* offset = pos; | 152 const unsigned char* offset = pos; |
| 147 const char* key_end = key + key_length; | 153 const char* key_end = key + key_length; |
| 148 while (GetNextOffset(&pos, end, &offset)) { | 154 while (GetNextOffset(&pos, end, &offset)) { |
| 149 // char <char>+ end_char offsets | 155 // char <char>+ end_char offsets |
| 150 // char <char>+ return value | 156 // char <char>+ return value |
| 151 // char end_char offsets | 157 // char end_char offsets |
| 152 // char return value | 158 // char return value |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (did_consume) | 198 if (did_consume) |
| 193 return kNotFound; // Unexpected | 199 return kNotFound; // Unexpected |
| 194 continue; | 200 continue; |
| 195 } | 201 } |
| 196 ++key; | 202 ++key; |
| 197 pos = ++offset; // Dive into child | 203 pos = ++offset; // Dive into child |
| 198 } | 204 } |
| 199 return kNotFound; // No match | 205 return kNotFound; // No match |
| 200 } | 206 } |
| 201 | 207 |
| 202 size_t GetRegistryLengthImpl( | 208 size_t GetRegistryLengthImpl(const std::string& host, |
| 203 const std::string& host, | 209 UnknownRegistryFilter unknown_filter, |
| 204 UnknownRegistryFilter unknown_filter, | 210 PrivateRegistryFilter private_filter) { |
| 205 PrivateRegistryFilter private_filter) { | |
| 206 DCHECK(!host.empty()); | 211 DCHECK(!host.empty()); |
| 207 | 212 |
| 208 // Skip leading dots. | 213 // Skip leading dots. |
| 209 const size_t host_check_begin = host.find_first_not_of('.'); | 214 const size_t host_check_begin = host.find_first_not_of('.'); |
| 210 if (host_check_begin == std::string::npos) | 215 if (host_check_begin == std::string::npos) |
| 211 return 0; // Host is only dots. | 216 return 0; // Host is only dots. |
| 212 | 217 |
| 213 // A single trailing dot isn't relevant in this determination, but does need | 218 // A single trailing dot isn't relevant in this determination, but does need |
| 214 // to be included in the final returned length. | 219 // to be included in the final returned length. |
| 215 size_t host_check_len = host.length(); | 220 size_t host_check_len = host.length(); |
| 216 if (host[host_check_len - 1] == '.') { | 221 if (host[host_check_len - 1] == '.') { |
| 217 --host_check_len; | 222 --host_check_len; |
| 218 DCHECK(host_check_len > 0); // If this weren't true, the host would be ".", | 223 DCHECK(host_check_len > 0); // If this weren't true, the host would be ".", |
| 219 // and we'd have already returned above. | 224 // and we'd have already returned above. |
| 220 if (host[host_check_len - 1] == '.') | 225 if (host[host_check_len - 1] == '.') |
| 221 return 0; // Multiple trailing dots. | 226 return 0; // Multiple trailing dots. |
| 222 } | 227 } |
| 223 | 228 |
| 224 // Walk up the domain tree, most specific to least specific, | 229 // Walk up the domain tree, most specific to least specific, |
| 225 // looking for matches at each level. | 230 // looking for matches at each level. |
| 226 size_t prev_start = std::string::npos; | 231 size_t prev_start = std::string::npos; |
| 227 size_t curr_start = host_check_begin; | 232 size_t curr_start = host_check_begin; |
| 228 size_t next_dot = host.find('.', curr_start); | 233 size_t next_dot = host.find('.', curr_start); |
| 229 if (next_dot >= host_check_len) // Catches std::string::npos as well. | 234 if (next_dot >= host_check_len) // Catches std::string::npos as well. |
| 230 return 0; // This can't have a registry + domain. | 235 return 0; // This can't have a registry + domain. |
| 231 while (1) { | 236 while (1) { |
| 232 const char* domain_str = host.data() + curr_start; | 237 const char* domain_str = host.data() + curr_start; |
| 233 size_t domain_length = host_check_len - curr_start; | 238 size_t domain_length = host_check_len - curr_start; |
| 234 int type = LookupString(g_graph, g_graph_length, domain_str, domain_length); | 239 int type = LookupString(g_graph, g_graph_length, domain_str, domain_length); |
| 235 bool do_check = | 240 bool do_check = |
| 236 type != kNotFound && (!(type & kPrivateRule) || | 241 type != kNotFound && (!(type & kPrivateRule) || |
| 237 private_filter == INCLUDE_PRIVATE_REGISTRIES); | 242 private_filter == INCLUDE_PRIVATE_REGISTRIES); |
| 238 | 243 |
| 239 // If the apparent match is a private registry and we're not including | 244 // If the apparent match is a private registry and we're not including |
| 240 // those, it can't be an actual match. | 245 // those, it can't be an actual match. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 270 break; | 275 break; |
| 271 | 276 |
| 272 prev_start = curr_start; | 277 prev_start = curr_start; |
| 273 curr_start = next_dot + 1; | 278 curr_start = next_dot + 1; |
| 274 next_dot = host.find('.', curr_start); | 279 next_dot = host.find('.', curr_start); |
| 275 } | 280 } |
| 276 | 281 |
| 277 // No rule found in the registry. curr_start now points to the first | 282 // No rule found in the registry. curr_start now points to the first |
| 278 // character of the last subcomponent of the host, so if we allow unknown | 283 // character of the last subcomponent of the host, so if we allow unknown |
| 279 // registries, return the length of this subcomponent. | 284 // registries, return the length of this subcomponent. |
| 280 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES ? | 285 return unknown_filter == INCLUDE_UNKNOWN_REGISTRIES |
| 281 (host.length() - curr_start) : 0; | 286 ? (host.length() - curr_start) |
| 287 : 0; |
| 282 } | 288 } |
| 283 | 289 |
| 284 std::string GetDomainAndRegistryImpl( | 290 std::string GetDomainAndRegistryImpl(const std::string& host, |
| 285 const std::string& host, PrivateRegistryFilter private_filter) { | 291 PrivateRegistryFilter private_filter) { |
| 286 DCHECK(!host.empty()); | 292 DCHECK(!host.empty()); |
| 287 | 293 |
| 288 // Find the length of the registry for this host. | 294 // Find the length of the registry for this host. |
| 289 const size_t registry_length = | 295 const size_t registry_length = |
| 290 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); | 296 GetRegistryLengthImpl(host, INCLUDE_UNKNOWN_REGISTRIES, private_filter); |
| 291 if ((registry_length == std::string::npos) || (registry_length == 0)) | 297 if ((registry_length == std::string::npos) || (registry_length == 0)) |
| 292 return std::string(); // No registry. | 298 return std::string(); // No registry. |
| 293 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding | 299 // The "2" in this next line is 1 for the dot, plus a 1-char minimum preceding |
| 294 // subcomponent length. | 300 // subcomponent length. |
| 295 DCHECK(host.length() >= 2); | 301 DCHECK(host.length() >= 2); |
| 296 if (registry_length > (host.length() - 2)) { | 302 if (registry_length > (host.length() - 2)) { |
| 297 NOTREACHED() << | 303 NOTREACHED() |
| 298 "Host does not have at least one subcomponent before registry!"; | 304 << "Host does not have at least one subcomponent before registry!"; |
| 299 return std::string(); | 305 return std::string(); |
| 300 } | 306 } |
| 301 | 307 |
| 302 // Move past the dot preceding the registry, and search for the next previous | 308 // Move past the dot preceding the registry, and search for the next previous |
| 303 // dot. Return the host from after that dot, or the whole host when there is | 309 // dot. Return the host from after that dot, or the whole host when there is |
| 304 // no dot. | 310 // no dot. |
| 305 const size_t dot = host.rfind('.', host.length() - registry_length - 2); | 311 const size_t dot = host.rfind('.', host.length() - registry_length - 2); |
| 306 if (dot == std::string::npos) | 312 if (dot == std::string::npos) |
| 307 return host; | 313 return host; |
| 308 return host.substr(dot + 1); | 314 return host.substr(dot + 1); |
| 309 } | 315 } |
| 310 | 316 |
| 311 } // namespace | 317 } // namespace |
| 312 | 318 |
| 313 std::string GetDomainAndRegistry( | 319 std::string GetDomainAndRegistry(const GURL& gurl, |
| 314 const GURL& gurl, | 320 PrivateRegistryFilter filter) { |
| 315 PrivateRegistryFilter filter) { | |
| 316 const url::Component host = gurl.parsed_for_possibly_invalid_spec().host; | 321 const url::Component host = gurl.parsed_for_possibly_invalid_spec().host; |
| 317 if ((host.len <= 0) || gurl.HostIsIPAddress()) | 322 if ((host.len <= 0) || gurl.HostIsIPAddress()) |
| 318 return std::string(); | 323 return std::string(); |
| 319 return GetDomainAndRegistryImpl(std::string( | 324 return GetDomainAndRegistryImpl( |
| 320 gurl.possibly_invalid_spec().data() + host.begin, host.len), filter); | 325 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), |
| 326 filter); |
| 321 } | 327 } |
| 322 | 328 |
| 323 std::string GetDomainAndRegistry( | 329 std::string GetDomainAndRegistry(const std::string& host, |
| 324 const std::string& host, | 330 PrivateRegistryFilter filter) { |
| 325 PrivateRegistryFilter filter) { | |
| 326 url::CanonHostInfo host_info; | 331 url::CanonHostInfo host_info; |
| 327 const std::string canon_host(CanonicalizeHost(host, &host_info)); | 332 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
| 328 if (canon_host.empty() || host_info.IsIPAddress()) | 333 if (canon_host.empty() || host_info.IsIPAddress()) |
| 329 return std::string(); | 334 return std::string(); |
| 330 return GetDomainAndRegistryImpl(canon_host, filter); | 335 return GetDomainAndRegistryImpl(canon_host, filter); |
| 331 } | 336 } |
| 332 | 337 |
| 333 bool SameDomainOrHost( | 338 bool SameDomainOrHost(const GURL& gurl1, |
| 334 const GURL& gurl1, | 339 const GURL& gurl2, |
| 335 const GURL& gurl2, | 340 PrivateRegistryFilter filter) { |
| 336 PrivateRegistryFilter filter) { | |
| 337 // See if both URLs have a known domain + registry, and those values are the | 341 // See if both URLs have a known domain + registry, and those values are the |
| 338 // same. | 342 // same. |
| 339 const std::string domain1(GetDomainAndRegistry(gurl1, filter)); | 343 const std::string domain1(GetDomainAndRegistry(gurl1, filter)); |
| 340 const std::string domain2(GetDomainAndRegistry(gurl2, filter)); | 344 const std::string domain2(GetDomainAndRegistry(gurl2, filter)); |
| 341 if (!domain1.empty() || !domain2.empty()) | 345 if (!domain1.empty() || !domain2.empty()) |
| 342 return domain1 == domain2; | 346 return domain1 == domain2; |
| 343 | 347 |
| 344 // No domains. See if the hosts are identical. | 348 // No domains. See if the hosts are identical. |
| 345 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host; | 349 const url::Component host1 = gurl1.parsed_for_possibly_invalid_spec().host; |
| 346 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host; | 350 const url::Component host2 = gurl2.parsed_for_possibly_invalid_spec().host; |
| 347 if ((host1.len <= 0) || (host1.len != host2.len)) | 351 if ((host1.len <= 0) || (host1.len != host2.len)) |
| 348 return false; | 352 return false; |
| 349 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, | 353 return !strncmp(gurl1.possibly_invalid_spec().data() + host1.begin, |
| 350 gurl2.possibly_invalid_spec().data() + host2.begin, | 354 gurl2.possibly_invalid_spec().data() + host2.begin, |
| 351 host1.len); | 355 host1.len); |
| 352 } | 356 } |
| 353 | 357 |
| 354 size_t GetRegistryLength( | 358 size_t GetRegistryLength(const GURL& gurl, |
| 355 const GURL& gurl, | 359 UnknownRegistryFilter unknown_filter, |
| 356 UnknownRegistryFilter unknown_filter, | 360 PrivateRegistryFilter private_filter) { |
| 357 PrivateRegistryFilter private_filter) { | |
| 358 const url::Component host = gurl.parsed_for_possibly_invalid_spec().host; | 361 const url::Component host = gurl.parsed_for_possibly_invalid_spec().host; |
| 359 if (host.len <= 0) | 362 if (host.len <= 0) |
| 360 return std::string::npos; | 363 return std::string::npos; |
| 361 if (gurl.HostIsIPAddress()) | 364 if (gurl.HostIsIPAddress()) |
| 362 return 0; | 365 return 0; |
| 363 return GetRegistryLengthImpl( | 366 return GetRegistryLengthImpl( |
| 364 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), | 367 std::string(gurl.possibly_invalid_spec().data() + host.begin, host.len), |
| 365 unknown_filter, | 368 unknown_filter, |
| 366 private_filter); | 369 private_filter); |
| 367 } | 370 } |
| 368 | 371 |
| 369 size_t GetRegistryLength( | 372 size_t GetRegistryLength(const std::string& host, |
| 370 const std::string& host, | 373 UnknownRegistryFilter unknown_filter, |
| 371 UnknownRegistryFilter unknown_filter, | 374 PrivateRegistryFilter private_filter) { |
| 372 PrivateRegistryFilter private_filter) { | |
| 373 url::CanonHostInfo host_info; | 375 url::CanonHostInfo host_info; |
| 374 const std::string canon_host(CanonicalizeHost(host, &host_info)); | 376 const std::string canon_host(CanonicalizeHost(host, &host_info)); |
| 375 if (canon_host.empty()) | 377 if (canon_host.empty()) |
| 376 return std::string::npos; | 378 return std::string::npos; |
| 377 if (host_info.IsIPAddress()) | 379 if (host_info.IsIPAddress()) |
| 378 return 0; | 380 return 0; |
| 379 return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); | 381 return GetRegistryLengthImpl(canon_host, unknown_filter, private_filter); |
| 380 } | 382 } |
| 381 | 383 |
| 382 void SetFindDomainGraph() { | 384 void SetFindDomainGraph() { |
| 383 g_graph = kDafsa; | 385 g_graph = kDafsa; |
| 384 g_graph_length = sizeof(kDafsa); | 386 g_graph_length = sizeof(kDafsa); |
| 385 } | 387 } |
| 386 | 388 |
| 387 void SetFindDomainGraph(const unsigned char* domains, size_t length) { | 389 void SetFindDomainGraph(const unsigned char* domains, size_t length) { |
| 388 CHECK(domains); | 390 CHECK(domains); |
| 389 CHECK_NE(length, 0u); | 391 CHECK_NE(length, 0u); |
| 390 g_graph = domains; | 392 g_graph = domains; |
| 391 g_graph_length = length; | 393 g_graph_length = length; |
| 392 } | 394 } |
| 393 | 395 |
| 394 } // namespace registry_controlled_domains | 396 } // namespace registry_controlled_domains |
| 395 } // namespace net | 397 } // namespace net |
| OLD | NEW |