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.
5 #ifndef NET_TOOLS_BALSA_BALSA_FRAME_H_
6 #define NET_TOOLS_BALSA_BALSA_FRAME_H_
11 #include "base/compiler_specific.h"
12 #include "net/tools/balsa/balsa_enums.h"
13 #include "net/tools/balsa/balsa_headers.h"
14 #include "net/tools/balsa/balsa_visitor_interface.h"
15 #include "net/tools/balsa/buffer_interface.h"
16 #include "net/tools/balsa/http_message_constants.h"
17 #include "net/tools/balsa/simple_buffer.h"
19 // For additional debug output, uncomment the following:
20 // #define DEBUGFRAMER 1
24 // BalsaFrame is a 'Model' of a framer (haha).
25 // It exists as a proof of concept headers framer.
28 typedef std::vector
<std::pair
<size_t, size_t> > Lines
;
30 typedef BalsaHeaders::HeaderLineDescription HeaderLineDescription
;
31 typedef BalsaHeaders::HeaderLines HeaderLines
;
32 typedef BalsaHeaders::HeaderTokenList HeaderTokenList
;
34 // TODO(fenix): get rid of the 'kValidTerm*' stuff by using the 'since last
35 // index' strategy. Note that this implies getting rid of the HeaderFramed()
37 static const uint32 kValidTerm1
= '\n' << 16 |
40 static const uint32 kValidTerm1Mask
= 0xFF << 16 |
43 static const uint32 kValidTerm2
= '\n' << 8 |
45 static const uint32 kValidTerm2Mask
= 0xFF << 8 |
50 // Reset reinitializes all the member variables of the framer and clears the
51 // attached header object (but doesn't change the pointer value headers_).
54 const BalsaHeaders
* const_balsa_headers() const { return headers_
; }
55 BalsaHeaders
* balsa_headers() { return headers_
; }
56 // The method set_balsa_headers clears the headers provided and attaches them
57 // to the framer. This is a required step before the framer will process any
58 // input message data.
59 // To detach the header object from the framer, use set_balsa_headers(NULL).
60 void set_balsa_headers(BalsaHeaders
* headers
) {
61 if (headers_
!= headers
) {
65 // Clear the headers if they are non-null, even if the new headers are
66 // the same as the old.
71 void set_balsa_visitor(BalsaVisitorInterface
* visitor
) {
73 if (visitor_
== NULL
) {
74 visitor_
= &do_nothing_visitor_
;
78 void set_is_request(bool is_request
) { is_request_
= is_request
; }
80 bool is_request() const {
84 void set_request_was_head(bool request_was_head
) {
85 request_was_head_
= request_was_head
;
88 bool request_was_head() const {
89 return request_was_head_
;
92 void set_max_header_length(size_t max_header_length
) {
93 max_header_length_
= max_header_length
;
96 size_t max_header_length() const {
97 return max_header_length_
;
100 void set_max_request_uri_length(size_t max_request_uri_length
) {
101 max_request_uri_length_
= max_request_uri_length
;
104 size_t max_request_uri_length() const {
105 return max_request_uri_length_
;
109 bool MessageFullyRead() {
110 return parse_state_
== BalsaFrameEnums::MESSAGE_FULLY_READ
;
113 BalsaFrameEnums::ParseState
ParseState() const { return parse_state_
; }
117 return parse_state_
== BalsaFrameEnums::PARSE_ERROR
;
120 BalsaFrameEnums::ErrorCode
ErrorCode() const { return last_error_
; }
122 const BalsaHeaders
* headers() const { return headers_
; }
123 BalsaHeaders
* mutable_headers() { return headers_
; }
125 size_t BytesSafeToSplice() const;
126 void BytesSpliced(size_t bytes_spliced
);
128 size_t ProcessInput(const char* input
, size_t size
);
130 // Parses input and puts the key, value chunk extensions into extensions.
131 // TODO(phython): Find a better data structure to put the extensions into.
132 static void ProcessChunkExtensions(const char* input
, size_t size
,
133 BalsaHeaders
* extensions
);
136 // The utils object needs access to the ParseTokenList in order to do its
138 friend class BalsaHeadersTokenUtils
;
140 inline void ProcessContentLengthLine(
142 BalsaHeadersEnums::ContentLengthStatus
* status
,
145 inline void ProcessTransferEncodingLine(size_t line_idx
);
147 void ProcessFirstLine(const char* begin
,
150 void CleanUpKeyValueWhitespace(
151 const char* stream_begin
,
152 const char* line_begin
,
154 const char* line_end
,
155 HeaderLineDescription
* current_header_line
);
157 void FindColonsAndParseIntoKeyValue();
159 void ProcessHeaderLines();
161 inline size_t ProcessHeaders(const char* message_start
,
162 size_t message_length
);
164 void AssignParseStateAfterHeadersHaveBeenParsed();
166 inline bool LineFramingFound(char current_char
) {
167 return current_char
== '\n';
170 // TODO(fenix): get rid of the following function and its uses (and
171 // replace with something more efficient)
172 inline bool HeaderFramingFound(char current_char
) {
173 // Note that the 'if (current_char == '\n' ...)' test exists to ensure that
174 // the HeaderFramingMayBeFound test works properly. In benchmarking done on
175 // 2/13/2008, the 'if' actually speeds up performance of the function
177 if (current_char
== '\n' || current_char
== '\r') {
179 // This is necessary IFF architecture has > 8 bit char. Alas, I'm
181 term_chars_
|= current_char
& 0xFF;
183 if ((term_chars_
& kValidTerm1Mask
) == kValidTerm1
) {
187 if ((term_chars_
& kValidTerm2Mask
) == kValidTerm2
) {
197 inline bool HeaderFramingMayBeFound() const {
198 return term_chars_
!= 0;
202 class DoNothingBalsaVisitor
: public BalsaVisitorInterface
{
203 void ProcessBodyInput(const char* input
, size_t size
) override
{}
204 void ProcessBodyData(const char* input
, size_t size
) override
{}
205 void ProcessHeaderInput(const char* input
, size_t size
) override
{}
206 void ProcessTrailerInput(const char* input
, size_t size
) override
{}
207 void ProcessHeaders(const BalsaHeaders
& headers
) override
{}
208 void ProcessRequestFirstLine(const char* line_input
,
210 const char* method_input
,
211 size_t method_length
,
212 const char* request_uri_input
,
213 size_t request_uri_length
,
214 const char* version_input
,
215 size_t version_length
) override
{}
216 void ProcessResponseFirstLine(const char* line_input
,
218 const char* version_input
,
219 size_t version_length
,
220 const char* status_input
,
221 size_t status_length
,
222 const char* reason_input
,
223 size_t reason_length
) override
{}
224 void ProcessChunkLength(size_t chunk_length
) override
{}
225 void ProcessChunkExtensions(const char* input
, size_t size
) override
{}
226 void HeaderDone() override
{}
227 void MessageDone() override
{}
228 void HandleHeaderError(BalsaFrame
* framer
) override
{}
229 void HandleHeaderWarning(BalsaFrame
* framer
) override
{}
230 void HandleChunkingError(BalsaFrame
* framer
) override
{}
231 void HandleBodyError(BalsaFrame
* framer
) override
{}
234 bool last_char_was_slash_r_
;
235 bool saw_non_newline_char_
;
236 bool start_was_space_
;
237 bool chunk_length_character_extracted_
;
238 bool is_request_
; // This is not reset in Reset()
239 bool request_was_head_
; // This is not reset in Reset()
240 size_t max_header_length_
; // This is not reset in Reset()
241 size_t max_request_uri_length_
; // This is not reset in Reset()
242 BalsaVisitorInterface
* visitor_
;
243 size_t chunk_length_remaining_
;
244 size_t content_length_remaining_
;
245 const char* last_slash_n_loc_
;
246 const char* last_recorded_slash_n_loc_
;
247 size_t last_slash_n_idx_
;
249 BalsaFrameEnums::ParseState parse_state_
;
250 BalsaFrameEnums::ErrorCode last_error_
;
254 BalsaHeaders
* headers_
; // This is not reset to NULL in Reset().
255 DoNothingBalsaVisitor do_nothing_visitor_
;
260 #endif // NET_TOOLS_BALSA_BALSA_FRAME_H_