| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/quic/quic_in_memory_cache.h" | 5 #include "net/quic/quic_in_memory_cache.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 9 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 11 #include "net/tools/balsa/balsa_headers.h" | 10 #include "net/tools/balsa/balsa_headers.h" |
| 12 | 11 |
| 13 using base::FilePath; | 12 using base::FilePath; |
| 14 using base::StringPiece; | 13 using base::StringPiece; |
| 15 using std::string; | 14 using std::string; |
| 16 | 15 |
| 17 // Specifies the directory used during QuicInMemoryCache | 16 // Specifies the directory used during QuicInMemoryCache |
| 18 // construction to seed the cache. Cache directory can be | 17 // construction to seed the cache. Cache directory can be |
| 19 // generated using `wget -p --save-headers <url> | 18 // generated using `wget -p --save-headers <url> |
| 20 | 19 |
| 21 namespace net { | 20 namespace net { |
| 22 | 21 |
| 22 FilePath::StringType g_quic_in_memory_cache_dir = FILE_PATH_LITERAL(""); |
| 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 const FilePath::CharType* g_quic_in_memory_cache_dir = FILE_PATH_LITERAL(""); | |
| 26 | |
| 27 // BalsaVisitor implementation (glue) which caches response bodies. | 26 // BalsaVisitor implementation (glue) which caches response bodies. |
| 28 class CachingBalsaVisitor : public NoOpBalsaVisitor { | 27 class CachingBalsaVisitor : public NoOpBalsaVisitor { |
| 29 public: | 28 public: |
| 30 CachingBalsaVisitor() : done_framing_(false) {} | 29 CachingBalsaVisitor() : done_framing_(false) {} |
| 31 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { | 30 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { |
| 32 AppendToBody(input, size); | 31 AppendToBody(input, size); |
| 33 } | 32 } |
| 34 virtual void MessageDone() OVERRIDE { | 33 virtual void MessageDone() OVERRIDE { |
| 35 done_framing_ = true; | 34 done_framing_ = true; |
| 36 } | 35 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 StringPiece response_code, | 81 StringPiece response_code, |
| 83 StringPiece response_detail, | 82 StringPiece response_detail, |
| 84 StringPiece body) { | 83 StringPiece body) { |
| 85 BalsaHeaders request_headers, response_headers; | 84 BalsaHeaders request_headers, response_headers; |
| 86 request_headers.SetRequestFirstlineFromStringPieces(method, | 85 request_headers.SetRequestFirstlineFromStringPieces(method, |
| 87 path, | 86 path, |
| 88 version); | 87 version); |
| 89 response_headers.SetRequestFirstlineFromStringPieces(version, | 88 response_headers.SetRequestFirstlineFromStringPieces(version, |
| 90 response_code, | 89 response_code, |
| 91 response_detail); | 90 response_detail); |
| 92 response_headers.AppendHeader("content-length", | 91 response_headers.AppendHeader( |
| 93 base::IntToString(body.length())); | 92 "content-length", |
| 93 base::Uint64ToString(static_cast<uint64>(body.length()))); |
| 94 | 94 |
| 95 AddResponse(request_headers, response_headers, body); | 95 AddResponse(request_headers, response_headers, body); |
| 96 } | 96 } |
| 97 | 97 |
| 98 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 98 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
| 99 const BalsaHeaders& response_headers, | 99 const BalsaHeaders& response_headers, |
| 100 StringPiece response_body) { | 100 StringPiece response_body) { |
| 101 VLOG(1) << "Adding response for: " << GetKey(request_headers); | 101 VLOG(1) << "Adding response for: " << GetKey(request_headers); |
| 102 if (ContainsKey(responses_, GetKey(request_headers))) { | 102 if (ContainsKey(responses_, GetKey(request_headers))) { |
| 103 LOG(DFATAL) << "Response for given request already exists!"; | 103 LOG(DFATAL) << "Response for given request already exists!"; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 125 Initialize(); | 125 Initialize(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void QuicInMemoryCache::ResetForTests() { | 128 void QuicInMemoryCache::ResetForTests() { |
| 129 STLDeleteValues(&responses_); | 129 STLDeleteValues(&responses_); |
| 130 Initialize(); | 130 Initialize(); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void QuicInMemoryCache::Initialize() { | 133 void QuicInMemoryCache::Initialize() { |
| 134 // If there's no defined cache dir, we have no initialization to do. | 134 // If there's no defined cache dir, we have no initialization to do. |
| 135 if (g_quic_in_memory_cache_dir[0] == '\0') { | 135 if (g_quic_in_memory_cache_dir.size() == 0) { |
| 136 VLOG(1) << "No cache directory found. Skipping initialization."; | 136 VLOG(1) << "No cache directory found. Skipping initialization."; |
| 137 return; | 137 return; |
| 138 } | 138 } |
| 139 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " | 139 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " |
| 140 << g_quic_in_memory_cache_dir; | 140 << g_quic_in_memory_cache_dir; |
| 141 | 141 |
| 142 FilePath directory(g_quic_in_memory_cache_dir); | 142 FilePath directory(g_quic_in_memory_cache_dir); |
| 143 base::FileEnumerator file_list(directory, | 143 base::FileEnumerator file_list(directory, |
| 144 true, | 144 true, |
| 145 base::FileEnumerator::FILES); | 145 base::FileEnumerator::FILES); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 178 } |
| 179 if (processed < file_contents.length()) { | 179 if (processed < file_contents.length()) { |
| 180 // Didn't frame whole file. Assume remainder is body. | 180 // Didn't frame whole file. Assume remainder is body. |
| 181 // This sometimes happens as a result of incompatibilities between | 181 // This sometimes happens as a result of incompatibilities between |
| 182 // BalsaFramer and wget's serialization of HTTP sans content-length. | 182 // BalsaFramer and wget's serialization of HTTP sans content-length. |
| 183 caching_visitor.AppendToBody(file_contents.c_str() + processed, | 183 caching_visitor.AppendToBody(file_contents.c_str() + processed, |
| 184 file_contents.length() - processed); | 184 file_contents.length() - processed); |
| 185 processed += file_contents.length(); | 185 processed += file_contents.length(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 StringPiece base = file.AsUTF8Unsafe(); | 188 string utf8_file = file.AsUTF8Unsafe(); |
| 189 StringPiece base = utf8_file; |
| 189 if (response_headers.HasHeader("X-Original-Url")) { | 190 if (response_headers.HasHeader("X-Original-Url")) { |
| 190 base = response_headers.GetHeader("X-Original-Url"); | 191 base = response_headers.GetHeader("X-Original-Url"); |
| 191 response_headers.RemoveAllOfHeader("X-Original-Url"); | 192 response_headers.RemoveAllOfHeader("X-Original-Url"); |
| 192 // Remove the protocol so that the string is of the form host + path, | 193 // Remove the protocol so that the string is of the form host + path, |
| 193 // which is parsed properly below. | 194 // which is parsed properly below. |
| 194 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { | 195 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { |
| 195 base.remove_prefix(8); | 196 base.remove_prefix(8); |
| 196 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { | 197 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { |
| 197 base.remove_prefix(7); | 198 base.remove_prefix(7); |
| 198 } | 199 } |
| 199 } | 200 } |
| 200 int path_start = base.find_first_of('/'); | 201 size_t path_start = base.find_first_of('/'); |
| 201 DCHECK_LT(0, path_start); | 202 DCHECK_LT(0U, path_start); |
| 202 StringPiece host(base.substr(0, path_start)); | 203 StringPiece host(base.substr(0, path_start)); |
| 203 StringPiece path(base.substr(path_start)); | 204 StringPiece path(base.substr(path_start)); |
| 204 if (path[path.length() - 1] == ',') { | 205 if (path[path.length() - 1] == ',') { |
| 205 path.remove_suffix(1); | 206 path.remove_suffix(1); |
| 206 } | 207 } |
| 207 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. | 208 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. |
| 208 request_headers.SetRequestFirstlineFromStringPieces("GET", | 209 request_headers.SetRequestFirstlineFromStringPieces("GET", |
| 209 path, | 210 path, |
| 210 "HTTP/1.1"); | 211 "HTTP/1.1"); |
| 211 request_headers.ReplaceOrAppendHeader("host", host); | 212 request_headers.ReplaceOrAppendHeader("host", host); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 233 host = request_headers.GetHeader("host"); | 234 host = request_headers.GetHeader("host"); |
| 234 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 235 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 235 uri.remove_prefix(8); | 236 uri.remove_prefix(8); |
| 236 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 237 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 237 uri.remove_prefix(7); | 238 uri.remove_prefix(7); |
| 238 } | 239 } |
| 239 return host.as_string() + uri.as_string(); | 240 return host.as_string() + uri.as_string(); |
| 240 } | 241 } |
| 241 | 242 |
| 242 } // namespace net | 243 } // namespace net |
| OLD | NEW |