OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_BALSA_BALSA_FRAME_H_ | 5 #ifndef NET_TOOLS_BALSA_BALSA_FRAME_H_ |
6 #define NET_TOOLS_BALSA_BALSA_FRAME_H_ | 6 #define NET_TOOLS_BALSA_BALSA_FRAME_H_ |
7 | 7 |
8 #include <strings.h> | 8 #include <strings.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
(...skipping 19 matching lines...) Expand all Loading... |
30 public: | 30 public: |
31 typedef std::vector<std::pair<size_t, size_t> > Lines; | 31 typedef std::vector<std::pair<size_t, size_t> > Lines; |
32 | 32 |
33 typedef BalsaHeaders::HeaderLineDescription HeaderLineDescription; | 33 typedef BalsaHeaders::HeaderLineDescription HeaderLineDescription; |
34 typedef BalsaHeaders::HeaderLines HeaderLines; | 34 typedef BalsaHeaders::HeaderLines HeaderLines; |
35 typedef BalsaHeaders::HeaderTokenList HeaderTokenList; | 35 typedef BalsaHeaders::HeaderTokenList HeaderTokenList; |
36 | 36 |
37 // TODO(fenix): get rid of the 'kValidTerm*' stuff by using the 'since last | 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() | 38 // index' strategy. Note that this implies getting rid of the HeaderFramed() |
39 | 39 |
40 static const uint32 kValidTerm1 = '\n' << 16 | | 40 static const uint32 kValidTerm1 = '\n' << 16 | '\r' << 8 | '\n'; |
41 '\r' << 8 | | 41 static const uint32 kValidTerm1Mask = 0xFF << 16 | 0xFF << 8 | 0xFF; |
42 '\n'; | 42 static const uint32 kValidTerm2 = '\n' << 8 | '\n'; |
43 static const uint32 kValidTerm1Mask = 0xFF << 16 | | 43 static const uint32 kValidTerm2Mask = 0xFF << 8 | 0xFF; |
44 0xFF << 8 | | |
45 0xFF; | |
46 static const uint32 kValidTerm2 = '\n' << 8 | | |
47 '\n'; | |
48 static const uint32 kValidTerm2Mask = 0xFF << 8 | | |
49 0xFF; | |
50 BalsaFrame(); | 44 BalsaFrame(); |
51 ~BalsaFrame(); | 45 ~BalsaFrame(); |
52 | 46 |
53 // Reset reinitializes all the member variables of the framer and clears the | 47 // Reset reinitializes all the member variables of the framer and clears the |
54 // attached header object (but doesn't change the pointer value headers_). | 48 // attached header object (but doesn't change the pointer value headers_). |
55 void Reset(); | 49 void Reset(); |
56 | 50 |
57 const BalsaHeaders* const_balsa_headers() const { return headers_; } | 51 const BalsaHeaders* const_balsa_headers() const { return headers_; } |
58 BalsaHeaders* balsa_headers() { return headers_; } | 52 BalsaHeaders* balsa_headers() { return headers_; } |
59 // The method set_balsa_headers clears the headers provided and attaches them | 53 // The method set_balsa_headers clears the headers provided and attaches them |
(...skipping 13 matching lines...) Expand all Loading... |
73 | 67 |
74 void set_balsa_visitor(BalsaVisitorInterface* visitor) { | 68 void set_balsa_visitor(BalsaVisitorInterface* visitor) { |
75 visitor_ = visitor; | 69 visitor_ = visitor; |
76 if (visitor_ == NULL) { | 70 if (visitor_ == NULL) { |
77 visitor_ = &do_nothing_visitor_; | 71 visitor_ = &do_nothing_visitor_; |
78 } | 72 } |
79 } | 73 } |
80 | 74 |
81 void set_is_request(bool is_request) { is_request_ = is_request; } | 75 void set_is_request(bool is_request) { is_request_ = is_request; } |
82 | 76 |
83 bool is_request() const { | 77 bool is_request() const { return is_request_; } |
84 return is_request_; | |
85 } | |
86 | 78 |
87 void set_request_was_head(bool request_was_head) { | 79 void set_request_was_head(bool request_was_head) { |
88 request_was_head_ = request_was_head; | 80 request_was_head_ = request_was_head; |
89 } | 81 } |
90 | 82 |
91 bool request_was_head() const { | 83 bool request_was_head() const { return request_was_head_; } |
92 return request_was_head_; | |
93 } | |
94 | 84 |
95 void set_max_header_length(size_t max_header_length) { | 85 void set_max_header_length(size_t max_header_length) { |
96 max_header_length_ = max_header_length; | 86 max_header_length_ = max_header_length; |
97 } | 87 } |
98 | 88 |
99 size_t max_header_length() const { | 89 size_t max_header_length() const { return max_header_length_; } |
100 return max_header_length_; | |
101 } | |
102 | 90 |
103 void set_max_request_uri_length(size_t max_request_uri_length) { | 91 void set_max_request_uri_length(size_t max_request_uri_length) { |
104 max_request_uri_length_ = max_request_uri_length; | 92 max_request_uri_length_ = max_request_uri_length; |
105 } | 93 } |
106 | 94 |
107 size_t max_request_uri_length() const { | 95 size_t max_request_uri_length() const { return max_request_uri_length_; } |
108 return max_request_uri_length_; | |
109 } | |
110 | |
111 | 96 |
112 bool MessageFullyRead() { | 97 bool MessageFullyRead() { |
113 return parse_state_ == BalsaFrameEnums::MESSAGE_FULLY_READ; | 98 return parse_state_ == BalsaFrameEnums::MESSAGE_FULLY_READ; |
114 } | 99 } |
115 | 100 |
116 BalsaFrameEnums::ParseState ParseState() const { return parse_state_; } | 101 BalsaFrameEnums::ParseState ParseState() const { return parse_state_; } |
117 | 102 |
118 | 103 bool Error() { return parse_state_ == BalsaFrameEnums::PARSE_ERROR; } |
119 bool Error() { | |
120 return parse_state_ == BalsaFrameEnums::PARSE_ERROR; | |
121 } | |
122 | 104 |
123 BalsaFrameEnums::ErrorCode ErrorCode() const { return last_error_; } | 105 BalsaFrameEnums::ErrorCode ErrorCode() const { return last_error_; } |
124 | 106 |
125 const BalsaHeaders* headers() const { return headers_; } | 107 const BalsaHeaders* headers() const { return headers_; } |
126 BalsaHeaders* mutable_headers() { return headers_; } | 108 BalsaHeaders* mutable_headers() { return headers_; } |
127 | 109 |
128 size_t BytesSafeToSplice() const; | 110 size_t BytesSafeToSplice() const; |
129 void BytesSpliced(size_t bytes_spliced); | 111 void BytesSpliced(size_t bytes_spliced); |
130 | 112 |
131 size_t ProcessInput(const char* input, size_t size); | 113 size_t ProcessInput(const char* input, size_t size); |
132 | 114 |
133 // Parses input and puts the key, value chunk extensions into extensions. | 115 // Parses input and puts the key, value chunk extensions into extensions. |
134 // TODO(phython): Find a better data structure to put the extensions into. | 116 // TODO(phython): Find a better data structure to put the extensions into. |
135 static void ProcessChunkExtensions(const char* input, size_t size, | 117 static void ProcessChunkExtensions(const char* input, |
| 118 size_t size, |
136 BalsaHeaders* extensions); | 119 BalsaHeaders* extensions); |
137 | 120 |
138 protected: | 121 protected: |
139 // The utils object needs access to the ParseTokenList in order to do its | 122 // The utils object needs access to the ParseTokenList in order to do its |
140 // job. | 123 // job. |
141 friend class BalsaHeadersTokenUtils; | 124 friend class BalsaHeadersTokenUtils; |
142 | 125 |
143 inline void ProcessContentLengthLine( | 126 inline void ProcessContentLengthLine( |
144 size_t line_idx, | 127 size_t line_idx, |
145 BalsaHeadersEnums::ContentLengthStatus* status, | 128 BalsaHeadersEnums::ContentLengthStatus* status, |
146 size_t* length); | 129 size_t* length); |
147 | 130 |
148 inline void ProcessTransferEncodingLine(size_t line_idx); | 131 inline void ProcessTransferEncodingLine(size_t line_idx); |
149 | 132 |
150 void ProcessFirstLine(const char* begin, | 133 void ProcessFirstLine(const char* begin, const char* end); |
151 const char* end); | |
152 | 134 |
153 void CleanUpKeyValueWhitespace( | 135 void CleanUpKeyValueWhitespace(const char* stream_begin, |
154 const char* stream_begin, | 136 const char* line_begin, |
155 const char* line_begin, | 137 const char* current, |
156 const char* current, | 138 const char* line_end, |
157 const char* line_end, | 139 HeaderLineDescription* current_header_line); |
158 HeaderLineDescription* current_header_line); | |
159 | 140 |
160 void FindColonsAndParseIntoKeyValue(); | 141 void FindColonsAndParseIntoKeyValue(); |
161 | 142 |
162 void ProcessHeaderLines(); | 143 void ProcessHeaderLines(); |
163 | 144 |
164 inline size_t ProcessHeaders(const char* message_start, | 145 inline size_t ProcessHeaders(const char* message_start, |
165 size_t message_length); | 146 size_t message_length); |
166 | 147 |
167 void AssignParseStateAfterHeadersHaveBeenParsed(); | 148 void AssignParseStateAfterHeadersHaveBeenParsed(); |
168 | 149 |
(...skipping 21 matching lines...) Expand all Loading... |
190 if ((term_chars_ & kValidTerm2Mask) == kValidTerm2) { | 171 if ((term_chars_ & kValidTerm2Mask) == kValidTerm2) { |
191 term_chars_ = 0; | 172 term_chars_ = 0; |
192 return true; | 173 return true; |
193 } | 174 } |
194 } else { | 175 } else { |
195 term_chars_ = 0; | 176 term_chars_ = 0; |
196 } | 177 } |
197 return false; | 178 return false; |
198 } | 179 } |
199 | 180 |
200 inline bool HeaderFramingMayBeFound() const { | 181 inline bool HeaderFramingMayBeFound() const { return term_chars_ != 0; } |
201 return term_chars_ != 0; | |
202 } | |
203 | 182 |
204 private: | 183 private: |
205 class DoNothingBalsaVisitor : public BalsaVisitorInterface { | 184 class DoNothingBalsaVisitor : public BalsaVisitorInterface { |
206 virtual void ProcessBodyInput(const char *input, size_t size) OVERRIDE {} | 185 virtual void ProcessBodyInput(const char* input, size_t size) OVERRIDE {} |
207 virtual void ProcessBodyData(const char *input, size_t size) OVERRIDE {} | 186 virtual void ProcessBodyData(const char* input, size_t size) OVERRIDE {} |
208 virtual void ProcessHeaderInput(const char *input, size_t size) OVERRIDE {} | 187 virtual void ProcessHeaderInput(const char* input, size_t size) OVERRIDE {} |
209 virtual void ProcessTrailerInput(const char *input, size_t size) OVERRIDE {} | 188 virtual void ProcessTrailerInput(const char* input, size_t size) OVERRIDE {} |
210 virtual void ProcessHeaders(const BalsaHeaders& headers) OVERRIDE {} | 189 virtual void ProcessHeaders(const BalsaHeaders& headers) OVERRIDE {} |
211 virtual void ProcessRequestFirstLine(const char* line_input, | 190 virtual void ProcessRequestFirstLine(const char* line_input, |
212 size_t line_length, | 191 size_t line_length, |
213 const char* method_input, | 192 const char* method_input, |
214 size_t method_length, | 193 size_t method_length, |
215 const char* request_uri_input, | 194 const char* request_uri_input, |
216 size_t request_uri_length, | 195 size_t request_uri_length, |
217 const char* version_input, | 196 const char* version_input, |
218 size_t version_length) OVERRIDE {} | 197 size_t version_length) OVERRIDE {} |
219 virtual void ProcessResponseFirstLine(const char *line_input, | 198 virtual void ProcessResponseFirstLine(const char* line_input, |
220 size_t line_length, | 199 size_t line_length, |
221 const char *version_input, | 200 const char* version_input, |
222 size_t version_length, | 201 size_t version_length, |
223 const char *status_input, | 202 const char* status_input, |
224 size_t status_length, | 203 size_t status_length, |
225 const char *reason_input, | 204 const char* reason_input, |
226 size_t reason_length) OVERRIDE {} | 205 size_t reason_length) OVERRIDE {} |
227 virtual void ProcessChunkLength(size_t chunk_length) OVERRIDE {} | 206 virtual void ProcessChunkLength(size_t chunk_length) OVERRIDE {} |
228 virtual void ProcessChunkExtensions(const char *input, | 207 virtual void ProcessChunkExtensions(const char* input, |
229 size_t size) OVERRIDE {} | 208 size_t size) OVERRIDE {} |
230 virtual void HeaderDone() OVERRIDE {} | 209 virtual void HeaderDone() OVERRIDE {} |
231 virtual void MessageDone() OVERRIDE {} | 210 virtual void MessageDone() OVERRIDE {} |
232 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE {} | 211 virtual void HandleHeaderError(BalsaFrame* framer) OVERRIDE {} |
233 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE {} | 212 virtual void HandleHeaderWarning(BalsaFrame* framer) OVERRIDE {} |
234 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE {} | 213 virtual void HandleChunkingError(BalsaFrame* framer) OVERRIDE {} |
235 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE {} | 214 virtual void HandleBodyError(BalsaFrame* framer) OVERRIDE {} |
236 }; | 215 }; |
237 | 216 |
238 bool last_char_was_slash_r_; | 217 bool last_char_was_slash_r_; |
(...skipping 16 matching lines...) Expand all Loading... |
255 | 234 |
256 Lines lines_; | 235 Lines lines_; |
257 | 236 |
258 BalsaHeaders* headers_; // This is not reset to NULL in Reset(). | 237 BalsaHeaders* headers_; // This is not reset to NULL in Reset(). |
259 DoNothingBalsaVisitor do_nothing_visitor_; | 238 DoNothingBalsaVisitor do_nothing_visitor_; |
260 }; | 239 }; |
261 | 240 |
262 } // namespace net | 241 } // namespace net |
263 | 242 |
264 #endif // NET_TOOLS_BALSA_BALSA_FRAME_H_ | 243 #endif // NET_TOOLS_BALSA_BALSA_FRAME_H_ |
265 | |
OLD | NEW |