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"
15 template <typename Type
> struct DefaultSingletonTraits
;
21 class QuicInMemoryCachePeer
;
26 // In-memory cache for HTTP responses.
27 // Reads from disk cache generated by:
28 // `wget -p --save_headers <url>`
29 class QuicInMemoryCache
{
31 enum SpecialResponseType
{
32 REGULAR_RESPONSE
, // Send the headers and body like a server should.
33 CLOSE_CONNECTION
, // Close the connection (sending the close packet).
34 IGNORE_REQUEST
, // Do nothing, expect the client to time out.
37 // Container for response header/body pairs.
43 SpecialResponseType
response_type() const { return response_type_
; }
44 const SpdyHeaderBlock
& headers() const { return headers_
; }
45 const StringPiece
body() const { return StringPiece(body_
); }
48 friend class QuicInMemoryCache
;
50 void set_response_type(SpecialResponseType response_type
) {
51 response_type_
= response_type
;
53 void set_headers(const SpdyHeaderBlock
& headers
) {
56 void set_body(base::StringPiece body
) {
57 body
.CopyToString(&body_
);
60 SpecialResponseType response_type_
;
61 SpdyHeaderBlock headers_
;
64 DISALLOW_COPY_AND_ASSIGN(Response
);
67 // Returns the singleton instance of the cache.
68 static QuicInMemoryCache
* GetInstance();
70 // Retrieve a response from this cache for a given host and path..
71 // If no appropriate response exists, nullptr is returned.
72 const Response
* GetResponse(base::StringPiece host
,
73 base::StringPiece path
) const;
75 // Adds a simple response to the cache. The response headers will
76 // only contain the "content-length" header with the length of |body|.
77 void AddSimpleResponse(base::StringPiece host
,
78 base::StringPiece path
,
80 base::StringPiece response_detail
,
81 base::StringPiece body
);
83 // Add a response to the cache.
84 void AddResponse(base::StringPiece host
,
85 base::StringPiece path
,
86 const SpdyHeaderBlock
& response_headers
,
87 base::StringPiece response_body
);
89 // Simulate a special behavior at a particular path.
90 void AddSpecialResponse(base::StringPiece host
,
91 base::StringPiece path
,
92 SpecialResponseType response_type
);
94 // |cache_cirectory| can be generated using `wget -p --save-headers <url>`.
95 void InitializeFromDirectory(const std::string
& cache_directory
);
98 typedef base::hash_map
<std::string
, Response
*> ResponseMap
;
100 friend struct DefaultSingletonTraits
<QuicInMemoryCache
>;
101 friend class test::QuicInMemoryCachePeer
;
104 ~QuicInMemoryCache();
106 void ResetForTests();
108 void AddResponseImpl(base::StringPiece host
,
109 base::StringPiece path
,
110 SpecialResponseType response_type
,
111 const SpdyHeaderBlock
& response_headers
,
112 base::StringPiece response_body
);
114 std::string
GetKey(base::StringPiece host
, base::StringPiece path
) const;
117 ResponseMap responses_
;
119 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache
);
125 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_