| 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 #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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "crypto/sha2.h" | 12 #include "crypto/sha2.h" |
| 13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 13 #include "net/base/registry_controlled_domains/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 namespace { |
| 17 |
| 18 void StripTrailingDot(GURL* gurl) { |
| 19 std::string host(gurl->host()); |
| 20 |
| 21 if (host.empty()) |
| 22 return; |
| 23 |
| 24 if (*host.rbegin() != '.') |
| 25 return; |
| 26 |
| 27 host.resize(host.size() - 1); |
| 28 |
| 29 GURL::Replacements replacements; |
| 30 replacements.SetHostStr(host); |
| 31 *gurl = gurl->ReplaceComponents(replacements); |
| 32 return; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 16 namespace net { | 37 namespace net { |
| 17 | 38 |
| 18 //------------------------------------------------------------------------------ | 39 //------------------------------------------------------------------------------ |
| 19 // static | 40 // static |
| 20 | 41 |
| 21 // Adjust SDCH limits downwards for mobile. | 42 // Adjust SDCH limits downwards for mobile. |
| 22 #if defined(OS_ANDROID) || defined(OS_IOS) | 43 #if defined(OS_ANDROID) || defined(OS_IOS) |
| 23 // static | 44 // static |
| 24 const size_t SdchManager::kMaxDictionaryCount = 1; | 45 const size_t SdchManager::kMaxDictionaryCount = 1; |
| 25 const size_t SdchManager::kMaxDictionarySize = 500 * 1000; | 46 const size_t SdchManager::kMaxDictionarySize = 500 * 1000; |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 543 if (port >= 0) | 564 if (port >= 0) |
| 544 ports.insert(port); | 565 ports.insert(port); |
| 545 } | 566 } |
| 546 } | 567 } |
| 547 | 568 |
| 548 if (line_end >= header_end) | 569 if (line_end >= header_end) |
| 549 break; | 570 break; |
| 550 line_start = line_end + 1; | 571 line_start = line_end + 1; |
| 551 } | 572 } |
| 552 | 573 |
| 553 if (!IsInSupportedDomain(dictionary_url)) | 574 // Narrow fix for http://crbug.com/389451. |
| 575 GURL dictionary_url_normalized(dictionary_url); |
| 576 StripTrailingDot(&dictionary_url_normalized); |
| 577 |
| 578 if (!IsInSupportedDomain(dictionary_url_normalized)) |
| 554 return; | 579 return; |
| 555 | 580 |
| 556 if (!Dictionary::CanSet(domain, path, ports, dictionary_url)) | 581 if (!Dictionary::CanSet(domain, path, ports, dictionary_url_normalized)) |
| 557 return; | 582 return; |
| 558 | 583 |
| 559 // TODO(jar): Remove these hacks to preclude a DOS attack involving piles of | 584 // TODO(jar): Remove these hacks to preclude a DOS attack involving piles of |
| 560 // useless dictionaries. We should probably have a cache eviction plan, | 585 // useless dictionaries. We should probably have a cache eviction plan, |
| 561 // instead of just blocking additions. For now, with the spec in flux, it | 586 // instead of just blocking additions. For now, with the spec in flux, it |
| 562 // is probably not worth doing eviction handling. | 587 // is probably not worth doing eviction handling. |
| 563 if (kMaxDictionarySize < dictionary_text.size()) { | 588 if (kMaxDictionarySize < dictionary_text.size()) { |
| 564 SdchErrorRecovery(DICTIONARY_IS_TOO_LARGE); | 589 SdchErrorRecovery(DICTIONARY_IS_TOO_LARGE); |
| 565 return; | 590 return; |
| 566 } | 591 } |
| 567 if (kMaxDictionaryCount <= dictionaries_.size()) { | 592 if (kMaxDictionaryCount <= dictionaries_.size()) { |
| 568 SdchErrorRecovery(DICTIONARY_COUNT_EXCEEDED); | 593 SdchErrorRecovery(DICTIONARY_COUNT_EXCEEDED); |
| 569 return; | 594 return; |
| 570 } | 595 } |
| 571 | 596 |
| 572 UMA_HISTOGRAM_COUNTS("Sdch3.Dictionary size loaded", dictionary_text.size()); | 597 UMA_HISTOGRAM_COUNTS("Sdch3.Dictionary size loaded", dictionary_text.size()); |
| 573 DVLOG(1) << "Loaded dictionary with client hash " << client_hash | 598 DVLOG(1) << "Loaded dictionary with client hash " << client_hash |
| 574 << " and server hash " << server_hash; | 599 << " and server hash " << server_hash; |
| 575 Dictionary* dictionary = | 600 Dictionary* dictionary = |
| 576 new Dictionary(dictionary_text, header_end + 2, client_hash, | 601 new Dictionary(dictionary_text, header_end + 2, client_hash, |
| 577 dictionary_url, domain, path, expiration, ports); | 602 dictionary_url_normalized, domain, |
| 603 path, expiration, ports); |
| 578 dictionaries_[server_hash] = dictionary; | 604 dictionaries_[server_hash] = dictionary; |
| 579 return; | 605 return; |
| 580 } | 606 } |
| 581 | 607 |
| 582 // static | 608 // static |
| 583 void SdchManager::UrlSafeBase64Encode(const std::string& input, | 609 void SdchManager::UrlSafeBase64Encode(const std::string& input, |
| 584 std::string* output) { | 610 std::string* output) { |
| 585 // Since this is only done during a dictionary load, and hashes are only 8 | 611 // Since this is only done during a dictionary load, and hashes are only 8 |
| 586 // characters, we just do the simple fixup, rather than rewriting the encoder. | 612 // characters, we just do the simple fixup, rather than rewriting the encoder. |
| 587 base::Base64Encode(input, output); | 613 base::Base64Encode(input, output); |
| 588 std::replace(output->begin(), output->end(), '+', '-'); | 614 std::replace(output->begin(), output->end(), '+', '-'); |
| 589 std::replace(output->begin(), output->end(), '/', '_'); | 615 std::replace(output->begin(), output->end(), '/', '_'); |
| 590 } | 616 } |
| 591 | 617 |
| 592 } // namespace net | 618 } // namespace net |
| OLD | NEW |