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_); |
| 111 Initialize(); |
120 } | 112 } |
121 | 113 |
122 QuicInMemoryCache::QuicInMemoryCache() { | 114 QuicInMemoryCache::QuicInMemoryCache() { |
| 115 Initialize(); |
| 116 } |
| 117 |
| 118 void QuicInMemoryCache::Initialize() { |
123 // If there's no defined cache dir, we have no initialization to do. | 119 // If there's no defined cache dir, we have no initialization to do. |
124 if (FLAGS_quic_in_memory_cache_dir.empty()) { | 120 if (FLAGS_quic_in_memory_cache_dir.empty()) { |
125 LOG(WARNING) << "No cache directory found. Skipping initialization."; | 121 LOG(WARNING) << "No cache directory found. Skipping initialization."; |
126 return; | 122 return; |
127 } | 123 } |
128 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " | 124 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " |
129 << FLAGS_quic_in_memory_cache_dir; | 125 << FLAGS_quic_in_memory_cache_dir; |
130 | 126 |
131 FilePath directory(FLAGS_quic_in_memory_cache_dir); | 127 FilePath directory(FLAGS_quic_in_memory_cache_dir); |
132 base::FileEnumerator file_list(directory, | 128 base::FileEnumerator file_list(directory, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 196 |
201 file = file_list.Next(); | 197 file = file_list.Next(); |
202 } | 198 } |
203 } | 199 } |
204 | 200 |
205 QuicInMemoryCache::~QuicInMemoryCache() { | 201 QuicInMemoryCache::~QuicInMemoryCache() { |
206 STLDeleteValues(&responses_); | 202 STLDeleteValues(&responses_); |
207 } | 203 } |
208 | 204 |
209 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { | 205 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { |
210 return request_headers.GetHeader("host").as_string() + | 206 StringPiece uri = request_headers.request_uri(); |
211 request_headers.request_uri().as_string(); | 207 StringPiece host; |
| 208 if (uri[0] == '/') { |
| 209 host = request_headers.GetHeader("host"); |
| 210 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 211 uri.remove_prefix(8); |
| 212 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 213 uri.remove_prefix(7); |
| 214 } |
| 215 return host.as_string() + uri.as_string(); |
212 } | 216 } |
213 | 217 |
214 } // namespace tools | 218 } // namespace tools |
215 } // namespace net | 219 } // namespace net |
OLD | NEW |