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