OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 | 10 |
11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/pickle.h" | 16 #include "base/pickle.h" |
17 #include "base/ref_counted.h" | 17 #include "base/ref_counted.h" |
18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
19 #include "base/time.h" | 19 #include "base/time.h" |
20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
21 #include "net/base/load_flags.h" | 21 #include "net/base/load_flags.h" |
22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
23 #include "net/disk_cache/disk_cache.h" | 23 #include "net/disk_cache/disk_cache.h" |
24 #include "net/http/http_network_layer.h" | 24 #include "net/http/http_network_layer.h" |
| 25 #include "net/http/http_network_session.h" |
25 #include "net/http/http_request_info.h" | 26 #include "net/http/http_request_info.h" |
26 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
27 #include "net/http/http_response_info.h" | 28 #include "net/http/http_response_info.h" |
28 #include "net/http/http_transaction.h" | 29 #include "net/http/http_transaction.h" |
29 #include "net/http/http_util.h" | 30 #include "net/http/http_util.h" |
30 | 31 |
31 using base::Time; | 32 using base::Time; |
32 | 33 |
33 namespace net { | 34 namespace net { |
34 | 35 |
(...skipping 534 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 DCHECK(rv != ERR_IO_PENDING); | 570 DCHECK(rv != ERR_IO_PENDING); |
570 if (callback_) | 571 if (callback_) |
571 DoCallback(rv); | 572 DoCallback(rv); |
572 return rv; | 573 return rv; |
573 } | 574 } |
574 | 575 |
575 void HttpCache::Transaction::SetRequest(const HttpRequestInfo* request) { | 576 void HttpCache::Transaction::SetRequest(const HttpRequestInfo* request) { |
576 request_ = request; | 577 request_ = request; |
577 effective_load_flags_ = request_->load_flags; | 578 effective_load_flags_ = request_->load_flags; |
578 | 579 |
579 // When in playback mode, we want to load exclusively from the cache. | 580 switch(cache_->mode()) { |
580 if (cache_->mode() == PLAYBACK) | 581 case NORMAL: |
581 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE; | 582 break; |
582 | 583 case RECORD: |
583 // When in record mode, we want to NEVER load from the cache. | 584 // When in record mode, we want to NEVER load from the cache. |
584 // The reason for this is beacuse we save the Set-Cookie headers | 585 // The reason for this is beacuse we save the Set-Cookie headers |
585 // (intentionally). If we read from the cache, we replay them | 586 // (intentionally). If we read from the cache, we replay them |
586 // prematurely. | 587 // prematurely. |
587 if (cache_->mode() == RECORD) | 588 effective_load_flags_ |= LOAD_BYPASS_CACHE; |
588 effective_load_flags_ |= LOAD_BYPASS_CACHE; | 589 case PLAYBACK: |
| 590 // When in playback mode, we want to load exclusively from the cache. |
| 591 effective_load_flags_ |= LOAD_ONLY_FROM_CACHE; |
| 592 break; |
| 593 case DISABLE: |
| 594 effective_load_flags_ |= LOAD_DISABLE_CACHE; |
| 595 break; |
| 596 } |
589 | 597 |
590 // Some headers imply load flags. The order here is significant. | 598 // Some headers imply load flags. The order here is significant. |
591 // | 599 // |
592 // LOAD_DISABLE_CACHE : no cache read or write | 600 // LOAD_DISABLE_CACHE : no cache read or write |
593 // LOAD_BYPASS_CACHE : no cache read | 601 // LOAD_BYPASS_CACHE : no cache read |
594 // LOAD_VALIDATE_CACHE : no cache read unless validation | 602 // LOAD_VALIDATE_CACHE : no cache read unless validation |
595 // | 603 // |
596 // The former modes trump latter modes, so if we find a matching header we | 604 // The former modes trump latter modes, so if we find a matching header we |
597 // can stop iterating kSpecialHeaders. | 605 // can stop iterating kSpecialHeaders. |
598 // | 606 // |
(...skipping 559 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1158 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, | 1166 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, |
1159 true) == len; | 1167 true) == len; |
1160 } | 1168 } |
1161 | 1169 |
1162 // Generate a key that can be used inside the cache. | 1170 // Generate a key that can be used inside the cache. |
1163 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) { | 1171 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) { |
1164 std::string url = request->url.spec(); | 1172 std::string url = request->url.spec(); |
1165 if (request->url.has_ref()) | 1173 if (request->url.has_ref()) |
1166 url.erase(url.find_last_of('#')); | 1174 url.erase(url.find_last_of('#')); |
1167 | 1175 |
| 1176 DCHECK(mode_ != DISABLE); |
1168 if (mode_ == NORMAL) { | 1177 if (mode_ == NORMAL) { |
1169 // No valid URL can begin with numerals, so we should not have to worry | 1178 // No valid URL can begin with numerals, so we should not have to worry |
1170 // about collisions with normal URLs. | 1179 // about collisions with normal URLs. |
1171 if (request->upload_data && request->upload_data->identifier()) | 1180 if (request->upload_data && request->upload_data->identifier()) |
1172 url.insert(0, StringPrintf("%lld/", request->upload_data->identifier())); | 1181 url.insert(0, StringPrintf("%lld/", request->upload_data->identifier())); |
1173 return url; | 1182 return url; |
1174 } | 1183 } |
1175 | 1184 |
1176 // In playback and record mode, we cache everything. | 1185 // In playback and record mode, we cache everything. |
1177 | 1186 |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1454 // Promote next transaction from the pending queue. | 1463 // Promote next transaction from the pending queue. |
1455 Transaction* next = entry->pending_queue.front(); | 1464 Transaction* next = entry->pending_queue.front(); |
1456 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) | 1465 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) |
1457 return; // have to wait | 1466 return; // have to wait |
1458 | 1467 |
1459 entry->pending_queue.erase(entry->pending_queue.begin()); | 1468 entry->pending_queue.erase(entry->pending_queue.begin()); |
1460 | 1469 |
1461 AddTransactionToEntry(entry, next); | 1470 AddTransactionToEntry(entry, next); |
1462 } | 1471 } |
1463 | 1472 |
| 1473 void HttpCache::CloseIdleConnections() { |
| 1474 net::HttpNetworkLayer* network = |
| 1475 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); |
| 1476 HttpNetworkSession* session = network->GetSession(); |
| 1477 if (session) { |
| 1478 session->connection_pool()->CloseIdleSockets(); |
| 1479 } |
| 1480 } |
| 1481 |
1464 //----------------------------------------------------------------------------- | 1482 //----------------------------------------------------------------------------- |
1465 | 1483 |
1466 } // namespace net | 1484 } // namespace net |
OLD | NEW |