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 g_quic_in_memory_cache_dir = FilePath(FILE_PATH_LITERAL("")); | |
wtc
2014/06/18 02:04:46
Global objects are banned in Chromium. We can prob
dmziegler
2014/06/18 20:13:41
I've made it a FilePath::StringType.
In general,
| |
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.value().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.value(); |
141 | 140 |
142 FilePath directory(g_quic_in_memory_cache_dir); | 141 base::FileEnumerator file_list(g_quic_in_memory_cache_dir, |
143 base::FileEnumerator file_list(directory, | |
144 true, | 142 true, |
145 base::FileEnumerator::FILES); | 143 base::FileEnumerator::FILES); |
146 | 144 |
147 FilePath file = file_list.Next(); | 145 FilePath file = file_list.Next(); |
148 while (!file.empty()) { | 146 while (!file.empty()) { |
149 // Need to skip files in .svn directories | 147 // Need to skip files in .svn directories |
150 if (file.value().find(FILE_PATH_LITERAL("/.svn/")) != std::string::npos) { | 148 if (file.value().find(FILE_PATH_LITERAL("/.svn/")) != std::string::npos) { |
151 file = file_list.Next(); | 149 file = file_list.Next(); |
152 continue; | 150 continue; |
153 } | 151 } |
(...skipping 24 matching lines...) Expand all Loading... | |
178 } | 176 } |
179 if (processed < file_contents.length()) { | 177 if (processed < file_contents.length()) { |
180 // Didn't frame whole file. Assume remainder is body. | 178 // Didn't frame whole file. Assume remainder is body. |
181 // This sometimes happens as a result of incompatibilities between | 179 // This sometimes happens as a result of incompatibilities between |
182 // BalsaFramer and wget's serialization of HTTP sans content-length. | 180 // BalsaFramer and wget's serialization of HTTP sans content-length. |
183 caching_visitor.AppendToBody(file_contents.c_str() + processed, | 181 caching_visitor.AppendToBody(file_contents.c_str() + processed, |
184 file_contents.length() - processed); | 182 file_contents.length() - processed); |
185 processed += file_contents.length(); | 183 processed += file_contents.length(); |
186 } | 184 } |
187 | 185 |
188 StringPiece base = file.AsUTF8Unsafe(); | 186 string utf8File = file.AsUTF8Unsafe(); |
wtc
2014/06/18 02:04:46
Name this variable "utf8_file".
Actually you shou
dmziegler
2014/06/18 20:13:41
This would require more unnecessary copying, since
dmz
2014/06/19 20:06:28
Update: My next CL changes things anyway.
| |
187 StringPiece base = utf8File; | |
189 if (response_headers.HasHeader("X-Original-Url")) { | 188 if (response_headers.HasHeader("X-Original-Url")) { |
190 base = response_headers.GetHeader("X-Original-Url"); | 189 base = response_headers.GetHeader("X-Original-Url"); |
191 response_headers.RemoveAllOfHeader("X-Original-Url"); | 190 response_headers.RemoveAllOfHeader("X-Original-Url"); |
192 // Remove the protocol so that the string is of the form host + path, | 191 // Remove the protocol so that the string is of the form host + path, |
193 // which is parsed properly below. | 192 // which is parsed properly below. |
194 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { | 193 if (StringPieceUtils::StartsWithIgnoreCase(base, "https://")) { |
195 base.remove_prefix(8); | 194 base.remove_prefix(8); |
196 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { | 195 } else if (StringPieceUtils::StartsWithIgnoreCase(base, "http://")) { |
197 base.remove_prefix(7); | 196 base.remove_prefix(7); |
198 } | 197 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
233 host = request_headers.GetHeader("host"); | 232 host = request_headers.GetHeader("host"); |
234 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 233 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
235 uri.remove_prefix(8); | 234 uri.remove_prefix(8); |
236 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 235 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
237 uri.remove_prefix(7); | 236 uri.remove_prefix(7); |
238 } | 237 } |
239 return host.as_string() + uri.as_string(); | 238 return host.as_string() + uri.as_string(); |
240 } | 239 } |
241 | 240 |
242 } // namespace net | 241 } // namespace net |
OLD | NEW |