| 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 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| 6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 13 #include "net/tools/flip_server/balsa_frame.h" | 13 #include "net/tools/flip_server/balsa_frame.h" |
| 14 #include "net/tools/flip_server/balsa_headers.h" | 14 #include "net/tools/flip_server/balsa_headers.h" |
| 15 #include "net/tools/flip_server/noop_balsa_visitor.h" |
| 15 | 16 |
| 16 template <typename T> struct DefaultSingletonTraits; | 17 template <typename T> struct DefaultSingletonTraits; |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 namespace tools { | 20 namespace tools { |
| 20 | 21 |
| 22 namespace test { |
| 23 class QuicInMemoryCachePeer; |
| 24 } // namespace |
| 25 |
| 21 extern std::string FLAGS_quic_in_memory_cache_dir; | 26 extern std::string FLAGS_quic_in_memory_cache_dir; |
| 22 | 27 |
| 23 class QuicServer; | 28 class QuicServer; |
| 24 | 29 |
| 25 // In-memory cache for HTTP responses. | 30 // In-memory cache for HTTP responses. |
| 26 // Reads from disk cache generated by: | 31 // Reads from disk cache generated by: |
| 27 // `wget -p --save_headers <url>` | 32 // `wget -p --save_headers <url>` |
| 28 class QuicInMemoryCache { | 33 class QuicInMemoryCache { |
| 29 public: | 34 public: |
| 30 // Container for response header/body pairs. | 35 // Container for response header/body pairs. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 44 } | 49 } |
| 45 void set_body(base::StringPiece body) { | 50 void set_body(base::StringPiece body) { |
| 46 body.CopyToString(&body_); | 51 body.CopyToString(&body_); |
| 47 } | 52 } |
| 48 | 53 |
| 49 BalsaHeaders headers_; | 54 BalsaHeaders headers_; |
| 50 std::string body_; | 55 std::string body_; |
| 51 | 56 |
| 52 DISALLOW_COPY_AND_ASSIGN(Response); | 57 DISALLOW_COPY_AND_ASSIGN(Response); |
| 53 }; | 58 }; |
| 59 |
| 60 // Returns the singleton instance of the cache. |
| 54 static QuicInMemoryCache* GetInstance(); | 61 static QuicInMemoryCache* GetInstance(); |
| 55 | 62 |
| 56 // Retrieve a response from this cache for a given request. | 63 // Retrieve a response from this cache for a given request. |
| 57 // If no appropriate response exists, NULL is returned. | 64 // If no appropriate response exists, NULL is returned. |
| 58 // Currently, responses are selected based on request URI only. | 65 // Currently, responses are selected based on request URI only. |
| 59 const Response* GetResponse(const BalsaHeaders& request_headers) const; | 66 const Response* GetResponse(const BalsaHeaders& request_headers) const; |
| 60 | 67 |
| 61 // Adds a response to the cache if no matching entry exists. | 68 // Adds a simple response to the cache. The response headers will |
| 62 // Otherwise it verifies that the existing entry matches. | 69 // only contain the "content-length" header with the lenght of |body|. |
| 63 void AddOrVerifyResponse(base::StringPiece method, | 70 void AddSimpleResponse(base::StringPiece method, |
| 64 base::StringPiece path, | 71 base::StringPiece path, |
| 65 base::StringPiece version, | 72 base::StringPiece version, |
| 66 base::StringPiece response_code, | 73 base::StringPiece response_code, |
| 67 base::StringPiece response_detail, | 74 base::StringPiece response_detail, |
| 68 base::StringPiece body); | 75 base::StringPiece body); |
| 69 | 76 |
| 70 // Add a response to the cache. | 77 // Add a response to the cache. |
| 71 void AddResponse(const BalsaHeaders& request_headers, | 78 void AddResponse(const BalsaHeaders& request_headers, |
| 72 const BalsaHeaders& response_headers, | 79 const BalsaHeaders& response_headers, |
| 73 base::StringPiece response_body); | 80 base::StringPiece response_body); |
| 74 | 81 |
| 75 void ResetForTests(); | |
| 76 | |
| 77 private: | 82 private: |
| 78 typedef base::hash_map<std::string, Response*> ResponseMap; | 83 typedef base::hash_map<std::string, Response*> ResponseMap; |
| 79 friend struct DefaultSingletonTraits<QuicInMemoryCache>; | 84 friend struct DefaultSingletonTraits<QuicInMemoryCache>; |
| 85 friend class test::QuicInMemoryCachePeer; |
| 80 | 86 |
| 81 QuicInMemoryCache(); | 87 QuicInMemoryCache(); |
| 82 ~QuicInMemoryCache(); | 88 ~QuicInMemoryCache(); |
| 83 | 89 |
| 90 void ResetForTests(); |
| 91 |
| 84 void Initialize(); | 92 void Initialize(); |
| 93 |
| 85 std::string GetKey(const BalsaHeaders& response_headers) const; | 94 std::string GetKey(const BalsaHeaders& response_headers) const; |
| 86 | 95 |
| 87 // Cached responses. | 96 // Cached responses. |
| 88 ResponseMap responses_; | 97 ResponseMap responses_; |
| 89 | 98 |
| 90 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 99 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |
| 91 }; | 100 }; |
| 92 | 101 |
| 93 } // namespace tools | 102 } // namespace tools |
| 94 } // namespace net | 103 } // namespace net |
| 95 | 104 |
| 96 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 105 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| OLD | NEW |