| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 Response() : response_type_(REGULAR_RESPONSE) {} | 44 Response() : response_type_(REGULAR_RESPONSE) {} |
| 45 ~Response() {} | 45 ~Response() {} |
| 46 | 46 |
| 47 SpecialResponseType response_type() const { return response_type_; } | 47 SpecialResponseType response_type() const { return response_type_; } |
| 48 const BalsaHeaders& headers() const { return headers_; } | 48 const BalsaHeaders& headers() const { return headers_; } |
| 49 const base::StringPiece body() const { return base::StringPiece(body_); } | 49 const base::StringPiece body() const { return base::StringPiece(body_); } |
| 50 | 50 |
| 51 private: | 51 private: |
| 52 friend class QuicInMemoryCache; | 52 friend class QuicInMemoryCache; |
| 53 | 53 |
| 54 void set_response_type(SpecialResponseType response_type) { |
| 55 response_type_ = response_type; |
| 56 } |
| 54 void set_headers(const BalsaHeaders& headers) { | 57 void set_headers(const BalsaHeaders& headers) { |
| 55 headers_.CopyFrom(headers); | 58 headers_.CopyFrom(headers); |
| 56 } | 59 } |
| 57 void set_body(base::StringPiece body) { | 60 void set_body(base::StringPiece body) { |
| 58 body.CopyToString(&body_); | 61 body.CopyToString(&body_); |
| 59 } | 62 } |
| 60 | 63 |
| 61 SpecialResponseType response_type_; | 64 SpecialResponseType response_type_; |
| 62 BalsaHeaders headers_; | 65 BalsaHeaders headers_; |
| 63 std::string body_; | 66 std::string body_; |
| 64 | 67 |
| 65 DISALLOW_COPY_AND_ASSIGN(Response); | 68 DISALLOW_COPY_AND_ASSIGN(Response); |
| 66 }; | 69 }; |
| 67 | 70 |
| 68 // Returns the singleton instance of the cache. | 71 // Returns the singleton instance of the cache. |
| 69 static QuicInMemoryCache* GetInstance(); | 72 static QuicInMemoryCache* GetInstance(); |
| 70 | 73 |
| 71 // Retrieve a response from this cache for a given request. | 74 // Retrieve a response from this cache for a given host and path.. |
| 72 // If no appropriate response exists, nullptr is returned. | 75 // If no appropriate response exists, nullptr is returned. |
| 73 // Currently, responses are selected based on request URI only. | 76 const Response* GetResponse(base::StringPiece host, |
| 74 const Response* GetResponse(const BalsaHeaders& request_headers) const; | 77 base::StringPiece path) const; |
| 75 | 78 |
| 76 // Adds a simple response to the cache. The response headers will | 79 // Adds a simple response to the cache. The response headers will |
| 77 // only contain the "content-length" header with the lenght of |body|. | 80 // only contain the "content-length" header with the length of |body|. |
| 78 void AddSimpleResponse(base::StringPiece method, | 81 void AddSimpleResponse(base::StringPiece host, |
| 79 base::StringPiece path, | 82 base::StringPiece path, |
| 80 base::StringPiece version, | 83 int response_code, |
| 81 base::StringPiece response_code, | |
| 82 base::StringPiece response_detail, | 84 base::StringPiece response_detail, |
| 83 base::StringPiece body); | 85 base::StringPiece body); |
| 84 | 86 |
| 85 // Add a response to the cache. | 87 // Add a response to the cache. |
| 86 void AddResponse(const BalsaHeaders& request_headers, | 88 void AddResponse(base::StringPiece host, |
| 89 base::StringPiece path, |
| 87 const BalsaHeaders& response_headers, | 90 const BalsaHeaders& response_headers, |
| 88 base::StringPiece response_body); | 91 base::StringPiece response_body); |
| 89 | 92 |
| 90 // Simulate a special behavior at a particular path. | 93 // Simulate a special behavior at a particular path. |
| 91 void AddSpecialResponse(base::StringPiece method, | 94 void AddSpecialResponse(base::StringPiece host, |
| 92 base::StringPiece path, | 95 base::StringPiece path, |
| 93 base::StringPiece version, | |
| 94 SpecialResponseType response_type); | 96 SpecialResponseType response_type); |
| 95 | 97 |
| 96 private: | 98 private: |
| 97 typedef base::hash_map<std::string, Response*> ResponseMap; | 99 typedef base::hash_map<std::string, Response*> ResponseMap; |
| 98 friend struct DefaultSingletonTraits<QuicInMemoryCache>; | 100 friend struct DefaultSingletonTraits<QuicInMemoryCache>; |
| 99 friend class test::QuicInMemoryCachePeer; | 101 friend class test::QuicInMemoryCachePeer; |
| 100 | 102 |
| 101 QuicInMemoryCache(); | 103 QuicInMemoryCache(); |
| 102 ~QuicInMemoryCache(); | 104 ~QuicInMemoryCache(); |
| 103 | 105 |
| 104 void ResetForTests(); | 106 void ResetForTests(); |
| 105 | 107 |
| 106 void Initialize(); | 108 void Initialize(); |
| 107 | 109 |
| 108 std::string GetKey(const BalsaHeaders& response_headers) const; | 110 void AddResponseImpl(base::StringPiece host, |
| 111 base::StringPiece path, |
| 112 SpecialResponseType response_type, |
| 113 const BalsaHeaders& response_headers, |
| 114 base::StringPiece response_body); |
| 115 |
| 116 std::string GetKey(base::StringPiece host, base::StringPiece path) const; |
| 109 | 117 |
| 110 // Cached responses. | 118 // Cached responses. |
| 111 ResponseMap responses_; | 119 ResponseMap responses_; |
| 112 | 120 |
| 113 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 121 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |
| 114 }; | 122 }; |
| 115 | 123 |
| 116 } // namespace tools | 124 } // namespace tools |
| 117 } // namespace net | 125 } // namespace net |
| 118 | 126 |
| 119 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 127 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| OLD | NEW |