• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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_FLIP_SERVER_MEM_CACHE_H_
6 #define NET_TOOLS_FLIP_SERVER_MEM_CACHE_H_
7 
8 #include <map>
9 #include <string>
10 
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/tools/balsa/balsa_headers.h"
14 #include "net/tools/balsa/balsa_visitor_interface.h"
15 #include "net/tools/flip_server/constants.h"
16 
17 namespace net {
18 
19 class StoreBodyAndHeadersVisitor : public BalsaVisitorInterface {
20  public:
HandleError()21   void HandleError() { error_ = true; }
22 
23   // BalsaVisitorInterface:
ProcessBodyInput(const char * input,size_t size)24   virtual void ProcessBodyInput(const char* input, size_t size) OVERRIDE {}
25   virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE;
ProcessHeaderInput(const char * input,size_t size)26   virtual void ProcessHeaderInput(const char* input, size_t size) OVERRIDE {}
ProcessTrailerInput(const char * input,size_t size)27   virtual void ProcessTrailerInput(const char* input, size_t size) OVERRIDE {}
ProcessHeaders(const BalsaHeaders & headers)28   virtual void ProcessHeaders(const BalsaHeaders& headers) OVERRIDE {
29     // nothing to do here-- we're assuming that the BalsaFrame has
30     // been handed our headers.
31   }
ProcessRequestFirstLine(const char * line_input,size_t line_length,const char * method_input,size_t method_length,const char * request_uri_input,size_t request_uri_length,const char * version_input,size_t version_length)32   virtual void ProcessRequestFirstLine(const char* line_input,
33                                        size_t line_length,
34                                        const char* method_input,
35                                        size_t method_length,
36                                        const char* request_uri_input,
37                                        size_t request_uri_length,
38                                        const char* version_input,
39                                        size_t version_length) OVERRIDE {}
ProcessResponseFirstLine(const char * line_input,size_t line_length,const char * version_input,size_t version_length,const char * status_input,size_t status_length,const char * reason_input,size_t reason_length)40   virtual void ProcessResponseFirstLine(const char* line_input,
41                                         size_t line_length,
42                                         const char* version_input,
43                                         size_t version_length,
44                                         const char* status_input,
45                                         size_t status_length,
46                                         const char* reason_input,
47                                         size_t reason_length) OVERRIDE {}
ProcessChunkLength(size_t chunk_length)48   virtual void ProcessChunkLength(size_t chunk_length) OVERRIDE {}
ProcessChunkExtensions(const char * input,size_t size)49   virtual void ProcessChunkExtensions(const char* input, size_t size) OVERRIDE {
50   }
HeaderDone()51   virtual void HeaderDone() OVERRIDE {}
MessageDone()52   virtual void MessageDone() OVERRIDE {}
53   virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE;
54   virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE;
55   virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE;
56   virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE;
57 
58   BalsaHeaders headers;
59   std::string body;
60   bool error_;
61 };
62 
63 class FileData {
64  public:
65   FileData();
66   FileData(const BalsaHeaders* headers,
67            const std::string& filename,
68            const std::string& body);
69   ~FileData();
70 
headers()71   BalsaHeaders* headers() { return headers_.get(); }
headers()72   const BalsaHeaders* headers() const { return headers_.get(); }
73 
filename()74   const std::string& filename() { return filename_; }
body()75   const std::string& body() { return body_; }
76 
77  private:
78   scoped_ptr<BalsaHeaders> headers_;
79   std::string filename_;
80   std::string body_;
81 
82   DISALLOW_COPY_AND_ASSIGN(FileData);
83 };
84 
85 class MemCacheIter {
86  public:
MemCacheIter()87   MemCacheIter()
88       : file_data(NULL),
89         priority(0),
90         transformed_header(false),
91         body_bytes_consumed(0),
92         stream_id(0),
93         max_segment_size(kInitialDataSendersThreshold),
94         bytes_sent(0) {}
MemCacheIter(FileData * fd)95   explicit MemCacheIter(FileData* fd)
96       : file_data(fd),
97         priority(0),
98         transformed_header(false),
99         body_bytes_consumed(0),
100         stream_id(0),
101         max_segment_size(kInitialDataSendersThreshold),
102         bytes_sent(0) {}
103   FileData* file_data;
104   int priority;
105   bool transformed_header;
106   size_t body_bytes_consumed;
107   uint32 stream_id;
108   uint32 max_segment_size;
109   size_t bytes_sent;
110 };
111 
112 class MemoryCache {
113  public:
114   typedef std::map<std::string, FileData*> Files;
115 
116  public:
117   MemoryCache();
118   virtual ~MemoryCache();
119 
120   void CloneFrom(const MemoryCache& mc);
121 
122   void AddFiles();
123 
124   // virtual for unittests
125   virtual void ReadToString(const char* filename, std::string* output);
126 
127   void ReadAndStoreFileContents(const char* filename);
128 
129   FileData* GetFileData(const std::string& filename);
130 
131   bool AssignFileData(const std::string& filename, MemCacheIter* mci);
132 
133   // For unittests
134   void InsertFile(const BalsaHeaders* headers,
135                   const std::string& filename,
136                   const std::string& body);
137 
138  private:
139   void InsertFile(FileData* file_data);
140   void ClearFiles();
141 
142   Files files_;
143   std::string cwd_;
144 };
145 
146 class NotifierInterface {
147  public:
~NotifierInterface()148   virtual ~NotifierInterface() {}
149   virtual void Notify() = 0;
150 };
151 
152 }  // namespace net
153 
154 #endif  // NET_TOOLS_FLIP_SERVER_MEM_CACHE_H_
155