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/tools/flip_server/balsa_frame.h"
14 #include "net/tools/flip_server/balsa_headers.h"
16 template <typename T
> struct DefaultSingletonTraits
;
21 extern std::string FLAGS_quic_in_memory_cache_dir
;
25 // In-memory cache for HTTP responses.
26 // Reads from disk cache generated by:
27 // `wget -p --save_headers <url>`
28 class QuicInMemoryCache
{
30 // Container for response header/body pairs.
36 const BalsaHeaders
& headers() const { return headers_
; }
37 const base::StringPiece
body() const { return base::StringPiece(body_
); }
40 friend class QuicInMemoryCache
;
42 void set_headers(const BalsaHeaders
& headers
) {
43 headers_
.CopyFrom(headers
);
45 void set_body(base::StringPiece body
) {
46 body
.CopyToString(&body_
);
49 BalsaHeaders headers_
;
52 DISALLOW_COPY_AND_ASSIGN(Response
);
54 static QuicInMemoryCache
* GetInstance();
56 // Retrieve a response from this cache for a given request.
57 // If no appropriate response exists, NULL is returned.
58 // Currently, responses are selected based on request URI only.
59 const Response
* GetResponse(const BalsaHeaders
& request_headers
) const;
61 // Add a response to the cache.
62 void AddResponse(const BalsaHeaders
& request_headers
,
63 const BalsaHeaders
& response_headers
,
64 base::StringPiece response_body
);
69 typedef base::hash_map
<std::string
, Response
*> ResponseMap
;
73 friend struct DefaultSingletonTraits
<QuicInMemoryCache
>;
77 std::string
GetKey(const BalsaHeaders
& response_headers
) const;
80 ResponseMap responses_
;
82 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache
);
88 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_