| 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 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 response_detail); | 92 response_detail); |
| 93 response_headers.AppendHeader("content-length", | 93 response_headers.AppendHeader("content-length", |
| 94 base::IntToString(body.length())); | 94 base::IntToString(body.length())); |
| 95 | 95 |
| 96 AddResponse(request_headers, response_headers, body); | 96 AddResponse(request_headers, response_headers, body); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 99 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
| 100 const BalsaHeaders& response_headers, | 100 const BalsaHeaders& response_headers, |
| 101 StringPiece response_body) { | 101 StringPiece response_body) { |
| 102 LOG(INFO) << "Adding response for: " << GetKey(request_headers); | 102 VLOG(0) << "Adding response for: " << GetKey(request_headers); |
| 103 if (ContainsKey(responses_, GetKey(request_headers))) { | 103 if (ContainsKey(responses_, GetKey(request_headers))) { |
| 104 LOG(DFATAL) << "Response for given request already exists!"; | 104 LOG(DFATAL) << "Response for given request already exists!"; |
| 105 return; | 105 return; |
| 106 } | 106 } |
| 107 Response* new_response = new Response(); | 107 Response* new_response = new Response(); |
| 108 new_response->set_headers(response_headers); | 108 new_response->set_headers(response_headers); |
| 109 new_response->set_body(response_body); | 109 new_response->set_body(response_body); |
| 110 responses_[GetKey(request_headers)] = new_response; | 110 responses_[GetKey(request_headers)] = new_response; |
| 111 } | 111 } |
| 112 | 112 |
| 113 QuicInMemoryCache::QuicInMemoryCache() { | 113 QuicInMemoryCache::QuicInMemoryCache() { |
| 114 Initialize(); | 114 Initialize(); |
| 115 } | 115 } |
| 116 | 116 |
| 117 void QuicInMemoryCache::ResetForTests() { | 117 void QuicInMemoryCache::ResetForTests() { |
| 118 STLDeleteValues(&responses_); | 118 STLDeleteValues(&responses_); |
| 119 Initialize(); | 119 Initialize(); |
| 120 } | 120 } |
| 121 | 121 |
| 122 void QuicInMemoryCache::Initialize() { | 122 void QuicInMemoryCache::Initialize() { |
| 123 // If there's no defined cache dir, we have no initialization to do. | 123 // If there's no defined cache dir, we have no initialization to do. |
| 124 if (FLAGS_quic_in_memory_cache_dir.empty()) { | 124 if (FLAGS_quic_in_memory_cache_dir.empty()) { |
| 125 LOG(WARNING) << "No cache directory found. Skipping initialization."; | 125 LOG(WARNING) << "No cache directory found. Skipping initialization."; |
| 126 return; | 126 return; |
| 127 } | 127 } |
| 128 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " | 128 VLOG(0) << "Attempting to initialize QuicInMemoryCache from directory: " |
| 129 << FLAGS_quic_in_memory_cache_dir; | 129 << FLAGS_quic_in_memory_cache_dir; |
| 130 | 130 |
| 131 FilePath directory(FLAGS_quic_in_memory_cache_dir); | 131 FilePath directory(FLAGS_quic_in_memory_cache_dir); |
| 132 base::FileEnumerator file_list(directory, | 132 base::FileEnumerator file_list(directory, |
| 133 true, | 133 true, |
| 134 base::FileEnumerator::FILES); | 134 base::FileEnumerator::FILES); |
| 135 | 135 |
| 136 FilePath file = file_list.Next(); | 136 FilePath file = file_list.Next(); |
| 137 while (!file.empty()) { | 137 while (!file.empty()) { |
| 138 // Need to skip files in .svn directories | 138 // Need to skip files in .svn directories |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 StringPiece path(base.substr(path_start)); | 192 StringPiece path(base.substr(path_start)); |
| 193 if (path[path.length() - 1] == ',') { | 193 if (path[path.length() - 1] == ',') { |
| 194 path.remove_suffix(1); | 194 path.remove_suffix(1); |
| 195 } | 195 } |
| 196 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. | 196 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. |
| 197 request_headers.SetRequestFirstlineFromStringPieces("GET", | 197 request_headers.SetRequestFirstlineFromStringPieces("GET", |
| 198 path, | 198 path, |
| 199 "HTTP/1.1"); | 199 "HTTP/1.1"); |
| 200 request_headers.ReplaceOrAppendHeader("host", host); | 200 request_headers.ReplaceOrAppendHeader("host", host); |
| 201 | 201 |
| 202 LOG(INFO) << "Inserting 'http://" << GetKey(request_headers) | 202 VLOG(0) << "Inserting 'http://" << GetKey(request_headers) |
| 203 << "' into QuicInMemoryCache."; | 203 << "' into QuicInMemoryCache."; |
| 204 | 204 |
| 205 AddResponse(request_headers, response_headers, caching_visitor.body()); | 205 AddResponse(request_headers, response_headers, caching_visitor.body()); |
| 206 | 206 |
| 207 file = file_list.Next(); | 207 file = file_list.Next(); |
| 208 } | 208 } |
| 209 } | 209 } |
| 210 | 210 |
| 211 QuicInMemoryCache::~QuicInMemoryCache() { | 211 QuicInMemoryCache::~QuicInMemoryCache() { |
| 212 STLDeleteValues(&responses_); | 212 STLDeleteValues(&responses_); |
| 213 } | 213 } |
| 214 | 214 |
| 215 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { | 215 string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { |
| 216 StringPiece uri = request_headers.request_uri(); | 216 StringPiece uri = request_headers.request_uri(); |
| 217 StringPiece host; | 217 StringPiece host; |
| 218 if (uri[0] == '/') { | 218 if (uri[0] == '/') { |
| 219 host = request_headers.GetHeader("host"); | 219 host = request_headers.GetHeader("host"); |
| 220 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 220 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 221 uri.remove_prefix(8); | 221 uri.remove_prefix(8); |
| 222 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 222 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 223 uri.remove_prefix(7); | 223 uri.remove_prefix(7); |
| 224 } | 224 } |
| 225 return host.as_string() + uri.as_string(); | 225 return host.as_string() + uri.as_string(); |
| 226 } | 226 } |
| 227 | 227 |
| 228 } // namespace tools | 228 } // namespace tools |
| 229 } // namespace net | 229 } // namespace net |
| OLD | NEW |