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 <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 #include <vector> |
11 | 12 |
12 #include "base/containers/hash_tables.h" | 13 #include "base/containers/hash_tables.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/singleton.h" | 15 #include "base/memory/singleton.h" |
15 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
16 #include "net/quic/spdy_utils.h" | 17 #include "net/quic/spdy_utils.h" |
17 #include "net/spdy/spdy_framer.h" | 18 #include "net/spdy/spdy_framer.h" |
18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
19 | 20 |
20 using base::StringPiece; | 21 using base::StringPiece; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 GURL request_url; | 53 GURL request_url; |
53 net::SpdyHeaderBlock headers; | 54 net::SpdyHeaderBlock headers; |
54 net::SpdyPriority priority; | 55 net::SpdyPriority priority; |
55 string body; | 56 string body; |
56 }; | 57 }; |
57 | 58 |
58 enum SpecialResponseType { | 59 enum SpecialResponseType { |
59 REGULAR_RESPONSE, // Send the headers and body like a server should. | 60 REGULAR_RESPONSE, // Send the headers and body like a server should. |
60 CLOSE_CONNECTION, // Close the connection (sending the close packet). | 61 CLOSE_CONNECTION, // Close the connection (sending the close packet). |
61 IGNORE_REQUEST, // Do nothing, expect the client to time out. | 62 IGNORE_REQUEST, // Do nothing, expect the client to time out. |
| 63 BIDIRECTIONAL, // Send headers, multiple chuncks of body and trailers. |
62 }; | 64 }; |
63 | 65 |
64 // Container for response header/body pairs. | 66 // Container for response header/body pairs. |
65 class Response { | 67 class Response { |
66 public: | 68 public: |
67 Response(); | 69 Response(); |
68 ~Response(); | 70 ~Response(); |
69 | 71 |
70 SpecialResponseType response_type() const { return response_type_; } | 72 SpecialResponseType response_type() const { return response_type_; } |
71 const SpdyHeaderBlock& headers() const { return headers_; } | 73 const SpdyHeaderBlock& headers() const { return headers_; } |
72 const SpdyHeaderBlock& trailers() const { return trailers_; } | 74 const SpdyHeaderBlock& trailers() const { return trailers_; } |
73 const base::StringPiece body() const { return base::StringPiece(body_); } | 75 const base::StringPiece body() const { return base::StringPiece(body_); } |
| 76 const std::vector<std::string>& body_chunks() const { return body_chunks_; } |
74 | 77 |
75 void set_response_type(SpecialResponseType response_type) { | 78 void set_response_type(SpecialResponseType response_type) { |
76 response_type_ = response_type; | 79 response_type_ = response_type; |
77 } | 80 } |
78 void set_headers(const SpdyHeaderBlock& headers) { headers_ = headers; } | 81 void set_headers(const SpdyHeaderBlock& headers) { headers_ = headers; } |
79 void set_trailers(const SpdyHeaderBlock& trailers) { trailers_ = trailers; } | 82 void set_trailers(const SpdyHeaderBlock& trailers) { trailers_ = trailers; } |
80 void set_body(base::StringPiece body) { body.CopyToString(&body_); } | 83 void set_body(base::StringPiece body) { body.CopyToString(&body_); } |
| 84 void SetBodyChunks(std::vector<std::string> body_chunks) { |
| 85 for (auto i : body_chunks) { |
| 86 body_chunks_.push_back(i); |
| 87 } |
| 88 } |
81 | 89 |
82 private: | 90 private: |
83 SpecialResponseType response_type_; | 91 SpecialResponseType response_type_; |
84 SpdyHeaderBlock headers_; | 92 SpdyHeaderBlock headers_; |
85 SpdyHeaderBlock trailers_; | 93 SpdyHeaderBlock trailers_; |
86 std::string body_; | 94 std::string body_; |
| 95 std::vector<std::string> body_chunks_; |
87 | 96 |
88 DISALLOW_COPY_AND_ASSIGN(Response); | 97 DISALLOW_COPY_AND_ASSIGN(Response); |
89 }; | 98 }; |
90 | 99 |
91 // Returns the singleton instance of the cache. | 100 // Returns the singleton instance of the cache. |
92 static QuicInMemoryCache* GetInstance(); | 101 static QuicInMemoryCache* GetInstance(); |
93 | 102 |
94 // Retrieve a response from this cache for a given host and path.. | 103 // Retrieve a response from this cache for a given host and path.. |
95 // If no appropriate response exists, nullptr is returned. | 104 // If no appropriate response exists, nullptr is returned. |
96 const Response* GetResponse(base::StringPiece host, | 105 const Response* GetResponse(base::StringPiece host, |
97 base::StringPiece path) const; | 106 base::StringPiece path) const; |
98 | 107 |
99 // Adds a simple response to the cache. The response headers will | 108 // Adds a simple response to the cache. The response headers will |
100 // only contain the "content-length" header with the length of |body|. | 109 // only contain the "content-length" header with the length of |body|. |
101 void AddSimpleResponse(base::StringPiece host, | 110 void AddSimpleResponse(base::StringPiece host, |
102 base::StringPiece path, | 111 base::StringPiece path, |
103 int response_code, | 112 int response_code, |
104 base::StringPiece body); | 113 base::StringPiece body); |
105 | 114 |
| 115 void AddBidirectionalResponse(StringPiece host, |
| 116 StringPiece path, |
| 117 int response_code, |
| 118 const std::vector<std::string>& body_chunks, |
| 119 const SpdyHeaderBlock& response_trailers); |
| 120 |
106 // Add a simple response to the cache as AddSimpleResponse() does, and add | 121 // Add a simple response to the cache as AddSimpleResponse() does, and add |
107 // some server push resources(resource path, corresponding response status and | 122 // some server push resources(resource path, corresponding response status and |
108 // path) associated with it. | 123 // path) associated with it. |
109 // Push resource implicitly come from the same host. | 124 // Push resource implicitly come from the same host. |
110 void AddSimpleResponseWithServerPushResources( | 125 void AddSimpleResponseWithServerPushResources( |
111 base::StringPiece host, | 126 base::StringPiece host, |
112 base::StringPiece path, | 127 base::StringPiece path, |
113 int response_code, | 128 int response_code, |
114 base::StringPiece body, | 129 base::StringPiece body, |
115 std::list<ServerPushInfo> push_resources); | 130 std::list<ServerPushInfo> push_resources); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 168 |
154 void ResetForTests(); | 169 void ResetForTests(); |
155 | 170 |
156 void AddResponseImpl(base::StringPiece host, | 171 void AddResponseImpl(base::StringPiece host, |
157 base::StringPiece path, | 172 base::StringPiece path, |
158 SpecialResponseType response_type, | 173 SpecialResponseType response_type, |
159 const SpdyHeaderBlock& response_headers, | 174 const SpdyHeaderBlock& response_headers, |
160 base::StringPiece response_body, | 175 base::StringPiece response_body, |
161 const SpdyHeaderBlock& response_trailers); | 176 const SpdyHeaderBlock& response_trailers); |
162 | 177 |
| 178 void AddResponseImpl(base::StringPiece host, |
| 179 base::StringPiece path, |
| 180 SpecialResponseType response_type, |
| 181 const SpdyHeaderBlock& response_headers, |
| 182 base::StringPiece response_body, |
| 183 const std::vector<std::string>& body_chunks, |
| 184 const SpdyHeaderBlock& response_trailers); |
| 185 |
163 string GetKey(base::StringPiece host, base::StringPiece path) const; | 186 string GetKey(base::StringPiece host, base::StringPiece path) const; |
164 | 187 |
165 // Add some server push urls with given responses for specified | 188 // Add some server push urls with given responses for specified |
166 // request if these push resources are not associated with this request yet. | 189 // request if these push resources are not associated with this request yet. |
167 void MaybeAddServerPushResources(base::StringPiece request_host, | 190 void MaybeAddServerPushResources(base::StringPiece request_host, |
168 base::StringPiece request_path, | 191 base::StringPiece request_path, |
169 std::list<ServerPushInfo> push_resources); | 192 std::list<ServerPushInfo> push_resources); |
170 | 193 |
171 // Check if push resource(push_host/push_path) associated with given request | 194 // Check if push resource(push_host/push_path) associated with given request |
172 // url already exists in server push map. | 195 // url already exists in server push map. |
173 bool PushResourceExistsInCache(string original_request_url, | 196 bool PushResourceExistsInCache(string original_request_url, |
174 ServerPushInfo resource); | 197 ServerPushInfo resource); |
175 | 198 |
176 // Cached responses. | 199 // Cached responses. |
177 ResponseMap responses_; | 200 ResponseMap responses_; |
178 | 201 |
179 // The default response for cache misses, if set. | 202 // The default response for cache misses, if set. |
180 scoped_ptr<Response> default_response_; | 203 scoped_ptr<Response> default_response_; |
181 | 204 |
182 // A map from request URL to associated server push responses (if any). | 205 // A map from request URL to associated server push responses (if any). |
183 std::multimap<string, ServerPushInfo> server_push_resources_; | 206 std::multimap<string, ServerPushInfo> server_push_resources_; |
184 | 207 |
185 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 208 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |
186 }; | 209 }; |
187 | 210 |
188 } // namespace net | 211 } // namespace net |
189 | 212 |
190 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 213 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
OLD | NEW |