Chromium Code Reviews| 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/tools/quic/quic_in_memory_cache.h" | 5 #include "net/tools/quic/quic_in_memory_cache.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 }; | 79 }; |
| 80 | 80 |
| 81 } // namespace | 81 } // namespace |
| 82 | 82 |
| 83 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { | 83 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { |
| 84 return Singleton<QuicInMemoryCache>::get(); | 84 return Singleton<QuicInMemoryCache>::get(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( | 87 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( |
| 88 const BalsaHeaders& request_headers) const { | 88 const BalsaHeaders& request_headers) const { |
| 89 string key = GetKey(request_headers); | 89 ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); |
| 90 StringPiece url(key); | |
| 91 // Removing the leading https:// or http://. | |
| 92 if (StringPieceUtils::StartsWithIgnoreCase(url, "https://")) { | |
| 93 url.remove_prefix(8); | |
| 94 } else if (StringPieceUtils::StartsWithIgnoreCase(url, "http://")) { | |
| 95 url.remove_prefix(7); | |
| 96 } | |
| 97 | |
| 98 ResponseMap::const_iterator it = responses_.find(url.as_string()); | |
| 99 if (it == responses_.end()) { | 90 if (it == responses_.end()) { |
| 100 return NULL; | 91 return NULL; |
| 101 } | 92 } |
| 102 return it->second; | 93 return it->second; |
| 103 } | 94 } |
| 104 | 95 |
| 105 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 96 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
| 106 const BalsaHeaders& response_headers, | 97 const BalsaHeaders& response_headers, |
| 107 StringPiece response_body) { | 98 StringPiece response_body) { |
| 108 LOG(INFO) << "Adding response for: " << GetKey(request_headers); | 99 LOG(INFO) << "Adding response for: " << GetKey(request_headers); |
| 109 if (ContainsKey(responses_, GetKey(request_headers))) { | 100 if (ContainsKey(responses_, GetKey(request_headers))) { |
| 110 LOG(DFATAL) << "Response for given request already exists!"; | 101 LOG(DFATAL) << "Response for given request already exists!"; |
| 111 } | 102 } |
| 112 Response* new_response = new Response(); | 103 Response* new_response = new Response(); |
| 113 new_response->set_headers(response_headers); | 104 new_response->set_headers(response_headers); |
| 114 new_response->set_body(response_body); | 105 new_response->set_body(response_body); |
| 115 responses_[GetKey(request_headers)] = new_response; | 106 responses_[GetKey(request_headers)] = new_response; |
| 116 } | 107 } |
| 117 | 108 |
| 118 void QuicInMemoryCache::ResetForTests() { | 109 void QuicInMemoryCache::ResetForTests() { |
| 119 STLDeleteValues(&responses_); | 110 STLDeleteValues(&responses_); |
|
Ryan Hamilton
2013/07/04 14:46:03
I think you should call Initialize here, as well.
honghaiz
2013/07/04 17:34:04
Done.
| |
| 120 } | 111 } |
| 121 | 112 |
| 122 QuicInMemoryCache::QuicInMemoryCache() { | 113 QuicInMemoryCache::QuicInMemoryCache() { |
| 114 Initialize(); | |
| 115 } | |
| 116 | |
| 117 void QuicInMemoryCache::Initialize() { | |
| 123 // If there's no defined cache dir, we have no initialization to do. | 118 // If there's no defined cache dir, we have no initialization to do. |
| 124 if (FLAGS_quic_in_memory_cache_dir.empty()) { | 119 if (FLAGS_quic_in_memory_cache_dir.empty()) { |
| 125 LOG(WARNING) << "No cache directory found. Skipping initialization."; | 120 LOG(WARNING) << "No cache directory found. Skipping initialization."; |
| 126 return; | 121 return; |
| 127 } | 122 } |
| 128 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " | 123 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " |
| 129 << FLAGS_quic_in_memory_cache_dir; | 124 << FLAGS_quic_in_memory_cache_dir; |
| 130 | 125 |
| 131 FilePath directory(FLAGS_quic_in_memory_cache_dir); | 126 FilePath directory(FLAGS_quic_in_memory_cache_dir); |
| 132 base::FileEnumerator file_list(directory, | 127 base::FileEnumerator file_list(directory, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 | 195 |
| 201 file = file_list.Next(); | 196 file = file_list.Next(); |
| 202 } | 197 } |
| 203 } | 198 } |
| 204 | 199 |
| 205 QuicInMemoryCache::~QuicInMemoryCache() { | 200 QuicInMemoryCache::~QuicInMemoryCache() { |
| 206 STLDeleteValues(&responses_); | 201 STLDeleteValues(&responses_); |
| 207 } | 202 } |
| 208 | 203 |
| 209 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { | 204 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { |
| 210 return request_headers.GetHeader("host").as_string() + | 205 StringPiece uri = request_headers.request_uri(); |
| 211 request_headers.request_uri().as_string(); | 206 if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 207 uri.remove_prefix(8); | |
| 208 return uri.as_string(); | |
| 209 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | |
| 210 uri.remove_prefix(7); | |
| 211 return uri.as_string(); | |
| 212 } | |
| 213 return request_headers.GetHeader("host").as_string() + uri.as_string(); | |
|
Ryan Hamilton
2013/07/04 14:46:03
So if the host header is set and the URI is fully
honghaiz
2013/07/04 17:34:04
Fixed.
On 2013/07/04 14:46:03, Ryan Hamilton wrot
| |
| 212 } | 214 } |
| 213 | 215 |
| 214 } // namespace tools | 216 } // namespace tools |
| 215 } // namespace net | 217 } // namespace net |
| OLD | NEW |