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 14 matching lines...) Expand all Loading... |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 // BalsaVisitor implementation (glue) which caches response bodies. | 28 // BalsaVisitor implementation (glue) which caches response bodies. |
29 class CachingBalsaVisitor : public NoOpBalsaVisitor { | 29 class CachingBalsaVisitor : public NoOpBalsaVisitor { |
30 public: | 30 public: |
31 CachingBalsaVisitor() : done_framing_(false) {} | 31 CachingBalsaVisitor() : done_framing_(false) {} |
32 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { | 32 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { |
33 AppendToBody(input, size); | 33 AppendToBody(input, size); |
34 } | 34 } |
35 virtual void MessageDone() OVERRIDE { | 35 virtual void MessageDone() OVERRIDE { done_framing_ = true; } |
36 done_framing_ = true; | |
37 } | |
38 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE { | 36 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE { |
39 UnhandledError(); | 37 UnhandledError(); |
40 } | 38 } |
41 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE { | 39 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE { |
42 UnhandledError(); | 40 UnhandledError(); |
43 } | 41 } |
44 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE { | 42 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE { |
45 UnhandledError(); | 43 UnhandledError(); |
46 } | 44 } |
47 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE { | 45 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE { |
48 UnhandledError(); | 46 UnhandledError(); |
49 } | 47 } |
50 void UnhandledError() { | 48 void UnhandledError() { LOG(DFATAL) << "Unhandled error framing HTTP."; } |
51 LOG(DFATAL) << "Unhandled error framing HTTP."; | |
52 } | |
53 void AppendToBody(const char* input, size_t size) { | 49 void AppendToBody(const char* input, size_t size) { |
54 body_.append(input, size); | 50 body_.append(input, size); |
55 } | 51 } |
56 bool done_framing() const { return done_framing_; } | 52 bool done_framing() const { return done_framing_; } |
57 const string& body() const { return body_; } | 53 const string& body() const { return body_; } |
58 | 54 |
59 private: | 55 private: |
60 bool done_framing_; | 56 bool done_framing_; |
61 string body_; | 57 string body_; |
62 }; | 58 }; |
(...skipping 14 matching lines...) Expand all Loading... |
77 return it->second; | 73 return it->second; |
78 } | 74 } |
79 | 75 |
80 void QuicInMemoryCache::AddSimpleResponse(StringPiece method, | 76 void QuicInMemoryCache::AddSimpleResponse(StringPiece method, |
81 StringPiece path, | 77 StringPiece path, |
82 StringPiece version, | 78 StringPiece version, |
83 StringPiece response_code, | 79 StringPiece response_code, |
84 StringPiece response_detail, | 80 StringPiece response_detail, |
85 StringPiece body) { | 81 StringPiece body) { |
86 BalsaHeaders request_headers, response_headers; | 82 BalsaHeaders request_headers, response_headers; |
87 request_headers.SetRequestFirstlineFromStringPieces(method, | 83 request_headers.SetRequestFirstlineFromStringPieces(method, path, version); |
88 path, | 84 response_headers.SetRequestFirstlineFromStringPieces( |
89 version); | 85 version, response_code, response_detail); |
90 response_headers.SetRequestFirstlineFromStringPieces(version, | |
91 response_code, | |
92 response_detail); | |
93 response_headers.AppendHeader("content-length", | 86 response_headers.AppendHeader("content-length", |
94 base::IntToString(body.length())); | 87 base::IntToString(body.length())); |
95 | 88 |
96 AddResponse(request_headers, response_headers, body); | 89 AddResponse(request_headers, response_headers, body); |
97 } | 90 } |
98 | 91 |
99 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 92 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
100 const BalsaHeaders& response_headers, | 93 const BalsaHeaders& response_headers, |
101 StringPiece response_body) { | 94 StringPiece response_body) { |
102 VLOG(1) << "Adding response for: " << GetKey(request_headers); | 95 VLOG(1) << "Adding response for: " << GetKey(request_headers); |
103 if (ContainsKey(responses_, GetKey(request_headers))) { | 96 if (ContainsKey(responses_, GetKey(request_headers))) { |
104 LOG(DFATAL) << "Response for given request already exists!"; | 97 LOG(DFATAL) << "Response for given request already exists!"; |
105 return; | 98 return; |
106 } | 99 } |
107 Response* new_response = new Response(); | 100 Response* new_response = new Response(); |
108 new_response->set_headers(response_headers); | 101 new_response->set_headers(response_headers); |
109 new_response->set_body(response_body); | 102 new_response->set_body(response_body); |
110 responses_[GetKey(request_headers)] = new_response; | 103 responses_[GetKey(request_headers)] = new_response; |
111 } | 104 } |
112 | 105 |
113 void QuicInMemoryCache::AddSpecialResponse(StringPiece method, | 106 void QuicInMemoryCache::AddSpecialResponse(StringPiece method, |
114 StringPiece path, | 107 StringPiece path, |
115 StringPiece version, | 108 StringPiece version, |
116 SpecialResponseType response_type) { | 109 SpecialResponseType response_type) { |
117 BalsaHeaders request_headers, response_headers; | 110 BalsaHeaders request_headers, response_headers; |
118 request_headers.SetRequestFirstlineFromStringPieces(method, | 111 request_headers.SetRequestFirstlineFromStringPieces(method, path, version); |
119 path, | |
120 version); | |
121 AddResponse(request_headers, response_headers, ""); | 112 AddResponse(request_headers, response_headers, ""); |
122 responses_[GetKey(request_headers)]->response_type_ = response_type; | 113 responses_[GetKey(request_headers)]->response_type_ = response_type; |
123 } | 114 } |
124 | 115 |
125 QuicInMemoryCache::QuicInMemoryCache() { | 116 QuicInMemoryCache::QuicInMemoryCache() { |
126 Initialize(); | 117 Initialize(); |
127 } | 118 } |
128 | 119 |
129 void QuicInMemoryCache::ResetForTests() { | 120 void QuicInMemoryCache::ResetForTests() { |
130 STLDeleteValues(&responses_); | 121 STLDeleteValues(&responses_); |
131 Initialize(); | 122 Initialize(); |
132 } | 123 } |
133 | 124 |
134 void QuicInMemoryCache::Initialize() { | 125 void QuicInMemoryCache::Initialize() { |
135 // If there's no defined cache dir, we have no initialization to do. | 126 // If there's no defined cache dir, we have no initialization to do. |
136 if (FLAGS_quic_in_memory_cache_dir.empty()) { | 127 if (FLAGS_quic_in_memory_cache_dir.empty()) { |
137 VLOG(1) << "No cache directory found. Skipping initialization."; | 128 VLOG(1) << "No cache directory found. Skipping initialization."; |
138 return; | 129 return; |
139 } | 130 } |
140 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " | 131 VLOG(1) << "Attempting to initialize QuicInMemoryCache from directory: " |
141 << FLAGS_quic_in_memory_cache_dir; | 132 << FLAGS_quic_in_memory_cache_dir; |
142 | 133 |
143 FilePath directory(FLAGS_quic_in_memory_cache_dir); | 134 FilePath directory(FLAGS_quic_in_memory_cache_dir); |
144 base::FileEnumerator file_list(directory, | 135 base::FileEnumerator file_list(directory, true, base::FileEnumerator::FILES); |
145 true, | |
146 base::FileEnumerator::FILES); | |
147 | 136 |
148 FilePath file = file_list.Next(); | 137 FilePath file = file_list.Next(); |
149 while (!file.empty()) { | 138 while (!file.empty()) { |
150 // Need to skip files in .svn directories | 139 // Need to skip files in .svn directories |
151 if (file.value().find("/.svn/") != std::string::npos) { | 140 if (file.value().find("/.svn/") != std::string::npos) { |
152 file = file_list.Next(); | 141 file = file_list.Next(); |
153 continue; | 142 continue; |
154 } | 143 } |
155 | 144 |
156 BalsaHeaders request_headers, response_headers; | 145 BalsaHeaders request_headers, response_headers; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 188 } |
200 } | 189 } |
201 int path_start = base.find_first_of('/'); | 190 int path_start = base.find_first_of('/'); |
202 DCHECK_LT(0, path_start); | 191 DCHECK_LT(0, path_start); |
203 StringPiece host(base.substr(0, path_start)); | 192 StringPiece host(base.substr(0, path_start)); |
204 StringPiece path(base.substr(path_start)); | 193 StringPiece path(base.substr(path_start)); |
205 if (path[path.length() - 1] == ',') { | 194 if (path[path.length() - 1] == ',') { |
206 path.remove_suffix(1); | 195 path.remove_suffix(1); |
207 } | 196 } |
208 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. | 197 // Set up request headers. Assume method is GET and protocol is HTTP/1.1. |
209 request_headers.SetRequestFirstlineFromStringPieces("GET", | 198 request_headers.SetRequestFirstlineFromStringPieces( |
210 path, | 199 "GET", path, "HTTP/1.1"); |
211 "HTTP/1.1"); | |
212 request_headers.ReplaceOrAppendHeader("host", host); | 200 request_headers.ReplaceOrAppendHeader("host", host); |
213 | 201 |
214 VLOG(1) << "Inserting 'http://" << GetKey(request_headers) | 202 VLOG(1) << "Inserting 'http://" << GetKey(request_headers) |
215 << "' into QuicInMemoryCache."; | 203 << "' into QuicInMemoryCache."; |
216 | 204 |
217 AddResponse(request_headers, response_headers, caching_visitor.body()); | 205 AddResponse(request_headers, response_headers, caching_visitor.body()); |
218 | 206 |
219 file = file_list.Next(); | 207 file = file_list.Next(); |
220 } | 208 } |
221 } | 209 } |
(...skipping 13 matching lines...) Expand all Loading... |
235 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 223 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
236 uri.remove_prefix(8); | 224 uri.remove_prefix(8); |
237 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 225 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
238 uri.remove_prefix(7); | 226 uri.remove_prefix(7); |
239 } | 227 } |
240 return host.as_string() + uri.as_string(); | 228 return host.as_string() + uri.as_string(); |
241 } | 229 } |
242 | 230 |
243 } // namespace tools | 231 } // namespace tools |
244 } // namespace net | 232 } // namespace net |
OLD | NEW |