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