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