| 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 | 11 |
| 11 using base::FilePath; | 12 using base::FilePath; |
| 12 using base::StringPiece; | 13 using base::StringPiece; |
| 13 using std::string; | 14 using std::string; |
| 14 | 15 |
| 15 // Specifies the directory used during QuicInMemoryCache | 16 // Specifies the directory used during QuicInMemoryCache |
| 16 // construction to seed the cache. Cache directory can be | 17 // construction to seed the cache. Cache directory can be |
| 17 // generated using `wget -p --save-headers <url> | 18 // generated using `wget -p --save-headers <url> |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 87 |
| 87 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( | 88 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( |
| 88 const BalsaHeaders& request_headers) const { | 89 const BalsaHeaders& request_headers) const { |
| 89 ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); | 90 ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); |
| 90 if (it == responses_.end()) { | 91 if (it == responses_.end()) { |
| 91 return NULL; | 92 return NULL; |
| 92 } | 93 } |
| 93 return it->second; | 94 return it->second; |
| 94 } | 95 } |
| 95 | 96 |
| 97 void QuicInMemoryCache::AddOrVerifyResponse(StringPiece method, |
| 98 StringPiece path, |
| 99 StringPiece version, |
| 100 StringPiece response_code, |
| 101 StringPiece response_detail, |
| 102 StringPiece body) { |
| 103 BalsaHeaders request_headers, response_headers; |
| 104 request_headers.SetRequestFirstlineFromStringPieces(method, |
| 105 path, |
| 106 version); |
| 107 response_headers.SetRequestFirstlineFromStringPieces(version, |
| 108 response_code, |
| 109 response_detail); |
| 110 response_headers.AppendHeader("content-length", |
| 111 base::IntToString(body.length())); |
| 112 |
| 113 // Check if response already exists and matches. |
| 114 const QuicInMemoryCache::Response* cached_response = |
| 115 GetResponse(request_headers); |
| 116 if (cached_response == NULL) { |
| 117 AddResponse(request_headers, response_headers, body); |
| 118 return; |
| 119 } |
| 120 string cached_response_headers_str, response_headers_str; |
| 121 cached_response->headers().DumpToString(&cached_response_headers_str); |
| 122 response_headers.DumpToString(&response_headers_str); |
| 123 CHECK_EQ(cached_response_headers_str, response_headers_str); |
| 124 CHECK_EQ(cached_response->body(), body); |
| 125 } |
| 126 |
| 96 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 127 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
| 97 const BalsaHeaders& response_headers, | 128 const BalsaHeaders& response_headers, |
| 98 StringPiece response_body) { | 129 StringPiece response_body) { |
| 99 LOG(INFO) << "Adding response for: " << GetKey(request_headers); | 130 LOG(INFO) << "Adding response for: " << GetKey(request_headers); |
| 100 if (ContainsKey(responses_, GetKey(request_headers))) { | 131 if (ContainsKey(responses_, GetKey(request_headers))) { |
| 101 LOG(DFATAL) << "Response for given request already exists!"; | 132 LOG(DFATAL) << "Response for given request already exists!"; |
| 102 } | 133 } |
| 103 Response* new_response = new Response(); | 134 Response* new_response = new Response(); |
| 104 new_response->set_headers(response_headers); | 135 new_response->set_headers(response_headers); |
| 105 new_response->set_body(response_body); | 136 new_response->set_body(response_body); |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 247 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 217 uri.remove_prefix(8); | 248 uri.remove_prefix(8); |
| 218 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 249 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 219 uri.remove_prefix(7); | 250 uri.remove_prefix(7); |
| 220 } | 251 } |
| 221 return host.as_string() + uri.as_string(); | 252 return host.as_string() + uri.as_string(); |
| 222 } | 253 } |
| 223 | 254 |
| 224 } // namespace tools | 255 } // namespace tools |
| 225 } // namespace net | 256 } // namespace net |
| OLD | NEW |