OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/base/swr_histogram_domains/swr_histogram_domains.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "net/base/dafsa/lookup_string.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 namespace swr_histogram_domains { |
| 15 |
| 16 namespace { |
| 17 |
| 18 #include "net/base/swr_histogram_domains/swr_histogram_domains-inc.cc" |
| 19 |
| 20 // The maximum number of dots in any domain in the list. This is used to ignore |
| 21 // parts of the host name that are irrelevant, and so must be correct. |
| 22 const int kMaxDots = 2; |
| 23 |
| 24 // The minimum number of dots in a host necessary for it to be considered a |
| 25 // match. |
| 26 const int kMinDots = 1; |
| 27 |
| 28 const size_t npos = base::StringPiece::npos; |
| 29 |
| 30 bool LookupTail(const base::StringPiece& host, size_t pos) { |
| 31 DCHECK_LT(pos, host.size()); |
| 32 return LookupStringInDafsa(kDafsa, sizeof(kDafsa), host.data() + pos, |
| 33 host.size() - pos) == 0; |
| 34 } |
| 35 |
| 36 bool LookupTrimmedHost(const base::StringPiece& trimmed) { |
| 37 // |trimmed| contains at least one non-dot. The maximum number of dots we want |
| 38 // to look for is kMaxInterestingDots; any dots before that will not affect |
| 39 // the outcome of the match. |
| 40 const int kMaxInterestingDots = kMaxDots + 1; |
| 41 |
| 42 // Scan |trimmed| from the right for up to kMaxInterestingDots dots, checking |
| 43 // for a domain match at each position. |
| 44 int found_dots = 0; |
| 45 size_t pos = npos; |
| 46 while (found_dots < kMaxInterestingDots) { |
| 47 pos = trimmed.find_last_of('.', pos); |
| 48 if (pos == npos) |
| 49 break; |
| 50 ++found_dots; |
| 51 if (found_dots > kMinDots && LookupTail(trimmed, pos + 1)) |
| 52 return true; |
| 53 if (pos == 0) |
| 54 return false; |
| 55 --pos; |
| 56 } |
| 57 |
| 58 if (found_dots >= kMinDots && found_dots <= kMaxDots) { |
| 59 // We might have an exact match. |
| 60 return LookupTail(trimmed, 0); |
| 61 } |
| 62 |
| 63 return false; |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 68 bool IsHostInSWRHistogramDomain(const base::StringPiece& host) { |
| 69 // Ignore trailing dots. |
| 70 size_t last_interesting_pos = host.find_last_not_of('.'); |
| 71 if (last_interesting_pos == npos) |
| 72 return false; |
| 73 |
| 74 return LookupTrimmedHost(host.substr(0, last_interesting_pos + 1)); |
| 75 } |
| 76 |
| 77 } // namespace swr_histogram_domains |
| 78 |
| 79 } // namespace net |
OLD | NEW |