Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/sdch_dictionary.h" | 5 #include "net/base/sdch_dictionary.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | |
| 7 #include "base/time/clock.h" | 8 #include "base/time/clock.h" |
| 8 #include "base/time/default_clock.h" | 9 #include "base/time/default_clock.h" |
| 9 #include "base/values.h" | 10 #include "base/values.h" |
| 10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 11 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 11 | 12 |
| 12 namespace { | |
| 13 | |
| 14 bool DomainMatch(const GURL& gurl, const std::string& restriction) { | |
| 15 // TODO(jar): This is not precisely a domain match definition. | |
| 16 return gurl.DomainIs(restriction); | |
| 17 } | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 namespace net { | 13 namespace net { |
| 22 | 14 |
| 23 SdchDictionary::SdchDictionary(const std::string& dictionary_text, | 15 SdchDictionary::SdchDictionary(const std::string& dictionary_text, |
| 24 size_t offset, | 16 size_t offset, |
| 25 const std::string& client_hash, | 17 const std::string& client_hash, |
| 26 const std::string& server_hash, | 18 const std::string& server_hash, |
| 27 const GURL& gurl, | 19 const GURL& gurl, |
| 28 const std::string& domain, | 20 const std::string& domain, |
| 29 const std::string& path, | 21 const std::string& path, |
| 30 const base::Time& expiration, | 22 const base::Time& expiration, |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 150 if (path == restriction) | 142 if (path == restriction) |
| 151 return true; | 143 return true; |
| 152 size_t prefix_length = restriction.size(); | 144 size_t prefix_length = restriction.size(); |
| 153 if (prefix_length > path.size()) | 145 if (prefix_length > path.size()) |
| 154 return false; // Can't be a prefix. | 146 return false; // Can't be a prefix. |
| 155 if (0 != path.compare(0, prefix_length, restriction)) | 147 if (0 != path.compare(0, prefix_length, restriction)) |
| 156 return false; | 148 return false; |
| 157 return restriction[prefix_length - 1] == '/' || path[prefix_length] == '/'; | 149 return restriction[prefix_length - 1] == '/' || path[prefix_length] == '/'; |
| 158 } | 150 } |
| 159 | 151 |
| 152 // static | |
| 153 bool SdchDictionary::DomainMatch(const GURL& gurl, | |
| 154 const std::string& restriction) { | |
| 155 /* RFC 2965: | |
|
Mike West
2016/04/14 07:32:49
1. This RFC has been updated by https://tools.ietf
| |
| 156 * Host A's name domain-matches host B's if | |
| 157 * * their host name strings string-compare equal; or | |
| 158 * * A is a HDN string and has the form NB, where N is a non-empty | |
| 159 * name string, B has the form .B', and B' is a HDN string. (So, | |
| 160 * x.y.com domain-matches .Y.com but not Y.com.) | |
| 161 * | |
| 162 * In our case |gurl| is B, |restriction| is "A". | |
| 163 */ | |
| 164 if (!gurl.is_valid() || restriction.empty()) | |
| 165 return false; | |
| 166 | |
| 167 // Avoid useless string allocation by using GURL::host() | |
| 168 base::StringPiece host(gurl.possibly_invalid_spec().data() + | |
| 169 gurl.parsed_for_possibly_invalid_spec().host.begin, | |
| 170 gurl.parsed_for_possibly_invalid_spec().host.len); | |
| 171 if (host.empty()) | |
| 172 return false; | |
| 173 | |
| 174 base::StringPiece domain(restriction); | |
| 175 | |
| 176 // Ignore trailing '.'s | |
| 177 if (*host.rbegin() == '.') | |
| 178 host.remove_suffix(1); | |
| 179 | |
| 180 if (*domain.rbegin() == '.') | |
| 181 domain.remove_suffix(1); | |
| 182 | |
| 183 // Skip leading '.' in domain and remember it for future use | |
| 184 bool has_leading_dot = domain[0] == '.'; | |
| 185 if (has_leading_dot) { | |
| 186 domain.remove_prefix(1); | |
| 187 } | |
| 188 | |
| 189 if (host.length() < domain.length()) | |
| 190 return false; | |
| 191 | |
| 192 // Advance host to be same length as domain. | |
| 193 auto distance = host.length() - domain.length(); | |
| 194 if (distance > 0) { | |
| 195 // Make sure there aren't extra characters in host before the compared part; | |
| 196 // if the host name is longer than the input domain name, then the character | |
| 197 // immediately before the compared part should be a dot. For example, | |
| 198 // www.google.com has domain "google.com", but www.iamnotgoogle.com does | |
| 199 // not. | |
| 200 if (host[distance - 1] != '.') | |
| 201 return false; | |
| 202 | |
| 203 host.remove_prefix(distance); | |
| 204 } | |
| 205 | |
| 206 if (!base::LowerCaseEqualsASCII(host, domain)) | |
| 207 return false; | |
| 208 | |
| 209 // If we compared only part of host with domain check that domain starts with | |
| 210 // '.' | |
| 211 if (distance > 0) | |
| 212 return has_leading_dot; | |
| 213 | |
| 214 return true; | |
| 215 } | |
| 216 | |
| 160 bool SdchDictionary::Expired() const { | 217 bool SdchDictionary::Expired() const { |
| 161 return base::Time::Now() > expiration_; | 218 return base::Time::Now() > expiration_; |
| 162 } | 219 } |
| 163 | 220 |
| 164 } // namespace net | 221 } // namespace net |
| OLD | NEW |