| 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_http_response_cache.h" | 5 #include "net/tools/quic/quic_http_response_cache.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/file_enumerator.h" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 13 #include "net/http/http_util.h" | 13 #include "net/http/http_util.h" |
| 14 #include "net/quic/platform/api/quic_bug_tracker.h" | 14 #include "net/quic/platform/api/quic_bug_tracker.h" |
| 15 #include "net/quic/platform/api/quic_logging.h" |
| 15 #include "net/quic/platform/api/quic_text_utils.h" | 16 #include "net/quic/platform/api/quic_text_utils.h" |
| 16 #include "net/spdy/spdy_http_utils.h" | 17 #include "net/spdy/spdy_http_utils.h" |
| 17 | 18 |
| 18 using base::FilePath; | 19 using base::FilePath; |
| 19 using base::IntToString; | 20 using base::IntToString; |
| 20 using base::StringPiece; | 21 using base::StringPiece; |
| 21 using std::string; | 22 using std::string; |
| 22 | 23 |
| 23 namespace net { | 24 namespace net { |
| 24 | 25 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 QuicHttpResponseCache::Response::~Response() {} | 46 QuicHttpResponseCache::Response::~Response() {} |
| 46 | 47 |
| 47 void QuicHttpResponseCache::ResourceFile::Read() { | 48 void QuicHttpResponseCache::ResourceFile::Read() { |
| 48 base::ReadFileToString(FilePath(file_name_), &file_contents_); | 49 base::ReadFileToString(FilePath(file_name_), &file_contents_); |
| 49 | 50 |
| 50 // First read the headers. | 51 // First read the headers. |
| 51 size_t start = 0; | 52 size_t start = 0; |
| 52 while (start < file_contents_.length()) { | 53 while (start < file_contents_.length()) { |
| 53 size_t pos = file_contents_.find("\n", start); | 54 size_t pos = file_contents_.find("\n", start); |
| 54 if (pos == string::npos) { | 55 if (pos == string::npos) { |
| 55 LOG(DFATAL) << "Headers invalid or empty, ignoring: " | 56 QUIC_LOG(DFATAL) << "Headers invalid or empty, ignoring: " |
| 56 << file_name_.value(); | 57 << file_name_.value(); |
| 57 return; | 58 return; |
| 58 } | 59 } |
| 59 size_t len = pos - start; | 60 size_t len = pos - start; |
| 60 // Support both dos and unix line endings for convenience. | 61 // Support both dos and unix line endings for convenience. |
| 61 if (file_contents_[pos - 1] == '\r') { | 62 if (file_contents_[pos - 1] == '\r') { |
| 62 len -= 1; | 63 len -= 1; |
| 63 } | 64 } |
| 64 StringPiece line(file_contents_.data() + start, len); | 65 StringPiece line(file_contents_.data() + start, len); |
| 65 start = pos + 1; | 66 start = pos + 1; |
| 66 // Headers end with an empty line. | 67 // Headers end with an empty line. |
| 67 if (line.empty()) { | 68 if (line.empty()) { |
| 68 break; | 69 break; |
| 69 } | 70 } |
| 70 // Extract the status from the HTTP first line. | 71 // Extract the status from the HTTP first line. |
| 71 if (line.substr(0, 4) == "HTTP") { | 72 if (line.substr(0, 4) == "HTTP") { |
| 72 pos = line.find(" "); | 73 pos = line.find(" "); |
| 73 if (pos == string::npos) { | 74 if (pos == string::npos) { |
| 74 LOG(DFATAL) << "Headers invalid or empty, ignoring: " | 75 QUIC_LOG(DFATAL) << "Headers invalid or empty, ignoring: " |
| 75 << file_name_.value(); | 76 << file_name_.value(); |
| 76 return; | 77 return; |
| 77 } | 78 } |
| 78 spdy_headers_[":status"] = line.substr(pos + 1, 3); | 79 spdy_headers_[":status"] = line.substr(pos + 1, 3); |
| 79 continue; | 80 continue; |
| 80 } | 81 } |
| 81 // Headers are "key: value". | 82 // Headers are "key: value". |
| 82 pos = line.find(": "); | 83 pos = line.find(": "); |
| 83 if (pos == string::npos) { | 84 if (pos == string::npos) { |
| 84 LOG(DFATAL) << "Headers invalid or empty, ignoring: " | 85 QUIC_LOG(DFATAL) << "Headers invalid or empty, ignoring: " |
| 85 << file_name_.value(); | 86 << file_name_.value(); |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 spdy_headers_.AppendValueOrAddHeader( | 89 spdy_headers_.AppendValueOrAddHeader( |
| 89 QuicTextUtils::ToLower(line.substr(0, pos)), line.substr(pos + 2)); | 90 QuicTextUtils::ToLower(line.substr(0, pos)), line.substr(pos + 2)); |
| 90 } | 91 } |
| 91 | 92 |
| 92 // The connection header is prohibited in HTTP/2. | 93 // The connection header is prohibited in HTTP/2. |
| 93 spdy_headers_.erase("connection"); | 94 spdy_headers_.erase("connection"); |
| 94 | 95 |
| 95 // Override the URL with the X-Original-Url header, if present. | 96 // Override the URL with the X-Original-Url header, if present. |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 229 } |
| 229 | 230 |
| 230 QuicHttpResponseCache::QuicHttpResponseCache() {} | 231 QuicHttpResponseCache::QuicHttpResponseCache() {} |
| 231 | 232 |
| 232 void QuicHttpResponseCache::InitializeFromDirectory( | 233 void QuicHttpResponseCache::InitializeFromDirectory( |
| 233 const string& cache_directory) { | 234 const string& cache_directory) { |
| 234 if (cache_directory.empty()) { | 235 if (cache_directory.empty()) { |
| 235 QUIC_BUG << "cache_directory must not be empty."; | 236 QUIC_BUG << "cache_directory must not be empty."; |
| 236 return; | 237 return; |
| 237 } | 238 } |
| 238 VLOG(1) << "Attempting to initialize QuicHttpResponseCache from directory: " | 239 QUIC_LOG(INFO) |
| 239 << cache_directory; | 240 << "Attempting to initialize QuicHttpResponseCache from directory: " |
| 241 << cache_directory; |
| 240 FilePath directory(FilePath::FromUTF8Unsafe(cache_directory)); | 242 FilePath directory(FilePath::FromUTF8Unsafe(cache_directory)); |
| 241 base::FileEnumerator file_list(directory, true, base::FileEnumerator::FILES); | 243 base::FileEnumerator file_list(directory, true, base::FileEnumerator::FILES); |
| 242 std::list<std::unique_ptr<ResourceFile>> resource_files; | 244 std::list<std::unique_ptr<ResourceFile>> resource_files; |
| 243 for (FilePath file_iter = file_list.Next(); !file_iter.empty(); | 245 for (FilePath file_iter = file_list.Next(); !file_iter.empty(); |
| 244 file_iter = file_list.Next()) { | 246 file_iter = file_list.Next()) { |
| 245 // Need to skip files in .svn directories | 247 // Need to skip files in .svn directories |
| 246 if (file_iter.value().find(FILE_PATH_LITERAL("/.svn/")) != string::npos) { | 248 if (file_iter.value().find(FILE_PATH_LITERAL("/.svn/")) != string::npos) { |
| 247 continue; | 249 continue; |
| 248 } | 250 } |
| 249 | 251 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 287 |
| 286 std::list<ServerPushInfo> QuicHttpResponseCache::GetServerPushResources( | 288 std::list<ServerPushInfo> QuicHttpResponseCache::GetServerPushResources( |
| 287 string request_url) { | 289 string request_url) { |
| 288 QuicWriterMutexLock lock(&response_mutex_); | 290 QuicWriterMutexLock lock(&response_mutex_); |
| 289 | 291 |
| 290 std::list<ServerPushInfo> resources; | 292 std::list<ServerPushInfo> resources; |
| 291 auto resource_range = server_push_resources_.equal_range(request_url); | 293 auto resource_range = server_push_resources_.equal_range(request_url); |
| 292 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 294 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 293 resources.push_back(it->second); | 295 resources.push_back(it->second); |
| 294 } | 296 } |
| 295 DVLOG(1) << "Found " << resources.size() << " push resources for " | 297 QUIC_DVLOG(1) << "Found " << resources.size() << " push resources for " |
| 296 << request_url; | 298 << request_url; |
| 297 return resources; | 299 return resources; |
| 298 } | 300 } |
| 299 | 301 |
| 300 QuicHttpResponseCache::~QuicHttpResponseCache() { | 302 QuicHttpResponseCache::~QuicHttpResponseCache() { |
| 301 { | 303 { |
| 302 QuicWriterMutexLock lock(&response_mutex_); | 304 QuicWriterMutexLock lock(&response_mutex_); |
| 303 responses_.clear(); | 305 responses_.clear(); |
| 304 } | 306 } |
| 305 } | 307 } |
| 306 | 308 |
| 307 void QuicHttpResponseCache::AddResponseImpl(StringPiece host, | 309 void QuicHttpResponseCache::AddResponseImpl(StringPiece host, |
| 308 StringPiece path, | 310 StringPiece path, |
| 309 SpecialResponseType response_type, | 311 SpecialResponseType response_type, |
| 310 SpdyHeaderBlock response_headers, | 312 SpdyHeaderBlock response_headers, |
| 311 StringPiece response_body, | 313 StringPiece response_body, |
| 312 SpdyHeaderBlock response_trailers) { | 314 SpdyHeaderBlock response_trailers) { |
| 313 QuicWriterMutexLock lock(&response_mutex_); | 315 QuicWriterMutexLock lock(&response_mutex_); |
| 314 | 316 |
| 315 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; | 317 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; |
| 316 string key = GetKey(host, path); | 318 string key = GetKey(host, path); |
| 317 if (base::ContainsKey(responses_, key)) { | 319 if (base::ContainsKey(responses_, key)) { |
| 318 QUIC_BUG << "Response for '" << key << "' already exists!"; | 320 QUIC_BUG << "Response for '" << key << "' already exists!"; |
| 319 return; | 321 return; |
| 320 } | 322 } |
| 321 std::unique_ptr<Response> new_response = base::MakeUnique<Response>(); | 323 std::unique_ptr<Response> new_response = base::MakeUnique<Response>(); |
| 322 new_response->set_response_type(response_type); | 324 new_response->set_response_type(response_type); |
| 323 new_response->set_headers(std::move(response_headers)); | 325 new_response->set_headers(std::move(response_headers)); |
| 324 new_response->set_body(response_body); | 326 new_response->set_body(response_body); |
| 325 new_response->set_trailers(std::move(response_trailers)); | 327 new_response->set_trailers(std::move(response_trailers)); |
| 326 DVLOG(1) << "Add response with key " << key; | 328 QUIC_DVLOG(1) << "Add response with key " << key; |
| 327 responses_[key] = std::move(new_response); | 329 responses_[key] = std::move(new_response); |
| 328 } | 330 } |
| 329 | 331 |
| 330 string QuicHttpResponseCache::GetKey(StringPiece host, StringPiece path) const { | 332 string QuicHttpResponseCache::GetKey(StringPiece host, StringPiece path) const { |
| 331 return host.as_string() + path.as_string(); | 333 return host.as_string() + path.as_string(); |
| 332 } | 334 } |
| 333 | 335 |
| 334 void QuicHttpResponseCache::MaybeAddServerPushResources( | 336 void QuicHttpResponseCache::MaybeAddServerPushResources( |
| 335 StringPiece request_host, | 337 StringPiece request_host, |
| 336 StringPiece request_path, | 338 StringPiece request_path, |
| 337 std::list<ServerPushInfo> push_resources) { | 339 std::list<ServerPushInfo> push_resources) { |
| 338 string request_url = GetKey(request_host, request_path); | 340 string request_url = GetKey(request_host, request_path); |
| 339 | 341 |
| 340 for (const auto& push_resource : push_resources) { | 342 for (const auto& push_resource : push_resources) { |
| 341 if (PushResourceExistsInCache(request_url, push_resource)) { | 343 if (PushResourceExistsInCache(request_url, push_resource)) { |
| 342 continue; | 344 continue; |
| 343 } | 345 } |
| 344 | 346 |
| 345 DVLOG(1) << "Add request-resource association: request url " << request_url | 347 QUIC_DVLOG(1) << "Add request-resource association: request url " |
| 346 << " push url " << push_resource.request_url | 348 << request_url << " push url " << push_resource.request_url |
| 347 << " response headers " << push_resource.headers.DebugString(); | 349 << " response headers " |
| 350 << push_resource.headers.DebugString(); |
| 348 { | 351 { |
| 349 QuicWriterMutexLock lock(&response_mutex_); | 352 QuicWriterMutexLock lock(&response_mutex_); |
| 350 server_push_resources_.insert(std::make_pair(request_url, push_resource)); | 353 server_push_resources_.insert(std::make_pair(request_url, push_resource)); |
| 351 } | 354 } |
| 352 string host = push_resource.request_url.host(); | 355 string host = push_resource.request_url.host(); |
| 353 if (host.empty()) { | 356 if (host.empty()) { |
| 354 host = request_host.as_string(); | 357 host = request_host.as_string(); |
| 355 } | 358 } |
| 356 string path = push_resource.request_url.path(); | 359 string path = push_resource.request_url.path(); |
| 357 bool found_existing_response = false; | 360 bool found_existing_response = false; |
| 358 { | 361 { |
| 359 QuicWriterMutexLock lock(&response_mutex_); | 362 QuicWriterMutexLock lock(&response_mutex_); |
| 360 found_existing_response = | 363 found_existing_response = |
| 361 base::ContainsKey(responses_, GetKey(host, path)); | 364 base::ContainsKey(responses_, GetKey(host, path)); |
| 362 } | 365 } |
| 363 if (!found_existing_response) { | 366 if (!found_existing_response) { |
| 364 // Add a server push response to responses map, if it is not in the map. | 367 // Add a server push response to responses map, if it is not in the map. |
| 365 StringPiece body = push_resource.body; | 368 StringPiece body = push_resource.body; |
| 366 DVLOG(1) << "Add response for push resource: host " << host << " path " | 369 QUIC_DVLOG(1) << "Add response for push resource: host " << host |
| 367 << path; | 370 << " path " << path; |
| 368 AddResponse(host, path, push_resource.headers.Clone(), body); | 371 AddResponse(host, path, push_resource.headers.Clone(), body); |
| 369 } | 372 } |
| 370 } | 373 } |
| 371 } | 374 } |
| 372 | 375 |
| 373 bool QuicHttpResponseCache::PushResourceExistsInCache( | 376 bool QuicHttpResponseCache::PushResourceExistsInCache( |
| 374 string original_request_url, | 377 string original_request_url, |
| 375 ServerPushInfo resource) { | 378 ServerPushInfo resource) { |
| 376 QuicWriterMutexLock lock(&response_mutex_); | 379 QuicWriterMutexLock lock(&response_mutex_); |
| 377 auto resource_range = | 380 auto resource_range = |
| 378 server_push_resources_.equal_range(original_request_url); | 381 server_push_resources_.equal_range(original_request_url); |
| 379 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 382 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 380 ServerPushInfo push_resource = it->second; | 383 ServerPushInfo push_resource = it->second; |
| 381 if (push_resource.request_url.spec() == resource.request_url.spec()) { | 384 if (push_resource.request_url.spec() == resource.request_url.spec()) { |
| 382 return true; | 385 return true; |
| 383 } | 386 } |
| 384 } | 387 } |
| 385 return false; | 388 return false; |
| 386 } | 389 } |
| 387 | 390 |
| 388 } // namespace net | 391 } // namespace net |
| OLD | NEW |