Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 277 lines (233 sloc) 9.507 kb
63daf22 @ry Update copyright headers
ry authored
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
79947a7 @ry Remove EOL whitespace
ry authored
2 *
b283cd9 @ry copyright header
ry authored
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
79947a7 @ry Remove EOL whitespace
ry authored
9 *
b283cd9 @ry copyright header
ry authored
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
79947a7 @ry Remove EOL whitespace
ry authored
12 *
b283cd9 @ry copyright header
ry authored
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
79947a7 @ry Remove EOL whitespace
ry authored
19 * IN THE SOFTWARE.
b283cd9 @ry copyright header
ry authored
20 */
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
21 #ifndef http_parser_h
22 #define http_parser_h
23 #ifdef __cplusplus
24 extern "C" {
e244dbf @ry Fix whitespace, clean up some macros.
ry authored
25 #endif
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
26
32c0e11 @ry bump to v1.0
ry authored
27 #define HTTP_PARSER_VERSION_MAJOR 1
28 #define HTTP_PARSER_VERSION_MINOR 0
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
29
e244dbf @ry Fix whitespace, clean up some macros.
ry authored
30 #include <sys/types.h>
965f91b @ry Support MSVS
ry authored
31 #if defined(_WIN32) && !defined(__MINGW32__) && !defined(_MSC_VER)
24be793 @ewencp Provide typedefs instead of using stdint.h on Windows.
ewencp authored
32 typedef __int8 int8_t;
33 typedef unsigned __int8 uint8_t;
34 typedef __int16 int16_t;
35 typedef unsigned __int16 uint16_t;
36 typedef __int32 int32_t;
37 typedef unsigned __int32 uint32_t;
38 typedef __int64 int64_t;
39 typedef unsigned __int64 uint64_t;
40
4afe80a @ewencp Add definitions and typedefs to support compilation in Visual Studio und...
ewencp authored
41 typedef unsigned int size_t;
42 typedef int ssize_t;
24be793 @ewencp Provide typedefs instead of using stdint.h on Windows.
ewencp authored
43 #else
44 #include <stdint.h>
4afe80a @ewencp Add definitions and typedefs to support compilation in Visual Studio und...
ewencp authored
45 #endif
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
46
fb6dc67 @ry strict check
ry authored
47 /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
79947a7 @ry Remove EOL whitespace
ry authored
48 * faster
49 */
fb6dc67 @ry strict check
ry authored
50 #ifndef HTTP_PARSER_STRICT
51 # define HTTP_PARSER_STRICT 1
52 #endif
53
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
54 /* Compile with -DHTTP_PARSER_DEBUG=1 to add extra debugging information to
55 * the error reporting facility.
56 */
57 #ifndef HTTP_PARSER_DEBUG
58 # define HTTP_PARSER_DEBUG 0
59 #endif
60
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
61
1b30bf4 @ry Only allow 80kb of header bytes
ry authored
62 /* Maximium header size allowed */
63 #define HTTP_MAX_HEADER_SIZE (80*1024)
64
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
65
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
66 typedef struct http_parser http_parser;
dbd2dad @ry Introduce http_parser_settings
ry authored
67 typedef struct http_parser_settings http_parser_settings;
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
68 typedef struct http_parser_result http_parser_result;
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
69
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
70
d37a9c5 @ry fix typos
ry authored
71 /* Callbacks should return non-zero to indicate an error. The parser will
e244dbf @ry Fix whitespace, clean up some macros.
ry authored
72 * then halt execution.
73 *
c2acc21 @ry Skip body for HEAD responses
ry authored
74 * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
75 * returning '1' from on_headers_complete will tell the parser that it
76 * should not expect a body. This is used when receiving a response to a
77 * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
78 * chunked' headers that indicate the presence of a body.
79 *
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
80 * http_data_cb does not return data chunks. It will be call arbitrarally
81 * many times for each string. E.G. you might get 10 callbacks for "on_path"
82 * each providing just a few characters more data.
83 */
84 typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
85 typedef int (*http_cb) (http_parser*);
86
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
87
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
88 /* Request Methods */
ce38189 @ry Use enums instead of defines (for better debugging symbols)
ry authored
89 enum http_method
9eac636 save more space by removing buffer and shortening method
Cliff Frey authored
90 { HTTP_DELETE = 0
91 , HTTP_GET
92 , HTTP_HEAD
93 , HTTP_POST
94 , HTTP_PUT
4aebd8b @ry reorder methods
ry authored
95 /* pathological */
9eac636 save more space by removing buffer and shortening method
Cliff Frey authored
96 , HTTP_CONNECT
97 , HTTP_OPTIONS
98 , HTTP_TRACE
4aebd8b @ry reorder methods
ry authored
99 /* webdav */
9eac636 save more space by removing buffer and shortening method
Cliff Frey authored
100 , HTTP_COPY
101 , HTTP_LOCK
102 , HTTP_MKCOL
103 , HTTP_MOVE
104 , HTTP_PROPFIND
105 , HTTP_PROPPATCH
106 , HTTP_UNLOCK
9dc258f @ry Add subversion request methods
ry authored
107 /* subversion */
108 , HTTP_REPORT
109 , HTTP_MKACTIVITY
110 , HTTP_CHECKOUT
111 , HTTP_MERGE
f825b52 @TooTallNate Added support for "SUBSCRIBE" and "UNSUBSCRIBE" request methods.
TooTallNate authored
112 /* upnp */
d56a070 @TooTallNate Add support for "M-SEARCH" and "NOTIFY" request methods.
TooTallNate authored
113 , HTTP_MSEARCH
114 , HTTP_NOTIFY
f825b52 @TooTallNate Added support for "SUBSCRIBE" and "UNSUBSCRIBE" request methods.
TooTallNate authored
115 , HTTP_SUBSCRIBE
116 , HTTP_UNSUBSCRIBE
eee6012 @ry Support PATCH method
ry authored
117 /* RFC-5789 */
118 , HTTP_PATCH
ce38189 @ry Use enums instead of defines (for better debugging symbols)
ry authored
119 };
c288a96 @ry lint
ry authored
120
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
121
b8c3336 add support for HTTP_BOTH
Cliff Frey authored
122 enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
1a67704 @ry API: Define parser type in http_parser_init()
ry authored
123
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
124
b1c2cf8 @pgriess Expose F_* flags as public API.
pgriess authored
125 /* Flag values for http_parser.flags field */
126 enum flags
127 { F_CHUNKED = 1 << 0
128 , F_CONNECTION_KEEP_ALIVE = 1 << 1
129 , F_CONNECTION_CLOSE = 1 << 2
130 , F_TRAILING = 1 << 3
131 , F_UPGRADE = 1 << 4
132 , F_SKIPBODY = 1 << 5
133 };
134
135
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
136 /* Map for errno-related constants
137 *
138 * The provided argument should be a macro that takes 2 arguments.
139 */
140 #define HTTP_ERRNO_MAP(XX) \
141 /* No error */ \
142 XX(OK, "success") \
143 \
144 /* Callback-related errors */ \
145 XX(CB_message_begin, "the on_message_begin callback failed") \
146 XX(CB_path, "the on_path callback failed") \
147 XX(CB_query_string, "the on_query_string callback failed") \
148 XX(CB_url, "the on_url callback failed") \
149 XX(CB_fragment, "the on_fragment callback failed") \
150 XX(CB_header_field, "the on_header_field callback failed") \
151 XX(CB_header_value, "the on_header_value callback failed") \
152 XX(CB_headers_complete, "the on_headers_complete callback failed") \
761a5ea @pgriess Break out errno into its own field.
pgriess authored
153 XX(CB_body, "the on_body callback failed") \
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
154 XX(CB_message_complete, "the on_message_complete callback failed") \
155 \
156 /* Parsing-related errors */ \
157 XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
158 XX(HEADER_OVERFLOW, \
159 "too many header bytes seen; overflow detected") \
160 XX(CLOSED_CONNECTION, \
161 "data received after completed connection: close message") \
162 XX(INVALID_VERSION, "invalid HTTP version") \
163 XX(INVALID_STATUS, "invalid HTTP status code") \
164 XX(INVALID_METHOD, "invalid HTTP method") \
165 XX(INVALID_URL, "invalid URL") \
166 XX(INVALID_HOST, "invalid host") \
167 XX(INVALID_PORT, "invalid port") \
168 XX(INVALID_PATH, "invalid path") \
169 XX(INVALID_QUERY_STRING, "invalid query string") \
170 XX(INVALID_FRAGMENT, "invalid fragment") \
171 XX(LF_EXPECTED, "LF character expected") \
172 XX(INVALID_HEADER_TOKEN, "invalid character in header") \
173 XX(INVALID_CONTENT_LENGTH, \
174 "invalid character in content-length header") \
175 XX(INVALID_CHUNK_SIZE, \
176 "invalid character in chunk size header") \
177 XX(INVALID_CONSTANT, "invalid constant string") \
178 XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
179 XX(STRICT, "strict mode assertion failed") \
180 XX(UNKNOWN, "an unknown error occurred")
181
182
183 /* Define HPE_* values for each errno value above */
184 #define HTTP_ERRNO_GEN(n, s) HPE_##n,
185 enum http_errno {
186 HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
187 };
188 #undef HTTP_ERRNO_GEN
189
190
191 /* Get an http_errno value from an http_parser */
2b2ba2d @fmardini rename parser->errno to parser->http_errno; conflicts with errno.h where...
fmardini authored
192 #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
193
194 /* Get the line number that generated the current error */
195 #if HTTP_PARSER_DEBUG
196 #define HTTP_PARSER_ERRNO_LINE(p) ((p)->error_lineno)
197 #else
198 #define HTTP_PARSER_ERRNO_LINE(p) 0
199 #endif
200
201
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
202 struct http_parser {
203 /** PRIVATE **/
c30ea45 save 8 bytes in http_parser structure
Cliff Frey authored
204 unsigned char type : 2;
b1c2cf8 @pgriess Expose F_* flags as public API.
pgriess authored
205 unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
5502cdd reduce http_parser from 60 bytes to 48 bytes
Cliff Frey authored
206 unsigned char state;
207 unsigned char header_state;
208 unsigned char index;
88d11b3 @ry Support Upgrade header
ry authored
209
c30ea45 save 8 bytes in http_parser structure
Cliff Frey authored
210 uint32_t nread;
a59ba4d @ry Support long messages
ry authored
211 int64_t content_length;
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
212
213 /** READ-ONLY **/
fb6dc67 @ry strict check
ry authored
214 unsigned short http_major;
215 unsigned short http_minor;
9eac636 save more space by removing buffer and shortening method
Cliff Frey authored
216 unsigned short status_code; /* responses only */
217 unsigned char method; /* requests only */
2b2ba2d @fmardini rename parser->errno to parser->http_errno; conflicts with errno.h where...
fmardini authored
218 unsigned char http_errno : 7;
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
219
5502cdd reduce http_parser from 60 bytes to 48 bytes
Cliff Frey authored
220 /* 1 = Upgrade header was present and the parser has exited because of that.
221 * 0 = No upgrade header present.
222 * Should be checked when http_parser_execute() returns in addition to
223 * error checking.
224 */
75dc103 @bnoordhuis Single-bit bitfield 'upgrade' should be unsigned.
bnoordhuis authored
225 unsigned char upgrade : 1;
5502cdd reduce http_parser from 60 bytes to 48 bytes
Cliff Frey authored
226
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
227 #if HTTP_PARSER_DEBUG
228 uint32_t error_lineno;
229 #endif
230
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
231 /** PUBLIC **/
232 void *data; /* A pointer to get hook to the "connection" or "socket" object */
dbd2dad @ry Introduce http_parser_settings
ry authored
233 };
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
234
235
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
236 struct http_parser_settings {
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
237 http_cb on_message_begin;
3834853 @ry uri -> url
ry authored
238 http_data_cb on_url;
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
239 http_data_cb on_header_field;
240 http_data_cb on_header_value;
241 http_cb on_headers_complete;
242 http_data_cb on_body;
243 http_cb on_message_complete;
244 };
245
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
246
1a67704 @ry API: Define parser type in http_parser_init()
ry authored
247 void http_parser_init(http_parser *parser, enum http_parser_type type);
d37a9c5 @ry fix typos
ry authored
248
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
249
dbd2dad @ry Introduce http_parser_settings
ry authored
250 size_t http_parser_execute(http_parser *parser,
7239788 pass pointer to settings structure rather than pass by value
Cliff Frey authored
251 const http_parser_settings *settings,
dbd2dad @ry Introduce http_parser_settings
ry authored
252 const char *data,
253 size_t len);
d37a9c5 @ry fix typos
ry authored
254
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
255
d37a9c5 @ry fix typos
ry authored
256 /* If http_should_keep_alive() in the on_headers_complete or
257 * on_message_complete callback returns true, then this will be should be
258 * the last message on the connection.
259 * If you are the server, respond with the "Connection: close" header.
260 * If you are the client, close the connection.
5b00b6a @ry add http_should_keep_alive()
ry authored
261 */
262 int http_should_keep_alive(http_parser *parser);
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
263
ddbd5c3 @ry Expose http_method_str() to get a string version of a method
ry authored
264 /* Returns a string version of the HTTP method. */
9114e58 @pgriess Facility to report detailed parsing errors.
pgriess authored
265 const char *http_method_str(enum http_method m);
266
267 /* Return a string name of the given error */
268 const char *http_errno_name(enum http_errno err);
269
270 /* Return a string description of the given error */
271 const char *http_errno_description(enum http_errno err);
e07e0b9 @ry Tasteful vertical whitespace.
ry authored
272
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
273 #ifdef __cplusplus
274 }
e244dbf @ry Fix whitespace, clean up some macros.
ry authored
275 #endif
97d4572 @ry Remove "ebb_message" object from libebb's parser.
ry authored
276 #endif
Something went wrong with that request. Please try again.