| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_manager.h" | 5 #include "net/base/sdch_manager.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/sha2.h" | 10 #include "base/sha2.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "net/base/registry_controlled_domain.h" | 13 #include "net/base/registry_controlled_domain.h" |
| 14 #include "net/url_request/url_request_http_job.h" | 14 #include "net/url_request/url_request_http_job.h" |
| 15 | 15 |
| 16 using base::Time; | 16 namespace net { |
| 17 using base::TimeDelta; | |
| 18 | 17 |
| 19 //------------------------------------------------------------------------------ | 18 //------------------------------------------------------------------------------ |
| 20 // static | 19 // static |
| 21 const size_t SdchManager::kMaxDictionarySize = 1000000; | 20 const size_t SdchManager::kMaxDictionarySize = 1000000; |
| 22 | 21 |
| 23 // static | 22 // static |
| 24 const size_t SdchManager::kMaxDictionaryCount = 20; | 23 const size_t SdchManager::kMaxDictionaryCount = 20; |
| 25 | 24 |
| 26 // static | 25 // static |
| 27 SdchManager* SdchManager::global_; | 26 SdchManager* SdchManager::global_; |
| 28 | 27 |
| 29 //------------------------------------------------------------------------------ | 28 //------------------------------------------------------------------------------ |
| 30 SdchManager::Dictionary::Dictionary(const std::string& dictionary_text, | 29 SdchManager::Dictionary::Dictionary(const std::string& dictionary_text, |
| 31 size_t offset, const std::string& client_hash, const GURL& gurl, | 30 size_t offset, |
| 32 const std::string& domain, const std::string& path, const Time& expiration, | 31 const std::string& client_hash, |
| 33 const std::set<int> ports) | 32 const GURL& gurl, |
| 34 : text_(dictionary_text, offset), | 33 const std::string& domain, |
| 35 client_hash_(client_hash), | 34 const std::string& path, |
| 36 url_(gurl), | 35 const base::Time& expiration, |
| 37 domain_(domain), | 36 const std::set<int> ports) |
| 38 path_(path), | 37 : text_(dictionary_text, offset), |
| 39 expiration_(expiration), | 38 client_hash_(client_hash), |
| 40 ports_(ports) { | 39 url_(gurl), |
| 40 domain_(domain), |
| 41 path_(path), |
| 42 expiration_(expiration), |
| 43 ports_(ports) { |
| 41 } | 44 } |
| 42 | 45 |
| 43 SdchManager::Dictionary::~Dictionary() { | 46 SdchManager::Dictionary::~Dictionary() { |
| 44 } | 47 } |
| 45 | 48 |
| 46 bool SdchManager::Dictionary::CanAdvertise(const GURL& target_url) { | 49 bool SdchManager::Dictionary::CanAdvertise(const GURL& target_url) { |
| 47 if (!SdchManager::Global()->IsInSupportedDomain(target_url)) | 50 if (!SdchManager::Global()->IsInSupportedDomain(target_url)) |
| 48 return false; | 51 return false; |
| 49 /* The specific rules of when a dictionary should be advertised in an | 52 /* The specific rules of when a dictionary should be advertised in an |
| 50 Avail-Dictionary header are modeled after the rules for cookie scoping. The | 53 Avail-Dictionary header are modeled after the rules for cookie scoping. The |
| 51 terms "domain-match" and "pathmatch" are defined in RFC 2965 [6]. A | 54 terms "domain-match" and "pathmatch" are defined in RFC 2965 [6]. A |
| 52 dictionary may be advertised in the Avail-Dictionaries header exactly when | 55 dictionary may be advertised in the Avail-Dictionaries header exactly when |
| 53 all of the following are true: | 56 all of the following are true: |
| 54 1. The server's effective host name domain-matches the Domain attribute of | 57 1. The server's effective host name domain-matches the Domain attribute of |
| 55 the dictionary. | 58 the dictionary. |
| 56 2. If the dictionary has a Port attribute, the request port is one of the | 59 2. If the dictionary has a Port attribute, the request port is one of the |
| 57 ports listed in the Port attribute. | 60 ports listed in the Port attribute. |
| 58 3. The request URI path-matches the path header of the dictionary. | 61 3. The request URI path-matches the path header of the dictionary. |
| 59 4. The request is not an HTTPS request. | 62 4. The request is not an HTTPS request. |
| 60 */ | 63 */ |
| 61 if (!DomainMatch(target_url, domain_)) | 64 if (!DomainMatch(target_url, domain_)) |
| 62 return false; | 65 return false; |
| 63 if (!ports_.empty() && 0 == ports_.count(target_url.EffectiveIntPort())) | 66 if (!ports_.empty() && 0 == ports_.count(target_url.EffectiveIntPort())) |
| 64 return false; | 67 return false; |
| 65 if (path_.size() && !PathMatch(target_url.path(), path_)) | 68 if (path_.size() && !PathMatch(target_url.path(), path_)) |
| 66 return false; | 69 return false; |
| 67 if (target_url.SchemeIsSecure()) | 70 if (target_url.SchemeIsSecure()) |
| 68 return false; | 71 return false; |
| 69 if (Time::Now() > expiration_) | 72 if (base::Time::Now() > expiration_) |
| 70 return false; | 73 return false; |
| 71 return true; | 74 return true; |
| 72 } | 75 } |
| 73 | 76 |
| 74 //------------------------------------------------------------------------------ | 77 //------------------------------------------------------------------------------ |
| 75 // Security functions restricting loads and use of dictionaries. | 78 // Security functions restricting loads and use of dictionaries. |
| 76 | 79 |
| 77 // static | 80 // static |
| 78 bool SdchManager::Dictionary::CanSet(const std::string& domain, | 81 bool SdchManager::Dictionary::CanSet(const std::string& domain, |
| 79 const std::string& path, | 82 const std::string& path, |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 std::string client_hash; | 366 std::string client_hash; |
| 364 std::string server_hash; | 367 std::string server_hash; |
| 365 GenerateHash(dictionary_text, &client_hash, &server_hash); | 368 GenerateHash(dictionary_text, &client_hash, &server_hash); |
| 366 if (dictionaries_.find(server_hash) != dictionaries_.end()) { | 369 if (dictionaries_.find(server_hash) != dictionaries_.end()) { |
| 367 SdchErrorRecovery(DICTIONARY_ALREADY_LOADED); | 370 SdchErrorRecovery(DICTIONARY_ALREADY_LOADED); |
| 368 return false; // Already loaded. | 371 return false; // Already loaded. |
| 369 } | 372 } |
| 370 | 373 |
| 371 std::string domain, path; | 374 std::string domain, path; |
| 372 std::set<int> ports; | 375 std::set<int> ports; |
| 373 Time expiration(Time::Now() + TimeDelta::FromDays(30)); | 376 base::Time expiration(base::Time::Now() + base::TimeDelta::FromDays(30)); |
| 374 | 377 |
| 375 if (dictionary_text.empty()) { | 378 if (dictionary_text.empty()) { |
| 376 SdchErrorRecovery(DICTIONARY_HAS_NO_TEXT); | 379 SdchErrorRecovery(DICTIONARY_HAS_NO_TEXT); |
| 377 return false; // Missing header. | 380 return false; // Missing header. |
| 378 } | 381 } |
| 379 | 382 |
| 380 size_t header_end = dictionary_text.find("\n\n"); | 383 size_t header_end = dictionary_text.find("\n\n"); |
| 381 if (std::string::npos == header_end) { | 384 if (std::string::npos == header_end) { |
| 382 SdchErrorRecovery(DICTIONARY_HAS_NO_HEADER); | 385 SdchErrorRecovery(DICTIONARY_HAS_NO_HEADER); |
| 383 return false; // Missing header. | 386 return false; // Missing header. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 408 if (name == "domain") { | 411 if (name == "domain") { |
| 409 domain = value; | 412 domain = value; |
| 410 } else if (name == "path") { | 413 } else if (name == "path") { |
| 411 path = value; | 414 path = value; |
| 412 } else if (name == "format-version") { | 415 } else if (name == "format-version") { |
| 413 if (value != "1.0") | 416 if (value != "1.0") |
| 414 return false; | 417 return false; |
| 415 } else if (name == "max-age") { | 418 } else if (name == "max-age") { |
| 416 int64 seconds; | 419 int64 seconds; |
| 417 base::StringToInt64(value, &seconds); | 420 base::StringToInt64(value, &seconds); |
| 418 expiration = Time::Now() + TimeDelta::FromSeconds(seconds); | 421 expiration = base::Time::Now() + base::TimeDelta::FromSeconds(seconds); |
| 419 } else if (name == "port") { | 422 } else if (name == "port") { |
| 420 int port; | 423 int port; |
| 421 base::StringToInt(value, &port); | 424 base::StringToInt(value, &port); |
| 422 if (port >= 0) | 425 if (port >= 0) |
| 423 ports.insert(port); | 426 ports.insert(port); |
| 424 } | 427 } |
| 425 } | 428 } |
| 426 | 429 |
| 427 if (line_end >= header_end) | 430 if (line_end >= header_end) |
| 428 break; | 431 break; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 (*output)[i] = '-'; | 539 (*output)[i] = '-'; |
| 537 continue; | 540 continue; |
| 538 case '/': | 541 case '/': |
| 539 (*output)[i] = '_'; | 542 (*output)[i] = '_'; |
| 540 continue; | 543 continue; |
| 541 default: | 544 default: |
| 542 continue; | 545 continue; |
| 543 } | 546 } |
| 544 } | 547 } |
| 545 } | 548 } |
| 549 |
| 550 } // namespace net |
| OLD | NEW |