1// Copyright (c) 2009 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_BALSA_ENUMS_H_
6#define NET_TOOLS_FLIP_SERVER_BALSA_ENUMS_H_
7#pragma once
8
9namespace net {
10
11struct BalsaFrameEnums {
12  enum ParseState {
13    PARSE_ERROR,
14    READING_HEADER_AND_FIRSTLINE,
15    READING_CHUNK_LENGTH,
16    READING_CHUNK_EXTENSION,
17    READING_CHUNK_DATA,
18    READING_CHUNK_TERM,
19    READING_LAST_CHUNK_TERM,
20    READING_TRAILER,
21    READING_UNTIL_CLOSE,
22    READING_CONTENT,
23    MESSAGE_FULLY_READ,
24    NUM_STATES,
25  };
26
27  enum ErrorCode {
28    NO_ERROR = 0,  // A sentinel value for convenience, none of the callbacks
29    //                should ever see this error code.
30    // Header parsing errors
31    // Note that adding one to many of the REQUEST errors yields the
32    // appropriate RESPONSE error.
33    // Particularly, when parsing the first line of a request or response,
34    // there are three sequences of non-whitespace regardless of whether or
35    // not it is a request or response. These are listed below, in order.
36    //
37    //        firstline_a     firstline_b    firstline_c
38    //    REQ: method         request_uri    version
39    //   RESP: version        statuscode     reason
40    //
41    // As you can see, the first token is the 'method' field for a request,
42    // and 'version' field for a response. We call the first non whitespace
43    // token firstline_a, the second firstline_b, and the third token
44    // followed by [^\r\n]*) firstline_c.
45    //
46    // This organization is important, as it lets us determine the error code
47    // to use without a branch based on is_response. Instead, we simply add
48    // is_response to the response error code-- If is_response is true, then
49    // we'll get the response error code, thanks to the fact that the error
50    // code numbers are organized to ensure that response error codes always
51    // precede request error codes.
52    //                                                  | Triggered
53    //                                                  | while processing
54    //                                                  | this NONWS
55    //                                                  | sequence...
56    NO_STATUS_LINE_IN_RESPONSE,                      // |
57    NO_REQUEST_LINE_IN_REQUEST,                      // |
58    FAILED_TO_FIND_WS_AFTER_RESPONSE_VERSION,        // |  firstline_a
59    FAILED_TO_FIND_WS_AFTER_REQUEST_METHOD,          // |  firstline_a
60    FAILED_TO_FIND_WS_AFTER_RESPONSE_STATUSCODE,     // |  firstline_b
61    FAILED_TO_FIND_WS_AFTER_REQUEST_REQUEST_URI,     // |  firstline_b
62    FAILED_TO_FIND_NL_AFTER_RESPONSE_REASON_PHRASE,  // |  firstline_c
63    FAILED_TO_FIND_NL_AFTER_REQUEST_HTTP_VERSION,    // |  firstline_c
64
65    FAILED_CONVERTING_STATUS_CODE_TO_INT,
66    REQUEST_URI_TOO_LONG,  // Request URI greater than kMaxUrlLen.
67
68    HEADERS_TOO_LONG,
69    UNPARSABLE_CONTENT_LENGTH,
70    // Warning: there may be a body but there was no content-length/chunked
71    // encoding
72    MAYBE_BODY_BUT_NO_CONTENT_LENGTH,
73
74    // This is used if a body is required for a request.
75    REQUIRED_BODY_BUT_NO_CONTENT_LENGTH,
76
77    HEADER_MISSING_COLON,
78
79    // Chunking errors
80    INVALID_CHUNK_LENGTH,
81    CHUNK_LENGTH_OVERFLOW,
82
83    // Other errors.
84    CALLED_BYTES_SPLICED_WHEN_UNSAFE_TO_DO_SO,
85    CALLED_BYTES_SPLICED_AND_EXCEEDED_SAFE_SPLICE_AMOUNT,
86    MULTIPLE_CONTENT_LENGTH_KEYS,
87    MULTIPLE_TRANSFER_ENCODING_KEYS,
88    UNKNOWN_TRANSFER_ENCODING,
89    INVALID_HEADER_FORMAT,
90
91    // A detected internal inconsistency was found.
92    INTERNAL_LOGIC_ERROR,
93
94    NUM_ERROR_CODES
95  };
96  static const char* ParseStateToString(ParseState error_code);
97  static const char* ErrorCodeToString(ErrorCode error_code);
98};
99
100struct BalsaHeadersEnums {
101  enum ContentLengthStatus {
102    INVALID_CONTENT_LENGTH,
103    CONTENT_LENGTH_OVERFLOW,
104    NO_CONTENT_LENGTH,
105    VALID_CONTENT_LENGTH,
106  };
107};
108
109}  // namespace net
110
111#endif  // NET_TOOLS_FLIP_SERVER_BALSA_ENUMS_H_
112
113