| 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" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 extern std::string FLAGS_quic_in_memory_cache_dir; | 26 extern std::string FLAGS_quic_in_memory_cache_dir; |
| 27 | 27 |
| 28 class QuicServer; | 28 class QuicServer; |
| 29 | 29 |
| 30 // In-memory cache for HTTP responses. | 30 // In-memory cache for HTTP responses. |
| 31 // Reads from disk cache generated by: | 31 // Reads from disk cache generated by: |
| 32 // `wget -p --save_headers <url>` | 32 // `wget -p --save_headers <url>` |
| 33 class QuicInMemoryCache { | 33 class QuicInMemoryCache { |
| 34 public: | 34 public: |
| 35 enum SpecialResponseType { |
| 36 REGULAR_RESPONSE, // Send the headers and body like a server should. |
| 37 CLOSE_CONNECTION, // Close the connection (sending the close packet). |
| 38 IGNORE_REQUEST, // Do nothing, expect the client to time out. |
| 39 }; |
| 40 |
| 35 // Container for response header/body pairs. | 41 // Container for response header/body pairs. |
| 36 class Response { | 42 class Response { |
| 37 public: | 43 public: |
| 38 Response() {} | 44 Response() : response_type_(REGULAR_RESPONSE) {} |
| 39 ~Response() {} | 45 ~Response() {} |
| 40 | 46 |
| 47 const SpecialResponseType response_type() const { return response_type_; } |
| 41 const BalsaHeaders& headers() const { return headers_; } | 48 const BalsaHeaders& headers() const { return headers_; } |
| 42 const base::StringPiece body() const { return base::StringPiece(body_); } | 49 const base::StringPiece body() const { return base::StringPiece(body_); } |
| 43 | 50 |
| 44 private: | 51 private: |
| 45 friend class QuicInMemoryCache; | 52 friend class QuicInMemoryCache; |
| 46 | 53 |
| 47 void set_headers(const BalsaHeaders& headers) { | 54 void set_headers(const BalsaHeaders& headers) { |
| 48 headers_.CopyFrom(headers); | 55 headers_.CopyFrom(headers); |
| 49 } | 56 } |
| 50 void set_body(base::StringPiece body) { | 57 void set_body(base::StringPiece body) { |
| 51 body.CopyToString(&body_); | 58 body.CopyToString(&body_); |
| 52 } | 59 } |
| 53 | 60 |
| 61 SpecialResponseType response_type_; |
| 54 BalsaHeaders headers_; | 62 BalsaHeaders headers_; |
| 55 std::string body_; | 63 std::string body_; |
| 56 | 64 |
| 57 DISALLOW_COPY_AND_ASSIGN(Response); | 65 DISALLOW_COPY_AND_ASSIGN(Response); |
| 58 }; | 66 }; |
| 59 | 67 |
| 60 // Returns the singleton instance of the cache. | 68 // Returns the singleton instance of the cache. |
| 61 static QuicInMemoryCache* GetInstance(); | 69 static QuicInMemoryCache* GetInstance(); |
| 62 | 70 |
| 63 // Retrieve a response from this cache for a given request. | 71 // Retrieve a response from this cache for a given request. |
| 64 // If no appropriate response exists, NULL is returned. | 72 // If no appropriate response exists, NULL is returned. |
| 65 // Currently, responses are selected based on request URI only. | 73 // Currently, responses are selected based on request URI only. |
| 66 const Response* GetResponse(const BalsaHeaders& request_headers) const; | 74 const Response* GetResponse(const BalsaHeaders& request_headers) const; |
| 67 | 75 |
| 68 // Adds a simple response to the cache. The response headers will | 76 // Adds a simple response to the cache. The response headers will |
| 69 // only contain the "content-length" header with the lenght of |body|. | 77 // only contain the "content-length" header with the lenght of |body|. |
| 70 void AddSimpleResponse(base::StringPiece method, | 78 void AddSimpleResponse(base::StringPiece method, |
| 71 base::StringPiece path, | 79 base::StringPiece path, |
| 72 base::StringPiece version, | 80 base::StringPiece version, |
| 73 base::StringPiece response_code, | 81 base::StringPiece response_code, |
| 74 base::StringPiece response_detail, | 82 base::StringPiece response_detail, |
| 75 base::StringPiece body); | 83 base::StringPiece body); |
| 76 | 84 |
| 77 // Add a response to the cache. | 85 // Add a response to the cache. |
| 78 void AddResponse(const BalsaHeaders& request_headers, | 86 void AddResponse(const BalsaHeaders& request_headers, |
| 79 const BalsaHeaders& response_headers, | 87 const BalsaHeaders& response_headers, |
| 80 base::StringPiece response_body); | 88 base::StringPiece response_body); |
| 81 | 89 |
| 90 // Simulate a special behavior at a particular path. |
| 91 void AddSpecialResponse(base::StringPiece method, |
| 92 base::StringPiece path, |
| 93 base::StringPiece version, |
| 94 SpecialResponseType response_type); |
| 95 |
| 82 private: | 96 private: |
| 83 typedef base::hash_map<std::string, Response*> ResponseMap; | 97 typedef base::hash_map<std::string, Response*> ResponseMap; |
| 84 friend struct DefaultSingletonTraits<QuicInMemoryCache>; | 98 friend struct DefaultSingletonTraits<QuicInMemoryCache>; |
| 85 friend class test::QuicInMemoryCachePeer; | 99 friend class test::QuicInMemoryCachePeer; |
| 86 | 100 |
| 87 QuicInMemoryCache(); | 101 QuicInMemoryCache(); |
| 88 ~QuicInMemoryCache(); | 102 ~QuicInMemoryCache(); |
| 89 | 103 |
| 90 void ResetForTests(); | 104 void ResetForTests(); |
| 91 | 105 |
| 92 void Initialize(); | 106 void Initialize(); |
| 93 | 107 |
| 94 std::string GetKey(const BalsaHeaders& response_headers) const; | 108 std::string GetKey(const BalsaHeaders& response_headers) const; |
| 95 | 109 |
| 96 // Cached responses. | 110 // Cached responses. |
| 97 ResponseMap responses_; | 111 ResponseMap responses_; |
| 98 | 112 |
| 99 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 113 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |
| 100 }; | 114 }; |
| 101 | 115 |
| 102 } // namespace tools | 116 } // namespace tools |
| 103 } // namespace net | 117 } // namespace net |
| 104 | 118 |
| 105 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 119 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| OLD | NEW |