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