| 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" |
| 11 #include "net/tools/flip_server/balsa_headers.h" |
| 11 | 12 |
| 12 using base::FilePath; | 13 using base::FilePath; |
| 13 using base::StringPiece; | 14 using base::StringPiece; |
| 14 using std::string; | 15 using std::string; |
| 15 | 16 |
| 16 // Specifies the directory used during QuicInMemoryCache | 17 // Specifies the directory used during QuicInMemoryCache |
| 17 // construction to seed the cache. Cache directory can be | 18 // construction to seed the cache. Cache directory can be |
| 18 // generated using `wget -p --save-headers <url> | 19 // generated using `wget -p --save-headers <url> |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 namespace tools { | 22 namespace tools { |
| 22 | 23 |
| 23 std::string FLAGS_quic_in_memory_cache_dir = "/tmp/quic-data"; | 24 std::string FLAGS_quic_in_memory_cache_dir = ""; |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // BalsaVisitor implementation (glue) which caches response bodies. | 28 // BalsaVisitor implementation (glue) which caches response bodies. |
| 28 class CachingBalsaVisitor : public BalsaVisitorInterface { | 29 class CachingBalsaVisitor : public NoOpBalsaVisitor { |
| 29 public: | 30 public: |
| 30 CachingBalsaVisitor() : done_framing_(false) {} | 31 CachingBalsaVisitor() : done_framing_(false) {} |
| 31 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { | 32 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE { |
| 32 AppendToBody(input, size); | 33 AppendToBody(input, size); |
| 33 } | 34 } |
| 34 virtual void ProcessTrailers(const BalsaHeaders& trailer) { | |
| 35 LOG(DFATAL) << "Trailers not supported."; | |
| 36 } | |
| 37 virtual void MessageDone() OVERRIDE { | 35 virtual void MessageDone() OVERRIDE { |
| 38 done_framing_ = true; | 36 done_framing_ = true; |
| 39 } | 37 } |
| 40 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE { | 38 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE { |
| 41 UnhandledError(); | 39 UnhandledError(); |
| 42 } | 40 } |
| 43 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE { | 41 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE { |
| 44 UnhandledError(); | 42 UnhandledError(); |
| 45 } | 43 } |
| 46 virtual void HandleTrailerError(BalsaFrame* framer) { UnhandledError(); } | |
| 47 virtual void HandleTrailerWarning(BalsaFrame* framer) { UnhandledError(); } | |
| 48 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE { | 44 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE { |
| 49 UnhandledError(); | 45 UnhandledError(); |
| 50 } | 46 } |
| 51 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE { | 47 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE { |
| 52 UnhandledError(); | 48 UnhandledError(); |
| 53 } | 49 } |
| 54 void UnhandledError() { | 50 void UnhandledError() { |
| 55 LOG(DFATAL) << "Unhandled error framing HTTP."; | 51 LOG(DFATAL) << "Unhandled error framing HTTP."; |
| 56 } | 52 } |
| 57 virtual void ProcessBodyInput(const char*, size_t) OVERRIDE {} | |
| 58 virtual void ProcessHeaderInput(const char*, size_t) OVERRIDE {} | |
| 59 virtual void ProcessTrailerInput(const char*, size_t) OVERRIDE {} | |
| 60 virtual void ProcessHeaders(const net::BalsaHeaders&) OVERRIDE {} | |
| 61 virtual void ProcessRequestFirstLine( | |
| 62 const char*, size_t, const char*, size_t, | |
| 63 const char*, size_t, const char*, size_t) OVERRIDE {} | |
| 64 virtual void ProcessResponseFirstLine( | |
| 65 const char*, size_t, const char*, | |
| 66 size_t, const char*, size_t, const char*, size_t) OVERRIDE {} | |
| 67 virtual void ProcessChunkLength(size_t) OVERRIDE {} | |
| 68 virtual void ProcessChunkExtensions(const char*, size_t) OVERRIDE {} | |
| 69 virtual void HeaderDone() OVERRIDE {} | |
| 70 | |
| 71 void AppendToBody(const char* input, size_t size) { | 53 void AppendToBody(const char* input, size_t size) { |
| 72 body_.append(input, size); | 54 body_.append(input, size); |
| 73 } | 55 } |
| 74 bool done_framing() const { return done_framing_; } | 56 bool done_framing() const { return done_framing_; } |
| 75 const string& body() const { return body_; } | 57 const string& body() const { return body_; } |
| 76 | 58 |
| 77 private: | 59 private: |
| 78 bool done_framing_; | 60 bool done_framing_; |
| 79 string body_; | 61 string body_; |
| 80 }; | 62 }; |
| 81 | 63 |
| 82 } // namespace | 64 } // namespace |
| 83 | 65 |
| 66 // static |
| 84 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { | 67 QuicInMemoryCache* QuicInMemoryCache::GetInstance() { |
| 85 return Singleton<QuicInMemoryCache>::get(); | 68 return Singleton<QuicInMemoryCache>::get(); |
| 86 } | 69 } |
| 87 | 70 |
| 88 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( | 71 const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( |
| 89 const BalsaHeaders& request_headers) const { | 72 const BalsaHeaders& request_headers) const { |
| 90 ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); | 73 ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); |
| 91 if (it == responses_.end()) { | 74 if (it == responses_.end()) { |
| 92 return NULL; | 75 return NULL; |
| 93 } | 76 } |
| 94 return it->second; | 77 return it->second; |
| 95 } | 78 } |
| 96 | 79 |
| 97 void QuicInMemoryCache::AddOrVerifyResponse(StringPiece method, | 80 void QuicInMemoryCache::AddSimpleResponse(StringPiece method, |
| 98 StringPiece path, | 81 StringPiece path, |
| 99 StringPiece version, | 82 StringPiece version, |
| 100 StringPiece response_code, | 83 StringPiece response_code, |
| 101 StringPiece response_detail, | 84 StringPiece response_detail, |
| 102 StringPiece body) { | 85 StringPiece body) { |
| 103 BalsaHeaders request_headers, response_headers; | 86 BalsaHeaders request_headers, response_headers; |
| 104 request_headers.SetRequestFirstlineFromStringPieces(method, | 87 request_headers.SetRequestFirstlineFromStringPieces(method, |
| 105 path, | 88 path, |
| 106 version); | 89 version); |
| 107 response_headers.SetRequestFirstlineFromStringPieces(version, | 90 response_headers.SetRequestFirstlineFromStringPieces(version, |
| 108 response_code, | 91 response_code, |
| 109 response_detail); | 92 response_detail); |
| 110 response_headers.AppendHeader("content-length", | 93 response_headers.AppendHeader("content-length", |
| 111 base::IntToString(body.length())); | 94 base::IntToString(body.length())); |
| 112 | 95 |
| 113 // Check if response already exists and matches. | 96 AddResponse(request_headers, response_headers, body); |
| 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 } | 97 } |
| 126 | 98 |
| 127 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, | 99 void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, |
| 128 const BalsaHeaders& response_headers, | 100 const BalsaHeaders& response_headers, |
| 129 StringPiece response_body) { | 101 StringPiece response_body) { |
| 130 LOG(INFO) << "Adding response for: " << GetKey(request_headers); | 102 LOG(INFO) << "Adding response for: " << GetKey(request_headers); |
| 131 if (ContainsKey(responses_, GetKey(request_headers))) { | 103 if (ContainsKey(responses_, GetKey(request_headers))) { |
| 132 LOG(DFATAL) << "Response for given request already exists!"; | 104 LOG(DFATAL) << "Response for given request already exists!"; |
| 105 return; |
| 133 } | 106 } |
| 134 Response* new_response = new Response(); | 107 Response* new_response = new Response(); |
| 135 new_response->set_headers(response_headers); | 108 new_response->set_headers(response_headers); |
| 136 new_response->set_body(response_body); | 109 new_response->set_body(response_body); |
| 137 responses_[GetKey(request_headers)] = new_response; | 110 responses_[GetKey(request_headers)] = new_response; |
| 138 } | 111 } |
| 139 | 112 |
| 113 QuicInMemoryCache::QuicInMemoryCache() { |
| 114 Initialize(); |
| 115 } |
| 116 |
| 140 void QuicInMemoryCache::ResetForTests() { | 117 void QuicInMemoryCache::ResetForTests() { |
| 141 STLDeleteValues(&responses_); | 118 STLDeleteValues(&responses_); |
| 142 Initialize(); | 119 Initialize(); |
| 143 } | 120 } |
| 144 | 121 |
| 145 QuicInMemoryCache::QuicInMemoryCache() { | |
| 146 Initialize(); | |
| 147 } | |
| 148 | |
| 149 void QuicInMemoryCache::Initialize() { | 122 void QuicInMemoryCache::Initialize() { |
| 150 // If there's no defined cache dir, we have no initialization to do. | 123 // If there's no defined cache dir, we have no initialization to do. |
| 151 if (FLAGS_quic_in_memory_cache_dir.empty()) { | 124 if (FLAGS_quic_in_memory_cache_dir.empty()) { |
| 152 LOG(WARNING) << "No cache directory found. Skipping initialization."; | 125 LOG(WARNING) << "No cache directory found. Skipping initialization."; |
| 153 return; | 126 return; |
| 154 } | 127 } |
| 155 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " | 128 LOG(INFO) << "Attempting to initialize QuicInMemoryCache from directory: " |
| 156 << FLAGS_quic_in_memory_cache_dir; | 129 << FLAGS_quic_in_memory_cache_dir; |
| 157 | 130 |
| 158 FilePath directory(FLAGS_quic_in_memory_cache_dir); | 131 FilePath directory(FLAGS_quic_in_memory_cache_dir); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { | 220 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { |
| 248 uri.remove_prefix(8); | 221 uri.remove_prefix(8); |
| 249 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { | 222 } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { |
| 250 uri.remove_prefix(7); | 223 uri.remove_prefix(7); |
| 251 } | 224 } |
| 252 return host.as_string() + uri.as_string(); | 225 return host.as_string() + uri.as_string(); |
| 253 } | 226 } |
| 254 | 227 |
| 255 } // namespace tools | 228 } // namespace tools |
| 256 } // namespace net | 229 } // namespace net |
| OLD | NEW |