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
3 // found in the LICENSE file.
5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/singleton.h"
12 #include "base/strings/string_piece.h"
13 #include "net/spdy/spdy_framer.h"
17 template <typename Type
> struct DefaultSingletonTraits
;
25 class QuicInMemoryCachePeer
;
30 // In-memory cache for HTTP responses.
31 // Reads from disk cache generated by:
32 // `wget -p --save_headers <url>`
33 class QuicInMemoryCache
{
35 enum SpecialResponseType
{
36 REGULAR_RESPONSE
, // Send the headers and body like a server should.
37 CLOSE_CONNECTION
, // Close the connection (sending the close packet).
38 IGNORE_REQUEST
, // Do nothing, expect the client to time out.
41 // Container for response header/body pairs.
47 SpecialResponseType
response_type() const { return response_type_
; }
48 const SpdyHeaderBlock
& headers() const { return headers_
; }
49 const StringPiece
body() const { return StringPiece(body_
); }
51 void set_response_type(SpecialResponseType response_type
) {
52 response_type_
= response_type
;
54 void set_headers(const SpdyHeaderBlock
& headers
) {
57 void set_body(base::StringPiece body
) {
58 body
.CopyToString(&body_
);
62 SpecialResponseType response_type_
;
63 SpdyHeaderBlock headers_
;
66 DISALLOW_COPY_AND_ASSIGN(Response
);
69 // Returns the singleton instance of the cache.
70 static QuicInMemoryCache
* GetInstance();
72 // Retrieve a response from this cache for a given host and path..
73 // If no appropriate response exists, nullptr is returned.
74 const Response
* GetResponse(base::StringPiece host
,
75 base::StringPiece path
) const;
77 // Adds a simple response to the cache. The response headers will
78 // only contain the "content-length" header with the length of |body|.
79 void AddSimpleResponse(base::StringPiece host
,
80 base::StringPiece path
,
82 base::StringPiece response_detail
,
83 base::StringPiece body
);
85 // Add a response to the cache.
86 void AddResponse(base::StringPiece host
,
87 base::StringPiece path
,
88 const SpdyHeaderBlock
& response_headers
,
89 base::StringPiece response_body
);
91 // Simulate a special behavior at a particular path.
92 void AddSpecialResponse(base::StringPiece host
,
93 base::StringPiece path
,
94 SpecialResponseType response_type
);
96 // Sets a default response in case of cache misses. Takes ownership of
98 void AddDefaultResponse(Response
* response
);
100 // |cache_cirectory| can be generated using `wget -p --save-headers <url>`.
101 void InitializeFromDirectory(const std::string
& cache_directory
);
104 typedef base::hash_map
<std::string
, Response
*> ResponseMap
;
106 friend struct base::DefaultSingletonTraits
<QuicInMemoryCache
>;
107 friend class test::QuicInMemoryCachePeer
;
110 ~QuicInMemoryCache();
112 void ResetForTests();
114 void AddResponseImpl(base::StringPiece host
,
115 base::StringPiece path
,
116 SpecialResponseType response_type
,
117 const SpdyHeaderBlock
& response_headers
,
118 base::StringPiece response_body
);
120 std::string
GetKey(base::StringPiece host
, base::StringPiece path
) const;
123 ResponseMap responses_
;
125 // The default response for cache misses, if set.
126 scoped_ptr
<Response
> default_response_
;
128 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache
);
134 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_