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