Chromium Code Reviews| 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" |
|
wtc
2014/06/25 22:22:24
A forward declaration of class GURL should be enou
dmziegler
2014/06/26 01:23:28
Done.
| |
| 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> struct DefaultSingletonTraits; |
| 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 scoped_refptr<HttpResponseHeaders> headers() const { return headers_; } |
|
wtc
2014/06/25 22:22:24
The headers() getter method can still return a con
dmziegler
2014/06/26 01:23:29
Yes, done.
| |
| 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) { body.CopyToString(&body_); } |
| 59 body.CopyToString(&body_); | |
| 60 } | |
| 61 | 57 |
| 62 SpecialResponseType response_type_; | 58 SpecialResponseType response_type_; |
| 63 BalsaHeaders headers_; | 59 scoped_refptr<HttpResponseHeaders> headers_; |
| 64 std::string body_; | 60 std::string body_; |
| 65 | 61 |
| 66 DISALLOW_COPY_AND_ASSIGN(Response); | 62 DISALLOW_COPY_AND_ASSIGN(Response); |
| 67 }; | 63 }; |
| 68 | 64 |
| 69 // Returns the singleton instance of the cache. | 65 // Returns the singleton instance of the cache. |
| 70 static QuicInMemoryCache* GetInstance(); | 66 static QuicInMemoryCache* GetInstance(); |
| 71 | 67 |
| 72 // Retrieve a response from this cache for a given request. | 68 // Retrieve a response from this cache for a given request. |
| 73 // If no appropriate response exists, NULL is returned. | 69 // If no appropriate response exists, NULL is returned. |
| 74 // Currently, responses are selected based on request URI only. | 70 // Currently, responses are selected based on request URI only. |
| 75 const Response* GetResponse(const BalsaHeaders& request_headers) const; | 71 const Response* GetResponse(const GURL& url) const; |
| 76 | 72 |
| 77 // Adds a simple response to the cache. The response headers will | 73 // Adds a simple response to the cache. The response headers will |
| 78 // only contain the "content-length" header with the lenght of |body|. | 74 // only contain the "content-length" header with the lenght of |body|. |
| 79 void AddSimpleResponse(base::StringPiece method, | 75 void AddSimpleResponse(base::StringPiece path, |
| 80 base::StringPiece path, | |
| 81 base::StringPiece version, | |
| 82 base::StringPiece response_code, | 76 base::StringPiece response_code, |
| 83 base::StringPiece response_detail, | 77 base::StringPiece response_detail, |
| 84 base::StringPiece body); | 78 base::StringPiece body); |
| 85 | 79 |
| 86 // Add a response to the cache. | 80 // Add a response to the cache. |
| 87 void AddResponse(const BalsaHeaders& request_headers, | 81 void AddResponse(const GURL& url, |
| 88 const BalsaHeaders& response_headers, | 82 scoped_refptr<HttpResponseHeaders> response_headers, |
| 89 base::StringPiece response_body); | 83 base::StringPiece response_body); |
| 90 | 84 |
| 91 // Simulate a special behavior at a particular path. | 85 // Simulate a special behavior at a particular path. |
| 92 void AddSpecialResponse(base::StringPiece method, | 86 void AddSpecialResponse(base::StringPiece path, |
| 93 base::StringPiece path, | |
| 94 base::StringPiece version, | |
| 95 SpecialResponseType response_type); | 87 SpecialResponseType response_type); |
| 96 | 88 |
| 97 private: | 89 private: |
| 98 typedef base::hash_map<std::string, Response*> ResponseMap; | 90 typedef base::hash_map<std::string, Response*> ResponseMap; |
| 99 friend struct DefaultSingletonTraits<QuicInMemoryCache>; | 91 friend struct DefaultSingletonTraits<QuicInMemoryCache>; |
| 100 friend class test::QuicInMemoryCachePeer; | 92 friend class test::QuicInMemoryCachePeer; |
| 101 | 93 |
| 102 QuicInMemoryCache(); | 94 QuicInMemoryCache(); |
| 103 ~QuicInMemoryCache(); | 95 ~QuicInMemoryCache(); |
| 104 | 96 |
| 105 void ResetForTests(); | 97 void ResetForTests(); |
| 106 | 98 |
| 107 void Initialize(); | 99 void Initialize(); |
| 108 | 100 |
| 109 std::string GetKey(const BalsaHeaders& response_headers) const; | |
| 110 | |
| 111 // Cached responses. | 101 // Cached responses. |
| 112 ResponseMap responses_; | 102 ResponseMap responses_; |
| 113 | 103 |
| 114 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 104 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |
| 115 }; | 105 }; |
| 116 | 106 |
| 117 } // namespace net | 107 } // namespace net |
| 118 | 108 |
| 119 #endif // NET_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 109 #endif // NET_QUIC_QUIC_IN_MEMORY_CACHE_H_ |
| OLD | NEW |