OLD | NEW |
| (Empty) |
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: | |
21 void HandleError() { error_ = true; } | |
22 | |
23 // BalsaVisitorInterface: | |
24 void ProcessBodyInput(const char* input, size_t size) override {} | |
25 void ProcessBodyData(const char* input, size_t size) override; | |
26 void ProcessHeaderInput(const char* input, size_t size) override {} | |
27 void ProcessTrailerInput(const char* input, size_t size) override {} | |
28 void ProcessHeaders(const BalsaHeaders& headers) override { | |
29 // nothing to do here-- we're assuming that the BalsaFrame has | |
30 // been handed our headers. | |
31 } | |
32 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 {} | |
40 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 {} | |
48 void ProcessChunkLength(size_t chunk_length) override {} | |
49 void ProcessChunkExtensions(const char* input, size_t size) override {} | |
50 void HeaderDone() override {} | |
51 void MessageDone() override {} | |
52 void HandleHeaderError(BalsaFrame* framer) override; | |
53 void HandleHeaderWarning(BalsaFrame* framer) override; | |
54 void HandleChunkingError(BalsaFrame* framer) override; | |
55 void HandleBodyError(BalsaFrame* framer) override; | |
56 | |
57 BalsaHeaders headers; | |
58 std::string body; | |
59 bool error_; | |
60 }; | |
61 | |
62 class FileData { | |
63 public: | |
64 FileData(); | |
65 FileData(const BalsaHeaders* headers, | |
66 const std::string& filename, | |
67 const std::string& body); | |
68 ~FileData(); | |
69 | |
70 BalsaHeaders* headers() { return headers_.get(); } | |
71 const BalsaHeaders* headers() const { return headers_.get(); } | |
72 | |
73 const std::string& filename() { return filename_; } | |
74 const std::string& body() { return body_; } | |
75 | |
76 private: | |
77 scoped_ptr<BalsaHeaders> headers_; | |
78 std::string filename_; | |
79 std::string body_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(FileData); | |
82 }; | |
83 | |
84 class MemCacheIter { | |
85 public: | |
86 MemCacheIter() | |
87 : file_data(NULL), | |
88 priority(0), | |
89 transformed_header(false), | |
90 body_bytes_consumed(0), | |
91 stream_id(0), | |
92 max_segment_size(kInitialDataSendersThreshold), | |
93 bytes_sent(0) {} | |
94 explicit MemCacheIter(FileData* fd) | |
95 : file_data(fd), | |
96 priority(0), | |
97 transformed_header(false), | |
98 body_bytes_consumed(0), | |
99 stream_id(0), | |
100 max_segment_size(kInitialDataSendersThreshold), | |
101 bytes_sent(0) {} | |
102 FileData* file_data; | |
103 int priority; | |
104 bool transformed_header; | |
105 size_t body_bytes_consumed; | |
106 uint32 stream_id; | |
107 uint32 max_segment_size; | |
108 size_t bytes_sent; | |
109 }; | |
110 | |
111 class MemoryCache { | |
112 public: | |
113 typedef std::map<std::string, FileData*> Files; | |
114 | |
115 public: | |
116 MemoryCache(); | |
117 virtual ~MemoryCache(); | |
118 | |
119 void CloneFrom(const MemoryCache& mc); | |
120 | |
121 void AddFiles(); | |
122 | |
123 // virtual for unittests | |
124 virtual void ReadToString(const char* filename, std::string* output); | |
125 | |
126 void ReadAndStoreFileContents(const char* filename); | |
127 | |
128 FileData* GetFileData(const std::string& filename); | |
129 | |
130 bool AssignFileData(const std::string& filename, MemCacheIter* mci); | |
131 | |
132 // For unittests | |
133 void InsertFile(const BalsaHeaders* headers, | |
134 const std::string& filename, | |
135 const std::string& body); | |
136 | |
137 private: | |
138 void InsertFile(FileData* file_data); | |
139 void ClearFiles(); | |
140 | |
141 Files files_; | |
142 std::string cwd_; | |
143 }; | |
144 | |
145 class NotifierInterface { | |
146 public: | |
147 virtual ~NotifierInterface() {} | |
148 virtual void Notify() = 0; | |
149 }; | |
150 | |
151 } // namespace net | |
152 | |
153 #endif // NET_TOOLS_FLIP_SERVER_MEM_CACHE_H_ | |
OLD | NEW |