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 <utility> |
| 8 |
7 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
9 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
10 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
13 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
14 #include "net/http/http_util.h" | 16 #include "net/http/http_util.h" |
15 #include "net/quic/quic_bug_tracker.h" | 17 #include "net/quic/quic_bug_tracker.h" |
16 #include "net/spdy/spdy_http_utils.h" | 18 #include "net/spdy/spdy_http_utils.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 private: | 75 private: |
74 scoped_refptr<HttpResponseHeaders> http_headers_; | 76 scoped_refptr<HttpResponseHeaders> http_headers_; |
75 string url_; | 77 string url_; |
76 list<std::unique_ptr<string>> push_url_values_; | 78 list<std::unique_ptr<string>> push_url_values_; |
77 | 79 |
78 DISALLOW_COPY_AND_ASSIGN(ResourceFileImpl); | 80 DISALLOW_COPY_AND_ASSIGN(ResourceFileImpl); |
79 }; | 81 }; |
80 | 82 |
81 } // namespace | 83 } // namespace |
82 | 84 |
83 QuicInMemoryCache::ServerPushInfo::ServerPushInfo( | 85 QuicInMemoryCache::ServerPushInfo::ServerPushInfo(GURL request_url, |
84 GURL request_url, | 86 SpdyHeaderBlock headers, |
85 const SpdyHeaderBlock& headers, | 87 net::SpdyPriority priority, |
86 net::SpdyPriority priority, | 88 string body) |
87 string body) | |
88 : request_url(request_url), | 89 : request_url(request_url), |
89 headers(headers), | 90 headers(std::move(headers)), |
90 priority(priority), | 91 priority(priority), |
91 body(body) {} | 92 body(body) {} |
92 | 93 |
93 QuicInMemoryCache::ServerPushInfo::ServerPushInfo(const ServerPushInfo& other) = | 94 QuicInMemoryCache::ServerPushInfo::ServerPushInfo(const ServerPushInfo& other) |
94 default; | 95 : request_url(other.request_url), |
| 96 headers(other.headers.Clone()), |
| 97 priority(other.priority), |
| 98 body(other.body) {} |
95 | 99 |
96 QuicInMemoryCache::Response::Response() : response_type_(REGULAR_RESPONSE) {} | 100 QuicInMemoryCache::Response::Response() : response_type_(REGULAR_RESPONSE) {} |
97 | 101 |
98 QuicInMemoryCache::Response::~Response() {} | 102 QuicInMemoryCache::Response::~Response() {} |
99 | 103 |
100 QuicInMemoryCache::ResourceFile::ResourceFile(const base::FilePath& file_name) | 104 QuicInMemoryCache::ResourceFile::ResourceFile(const base::FilePath& file_name) |
101 : file_name_(file_name), file_name_string_(file_name.AsUTF8Unsafe()) {} | 105 : file_name_(file_name), file_name_string_(file_name.AsUTF8Unsafe()) {} |
102 | 106 |
103 QuicInMemoryCache::ResourceFile::~ResourceFile() {} | 107 QuicInMemoryCache::ResourceFile::~ResourceFile() {} |
104 | 108 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 typedef QuicInMemoryCache::ServerPushInfo ServerPushInfo; | 157 typedef QuicInMemoryCache::ServerPushInfo ServerPushInfo; |
154 | 158 |
155 void QuicInMemoryCache::AddSimpleResponse(StringPiece host, | 159 void QuicInMemoryCache::AddSimpleResponse(StringPiece host, |
156 StringPiece path, | 160 StringPiece path, |
157 int response_code, | 161 int response_code, |
158 StringPiece body) { | 162 StringPiece body) { |
159 SpdyHeaderBlock response_headers; | 163 SpdyHeaderBlock response_headers; |
160 response_headers[":status"] = IntToString(response_code); | 164 response_headers[":status"] = IntToString(response_code); |
161 response_headers["content-length"] = | 165 response_headers["content-length"] = |
162 IntToString(static_cast<int>(body.length())); | 166 IntToString(static_cast<int>(body.length())); |
163 AddResponse(host, path, response_headers, body); | 167 AddResponse(host, path, std::move(response_headers), body); |
164 } | 168 } |
165 | 169 |
166 void QuicInMemoryCache::AddSimpleResponseWithServerPushResources( | 170 void QuicInMemoryCache::AddSimpleResponseWithServerPushResources( |
167 StringPiece host, | 171 StringPiece host, |
168 StringPiece path, | 172 StringPiece path, |
169 int response_code, | 173 int response_code, |
170 StringPiece body, | 174 StringPiece body, |
171 list<ServerPushInfo> push_resources) { | 175 list<ServerPushInfo> push_resources) { |
172 AddSimpleResponse(host, path, response_code, body); | 176 AddSimpleResponse(host, path, response_code, body); |
173 MaybeAddServerPushResources(host, path, push_resources); | 177 MaybeAddServerPushResources(host, path, push_resources); |
174 } | 178 } |
175 | 179 |
176 void QuicInMemoryCache::AddDefaultResponse(Response* response) { | 180 void QuicInMemoryCache::AddDefaultResponse(Response* response) { |
177 default_response_.reset(response); | 181 default_response_.reset(response); |
178 } | 182 } |
179 | 183 |
180 void QuicInMemoryCache::AddResponse(StringPiece host, | 184 void QuicInMemoryCache::AddResponse(StringPiece host, |
181 StringPiece path, | 185 StringPiece path, |
182 const SpdyHeaderBlock& response_headers, | 186 SpdyHeaderBlock response_headers, |
183 StringPiece response_body) { | 187 StringPiece response_body) { |
184 AddResponseImpl(host, path, REGULAR_RESPONSE, response_headers, response_body, | 188 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), |
185 SpdyHeaderBlock()); | 189 response_body, SpdyHeaderBlock()); |
186 } | 190 } |
187 | 191 |
188 void QuicInMemoryCache::AddResponse(StringPiece host, | 192 void QuicInMemoryCache::AddResponse(StringPiece host, |
189 StringPiece path, | 193 StringPiece path, |
190 const SpdyHeaderBlock& response_headers, | 194 SpdyHeaderBlock response_headers, |
191 StringPiece response_body, | 195 StringPiece response_body, |
192 const SpdyHeaderBlock& response_trailers) { | 196 SpdyHeaderBlock response_trailers) { |
193 AddResponseImpl(host, path, REGULAR_RESPONSE, response_headers, response_body, | 197 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), |
194 response_trailers); | 198 response_body, std::move(response_trailers)); |
195 } | 199 } |
196 | 200 |
197 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, | 201 void QuicInMemoryCache::AddSpecialResponse(StringPiece host, |
198 StringPiece path, | 202 StringPiece path, |
199 SpecialResponseType response_type) { | 203 SpecialResponseType response_type) { |
200 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), "", | 204 AddResponseImpl(host, path, response_type, SpdyHeaderBlock(), "", |
201 SpdyHeaderBlock()); | 205 SpdyHeaderBlock()); |
202 } | 206 } |
203 | 207 |
204 QuicInMemoryCache::QuicInMemoryCache() {} | 208 QuicInMemoryCache::QuicInMemoryCache() {} |
(...skipping 27 matching lines...) Expand all Loading... |
232 StringPiece base(resource_file->file_name()); | 236 StringPiece base(resource_file->file_name()); |
233 base.remove_prefix(cache_directory.length()); | 237 base.remove_prefix(cache_directory.length()); |
234 if (base[0] == '/') { | 238 if (base[0] == '/') { |
235 base.remove_prefix(1); | 239 base.remove_prefix(1); |
236 } | 240 } |
237 | 241 |
238 resource_file->SetHostPathFromBase(base); | 242 resource_file->SetHostPathFromBase(base); |
239 resource_file->Read(); | 243 resource_file->Read(); |
240 | 244 |
241 AddResponse(resource_file->host(), resource_file->path(), | 245 AddResponse(resource_file->host(), resource_file->path(), |
242 resource_file->spdy_headers(), resource_file->body()); | 246 resource_file->spdy_headers().Clone(), resource_file->body()); |
243 | 247 |
244 resource_files.push_back(std::move(resource_file)); | 248 resource_files.push_back(std::move(resource_file)); |
245 } | 249 } |
246 | 250 |
247 for (const auto& resource_file : resource_files) { | 251 for (const auto& resource_file : resource_files) { |
248 list<ServerPushInfo> push_resources; | 252 list<ServerPushInfo> push_resources; |
249 for (const auto& push_url : resource_file->push_urls()) { | 253 for (const auto& push_url : resource_file->push_urls()) { |
250 GURL url(push_url); | 254 GURL url(push_url); |
251 const Response* response = GetResponse(url.host(), url.path()); | 255 const Response* response = GetResponse(url.host(), url.path()); |
252 if (!response) { | 256 if (!response) { |
253 QUIC_BUG << "Push URL '" << push_url << "' not found."; | 257 QUIC_BUG << "Push URL '" << push_url << "' not found."; |
254 return; | 258 return; |
255 } | 259 } |
256 push_resources.push_back(ServerPushInfo(url, response->headers(), | 260 push_resources.push_back(ServerPushInfo(url, response->headers().Clone(), |
257 net::kV3LowestPriority, | 261 net::kV3LowestPriority, |
258 response->body().as_string())); | 262 response->body().as_string())); |
259 } | 263 } |
260 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), | 264 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), |
261 push_resources); | 265 push_resources); |
262 } | 266 } |
263 } | 267 } |
264 | 268 |
265 list<ServerPushInfo> QuicInMemoryCache::GetServerPushResources( | 269 list<ServerPushInfo> QuicInMemoryCache::GetServerPushResources( |
266 string request_url) { | 270 string request_url) { |
267 list<ServerPushInfo> resources; | 271 list<ServerPushInfo> resources; |
268 auto resource_range = server_push_resources_.equal_range(request_url); | 272 auto resource_range = server_push_resources_.equal_range(request_url); |
269 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 273 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
270 resources.push_back(it->second); | 274 resources.push_back(it->second); |
271 } | 275 } |
272 DVLOG(1) << "Found " << resources.size() << " push resources for " | 276 DVLOG(1) << "Found " << resources.size() << " push resources for " |
273 << request_url; | 277 << request_url; |
274 return resources; | 278 return resources; |
275 } | 279 } |
276 | 280 |
277 QuicInMemoryCache::~QuicInMemoryCache() { | 281 QuicInMemoryCache::~QuicInMemoryCache() { |
278 STLDeleteValues(&responses_); | 282 STLDeleteValues(&responses_); |
279 } | 283 } |
280 | 284 |
281 void QuicInMemoryCache::AddResponseImpl( | 285 void QuicInMemoryCache::AddResponseImpl(StringPiece host, |
282 StringPiece host, | 286 StringPiece path, |
283 StringPiece path, | 287 SpecialResponseType response_type, |
284 SpecialResponseType response_type, | 288 SpdyHeaderBlock response_headers, |
285 const SpdyHeaderBlock& response_headers, | 289 StringPiece response_body, |
286 StringPiece response_body, | 290 SpdyHeaderBlock response_trailers) { |
287 const SpdyHeaderBlock& response_trailers) { | |
288 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; | 291 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; |
289 string key = GetKey(host, path); | 292 string key = GetKey(host, path); |
290 if (ContainsKey(responses_, key)) { | 293 if (ContainsKey(responses_, key)) { |
291 QUIC_BUG << "Response for '" << key << "' already exists!"; | 294 QUIC_BUG << "Response for '" << key << "' already exists!"; |
292 return; | 295 return; |
293 } | 296 } |
294 Response* new_response = new Response(); | 297 Response* new_response = new Response(); |
295 new_response->set_response_type(response_type); | 298 new_response->set_response_type(response_type); |
296 new_response->set_headers(response_headers); | 299 new_response->set_headers(std::move(response_headers)); |
297 new_response->set_body(response_body); | 300 new_response->set_body(response_body); |
298 new_response->set_trailers(response_trailers); | 301 new_response->set_trailers(std::move(response_trailers)); |
299 DVLOG(1) << "Add response with key " << key; | 302 DVLOG(1) << "Add response with key " << key; |
300 responses_[key] = new_response; | 303 responses_[key] = new_response; |
301 } | 304 } |
302 | 305 |
303 string QuicInMemoryCache::GetKey(StringPiece host, StringPiece path) const { | 306 string QuicInMemoryCache::GetKey(StringPiece host, StringPiece path) const { |
304 return host.as_string() + path.as_string(); | 307 return host.as_string() + path.as_string(); |
305 } | 308 } |
306 | 309 |
307 void QuicInMemoryCache::MaybeAddServerPushResources( | 310 void QuicInMemoryCache::MaybeAddServerPushResources( |
308 StringPiece request_host, | 311 StringPiece request_host, |
(...skipping 10 matching lines...) Expand all Loading... |
319 << " push url " << push_resource.request_url | 322 << " push url " << push_resource.request_url |
320 << " response headers " << push_resource.headers.DebugString(); | 323 << " response headers " << push_resource.headers.DebugString(); |
321 server_push_resources_.insert(std::make_pair(request_url, push_resource)); | 324 server_push_resources_.insert(std::make_pair(request_url, push_resource)); |
322 string host = push_resource.request_url.host(); | 325 string host = push_resource.request_url.host(); |
323 if (host.empty()) { | 326 if (host.empty()) { |
324 host = request_host.as_string(); | 327 host = request_host.as_string(); |
325 } | 328 } |
326 string path = push_resource.request_url.path(); | 329 string path = push_resource.request_url.path(); |
327 if (responses_.find(GetKey(host, path)) == responses_.end()) { | 330 if (responses_.find(GetKey(host, path)) == responses_.end()) { |
328 // Add a server push response to responses map, if it is not in the map. | 331 // Add a server push response to responses map, if it is not in the map. |
329 SpdyHeaderBlock headers = push_resource.headers; | |
330 StringPiece body = push_resource.body; | 332 StringPiece body = push_resource.body; |
331 DVLOG(1) << "Add response for push resource: host " << host << " path " | 333 DVLOG(1) << "Add response for push resource: host " << host << " path " |
332 << path; | 334 << path; |
333 AddResponse(host, path, headers, body); | 335 AddResponse(host, path, push_resource.headers.Clone(), body); |
334 } | 336 } |
335 } | 337 } |
336 } | 338 } |
337 | 339 |
338 bool QuicInMemoryCache::PushResourceExistsInCache(string original_request_url, | 340 bool QuicInMemoryCache::PushResourceExistsInCache(string original_request_url, |
339 ServerPushInfo resource) { | 341 ServerPushInfo resource) { |
340 auto resource_range = | 342 auto resource_range = |
341 server_push_resources_.equal_range(original_request_url); | 343 server_push_resources_.equal_range(original_request_url); |
342 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 344 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
343 ServerPushInfo push_resource = it->second; | 345 ServerPushInfo push_resource = it->second; |
344 if (push_resource.request_url.spec() == resource.request_url.spec()) { | 346 if (push_resource.request_url.spec() == resource.request_url.spec()) { |
345 return true; | 347 return true; |
346 } | 348 } |
347 } | 349 } |
348 return false; | 350 return false; |
349 } | 351 } |
350 | 352 |
351 } // namespace net | 353 } // namespace net |
OLD | NEW |