Home | History | Annotate | Download | only in quic
      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.
      4 
      5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
      6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
      7 
      8 #include <string>
      9 
     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"
     16 
     17 template <typename T> struct DefaultSingletonTraits;
     18 
     19 namespace net {
     20 namespace tools {
     21 
     22 namespace test {
     23 class QuicInMemoryCachePeer;
     24 }  // namespace
     25 
     26 extern std::string FLAGS_quic_in_memory_cache_dir;
     27 
     28 class QuicServer;
     29 
     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.
     39   };
     40 
     41   // Container for response header/body pairs.
     42   class Response {
     43    public:
     44     Response() : response_type_(REGULAR_RESPONSE) {}
     45     ~Response() {}
     46 
     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_); }
     50 
     51    private:
     52     friend class QuicInMemoryCache;
     53 
     54     void set_headers(const BalsaHeaders& headers) {
     55       headers_.CopyFrom(headers);
     56     }
     57     void set_body(base::StringPiece body) {
     58       body.CopyToString(&body_);
     59     }
     60 
     61     SpecialResponseType response_type_;
     62     BalsaHeaders headers_;
     63     std::string body_;
     64 
     65     DISALLOW_COPY_AND_ASSIGN(Response);
     66   };
     67 
     68   // Returns the singleton instance of the cache.
     69   static QuicInMemoryCache* GetInstance();
     70 
     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;
     75 
     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);
     84 
     85   // Add a response to the cache.
     86   void AddResponse(const BalsaHeaders& request_headers,
     87                    const BalsaHeaders& response_headers,
     88                    base::StringPiece response_body);
     89 
     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);
     95 
     96  private:
     97   typedef base::hash_map<std::string, Response*> ResponseMap;
     98   friend struct DefaultSingletonTraits<QuicInMemoryCache>;
     99   friend class test::QuicInMemoryCachePeer;
    100 
    101   QuicInMemoryCache();
    102   ~QuicInMemoryCache();
    103 
    104   void ResetForTests();
    105 
    106   void Initialize();
    107 
    108   std::string GetKey(const BalsaHeaders& response_headers) const;
    109 
    110   // Cached responses.
    111   ResponseMap responses_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache);
    114 };
    115 
    116 }  // namespace tools
    117 }  // namespace net
    118 
    119 #endif  // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
    120