| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    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 |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |    5 #ifndef NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 
|    6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |    6 #define NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 
|    7  |    7  | 
|    8 #include <list> |    8 #include <list> | 
|    9 #include <map> |    9 #include <map> | 
|   10 #include <memory> |   10 #include <memory> | 
|   11 #include <string> |   11 #include <string> | 
|   12 #include <unordered_map> |   12 #include <unordered_map> | 
|   13 #include <vector> |   13 #include <vector> | 
|   14  |   14  | 
|   15 #include "base/files/file_path.h" |   15 #include "base/files/file_path.h" | 
|   16 #include "base/macros.h" |   16 #include "base/macros.h" | 
|   17 #include "base/memory/singleton.h" |  | 
|   18 #include "base/strings/string_piece.h" |   17 #include "base/strings/string_piece.h" | 
|   19 #include "net/http/http_response_headers.h" |   18 #include "net/http/http_response_headers.h" | 
|   20 #include "net/quic/core/spdy_utils.h" |   19 #include "net/quic/core/spdy_utils.h" | 
|   21 #include "net/spdy/spdy_framer.h" |   20 #include "net/spdy/spdy_framer.h" | 
|   22 #include "url/gurl.h" |   21 #include "url/gurl.h" | 
|   23  |   22  | 
|   24 namespace base { |  | 
|   25  |  | 
|   26 template <typename Type> |  | 
|   27 struct DefaultSingletonTraits; |  | 
|   28  |  | 
|   29 }  // namespace base |  | 
|   30  |  | 
|   31 namespace net { |   23 namespace net { | 
|   32  |   24  | 
|   33 namespace test { |  | 
|   34 class QuicInMemoryCachePeer; |  | 
|   35 }  // namespace test |  | 
|   36  |  | 
|   37 class QuicServer; |   25 class QuicServer; | 
|   38  |   26  | 
|   39 // In-memory cache for HTTP responses. |   27 // In-memory cache for HTTP responses. | 
|   40 // Reads from disk cache generated by: |   28 // Reads from disk cache generated by: | 
|   41 // `wget -p --save_headers <url>` |   29 // `wget -p --save_headers <url>` | 
|   42 class QuicInMemoryCache { |   30 class QuicInMemoryCache { | 
|   43  public: |   31  public: | 
|   44   // A ServerPushInfo contains path of the push request and everything needed in |   32   // A ServerPushInfo contains path of the push request and everything needed in | 
|   45   // comprising a response for the push request. |   33   // comprising a response for the push request. | 
|   46   struct ServerPushInfo { |   34   struct ServerPushInfo { | 
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  132     std::vector<base::StringPiece> push_urls_; |  120     std::vector<base::StringPiece> push_urls_; | 
|  133  |  121  | 
|  134    private: |  122    private: | 
|  135     base::StringPiece host_; |  123     base::StringPiece host_; | 
|  136     base::StringPiece path_; |  124     base::StringPiece path_; | 
|  137     QuicInMemoryCache* cache_; |  125     QuicInMemoryCache* cache_; | 
|  138  |  126  | 
|  139     DISALLOW_COPY_AND_ASSIGN(ResourceFile); |  127     DISALLOW_COPY_AND_ASSIGN(ResourceFile); | 
|  140   }; |  128   }; | 
|  141  |  129  | 
|  142   // Returns the singleton instance of the cache. |  130   QuicInMemoryCache(); | 
|  143   static QuicInMemoryCache* GetInstance(); |  131   ~QuicInMemoryCache(); | 
|  144  |  132  | 
|  145   // Retrieve a response from this cache for a given host and path.. |  133   // Retrieve a response from this cache for a given host and path.. | 
|  146   // If no appropriate response exists, nullptr is returned. |  134   // If no appropriate response exists, nullptr is returned. | 
|  147   const Response* GetResponse(base::StringPiece host, |  135   const Response* GetResponse(base::StringPiece host, | 
|  148                               base::StringPiece path) const; |  136                               base::StringPiece path) const; | 
|  149  |  137  | 
|  150   // Adds a simple response to the cache.  The response headers will |  138   // Adds a simple response to the cache.  The response headers will | 
|  151   // only contain the "content-length" header with the length of |body|. |  139   // only contain the "content-length" header with the length of |body|. | 
|  152   void AddSimpleResponse(base::StringPiece host, |  140   void AddSimpleResponse(base::StringPiece host, | 
|  153                          base::StringPiece path, |  141                          base::StringPiece path, | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  187   // 'response'. |  175   // 'response'. | 
|  188   void AddDefaultResponse(Response* response); |  176   void AddDefaultResponse(Response* response); | 
|  189  |  177  | 
|  190   // |cache_cirectory| can be generated using `wget -p --save-headers <url>`. |  178   // |cache_cirectory| can be generated using `wget -p --save-headers <url>`. | 
|  191   void InitializeFromDirectory(const std::string& cache_directory); |  179   void InitializeFromDirectory(const std::string& cache_directory); | 
|  192  |  180  | 
|  193   // Find all the server push resources associated with |request_url|. |  181   // Find all the server push resources associated with |request_url|. | 
|  194   std::list<ServerPushInfo> GetServerPushResources(std::string request_url); |  182   std::list<ServerPushInfo> GetServerPushResources(std::string request_url); | 
|  195  |  183  | 
|  196  private: |  184  private: | 
|  197   friend struct base::DefaultSingletonTraits<QuicInMemoryCache>; |  | 
|  198   friend class test::QuicInMemoryCachePeer; |  | 
|  199  |  | 
|  200   QuicInMemoryCache(); |  | 
|  201   ~QuicInMemoryCache(); |  | 
|  202  |  | 
|  203   void ResetForTests(); |  | 
|  204  |  | 
|  205   void AddResponseImpl(base::StringPiece host, |  185   void AddResponseImpl(base::StringPiece host, | 
|  206                        base::StringPiece path, |  186                        base::StringPiece path, | 
|  207                        SpecialResponseType response_type, |  187                        SpecialResponseType response_type, | 
|  208                        SpdyHeaderBlock response_headers, |  188                        SpdyHeaderBlock response_headers, | 
|  209                        base::StringPiece response_body, |  189                        base::StringPiece response_body, | 
|  210                        SpdyHeaderBlock response_trailers); |  190                        SpdyHeaderBlock response_trailers); | 
|  211  |  191  | 
|  212   std::string GetKey(base::StringPiece host, base::StringPiece path) const; |  192   std::string GetKey(base::StringPiece host, base::StringPiece path) const; | 
|  213  |  193  | 
|  214   // Add some server push urls with given responses for specified |  194   // Add some server push urls with given responses for specified | 
| (...skipping 19 matching lines...) Expand all  Loading... | 
|  234   // Protects against concurrent access from test threads setting responses, and |  214   // Protects against concurrent access from test threads setting responses, and | 
|  235   // server threads accessing those responses. |  215   // server threads accessing those responses. | 
|  236   mutable base::Lock response_mutex_; |  216   mutable base::Lock response_mutex_; | 
|  237  |  217  | 
|  238   DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); |  218   DISALLOW_COPY_AND_ASSIGN(QuicInMemoryCache); | 
|  239 }; |  219 }; | 
|  240  |  220  | 
|  241 }  // namespace net |  221 }  // namespace net | 
|  242  |  222  | 
|  243 #endif  // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ |  223 #endif  // NET_TOOLS_QUIC_QUIC_IN_MEMORY_CACHE_H_ | 
| OLD | NEW |