1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
---|
2 | *
|
---|
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:
|
---|
9 | *
|
---|
10 | * The above copyright notice and this permission notice shall be included in
|
---|
11 | * all copies or substantial portions of the Software.
|
---|
12 | *
|
---|
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
|
---|
19 | * IN THE SOFTWARE.
|
---|
20 | */
|
---|
21 | #ifndef http_parser_h
|
---|
22 | #define http_parser_h
|
---|
23 | #ifdef __cplusplus
|
---|
24 | extern "C" {
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | /* Also update SONAME in the Makefile whenever you change these. */
|
---|
28 | #define HTTP_PARSER_VERSION_MAJOR 2
|
---|
29 | #define HTTP_PARSER_VERSION_MINOR 4
|
---|
30 | #define HTTP_PARSER_VERSION_PATCH 1
|
---|
31 |
|
---|
32 | #include <t_stddef.h>
|
---|
33 | #include <t_syslog.h>
|
---|
34 | #if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
|
---|
35 | #include <BaseTsd.h>
|
---|
36 | #include <stddef.h>
|
---|
37 | typedef __int8 int8_t;
|
---|
38 | typedef unsigned __int8 uint8_t;
|
---|
39 | typedef __int16 int16_t;
|
---|
40 | typedef unsigned __int16 uint16_t;
|
---|
41 | typedef __int32 int32_t;
|
---|
42 | typedef unsigned __int32 uint32_t;
|
---|
43 | typedef __int64 int64_t;
|
---|
44 | typedef unsigned __int64 uint64_t;
|
---|
45 | #else
|
---|
46 | #include <stdint.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
|
---|
50 | * faster
|
---|
51 | */
|
---|
52 | #ifndef HTTP_PARSER_STRICT
|
---|
53 | # define HTTP_PARSER_STRICT 1
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | /* Maximium header size allowed. If the macro is not defined
|
---|
57 | * before including this header then the default is used. To
|
---|
58 | * change the maximum header size, define the macro in the build
|
---|
59 | * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
|
---|
60 | * the effective limit on the size of the header, define the macro
|
---|
61 | * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
|
---|
62 | */
|
---|
63 | #ifndef HTTP_MAX_HEADER_SIZE
|
---|
64 | # define HTTP_MAX_HEADER_SIZE (80*1024)
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | typedef struct http_parser http_parser;
|
---|
68 | typedef struct http_parser_settings http_parser_settings;
|
---|
69 |
|
---|
70 |
|
---|
71 | /* Callbacks should return non-zero to indicate an error. The parser will
|
---|
72 | * then halt execution.
|
---|
73 | *
|
---|
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 | *
|
---|
80 | * http_data_cb does not return data chunks. It will be called arbitrarily
|
---|
81 | * many times for each string. E.G. you might get 10 callbacks for "on_url"
|
---|
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 |
|
---|
87 |
|
---|
88 | /* Request Methods */
|
---|
89 | #define HTTP_METHOD_MAP(XX) \
|
---|
90 | XX(0, DELETE, DELETE) \
|
---|
91 | XX(1, GET, GET) \
|
---|
92 | XX(2, HEAD, HEAD) \
|
---|
93 | XX(3, POST, POST) \
|
---|
94 | XX(4, PUT, PUT) \
|
---|
95 | /* pathological */ \
|
---|
96 | XX(5, CONNECT, CONNECT) \
|
---|
97 | XX(6, OPTIONS, OPTIONS) \
|
---|
98 | XX(7, TRACE, TRACE) \
|
---|
99 | /* webdav */ \
|
---|
100 | XX(8, COPY, COPY) \
|
---|
101 | XX(9, LOCK, LOCK) \
|
---|
102 | XX(10, MKCOL, MKCOL) \
|
---|
103 | XX(11, MOVE, MOVE) \
|
---|
104 | XX(12, PROPFIND, PROPFIND) \
|
---|
105 | XX(13, PROPPATCH, PROPPATCH) \
|
---|
106 | XX(14, SEARCH, SEARCH) \
|
---|
107 | XX(15, UNLOCK, UNLOCK) \
|
---|
108 | /* subversion */ \
|
---|
109 | XX(16, REPORT, REPORT) \
|
---|
110 | XX(17, MKACTIVITY, MKACTIVITY) \
|
---|
111 | XX(18, CHECKOUT, CHECKOUT) \
|
---|
112 | XX(19, MERGE, MERGE) \
|
---|
113 | /* upnp */ \
|
---|
114 | XX(20, MSEARCH, M-SEARCH) \
|
---|
115 | XX(21, NOTIFY, NOTIFY) \
|
---|
116 | XX(22, SUBSCRIBE, SUBSCRIBE) \
|
---|
117 | XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
|
---|
118 | /* RFC-5789 */ \
|
---|
119 | XX(24, PATCH, PATCH) \
|
---|
120 | XX(25, PURGE, PURGE) \
|
---|
121 | /* CalDAV */ \
|
---|
122 | XX(26, MKCALENDAR, MKCALENDAR) \
|
---|
123 |
|
---|
124 | enum http_method
|
---|
125 | {
|
---|
126 | #define XX(num, name, string) HTTP_##name = num,
|
---|
127 | HTTP_METHOD_MAP(XX)
|
---|
128 | #undef XX
|
---|
129 | };
|
---|
130 |
|
---|
131 |
|
---|
132 | enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
|
---|
133 |
|
---|
134 |
|
---|
135 | /* Flag values for http_parser.flags field */
|
---|
136 | enum flags
|
---|
137 | { F_CHUNKED = 1 << 0
|
---|
138 | , F_CONNECTION_KEEP_ALIVE = 1 << 1
|
---|
139 | , F_CONNECTION_CLOSE = 1 << 2
|
---|
140 | , F_CONNECTION_UPGRADE = 1 << 3
|
---|
141 | , F_TRAILING = 1 << 4
|
---|
142 | , F_UPGRADE = 1 << 5
|
---|
143 | , F_SKIPBODY = 1 << 6
|
---|
144 | };
|
---|
145 |
|
---|
146 |
|
---|
147 | /* Map for errno-related constants
|
---|
148 | *
|
---|
149 | * The provided argument should be a macro that takes 2 arguments.
|
---|
150 | */
|
---|
151 | #define HTTP_ERRNO_MAP(XX) \
|
---|
152 | /* No error */ \
|
---|
153 | XX(OK, "success") \
|
---|
154 | \
|
---|
155 | /* Callback-related errors */ \
|
---|
156 | XX(CB_message_begin, "the on_message_begin callback failed") \
|
---|
157 | XX(CB_url, "the on_url callback failed") \
|
---|
158 | XX(CB_header_field, "the on_header_field callback failed") \
|
---|
159 | XX(CB_header_value, "the on_header_value callback failed") \
|
---|
160 | XX(CB_headers_complete, "the on_headers_complete callback failed") \
|
---|
161 | XX(CB_body, "the on_body callback failed") \
|
---|
162 | XX(CB_message_complete, "the on_message_complete callback failed") \
|
---|
163 | XX(CB_status, "the on_status callback failed") \
|
---|
164 | \
|
---|
165 | /* Parsing-related errors */ \
|
---|
166 | XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
|
---|
167 | XX(HEADER_OVERFLOW, \
|
---|
168 | "too many header bytes seen; overflow detected") \
|
---|
169 | XX(CLOSED_CONNECTION, \
|
---|
170 | "data received after completed connection: close message") \
|
---|
171 | XX(INVALID_VERSION, "invalid HTTP version") \
|
---|
172 | XX(INVALID_STATUS, "invalid HTTP status code") \
|
---|
173 | XX(INVALID_METHOD, "invalid HTTP method") \
|
---|
174 | XX(INVALID_URL, "invalid URL") \
|
---|
175 | XX(INVALID_HOST, "invalid host") \
|
---|
176 | XX(INVALID_PORT, "invalid port") \
|
---|
177 | XX(INVALID_PATH, "invalid path") \
|
---|
178 | XX(INVALID_QUERY_STRING, "invalid query string") \
|
---|
179 | XX(INVALID_FRAGMENT, "invalid fragment") \
|
---|
180 | XX(LF_EXPECTED, "LF character expected") \
|
---|
181 | XX(INVALID_HEADER_TOKEN, "invalid character in header") \
|
---|
182 | XX(INVALID_CONTENT_LENGTH, \
|
---|
183 | "invalid character in content-length header") \
|
---|
184 | XX(INVALID_CHUNK_SIZE, \
|
---|
185 | "invalid character in chunk size header") \
|
---|
186 | XX(INVALID_CONSTANT, "invalid constant string") \
|
---|
187 | XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
|
---|
188 | XX(STRICT, "strict mode assertion failed") \
|
---|
189 | XX(PAUSED, "parser is paused") \
|
---|
190 | XX(UNKNOWN, "an unknown error occurred")
|
---|
191 |
|
---|
192 |
|
---|
193 | /* Define HPE_* values for each errno value above */
|
---|
194 | #define HTTP_ERRNO_GEN(n, s) HPE_##n,
|
---|
195 | enum http_errno {
|
---|
196 | HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
|
---|
197 | };
|
---|
198 | #undef HTTP_ERRNO_GEN
|
---|
199 |
|
---|
200 |
|
---|
201 | /* Get an http_errno value from an http_parser */
|
---|
202 | #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
|
---|
203 |
|
---|
204 |
|
---|
205 | struct http_parser {
|
---|
206 | /** PRIVATE **/
|
---|
207 | unsigned int type : 2; /* enum http_parser_type */
|
---|
208 | unsigned int flags : 6; /* F_* values from 'flags' enum; semi-public */
|
---|
209 | unsigned int state : 8; /* enum state from http_parser.c */
|
---|
210 | unsigned int header_state : 8; /* enum header_state from http_parser.c */
|
---|
211 | unsigned int index : 8; /* index into current matcher */
|
---|
212 |
|
---|
213 | uint32_t nread; /* # bytes read in various scenarios */
|
---|
214 | uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
|
---|
215 |
|
---|
216 | /** READ-ONLY **/
|
---|
217 | unsigned short http_major;
|
---|
218 | unsigned short http_minor;
|
---|
219 | unsigned int status_code : 16; /* responses only */
|
---|
220 | unsigned int method : 8; /* requests only */
|
---|
221 | unsigned int http_errno : 7;
|
---|
222 |
|
---|
223 | /* 1 = Upgrade header was present and the parser has exited because of that.
|
---|
224 | * 0 = No upgrade header present.
|
---|
225 | * Should be checked when http_parser_execute() returns in addition to
|
---|
226 | * error checking.
|
---|
227 | */
|
---|
228 | unsigned int upgrade : 1;
|
---|
229 |
|
---|
230 | /** PUBLIC **/
|
---|
231 | void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
---|
232 | };
|
---|
233 |
|
---|
234 |
|
---|
235 | struct http_parser_settings {
|
---|
236 | http_cb on_message_begin;
|
---|
237 | http_data_cb on_url;
|
---|
238 | http_data_cb on_status;
|
---|
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 |
|
---|
246 |
|
---|
247 | enum http_parser_url_fields
|
---|
248 | { UF_SCHEMA = 0
|
---|
249 | , UF_HOST = 1
|
---|
250 | , UF_PORT = 2
|
---|
251 | , UF_PATH = 3
|
---|
252 | , UF_QUERY = 4
|
---|
253 | , UF_FRAGMENT = 5
|
---|
254 | , UF_USERINFO = 6
|
---|
255 | , UF_MAX = 7
|
---|
256 | };
|
---|
257 |
|
---|
258 |
|
---|
259 | /* Result structure for http_parser_parse_url().
|
---|
260 | *
|
---|
261 | * Callers should index into field_data[] with UF_* values iff field_set
|
---|
262 | * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
|
---|
263 | * because we probably have padding left over), we convert any port to
|
---|
264 | * a uint16_t.
|
---|
265 | */
|
---|
266 | struct http_parser_url {
|
---|
267 | uint16_t field_set; /* Bitmask of (1 << UF_*) values */
|
---|
268 | uint16_t port; /* Converted UF_PORT string */
|
---|
269 |
|
---|
270 | struct {
|
---|
271 | uint16_t off; /* Offset into buffer in which field starts */
|
---|
272 | uint16_t len; /* Length of run in buffer */
|
---|
273 | } field_data[UF_MAX];
|
---|
274 | };
|
---|
275 |
|
---|
276 |
|
---|
277 | /* Returns the library version. Bits 16-23 contain the major version number,
|
---|
278 | * bits 8-15 the minor version number and bits 0-7 the patch level.
|
---|
279 | * Usage example:
|
---|
280 | *
|
---|
281 | * unsigned long version = http_parser_version();
|
---|
282 | * unsigned major = (version >> 16) & 255;
|
---|
283 | * unsigned minor = (version >> 8) & 255;
|
---|
284 | * unsigned patch = version & 255;
|
---|
285 | * printf("http_parser v%u.%u.%u\n", major, minor, patch);
|
---|
286 | */
|
---|
287 | unsigned long http_parser_version(void);
|
---|
288 |
|
---|
289 | void http_parser_init(http_parser *parser, enum http_parser_type type);
|
---|
290 |
|
---|
291 |
|
---|
292 | /* Executes the parser. Returns number of parsed bytes. Sets
|
---|
293 | * `parser->http_errno` on error. */
|
---|
294 | size_t http_parser_execute(http_parser *parser,
|
---|
295 | const http_parser_settings *settings,
|
---|
296 | const char *data,
|
---|
297 | size_t len);
|
---|
298 |
|
---|
299 |
|
---|
300 | /* If http_should_keep_alive() in the on_headers_complete or
|
---|
301 | * on_message_complete callback returns 0, then this should be
|
---|
302 | * the last message on the connection.
|
---|
303 | * If you are the server, respond with the "Connection: close" header.
|
---|
304 | * If you are the client, close the connection.
|
---|
305 | */
|
---|
306 | int http_should_keep_alive(const http_parser *parser);
|
---|
307 |
|
---|
308 | /* Returns a string version of the HTTP method. */
|
---|
309 | const char *http_method_str(enum http_method m);
|
---|
310 |
|
---|
311 | /* Return a string name of the given error */
|
---|
312 | const char *http_errno_name(enum http_errno err);
|
---|
313 |
|
---|
314 | /* Return a string description of the given error */
|
---|
315 | const char *http_errno_description(enum http_errno err);
|
---|
316 |
|
---|
317 | /* Parse a URL; return nonzero on failure */
|
---|
318 | int http_parser_parse_url(const char *buf, size_t buflen,
|
---|
319 | int is_connect,
|
---|
320 | struct http_parser_url *u);
|
---|
321 |
|
---|
322 | /* Pause or un-pause the parser; a nonzero value pauses */
|
---|
323 | void http_parser_pause(http_parser *parser, int paused);
|
---|
324 |
|
---|
325 | /* Checks if this is the final chunk of the body. */
|
---|
326 | int http_body_is_final(const http_parser *parser);
|
---|
327 |
|
---|
328 | #ifdef __cplusplus
|
---|
329 | }
|
---|
330 | #endif
|
---|
331 | #endif
|
---|