| 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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 Initialize(); | 124 Initialize(); |
| 126 } | 125 } |
| 127 | 126 |
| 128 void QuicInMemoryCache::ResetForTests() { | 127 void QuicInMemoryCache::ResetForTests() { |
| 129 STLDeleteValues(&responses_); | 128 STLDeleteValues(&responses_); |
| 130 Initialize(); | 129 Initialize(); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void QuicInMemoryCache::Initialize() { | 132 void QuicInMemoryCache::Initialize() { |
| 134 // If there's no defined cache dir, we have no initialization to do. | 133 // If there's no defined cache dir, we have no initialization to do. |
| 135 if (g_quic_in_memory_cache_dir[0] == '\0') { | 134 if (g_quic_in_memory_cache_dir.size() == 0) { |
| 136 VLOG(1) << "No cache directory found. Skipping initialization."; | 135 VLOG(1) << "No cache directory found. Skipping initialization."; |
| 137 return; | 136 return; |
| 138 } | 137 } |
| 139 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " | 138 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " |
| 140 << g_quic_in_memory_cache_dir; | 139 << g_quic_in_memory_cache_dir; |
| 141 | 140 |
| 142 FilePath directory(g_quic_in_memory_cache_dir); | 141 FilePath directory(g_quic_in_memory_cache_dir); |
| 143 base::FileEnumerator file_list(directory, | 142 base::FileEnumerator file_list(directory, |
| 144 true, | 143 true, |
| 145 base::FileEnumerator::FILES); | 144 base::FileEnumerator::FILES); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 177 } |
| 179 if (processed < file_contents.length()) { | 178 if (processed < file_contents.length()) { |
| 180 // Didn't frame whole file. Assume remainder is body. | 179 // Didn't frame whole file. Assume remainder is body. |
| 181 // This sometimes happens as a result of incompatibilities between | 180 // This sometimes happens as a result of incompatibilities between |
| 182 // BalsaFramer and wget's serialization of HTTP sans content-length. | 181 // BalsaFramer and wget's serialization of HTTP sans content-length. |
| 183 caching_visitor.AppendToBody(file_contents.c_str() + processed, | 182 caching_visitor.AppendToBody(file_contents.c_str() + processed, |
| 184 file_contents.length() - processed); | 183 file_contents.length() - processed); |
| 185 processed += file_contents.length(); | 184 processed += file_contents.length(); |
| 186 } | 185 } |
| 187 | 186 |
| 188 StringPiece base = file.AsUTF8Unsafe(); | 187 string utf8_file = file.AsUTF8Unsafe(); |
| 188 StringPiece base = utf8_file; |
| 189 if (response_headers.HasHeader("X-Original-Url")) { | 189 if (response_headers.HasHeader("X-Original-Url")) { |
| 190 base = response_headers.GetHeader("X-Original-Url"); | 190 base = response_headers.GetHeader("X-Original-Url"); |
| 191 response_headers.RemoveAllOfHeader("X-Original-Url"); | 191 response_headers.RemoveAllOfHeader("X-Original-Url"); |
| 192 // Remove the protocol so that the string is of the form host + path, | 192 // Remove the protocol so that the string is of the form host + path, |
| 193 // which is parsed properly below. | 193 // which is parsed properly below. |
| 194 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { | 194 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { |
| 195 base.remove_prefix(8); | 195 base.remove_prefix(8); |
| 196 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { | 196 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { |
| 197 base.remove_prefix(7); | 197 base.remove_prefix(7); |
| 198 } | 198 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 host = request_headers.GetHeader("host"); | 233 host = request_headers.GetHeader("host"); |
| 234 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 234 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 235 uri.remove_prefix(8); | 235 uri.remove_prefix(8); |
| 236 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 236 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 237 uri.remove_prefix(7); | 237 uri.remove_prefix(7); |
| 238 } | 238 } |
| 239 return host.as_string() + uri.as_string(); | 239 return host.as_string() + uri.as_string(); |
| 240 } | 240 } |
| 241 | 241 |
| 242 } // namespace net | 242 } // namespace net |
| OLD | NEW |