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/flip_server/balsa_frame.h"
     14 #include "net/tools/flip_server/balsa_headers.h"
     15 
     16 template <typename T> struct DefaultSingletonTraits;
     17 
     18 namespace net {
     19 namespace tools {
     20 
     21 extern std::string FLAGS_quic_in_memory_cache_dir;
     22 
     23 class QuicServer;
     24 
     25 // In-memory cache for HTTP responses.
     26 // Reads from disk cache generated by:
     27 // `wget -p --save_headers <url>`
     28 class QuicInMemoryCache {
     29  public:
     30   // Container for response header/body pairs.
     31   class Response {
     32    public:
     33     Response() {}
     34     ~Response() {}
     35 
     36     const BalsaHeaders& headers() const { return headers_; }
     37     const base::StringPiece body() const { return base::StringPiece(body_); }
     38 
     39    private:
     40     friend class QuicInMemoryCache;
     41 
     42     void set_headers(const BalsaHeaders& headers) {
     43       headers_.CopyFrom(headers);
     44     }
     45     void set_body(base::StringPiece body) {
     46       body.CopyToString(&body_);
     47     }
     48 
     49     BalsaHeaders headers_;
     50     std::string body_;
     51 
     52     DISALLOW_COPY_AND_ASSIGN(Response);
     53   };
     54   static QuicInMemoryCache* GetInstance();
     55 
     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;
     60 
     61   // Add a response to the cache.
     62   void AddResponse(const BalsaHeaders& request_headers,
     63                    const BalsaHeaders& response_headers,
     64                    base::StringPiece response_body);
     65 
     66   void ResetForTests();
     67 
     68  private:
     69   typedef base::hash_map<std::string, Response*> ResponseMap;
     70 
     71 
     72   QuicInMemoryCache();
     73   friend struct DefaultSingletonTraits<QuicInMemoryCache>;
     74   ~QuicInMemoryCache();
     75 
     76   void Initialize();
     77   std::string GetKey(const BalsaHeaders& response_headers) const;
     78 
     79   // Cached responses.
     80   ResponseMap responses_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache);
     83 };
     84 
     85 }  // namespace tools
     86 }  // namespace net
     87 
     88 #endif  // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_
     89