| 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" |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 void QuicHttpResponseCache::ResourceFile::HandleXOriginalUrl() { | 156 void QuicHttpResponseCache::ResourceFile::HandleXOriginalUrl() { |
| 157 StringPiece url(x_original_url_); | 157 StringPiece url(x_original_url_); |
| 158 // Remove the protocol so we can add it below. | 158 // Remove the protocol so we can add it below. |
| 159 url = RemoveScheme(url); | 159 url = RemoveScheme(url); |
| 160 SetHostPathFromBase(url); | 160 SetHostPathFromBase(url); |
| 161 } | 161 } |
| 162 | 162 |
| 163 const QuicHttpResponseCache::Response* QuicHttpResponseCache::GetResponse( | 163 const QuicHttpResponseCache::Response* QuicHttpResponseCache::GetResponse( |
| 164 StringPiece host, | 164 StringPiece host, |
| 165 StringPiece path) const { | 165 StringPiece path) const { |
| 166 base::AutoLock lock(response_mutex_); | 166 QuicWriterMutexLock lock(&response_mutex_); |
| 167 | 167 |
| 168 auto it = responses_.find(GetKey(host, path)); | 168 auto it = responses_.find(GetKey(host, path)); |
| 169 if (it == responses_.end()) { | 169 if (it == responses_.end()) { |
| 170 DVLOG(1) << "Get response for resource failed: host " << host << " path " | 170 DVLOG(1) << "Get response for resource failed: host " << host << " path " |
| 171 << path; | 171 << path; |
| 172 if (default_response_.get()) { | 172 if (default_response_.get()) { |
| 173 return default_response_.get(); | 173 return default_response_.get(); |
| 174 } | 174 } |
| 175 return nullptr; | 175 return nullptr; |
| 176 } | 176 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 194 StringPiece host, | 194 StringPiece host, |
| 195 StringPiece path, | 195 StringPiece path, |
| 196 int response_code, | 196 int response_code, |
| 197 StringPiece body, | 197 StringPiece body, |
| 198 std::list<ServerPushInfo> push_resources) { | 198 std::list<ServerPushInfo> push_resources) { |
| 199 AddSimpleResponse(host, path, response_code, body); | 199 AddSimpleResponse(host, path, response_code, body); |
| 200 MaybeAddServerPushResources(host, path, push_resources); | 200 MaybeAddServerPushResources(host, path, push_resources); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void QuicHttpResponseCache::AddDefaultResponse(Response* response) { | 203 void QuicHttpResponseCache::AddDefaultResponse(Response* response) { |
| 204 base::AutoLock lock(response_mutex_); | 204 QuicWriterMutexLock lock(&response_mutex_); |
| 205 default_response_.reset(response); | 205 default_response_.reset(response); |
| 206 } | 206 } |
| 207 | 207 |
| 208 void QuicHttpResponseCache::AddResponse(StringPiece host, | 208 void QuicHttpResponseCache::AddResponse(StringPiece host, |
| 209 StringPiece path, | 209 StringPiece path, |
| 210 SpdyHeaderBlock response_headers, | 210 SpdyHeaderBlock response_headers, |
| 211 StringPiece response_body) { | 211 StringPiece response_body) { |
| 212 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), | 212 AddResponseImpl(host, path, REGULAR_RESPONSE, std::move(response_headers), |
| 213 response_body, SpdyHeaderBlock()); | 213 response_body, SpdyHeaderBlock()); |
| 214 } | 214 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 net::kV3LowestPriority, | 281 net::kV3LowestPriority, |
| 282 response->body().as_string())); | 282 response->body().as_string())); |
| 283 } | 283 } |
| 284 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), | 284 MaybeAddServerPushResources(resource_file->host(), resource_file->path(), |
| 285 push_resources); | 285 push_resources); |
| 286 } | 286 } |
| 287 } | 287 } |
| 288 | 288 |
| 289 std::list<ServerPushInfo> QuicHttpResponseCache::GetServerPushResources( | 289 std::list<ServerPushInfo> QuicHttpResponseCache::GetServerPushResources( |
| 290 string request_url) { | 290 string request_url) { |
| 291 base::AutoLock lock(response_mutex_); | 291 QuicWriterMutexLock lock(&response_mutex_); |
| 292 | 292 |
| 293 std::list<ServerPushInfo> resources; | 293 std::list<ServerPushInfo> resources; |
| 294 auto resource_range = server_push_resources_.equal_range(request_url); | 294 auto resource_range = server_push_resources_.equal_range(request_url); |
| 295 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 295 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 296 resources.push_back(it->second); | 296 resources.push_back(it->second); |
| 297 } | 297 } |
| 298 DVLOG(1) << "Found " << resources.size() << " push resources for " | 298 DVLOG(1) << "Found " << resources.size() << " push resources for " |
| 299 << request_url; | 299 << request_url; |
| 300 return resources; | 300 return resources; |
| 301 } | 301 } |
| 302 | 302 |
| 303 QuicHttpResponseCache::~QuicHttpResponseCache() { | 303 QuicHttpResponseCache::~QuicHttpResponseCache() { |
| 304 { | 304 { |
| 305 base::AutoLock lock(response_mutex_); | 305 QuicWriterMutexLock lock(&response_mutex_); |
| 306 responses_.clear(); | 306 responses_.clear(); |
| 307 } | 307 } |
| 308 } | 308 } |
| 309 | 309 |
| 310 void QuicHttpResponseCache::AddResponseImpl(StringPiece host, | 310 void QuicHttpResponseCache::AddResponseImpl(StringPiece host, |
| 311 StringPiece path, | 311 StringPiece path, |
| 312 SpecialResponseType response_type, | 312 SpecialResponseType response_type, |
| 313 SpdyHeaderBlock response_headers, | 313 SpdyHeaderBlock response_headers, |
| 314 StringPiece response_body, | 314 StringPiece response_body, |
| 315 SpdyHeaderBlock response_trailers) { | 315 SpdyHeaderBlock response_trailers) { |
| 316 base::AutoLock lock(response_mutex_); | 316 QuicWriterMutexLock lock(&response_mutex_); |
| 317 | 317 |
| 318 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; | 318 DCHECK(!host.empty()) << "Host must be populated, e.g. \"www.google.com\""; |
| 319 string key = GetKey(host, path); | 319 string key = GetKey(host, path); |
| 320 if (base::ContainsKey(responses_, key)) { | 320 if (base::ContainsKey(responses_, key)) { |
| 321 QUIC_BUG << "Response for '" << key << "' already exists!"; | 321 QUIC_BUG << "Response for '" << key << "' already exists!"; |
| 322 return; | 322 return; |
| 323 } | 323 } |
| 324 std::unique_ptr<Response> new_response = base::MakeUnique<Response>(); | 324 std::unique_ptr<Response> new_response = base::MakeUnique<Response>(); |
| 325 new_response->set_response_type(response_type); | 325 new_response->set_response_type(response_type); |
| 326 new_response->set_headers(std::move(response_headers)); | 326 new_response->set_headers(std::move(response_headers)); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 342 | 342 |
| 343 for (const auto& push_resource : push_resources) { | 343 for (const auto& push_resource : push_resources) { |
| 344 if (PushResourceExistsInCache(request_url, push_resource)) { | 344 if (PushResourceExistsInCache(request_url, push_resource)) { |
| 345 continue; | 345 continue; |
| 346 } | 346 } |
| 347 | 347 |
| 348 DVLOG(1) << "Add request-resource association: request url " << request_url | 348 DVLOG(1) << "Add request-resource association: request url " << request_url |
| 349 << " push url " << push_resource.request_url | 349 << " push url " << push_resource.request_url |
| 350 << " response headers " << push_resource.headers.DebugString(); | 350 << " response headers " << push_resource.headers.DebugString(); |
| 351 { | 351 { |
| 352 base::AutoLock lock(response_mutex_); | 352 QuicWriterMutexLock lock(&response_mutex_); |
| 353 server_push_resources_.insert(std::make_pair(request_url, push_resource)); | 353 server_push_resources_.insert(std::make_pair(request_url, push_resource)); |
| 354 } | 354 } |
| 355 string host = push_resource.request_url.host(); | 355 string host = push_resource.request_url.host(); |
| 356 if (host.empty()) { | 356 if (host.empty()) { |
| 357 host = request_host.as_string(); | 357 host = request_host.as_string(); |
| 358 } | 358 } |
| 359 string path = push_resource.request_url.path(); | 359 string path = push_resource.request_url.path(); |
| 360 bool found_existing_response = false; | 360 bool found_existing_response = false; |
| 361 { | 361 { |
| 362 base::AutoLock lock(response_mutex_); | 362 QuicWriterMutexLock lock(&response_mutex_); |
| 363 found_existing_response = | 363 found_existing_response = |
| 364 base::ContainsKey(responses_, GetKey(host, path)); | 364 base::ContainsKey(responses_, GetKey(host, path)); |
| 365 } | 365 } |
| 366 if (!found_existing_response) { | 366 if (!found_existing_response) { |
| 367 // 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. |
| 368 StringPiece body = push_resource.body; | 368 StringPiece body = push_resource.body; |
| 369 DVLOG(1) << "Add response for push resource: host " << host << " path " | 369 DVLOG(1) << "Add response for push resource: host " << host << " path " |
| 370 << path; | 370 << path; |
| 371 AddResponse(host, path, push_resource.headers.Clone(), body); | 371 AddResponse(host, path, push_resource.headers.Clone(), body); |
| 372 } | 372 } |
| 373 } | 373 } |
| 374 } | 374 } |
| 375 | 375 |
| 376 bool QuicHttpResponseCache::PushResourceExistsInCache( | 376 bool QuicHttpResponseCache::PushResourceExistsInCache( |
| 377 string original_request_url, | 377 string original_request_url, |
| 378 ServerPushInfo resource) { | 378 ServerPushInfo resource) { |
| 379 base::AutoLock lock(response_mutex_); | 379 QuicWriterMutexLock lock(&response_mutex_); |
| 380 auto resource_range = | 380 auto resource_range = |
| 381 server_push_resources_.equal_range(original_request_url); | 381 server_push_resources_.equal_range(original_request_url); |
| 382 for (auto it = resource_range.first; it != resource_range.second; ++it) { | 382 for (auto it = resource_range.first; it != resource_range.second; ++it) { |
| 383 ServerPushInfo push_resource = it->second; | 383 ServerPushInfo push_resource = it->second; |
| 384 if (push_resource.request_url.spec() == resource.request_url.spec()) { | 384 if (push_resource.request_url.spec() == resource.request_url.spec()) { |
| 385 return true; | 385 return true; |
| 386 } | 386 } |
| 387 } | 387 } |
| 388 return false; | 388 return false; |
| 389 } | 389 } |
| 390 | 390 |
| 391 } // namespace net | 391 } // namespace net |
| OLD | NEW |