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
3 // found in the LICENSE file.
5 #ifndef NET_QUIC_QUIC_IN_MEMORY_CACHE_H_
6 #define NET_QUIC_QUIC_IN_MEMORY_CACHE_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_piece.h"
14 #include "net/http/http_response_headers.h"
16 template <typename T
> struct DefaultSingletonTraits
;
21 extern base::FilePath::StringType g_quic_in_memory_cache_dir
;
24 class QuicInMemoryCachePeer
;
29 // In-memory cache for HTTP responses.
30 // Reads from disk cache generated by:
31 // `wget -p --save_headers <url>`
32 class QuicInMemoryCache
{
34 enum SpecialResponseType
{
35 REGULAR_RESPONSE
, // Send the headers and body like a server should.
36 CLOSE_CONNECTION
, // Close the connection (sending the close packet).
37 IGNORE_REQUEST
, // Do nothing, expect the client to time out.
40 // Container for response header/body pairs.
46 SpecialResponseType
response_type() const { return response_type_
; }
47 const HttpResponseHeaders
& headers() const { return *headers_
.get(); }
48 const base::StringPiece
body() const { return base::StringPiece(body_
); }
51 friend class QuicInMemoryCache
;
53 void set_headers(scoped_refptr
<HttpResponseHeaders
> headers
) {
56 void set_body(base::StringPiece body
) {
57 body
.CopyToString(&body_
);
60 SpecialResponseType response_type_
;
61 scoped_refptr
<HttpResponseHeaders
> 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 request.
71 // If no appropriate response exists, NULL is returned.
72 // Currently, responses are selected based on request URI only.
73 const Response
* GetResponse(const GURL
& url
) 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 path
,
78 base::StringPiece version
,
79 base::StringPiece response_code
,
80 base::StringPiece response_detail
,
81 base::StringPiece body
);
83 // Add a response to the cache.
84 void AddResponse(const GURL
& url
,
85 scoped_refptr
<HttpResponseHeaders
> response_headers
,
86 base::StringPiece response_body
);
88 // Simulate a special behavior at a particular path.
89 void AddSpecialResponse(base::StringPiece path
,
90 SpecialResponseType response_type
);
93 typedef base::hash_map
<std::string
, Response
*> ResponseMap
;
94 friend struct DefaultSingletonTraits
<QuicInMemoryCache
>;
95 friend class test::QuicInMemoryCachePeer
;
100 void ResetForTests();
104 std::string
GetKey(const GURL
& url
) const;
107 ResponseMap responses_
;
109 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache
);
114 #endif // NET_QUIC_QUIC_IN_MEMORY_CACHE_H_