| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
| 6 | 6 |
| 7 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 &FastTransactionServer::FastNoStoreHandler, | 458 &FastTransactionServer::FastNoStoreHandler, |
| 459 0 | 459 0 |
| 460 }; | 460 }; |
| 461 | 461 |
| 462 // This class provides a handler for kRangeGET_TransactionOK so that the range | 462 // This class provides a handler for kRangeGET_TransactionOK so that the range |
| 463 // request can be served on demand. | 463 // request can be served on demand. |
| 464 class RangeTransactionServer { | 464 class RangeTransactionServer { |
| 465 public: | 465 public: |
| 466 RangeTransactionServer() { | 466 RangeTransactionServer() { |
| 467 not_modified_ = false; | 467 not_modified_ = false; |
| 468 modified_ = false; |
| 468 } | 469 } |
| 469 ~RangeTransactionServer() { | 470 ~RangeTransactionServer() { |
| 470 not_modified_ = false; | 471 not_modified_ = false; |
| 472 modified_ = false; |
| 471 } | 473 } |
| 472 | 474 |
| 475 // Returns only 416 or 304 when set. |
| 473 void set_not_modified(bool value) { not_modified_ = value; } | 476 void set_not_modified(bool value) { not_modified_ = value; } |
| 474 | 477 |
| 478 // Returns 206 when revalidating a range (instead of 304). |
| 479 void set_modified(bool value) { modified_ = value; } |
| 480 |
| 475 static void RangeHandler(const net::HttpRequestInfo* request, | 481 static void RangeHandler(const net::HttpRequestInfo* request, |
| 476 std::string* response_status, | 482 std::string* response_status, |
| 477 std::string* response_headers, | 483 std::string* response_headers, |
| 478 std::string* response_data); | 484 std::string* response_data); |
| 479 | 485 |
| 480 private: | 486 private: |
| 481 static bool not_modified_; | 487 static bool not_modified_; |
| 488 static bool modified_; |
| 482 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); | 489 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); |
| 483 }; | 490 }; |
| 484 bool RangeTransactionServer::not_modified_ = false; | 491 bool RangeTransactionServer::not_modified_ = false; |
| 492 bool RangeTransactionServer::modified_ = false; |
| 485 | 493 |
| 486 // Static. | 494 // Static. |
| 487 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, | 495 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, |
| 488 std::string* response_status, | 496 std::string* response_status, |
| 489 std::string* response_headers, | 497 std::string* response_headers, |
| 490 std::string* response_data) { | 498 std::string* response_data) { |
| 491 if (request->extra_headers.empty()) { | 499 if (request->extra_headers.empty()) { |
| 492 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | 500 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
| 493 return; | 501 return; |
| 494 } | 502 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 512 EXPECT_TRUE(byte_range.ComputeBounds(80)); | 520 EXPECT_TRUE(byte_range.ComputeBounds(80)); |
| 513 int start = static_cast<int>(byte_range.first_byte_position()); | 521 int start = static_cast<int>(byte_range.first_byte_position()); |
| 514 int end = static_cast<int>(byte_range.last_byte_position()); | 522 int end = static_cast<int>(byte_range.last_byte_position()); |
| 515 | 523 |
| 516 EXPECT_LT(end, 80); | 524 EXPECT_LT(end, 80); |
| 517 | 525 |
| 518 std::string content_range = StringPrintf("Content-Range: bytes %d-%d/80\n", | 526 std::string content_range = StringPrintf("Content-Range: bytes %d-%d/80\n", |
| 519 start, end); | 527 start, end); |
| 520 response_headers->append(content_range); | 528 response_headers->append(content_range); |
| 521 | 529 |
| 522 if (request->extra_headers.find("If-None-Match") == std::string::npos) { | 530 if (request->extra_headers.find("If-None-Match") == std::string::npos || |
| 531 modified_) { |
| 523 EXPECT_EQ(9, (end - start) % 10); | 532 EXPECT_EQ(9, (end - start) % 10); |
| 524 std::string data; | 533 std::string data; |
| 525 for (int block_start = start; block_start < end; block_start += 10) | 534 for (int block_start = start; block_start < end; block_start += 10) |
| 526 StringAppendF(&data, "rg: %02d-%02d ", block_start, block_start + 9); | 535 StringAppendF(&data, "rg: %02d-%02d ", block_start, block_start + 9); |
| 527 *response_data = data; | 536 *response_data = data; |
| 528 | 537 |
| 529 if (end - start != 9) { | 538 if (end - start != 9) { |
| 530 // We also have to fix content-length. | 539 // We also have to fix content-length. |
| 531 int len = end - start + 1; | 540 int len = end - start + 1; |
| 532 EXPECT_EQ(0, len % 10); | 541 EXPECT_EQ(0, len % 10); |
| (...skipping 1226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1759 &headers); | 1768 &headers); |
| 1760 | 1769 |
| 1761 EXPECT_TRUE(Verify206Response(headers, 40, 49)); | 1770 EXPECT_TRUE(Verify206Response(headers, 40, 49)); |
| 1762 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 1771 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1763 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 1772 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1764 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1773 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1765 | 1774 |
| 1766 RemoveMockTransaction(&kRangeGET_TransactionOK); | 1775 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 1767 } | 1776 } |
| 1768 | 1777 |
| 1778 // Tests that we deal with 206s when revalidating range requests. |
| 1779 TEST(HttpCache, DISABLED_RangeGET_ModifiedResult) { |
| 1780 MockHttpCache cache; |
| 1781 AddMockTransaction(&kRangeGET_TransactionOK); |
| 1782 std::string headers; |
| 1783 |
| 1784 // Write to the cache (40-49). |
| 1785 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 1786 &headers); |
| 1787 |
| 1788 EXPECT_TRUE(Verify206Response(headers, 40, 49)); |
| 1789 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 1790 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1791 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1792 |
| 1793 // Attempt to read from the cache (40-49). |
| 1794 RangeTransactionServer handler; |
| 1795 handler.set_modified(true); |
| 1796 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, |
| 1797 &headers); |
| 1798 |
| 1799 EXPECT_TRUE(Verify206Response(headers, 40, 49)); |
| 1800 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1801 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1802 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 1803 |
| 1804 // And the entry should be gone. |
| 1805 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
| 1806 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1807 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 1808 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1809 |
| 1810 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 1811 } |
| 1812 |
| 1769 // Tests that we can cache range requests when the start or end is unknown. | 1813 // Tests that we can cache range requests when the start or end is unknown. |
| 1770 // We start with one suffix request, followed by a request from a given point. | 1814 // We start with one suffix request, followed by a request from a given point. |
| 1771 TEST(HttpCache, DISABLED_UnknownRangeGET_1) { | 1815 TEST(HttpCache, DISABLED_UnknownRangeGET_1) { |
| 1772 MockHttpCache cache; | 1816 MockHttpCache cache; |
| 1773 AddMockTransaction(&kRangeGET_TransactionOK); | 1817 AddMockTransaction(&kRangeGET_TransactionOK); |
| 1774 std::string headers; | 1818 std::string headers; |
| 1775 | 1819 |
| 1776 // Write to the cache (70-79). | 1820 // Write to the cache (70-79). |
| 1777 MockTransaction transaction(kRangeGET_TransactionOK); | 1821 MockTransaction transaction(kRangeGET_TransactionOK); |
| 1778 transaction.request_headers = "Range: bytes = -10\r\n"; | 1822 transaction.request_headers = "Range: bytes = -10\r\n"; |
| (...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2386 | 2430 |
| 2387 // force this transaction to write to the cache again | 2431 // force this transaction to write to the cache again |
| 2388 MockTransaction transaction(kSimpleGET_Transaction); | 2432 MockTransaction transaction(kSimpleGET_Transaction); |
| 2389 | 2433 |
| 2390 RunTransactionTest(cache.http_cache(), transaction); | 2434 RunTransactionTest(cache.http_cache(), transaction); |
| 2391 | 2435 |
| 2392 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2436 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 2393 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2437 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 2394 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2438 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 2395 } | 2439 } |
| OLD | NEW |