Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / tools / quic / quic_in_memory_cache.h
blobbe091d5579e7cd8a02bfa0c4605ae89aae5accb5
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_
8 #include <string>
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/singleton.h"
12 #include "base/strings/string_piece.h"
13 #include "net/tools/balsa/balsa_frame.h"
14 #include "net/tools/balsa/balsa_headers.h"
15 #include "net/tools/balsa/noop_balsa_visitor.h"
17 template <typename T> struct DefaultSingletonTraits;
19 namespace net {
20 namespace tools {
22 namespace test {
23 class QuicInMemoryCachePeer;
24 } // namespace
26 extern std::string FLAGS_quic_in_memory_cache_dir;
28 class QuicServer;
30 // In-memory cache for HTTP responses.
31 // Reads from disk cache generated by:
32 // `wget -p --save_headers <url>`
33 class QuicInMemoryCache {
34 public:
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.
42 class Response {
43 public:
44 Response() : response_type_(REGULAR_RESPONSE) {}
45 ~Response() {}
47 const SpecialResponseType response_type() const { return response_type_; }
48 const BalsaHeaders& headers() const { return headers_; }
49 const base::StringPiece body() const { return base::StringPiece(body_); }
51 private:
52 friend class QuicInMemoryCache;
54 void set_headers(const BalsaHeaders& headers) {
55 headers_.CopyFrom(headers);
57 void set_body(base::StringPiece body) {
58 body.CopyToString(&body_);
61 SpecialResponseType response_type_;
62 BalsaHeaders headers_;
63 std::string body_;
65 DISALLOW_COPY_AND_ASSIGN(Response);
68 // Returns the singleton instance of the cache.
69 static QuicInMemoryCache* GetInstance();
71 // Retrieve a response from this cache for a given request.
72 // If no appropriate response exists, NULL is returned.
73 // Currently, responses are selected based on request URI only.
74 const Response* GetResponse(const BalsaHeaders& request_headers) const;
76 // Adds a simple response to the cache. The response headers will
77 // only contain the "content-length" header with the lenght of |body|.
78 void AddSimpleResponse(base::StringPiece method,
79 base::StringPiece path,
80 base::StringPiece version,
81 base::StringPiece response_code,
82 base::StringPiece response_detail,
83 base::StringPiece body);
85 // Add a response to the cache.
86 void AddResponse(const BalsaHeaders& request_headers,
87 const BalsaHeaders& response_headers,
88 base::StringPiece response_body);
90 // Simulate a special behavior at a particular path.
91 void AddSpecialResponse(base::StringPiece method,
92 base::StringPiece path,
93 base::StringPiece version,
94 SpecialResponseType response_type);
96 private:
97 typedef base::hash_map<std::string, Response*> ResponseMap;
98 friend struct DefaultSingletonTraits<QuicInMemoryCache>;
99 friend class test::QuicInMemoryCachePeer;
101 QuicInMemoryCache();
102 ~QuicInMemoryCache();
104 void ResetForTests();
106 void Initialize();
108 std::string GetKey(const BalsaHeaders& response_headers) const;
110 // Cached responses.
111 ResponseMap responses_;
113 DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache);
116 } // namespace tools
117 } // namespace net
119 #endif // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_