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 "base/port.h"
13 #include "net/tools/balsa/balsa_enums.h"
14 #include "net/tools/balsa/balsa_headers.h"
15 #include "net/tools/balsa/balsa_visitor_interface.h"
16 #include "net/tools/balsa/buffer_interface.h"
17 #include "net/tools/balsa/http_message_constants.h"
18 #include "net/tools/balsa/simple_buffer.h"
20 // For additional debug output, uncomment the following:
21 // #define DEBUGFRAMER 1
25 // BalsaFrame is a 'Model' of a framer (haha).
26 // It exists as a proof of concept headers framer.
29 typedef std::vector
<std::pair
<size_t, size_t> > Lines
;
31 typedef BalsaHeaders::HeaderLineDescription HeaderLineDescription
;
32 typedef BalsaHeaders::HeaderLines HeaderLines
;
33 typedef BalsaHeaders::HeaderTokenList HeaderTokenList
;
35 // TODO(fenix): get rid of the 'kValidTerm*' stuff by using the 'since last
36 // index' strategy. Note that this implies getting rid of the HeaderFramed()
38 static const uint32 kValidTerm1
= '\n' << 16 |
41 static const uint32 kValidTerm1Mask
= 0xFF << 16 |
44 static const uint32 kValidTerm2
= '\n' << 8 |
46 static const uint32 kValidTerm2Mask
= 0xFF << 8 |
51 // Reset reinitializes all the member variables of the framer and clears the
52 // attached header object (but doesn't change the pointer value headers_).
55 const BalsaHeaders
* const_balsa_headers() const { return headers_
; }
56 BalsaHeaders
* balsa_headers() { return headers_
; }
57 // The method set_balsa_headers clears the headers provided and attaches them
58 // to the framer. This is a required step before the framer will process any
59 // input message data.
60 // To detach the header object from the framer, use set_balsa_headers(NULL).
61 void set_balsa_headers(BalsaHeaders
* headers
) {
62 if (headers_
!= headers
) {
66 // Clear the headers if they are non-null, even if the new headers are
67 // the same as the old.
72 void set_balsa_visitor(BalsaVisitorInterface
* visitor
) {
74 if (visitor_
== NULL
) {
75 visitor_
= &do_nothing_visitor_
;
79 void set_is_request(bool is_request
) { is_request_
= is_request
; }
81 bool is_request() const {
85 void set_request_was_head(bool request_was_head
) {
86 request_was_head_
= request_was_head
;
89 bool request_was_head() const {
90 return request_was_head_
;
93 void set_max_header_length(size_t max_header_length
) {
94 max_header_length_
= max_header_length
;
97 size_t max_header_length() const {
98 return max_header_length_
;
101 void set_max_request_uri_length(size_t max_request_uri_length
) {
102 max_request_uri_length_
= max_request_uri_length
;
105 size_t max_request_uri_length() const {
106 return max_request_uri_length_
;
110 bool MessageFullyRead() {
111 return parse_state_
== BalsaFrameEnums::MESSAGE_FULLY_READ
;
114 BalsaFrameEnums::ParseState
ParseState() const { return parse_state_
; }
118 return parse_state_
== BalsaFrameEnums::PARSE_ERROR
;
121 BalsaFrameEnums::ErrorCode
ErrorCode() const { return last_error_
; }
123 const BalsaHeaders
* headers() const { return headers_
; }
124 BalsaHeaders
* mutable_headers() { return headers_
; }
126 size_t BytesSafeToSplice() const;
127 void BytesSpliced(size_t bytes_spliced
);
129 size_t ProcessInput(const char* input
, size_t size
);
131 // Parses input and puts the key, value chunk extensions into extensions.
132 // TODO(phython): Find a better data structure to put the extensions into.
133 static void ProcessChunkExtensions(const char* input
, size_t size
,
134 BalsaHeaders
* extensions
);
137 // The utils object needs access to the ParseTokenList in order to do its
139 friend class BalsaHeadersTokenUtils
;
141 inline void ProcessContentLengthLine(
143 BalsaHeadersEnums::ContentLengthStatus
* status
,
146 inline void ProcessTransferEncodingLine(size_t line_idx
);
148 void ProcessFirstLine(const char* begin
,
151 void CleanUpKeyValueWhitespace(
152 const char* stream_begin
,
153 const char* line_begin
,
155 const char* line_end
,
156 HeaderLineDescription
* current_header_line
);
158 void FindColonsAndParseIntoKeyValue();
160 void ProcessHeaderLines();
162 inline size_t ProcessHeaders(const char* message_start
,
163 size_t message_length
);
165 void AssignParseStateAfterHeadersHaveBeenParsed();
167 inline bool LineFramingFound(char current_char
) {
168 return current_char
== '\n';
171 // TODO(fenix): get rid of the following function and its uses (and
172 // replace with something more efficient)
173 inline bool HeaderFramingFound(char current_char
) {
174 // Note that the 'if (current_char == '\n' ...)' test exists to ensure that
175 // the HeaderFramingMayBeFound test works properly. In benchmarking done on
176 // 2/13/2008, the 'if' actually speeds up performance of the function
178 if (current_char
== '\n' || current_char
== '\r') {
180 // This is necessary IFF architecture has > 8 bit char. Alas, I'm
182 term_chars_
|= current_char
& 0xFF;
184 if ((term_chars_
& kValidTerm1Mask
) == kValidTerm1
) {
188 if ((term_chars_
& kValidTerm2Mask
) == kValidTerm2
) {
198 inline bool HeaderFramingMayBeFound() const {
199 return term_chars_
!= 0;
203 class DoNothingBalsaVisitor
: public BalsaVisitorInterface
{
204 void ProcessBodyInput(const char* input
, size_t size
) override
{}
205 void ProcessBodyData(const char* input
, size_t size
) override
{}
206 void ProcessHeaderInput(const char* input
, size_t size
) override
{}
207 void ProcessTrailerInput(const char* input
, size_t size
) override
{}
208 void ProcessHeaders(const BalsaHeaders
& headers
) override
{}
209 void ProcessRequestFirstLine(const char* line_input
,
211 const char* method_input
,
212 size_t method_length
,
213 const char* request_uri_input
,
214 size_t request_uri_length
,
215 const char* version_input
,
216 size_t version_length
) override
{}
217 void ProcessResponseFirstLine(const char* line_input
,
219 const char* version_input
,
220 size_t version_length
,
221 const char* status_input
,
222 size_t status_length
,
223 const char* reason_input
,
224 size_t reason_length
) override
{}
225 void ProcessChunkLength(size_t chunk_length
) override
{}
226 void ProcessChunkExtensions(const char* input
, size_t size
) override
{}
227 void HeaderDone() override
{}
228 void MessageDone() override
{}
229 void HandleHeaderError(BalsaFrame
* framer
) override
{}
230 void HandleHeaderWarning(BalsaFrame
* framer
) override
{}
231 void HandleChunkingError(BalsaFrame
* framer
) override
{}
232 void HandleBodyError(BalsaFrame
* framer
) override
{}
235 bool last_char_was_slash_r_
;
236 bool saw_non_newline_char_
;
237 bool start_was_space_
;
238 bool chunk_length_character_extracted_
;
239 bool is_request_
; // This is not reset in Reset()
240 bool request_was_head_
; // This is not reset in Reset()
241 size_t max_header_length_
; // This is not reset in Reset()
242 size_t max_request_uri_length_
; // This is not reset in Reset()
243 BalsaVisitorInterface
* visitor_
;
244 size_t chunk_length_remaining_
;
245 size_t content_length_remaining_
;
246 const char* last_slash_n_loc_
;
247 const char* last_recorded_slash_n_loc_
;
248 size_t last_slash_n_idx_
;
250 BalsaFrameEnums::ParseState parse_state_
;
251 BalsaFrameEnums::ErrorCode last_error_
;
255 BalsaHeaders
* headers_
; // This is not reset to NULL in Reset().
256 DoNothingBalsaVisitor do_nothing_visitor_
;
261 #endif // NET_TOOLS_BALSA_BALSA_FRAME_H_