OLD | NEW |
| (Empty) |
1 // Copyright 2013 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_BALSA_BALSA_FRAME_H_ | |
6 #define NET_TOOLS_BALSA_BALSA_FRAME_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
14 #include "base/compiler_specific.h" | |
15 #include "net/tools/balsa/balsa_enums.h" | |
16 #include "net/tools/balsa/balsa_headers.h" | |
17 #include "net/tools/balsa/balsa_visitor_interface.h" | |
18 #include "net/tools/balsa/buffer_interface.h" | |
19 #include "net/tools/balsa/http_message_constants.h" | |
20 #include "net/tools/balsa/simple_buffer.h" | |
21 | |
22 // For additional debug output, uncomment the following: | |
23 // #define DEBUGFRAMER 1 | |
24 | |
25 namespace net { | |
26 | |
27 // BalsaFrame is a 'Model' of a framer (haha). | |
28 // It exists as a proof of concept headers framer. | |
29 class BalsaFrame { | |
30 public: | |
31 typedef std::vector<std::pair<size_t, size_t> > Lines; | |
32 | |
33 typedef BalsaHeaders::HeaderLineDescription HeaderLineDescription; | |
34 typedef BalsaHeaders::HeaderLines HeaderLines; | |
35 typedef BalsaHeaders::HeaderTokenList HeaderTokenList; | |
36 | |
37 // TODO(fenix): get rid of the 'kValidTerm*' stuff by using the 'since last | |
38 // index' strategy. Note that this implies getting rid of the HeaderFramed() | |
39 | |
40 static const uint32_t kValidTerm1 = '\n' << 16 | '\r' << 8 | '\n'; | |
41 static const uint32_t kValidTerm1Mask = 0xFF << 16 | 0xFF << 8 | 0xFF; | |
42 static const uint32_t kValidTerm2 = '\n' << 8 | '\n'; | |
43 static const uint32_t kValidTerm2Mask = 0xFF << 8 | 0xFF; | |
44 BalsaFrame(); | |
45 ~BalsaFrame(); | |
46 | |
47 // Reset reinitializes all the member variables of the framer and clears the | |
48 // attached header object (but doesn't change the pointer value headers_). | |
49 void Reset(); | |
50 | |
51 const BalsaHeaders* const_balsa_headers() const { return headers_; } | |
52 BalsaHeaders* balsa_headers() { return headers_; } | |
53 // The method set_balsa_headers clears the headers provided and attaches them | |
54 // to the framer. This is a required step before the framer will process any | |
55 // input message data. | |
56 // To detach the header object from the framer, use set_balsa_headers(NULL). | |
57 void set_balsa_headers(BalsaHeaders* headers) { | |
58 if (headers_ != headers) { | |
59 headers_ = headers; | |
60 } | |
61 if (headers_) { | |
62 // Clear the headers if they are non-null, even if the new headers are | |
63 // the same as the old. | |
64 headers_->Clear(); | |
65 } | |
66 } | |
67 | |
68 void set_balsa_visitor(BalsaVisitorInterface* visitor) { | |
69 visitor_ = visitor; | |
70 if (visitor_ == NULL) { | |
71 visitor_ = &do_nothing_visitor_; | |
72 } | |
73 } | |
74 | |
75 void set_is_request(bool is_request) { is_request_ = is_request; } | |
76 | |
77 bool is_request() const { | |
78 return is_request_; | |
79 } | |
80 | |
81 void set_request_was_head(bool request_was_head) { | |
82 request_was_head_ = request_was_head; | |
83 } | |
84 | |
85 bool request_was_head() const { | |
86 return request_was_head_; | |
87 } | |
88 | |
89 void set_max_header_length(size_t max_header_length) { | |
90 max_header_length_ = max_header_length; | |
91 } | |
92 | |
93 size_t max_header_length() const { | |
94 return max_header_length_; | |
95 } | |
96 | |
97 void set_max_request_uri_length(size_t max_request_uri_length) { | |
98 max_request_uri_length_ = max_request_uri_length; | |
99 } | |
100 | |
101 size_t max_request_uri_length() const { | |
102 return max_request_uri_length_; | |
103 } | |
104 | |
105 | |
106 bool MessageFullyRead() { | |
107 return parse_state_ == BalsaFrameEnums::MESSAGE_FULLY_READ; | |
108 } | |
109 | |
110 BalsaFrameEnums::ParseState ParseState() const { return parse_state_; } | |
111 | |
112 | |
113 bool Error() { | |
114 return parse_state_ == BalsaFrameEnums::PARSE_ERROR; | |
115 } | |
116 | |
117 BalsaFrameEnums::ErrorCode ErrorCode() const { return last_error_; } | |
118 | |
119 const BalsaHeaders* headers() const { return headers_; } | |
120 BalsaHeaders* mutable_headers() { return headers_; } | |
121 | |
122 size_t BytesSafeToSplice() const; | |
123 void BytesSpliced(size_t bytes_spliced); | |
124 | |
125 size_t ProcessInput(const char* input, size_t size); | |
126 | |
127 // Parses input and puts the key, value chunk extensions into extensions. | |
128 // TODO(phython): Find a better data structure to put the extensions into. | |
129 static void ProcessChunkExtensions(const char* input, size_t size, | |
130 BalsaHeaders* extensions); | |
131 | |
132 protected: | |
133 // The utils object needs access to the ParseTokenList in order to do its | |
134 // job. | |
135 friend class BalsaHeadersTokenUtils; | |
136 | |
137 inline void ProcessContentLengthLine( | |
138 size_t line_idx, | |
139 BalsaHeadersEnums::ContentLengthStatus* status, | |
140 size_t* length); | |
141 | |
142 inline void ProcessTransferEncodingLine(size_t line_idx); | |
143 | |
144 void ProcessFirstLine(const char* begin, | |
145 const char* end); | |
146 | |
147 void CleanUpKeyValueWhitespace( | |
148 const char* stream_begin, | |
149 const char* line_begin, | |
150 const char* current, | |
151 const char* line_end, | |
152 HeaderLineDescription* current_header_line); | |
153 | |
154 void FindColonsAndParseIntoKeyValue(); | |
155 | |
156 void ProcessHeaderLines(); | |
157 | |
158 inline size_t ProcessHeaders(const char* message_start, | |
159 size_t message_length); | |
160 | |
161 void AssignParseStateAfterHeadersHaveBeenParsed(); | |
162 | |
163 inline bool LineFramingFound(char current_char) { | |
164 return current_char == '\n'; | |
165 } | |
166 | |
167 // TODO(fenix): get rid of the following function and its uses (and | |
168 // replace with something more efficient) | |
169 inline bool HeaderFramingFound(char current_char) { | |
170 // Note that the 'if (current_char == '\n' ...)' test exists to ensure that | |
171 // the HeaderFramingMayBeFound test works properly. In benchmarking done on | |
172 // 2/13/2008, the 'if' actually speeds up performance of the function | |
173 // anyway.. | |
174 if (current_char == '\n' || current_char == '\r') { | |
175 term_chars_ <<= 8; | |
176 // This is necessary IFF architecture has > 8 bit char. Alas, I'm | |
177 // paranoid. | |
178 term_chars_ |= current_char & 0xFF; | |
179 | |
180 if ((term_chars_ & kValidTerm1Mask) == kValidTerm1) { | |
181 term_chars_ = 0; | |
182 return true; | |
183 } | |
184 if ((term_chars_ & kValidTerm2Mask) == kValidTerm2) { | |
185 term_chars_ = 0; | |
186 return true; | |
187 } | |
188 } else { | |
189 term_chars_ = 0; | |
190 } | |
191 return false; | |
192 } | |
193 | |
194 inline bool HeaderFramingMayBeFound() const { | |
195 return term_chars_ != 0; | |
196 } | |
197 | |
198 private: | |
199 class DoNothingBalsaVisitor : public BalsaVisitorInterface { | |
200 void ProcessBodyInput(const char* input, size_t size) override {} | |
201 void ProcessBodyData(const char* input, size_t size) override {} | |
202 void ProcessHeaderInput(const char* input, size_t size) override {} | |
203 void ProcessTrailerInput(const char* input, size_t size) override {} | |
204 void ProcessHeaders(const BalsaHeaders& headers) override {} | |
205 void ProcessRequestFirstLine(const char* line_input, | |
206 size_t line_length, | |
207 const char* method_input, | |
208 size_t method_length, | |
209 const char* request_uri_input, | |
210 size_t request_uri_length, | |
211 const char* version_input, | |
212 size_t version_length) override {} | |
213 void ProcessResponseFirstLine(const char* line_input, | |
214 size_t line_length, | |
215 const char* version_input, | |
216 size_t version_length, | |
217 const char* status_input, | |
218 size_t status_length, | |
219 const char* reason_input, | |
220 size_t reason_length) override {} | |
221 void ProcessChunkLength(size_t chunk_length) override {} | |
222 void ProcessChunkExtensions(const char* input, size_t size) override {} | |
223 void HeaderDone() override {} | |
224 void MessageDone() override {} | |
225 void HandleHeaderError(BalsaFrame* framer) override {} | |
226 void HandleHeaderWarning(BalsaFrame* framer) override {} | |
227 void HandleChunkingError(BalsaFrame* framer) override {} | |
228 void HandleBodyError(BalsaFrame* framer) override {} | |
229 }; | |
230 | |
231 bool last_char_was_slash_r_; | |
232 bool saw_non_newline_char_; | |
233 bool start_was_space_; | |
234 bool chunk_length_character_extracted_; | |
235 bool is_request_; // This is not reset in Reset() | |
236 bool request_was_head_; // This is not reset in Reset() | |
237 size_t max_header_length_; // This is not reset in Reset() | |
238 size_t max_request_uri_length_; // This is not reset in Reset() | |
239 BalsaVisitorInterface* visitor_; | |
240 size_t chunk_length_remaining_; | |
241 size_t content_length_remaining_; | |
242 const char* last_slash_n_loc_; | |
243 const char* last_recorded_slash_n_loc_; | |
244 size_t last_slash_n_idx_; | |
245 uint32_t term_chars_; | |
246 BalsaFrameEnums::ParseState parse_state_; | |
247 BalsaFrameEnums::ErrorCode last_error_; | |
248 | |
249 Lines lines_; | |
250 | |
251 BalsaHeaders* headers_; // This is not reset to NULL in Reset(). | |
252 DoNothingBalsaVisitor do_nothing_visitor_; | |
253 }; | |
254 | |
255 } // namespace net | |
256 | |
257 #endif // NET_TOOLS_BALSA_BALSA_FRAME_H_ | |
258 | |
OLD | NEW |