blob: 880ed278ef992d34ef9250a0c827690caf4c666d [file] [log] [blame]
Ryan Dahl63daf222011-03-22 13:29:58 -07001/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
Ryan Dahl79947a72010-01-06 18:54:51 -08002 *
Ryan Dahlb283cd92009-11-19 21:11:06 +01003 * 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:
Ryan Dahl79947a72010-01-06 18:54:51 -08009 *
Ryan Dahlb283cd92009-11-19 21:11:06 +010010 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
Ryan Dahl79947a72010-01-06 18:54:51 -080012 *
Ryan Dahlb283cd92009-11-19 21:11:06 +010013 * 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
Ryan Dahl79947a72010-01-06 18:54:51 -080019 * IN THE SOFTWARE.
Ryan Dahlb283cd92009-11-19 21:11:06 +010020 */
Ryan97d45722009-04-25 13:33:16 +020021#ifndef http_parser_h
22#define http_parser_h
23#ifdef __cplusplus
24extern "C" {
Ryane244dbf2009-08-20 12:45:41 +020025#endif
Ryan97d45722009-04-25 13:33:16 +020026
Ben Noordhuis6df37aa2013-06-13 19:06:41 +020027/* Also update SONAME in the Makefile whenever you change these. */
Ben Noordhuis3fb4e062012-10-10 22:38:20 +020028#define HTTP_PARSER_VERSION_MAJOR 2
Ben Noordhuis0d0a24e2018-12-28 08:28:50 +010029#define HTTP_PARSER_VERSION_MINOR 9
30#define HTTP_PARSER_VERSION_PATCH 0
Ryan Dahle07e0b92010-02-27 21:23:53 -080031
Stephan Gatzka291a32c2017-03-15 17:46:55 +010032#include <stddef.h>
Andrew Udvareb36c2a92015-07-11 23:56:25 -070033#if defined(_WIN32) && !defined(__MINGW32__) && \
34 (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
James McLaughlin8ee3b0d2012-07-14 12:38:56 +010035#include <BaseTsd.h>
Ewen Cheslack-Postava24be7932010-11-03 00:51:37 -070036typedef __int8 int8_t;
37typedef unsigned __int8 uint8_t;
38typedef __int16 int16_t;
39typedef unsigned __int16 uint16_t;
40typedef __int32 int32_t;
41typedef unsigned __int32 uint32_t;
42typedef __int64 int64_t;
43typedef unsigned __int64 uint64_t;
Ewen Cheslack-Postava24be7932010-11-03 00:51:37 -070044#else
45#include <stdint.h>
Ewen Cheslack-Postava4afe80a2010-06-27 23:14:29 -070046#endif
Ryan Dahle07e0b92010-02-27 21:23:53 -080047
Ryan Dahlfb6dc672009-11-20 14:24:05 +010048/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
Ryan Dahl79947a72010-01-06 18:54:51 -080049 * faster
50 */
Ryan Dahlfb6dc672009-11-20 14:24:05 +010051#ifndef HTTP_PARSER_STRICT
52# define HTTP_PARSER_STRICT 1
Ryan Dahlfb6dc672009-11-20 14:24:05 +010053#endif
54
Vinnie Falco24e2d2d2014-03-15 19:17:48 -070055/* Maximium header size allowed. If the macro is not defined
56 * before including this header then the default is used. To
57 * change the maximum header size, define the macro in the build
58 * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
59 * the effective limit on the size of the header, define the macro
60 * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
61 */
62#ifndef HTTP_MAX_HEADER_SIZE
63# define HTTP_MAX_HEADER_SIZE (80*1024)
64#endif
Ryan Dahle07e0b92010-02-27 21:23:53 -080065
Ryan97d45722009-04-25 13:33:16 +020066typedef struct http_parser http_parser;
Ryan Dahldbd2dad2010-02-27 20:23:29 -080067typedef struct http_parser_settings http_parser_settings;
Ryan97d45722009-04-25 13:33:16 +020068
Ryan Dahle07e0b92010-02-27 21:23:53 -080069
Ryan Dahld37a9c52010-02-22 08:11:01 -080070/* Callbacks should return non-zero to indicate an error. The parser will
Ryane244dbf2009-08-20 12:45:41 +020071 * then halt execution.
72 *
Ryan Dahlc2acc212010-05-25 16:25:26 -070073 * The one exception is on_headers_complete. In a HTTP_RESPONSE parser
74 * returning '1' from on_headers_complete will tell the parser that it
75 * should not expect a body. This is used when receiving a response to a
76 * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
77 * chunked' headers that indicate the presence of a body.
78 *
Fedor Indutny04d28a72016-04-17 00:14:00 +020079 * Returning `2` from on_headers_complete will tell parser that it should not
80 * expect neither a body nor any futher responses on this connection. This is
81 * useful for handling responses to a CONNECT request which may not contain
82 * `Upgrade` or `Connection: upgrade` headers.
83 *
George Miroshnykov17ed7de2014-08-21 12:42:10 +030084 * http_data_cb does not return data chunks. It will be called arbitrarily
Peter Faimanb4fc7772012-10-28 13:23:13 -070085 * many times for each string. E.G. you might get 10 callbacks for "on_url"
Ryan97d45722009-04-25 13:33:16 +020086 * each providing just a few characters more data.
87 */
88typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
89typedef int (*http_cb) (http_parser*);
90
Ryan Dahle07e0b92010-02-27 21:23:53 -080091
Nathaniel McCallum335850f2016-10-06 02:03:36 -040092/* Status Codes */
93#define HTTP_STATUS_MAP(XX) \
94 XX(100, CONTINUE, Continue) \
95 XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \
96 XX(102, PROCESSING, Processing) \
97 XX(200, OK, OK) \
98 XX(201, CREATED, Created) \
99 XX(202, ACCEPTED, Accepted) \
100 XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \
101 XX(204, NO_CONTENT, No Content) \
102 XX(205, RESET_CONTENT, Reset Content) \
103 XX(206, PARTIAL_CONTENT, Partial Content) \
104 XX(207, MULTI_STATUS, Multi-Status) \
105 XX(208, ALREADY_REPORTED, Already Reported) \
106 XX(226, IM_USED, IM Used) \
107 XX(300, MULTIPLE_CHOICES, Multiple Choices) \
108 XX(301, MOVED_PERMANENTLY, Moved Permanently) \
109 XX(302, FOUND, Found) \
110 XX(303, SEE_OTHER, See Other) \
111 XX(304, NOT_MODIFIED, Not Modified) \
112 XX(305, USE_PROXY, Use Proxy) \
113 XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \
114 XX(308, PERMANENT_REDIRECT, Permanent Redirect) \
115 XX(400, BAD_REQUEST, Bad Request) \
116 XX(401, UNAUTHORIZED, Unauthorized) \
117 XX(402, PAYMENT_REQUIRED, Payment Required) \
118 XX(403, FORBIDDEN, Forbidden) \
119 XX(404, NOT_FOUND, Not Found) \
120 XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \
121 XX(406, NOT_ACCEPTABLE, Not Acceptable) \
122 XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \
123 XX(408, REQUEST_TIMEOUT, Request Timeout) \
124 XX(409, CONFLICT, Conflict) \
125 XX(410, GONE, Gone) \
126 XX(411, LENGTH_REQUIRED, Length Required) \
127 XX(412, PRECONDITION_FAILED, Precondition Failed) \
128 XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \
129 XX(414, URI_TOO_LONG, URI Too Long) \
130 XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \
131 XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \
132 XX(417, EXPECTATION_FAILED, Expectation Failed) \
133 XX(421, MISDIRECTED_REQUEST, Misdirected Request) \
134 XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \
135 XX(423, LOCKED, Locked) \
136 XX(424, FAILED_DEPENDENCY, Failed Dependency) \
137 XX(426, UPGRADE_REQUIRED, Upgrade Required) \
138 XX(428, PRECONDITION_REQUIRED, Precondition Required) \
139 XX(429, TOO_MANY_REQUESTS, Too Many Requests) \
140 XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \
141 XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \
142 XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \
143 XX(501, NOT_IMPLEMENTED, Not Implemented) \
144 XX(502, BAD_GATEWAY, Bad Gateway) \
145 XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \
146 XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \
147 XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \
148 XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \
149 XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \
150 XX(508, LOOP_DETECTED, Loop Detected) \
151 XX(510, NOT_EXTENDED, Not Extended) \
152 XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \
153
154enum http_status
155 {
156#define XX(num, name, string) HTTP_STATUS_##name = num,
157 HTTP_STATUS_MAP(XX)
158#undef XX
159 };
160
161
Ryan97d45722009-04-25 13:33:16 +0200162/* Request Methods */
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100163#define HTTP_METHOD_MAP(XX) \
164 XX(0, DELETE, DELETE) \
165 XX(1, GET, GET) \
166 XX(2, HEAD, HEAD) \
167 XX(3, POST, POST) \
168 XX(4, PUT, PUT) \
169 /* pathological */ \
170 XX(5, CONNECT, CONNECT) \
171 XX(6, OPTIONS, OPTIONS) \
172 XX(7, TRACE, TRACE) \
Ivan Enderlin8b1d6522015-04-24 17:36:26 +0200173 /* WebDAV */ \
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100174 XX(8, COPY, COPY) \
175 XX(9, LOCK, LOCK) \
176 XX(10, MKCOL, MKCOL) \
177 XX(11, MOVE, MOVE) \
178 XX(12, PROPFIND, PROPFIND) \
179 XX(13, PROPPATCH, PROPPATCH) \
180 XX(14, SEARCH, SEARCH) \
181 XX(15, UNLOCK, UNLOCK) \
Ivan Enderlin8b1d6522015-04-24 17:36:26 +0200182 XX(16, BIND, BIND) \
183 XX(17, REBIND, REBIND) \
184 XX(18, UNBIND, UNBIND) \
Ivan Enderlineb5e9922015-04-24 17:55:26 +0200185 XX(19, ACL, ACL) \
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100186 /* subversion */ \
Ivan Enderlineb5e9922015-04-24 17:55:26 +0200187 XX(20, REPORT, REPORT) \
188 XX(21, MKACTIVITY, MKACTIVITY) \
189 XX(22, CHECKOUT, CHECKOUT) \
190 XX(23, MERGE, MERGE) \
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100191 /* upnp */ \
Ivan Enderlineb5e9922015-04-24 17:55:26 +0200192 XX(24, MSEARCH, M-SEARCH) \
193 XX(25, NOTIFY, NOTIFY) \
194 XX(26, SUBSCRIBE, SUBSCRIBE) \
195 XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100196 /* RFC-5789 */ \
Ivan Enderlineb5e9922015-04-24 17:55:26 +0200197 XX(28, PATCH, PATCH) \
198 XX(29, PURGE, PURGE) \
Helge Heß1317eec2014-06-24 11:50:13 +0200199 /* CalDAV */ \
Ivan Enderlineb5e9922015-04-24 17:55:26 +0200200 XX(30, MKCALENDAR, MKCALENDAR) \
Olivier Menguée557b622015-10-12 19:37:28 +0200201 /* RFC-2068, section 19.6.1.2 */ \
202 XX(31, LINK, LINK) \
203 XX(32, UNLINK, UNLINK) \
Ben Noordhuis921d5582018-02-05 14:03:33 +0100204 /* icecast */ \
205 XX(33, SOURCE, SOURCE) \
Ben Noordhuis36808f42012-02-27 23:35:47 +0100206
Ryan Dahlce381892009-10-27 13:50:54 +0100207enum http_method
Ben Noordhuis36808f42012-02-27 23:35:47 +0100208 {
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100209#define XX(num, name, string) HTTP_##name = num,
Ben Noordhuis36808f42012-02-27 23:35:47 +0100210 HTTP_METHOD_MAP(XX)
Ben Noordhuis8bec3ea2012-03-12 02:13:51 +0100211#undef XX
Ryan Dahlce381892009-10-27 13:50:54 +0100212 };
Ryanc288a962009-04-26 20:52:53 +0200213
Ryan Dahle07e0b92010-02-27 21:23:53 -0800214
Cliff Freyb8c33362010-05-26 19:30:13 -0700215enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
Ryan Dahl1a677042010-01-08 21:38:17 -0800216
Ryan Dahle07e0b92010-02-27 21:23:53 -0800217
Peter Griessb1c2cf82011-05-23 11:42:13 -0500218/* Flag values for http_parser.flags field */
219enum flags
220 { F_CHUNKED = 1 << 0
221 , F_CONNECTION_KEEP_ALIVE = 1 << 1
222 , F_CONNECTION_CLOSE = 1 << 2
Jon Kolb091ebb82012-03-01 19:09:42 -0500223 , F_CONNECTION_UPGRADE = 1 << 3
224 , F_TRAILING = 1 << 4
225 , F_UPGRADE = 1 << 5
226 , F_SKIPBODY = 1 << 6
James M Snelle2e467b2016-02-10 11:33:49 +1100227 , F_CONTENTLENGTH = 1 << 7
Peter Griessb1c2cf82011-05-23 11:42:13 -0500228 };
229
230
Peter Griess9114e582011-05-23 14:09:19 -0500231/* Map for errno-related constants
James M Snellbee48172015-10-28 21:47:12 -0700232 *
Peter Griess9114e582011-05-23 14:09:19 -0500233 * The provided argument should be a macro that takes 2 arguments.
234 */
235#define HTTP_ERRNO_MAP(XX) \
236 /* No error */ \
237 XX(OK, "success") \
238 \
239 /* Callback-related errors */ \
240 XX(CB_message_begin, "the on_message_begin callback failed") \
Peter Griess9114e582011-05-23 14:09:19 -0500241 XX(CB_url, "the on_url callback failed") \
Peter Griess9114e582011-05-23 14:09:19 -0500242 XX(CB_header_field, "the on_header_field callback failed") \
243 XX(CB_header_value, "the on_header_value callback failed") \
244 XX(CB_headers_complete, "the on_headers_complete callback failed") \
Peter Griess761a5ea2011-07-09 11:47:34 -0500245 XX(CB_body, "the on_body callback failed") \
Peter Griess9114e582011-05-23 14:09:19 -0500246 XX(CB_message_complete, "the on_message_complete callback failed") \
Patrik Stutzd7b938b2013-01-17 08:38:34 -0600247 XX(CB_status, "the on_status callback failed") \
Kjell Schubertd7675452015-04-17 17:36:02 -0700248 XX(CB_chunk_header, "the on_chunk_header callback failed") \
249 XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
Peter Griess9114e582011-05-23 14:09:19 -0500250 \
251 /* Parsing-related errors */ \
252 XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
253 XX(HEADER_OVERFLOW, \
254 "too many header bytes seen; overflow detected") \
255 XX(CLOSED_CONNECTION, \
256 "data received after completed connection: close message") \
257 XX(INVALID_VERSION, "invalid HTTP version") \
258 XX(INVALID_STATUS, "invalid HTTP status code") \
259 XX(INVALID_METHOD, "invalid HTTP method") \
260 XX(INVALID_URL, "invalid URL") \
261 XX(INVALID_HOST, "invalid host") \
262 XX(INVALID_PORT, "invalid port") \
263 XX(INVALID_PATH, "invalid path") \
264 XX(INVALID_QUERY_STRING, "invalid query string") \
265 XX(INVALID_FRAGMENT, "invalid fragment") \
266 XX(LF_EXPECTED, "LF character expected") \
267 XX(INVALID_HEADER_TOKEN, "invalid character in header") \
268 XX(INVALID_CONTENT_LENGTH, \
269 "invalid character in content-length header") \
James M Snelle2e467b2016-02-10 11:33:49 +1100270 XX(UNEXPECTED_CONTENT_LENGTH, \
271 "unexpected content-length header") \
Peter Griess9114e582011-05-23 14:09:19 -0500272 XX(INVALID_CHUNK_SIZE, \
273 "invalid character in chunk size header") \
274 XX(INVALID_CONSTANT, "invalid constant string") \
275 XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
276 XX(STRICT, "strict mode assertion failed") \
Peter Griessd0bb8672012-01-06 18:13:41 -0600277 XX(PAUSED, "parser is paused") \
Peter Griess9114e582011-05-23 14:09:19 -0500278 XX(UNKNOWN, "an unknown error occurred")
279
280
281/* Define HPE_* values for each errno value above */
282#define HTTP_ERRNO_GEN(n, s) HPE_##n,
283enum http_errno {
284 HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
285};
286#undef HTTP_ERRNO_GEN
287
288
289/* Get an http_errno value from an http_parser */
Fouad Mardini2b2ba2d2011-07-24 18:49:54 +0300290#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
Peter Griess9114e582011-05-23 14:09:19 -0500291
Peter Griess9114e582011-05-23 14:09:19 -0500292
Ryan97d45722009-04-25 13:33:16 +0200293struct http_parser {
294 /** PRIVATE **/
Charlie Somerville11419c82013-11-19 01:53:05 -0800295 unsigned int type : 2; /* enum http_parser_type */
James M Snelle2e467b2016-02-10 11:33:49 +1100296 unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
Fedor Indutny7ba31232015-02-15 17:44:48 +0100297 unsigned int state : 7; /* enum state from http_parser.c */
James M Snelle2e467b2016-02-10 11:33:49 +1100298 unsigned int header_state : 7; /* enum header_state from http_parser.c */
299 unsigned int index : 7; /* index into current matcher */
300 unsigned int lenient_http_headers : 1;
Ryan Dahl88d11b32010-04-14 03:17:11 -0700301
Peter Griessd0bb8672012-01-06 18:13:41 -0600302 uint32_t nread; /* # bytes read in various scenarios */
Ben Noordhuisf668e722012-01-22 01:20:35 +0100303 uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
Ryan97d45722009-04-25 13:33:16 +0200304
Ryan97d45722009-04-25 13:33:16 +0200305 /** READ-ONLY **/
Ryan Dahlfb6dc672009-11-20 14:24:05 +0100306 unsigned short http_major;
307 unsigned short http_minor;
Charlie Somerville11419c82013-11-19 01:53:05 -0800308 unsigned int status_code : 16; /* responses only */
309 unsigned int method : 8; /* requests only */
310 unsigned int http_errno : 7;
Ryan97d45722009-04-25 13:33:16 +0200311
Cliff Frey5502cdd2010-05-27 15:04:24 -0700312 /* 1 = Upgrade header was present and the parser has exited because of that.
313 * 0 = No upgrade header present.
314 * Should be checked when http_parser_execute() returns in addition to
315 * error checking.
316 */
Charlie Somerville11419c82013-11-19 01:53:05 -0800317 unsigned int upgrade : 1;
Cliff Frey5502cdd2010-05-27 15:04:24 -0700318
Ryan97d45722009-04-25 13:33:16 +0200319 /** PUBLIC **/
320 void *data; /* A pointer to get hook to the "connection" or "socket" object */
Ryan Dahldbd2dad2010-02-27 20:23:29 -0800321};
Ryan97d45722009-04-25 13:33:16 +0200322
Ryan Dahle07e0b92010-02-27 21:23:53 -0800323
Ryan Dahldbd2dad2010-02-27 20:23:29 -0800324struct http_parser_settings {
Ryan97d45722009-04-25 13:33:16 +0200325 http_cb on_message_begin;
Ryan Dahl38348532009-11-19 21:10:39 +0100326 http_data_cb on_url;
Patrik Stutzd7b938b2013-01-17 08:38:34 -0600327 http_data_cb on_status;
Ryan97d45722009-04-25 13:33:16 +0200328 http_data_cb on_header_field;
329 http_data_cb on_header_value;
330 http_cb on_headers_complete;
331 http_data_cb on_body;
332 http_cb on_message_complete;
Kjell Schubertd7675452015-04-17 17:36:02 -0700333 /* When on_chunk_header is called, the current chunk length is stored
334 * in parser->content_length.
335 */
336 http_cb on_chunk_header;
337 http_cb on_chunk_complete;
Ryan97d45722009-04-25 13:33:16 +0200338};
339
Ryan Dahle07e0b92010-02-27 21:23:53 -0800340
Peter Griessd7675cd2011-07-21 18:36:56 -0500341enum http_parser_url_fields
342 { UF_SCHEMA = 0
343 , UF_HOST = 1
344 , UF_PORT = 2
345 , UF_PATH = 3
346 , UF_QUERY = 4
347 , UF_FRAGMENT = 5
Bertrand Paquet79650962012-07-08 01:44:31 +0200348 , UF_USERINFO = 6
349 , UF_MAX = 7
Peter Griessd7675cd2011-07-21 18:36:56 -0500350 };
351
352
353/* Result structure for http_parser_parse_url().
354 *
355 * Callers should index into field_data[] with UF_* values iff field_set
356 * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
357 * because we probably have padding left over), we convert any port to
358 * a uint16_t.
359 */
360struct http_parser_url {
361 uint16_t field_set; /* Bitmask of (1 << UF_*) values */
362 uint16_t port; /* Converted UF_PORT string */
363
364 struct {
365 uint16_t off; /* Offset into buffer in which field starts */
366 uint16_t len; /* Length of run in buffer */
367 } field_data[UF_MAX];
368};
369
370
Ben Noordhuisd3264312012-11-15 17:05:40 +0100371/* Returns the library version. Bits 16-23 contain the major version number,
372 * bits 8-15 the minor version number and bits 0-7 the patch level.
373 * Usage example:
374 *
375 * unsigned long version = http_parser_version();
376 * unsigned major = (version >> 16) & 255;
377 * unsigned minor = (version >> 8) & 255;
378 * unsigned patch = version & 255;
Marc O'Morain11ecb422014-09-08 11:24:18 +0100379 * printf("http_parser v%u.%u.%u\n", major, minor, patch);
Ben Noordhuisd3264312012-11-15 17:05:40 +0100380 */
381unsigned long http_parser_version(void);
382
Ryan Dahl1a677042010-01-08 21:38:17 -0800383void http_parser_init(http_parser *parser, enum http_parser_type type);
Ryan Dahld37a9c52010-02-22 08:11:01 -0800384
Ryan Dahle07e0b92010-02-27 21:23:53 -0800385
Romain Giraud53063b72015-01-23 16:25:31 +0100386/* Initialize http_parser_settings members to 0
387 */
388void http_parser_settings_init(http_parser_settings *settings);
389
390
Maciej MaƂecki7bbb7742014-08-14 03:22:20 +0200391/* Executes the parser. Returns number of parsed bytes. Sets
392 * `parser->http_errno` on error. */
Ryan Dahldbd2dad2010-02-27 20:23:29 -0800393size_t http_parser_execute(http_parser *parser,
Cliff Frey72397882010-05-11 11:33:24 -0700394 const http_parser_settings *settings,
Ryan Dahldbd2dad2010-02-27 20:23:29 -0800395 const char *data,
396 size_t len);
Ryan Dahld37a9c52010-02-22 08:11:01 -0800397
Ryan Dahle07e0b92010-02-27 21:23:53 -0800398
Ryan Dahld37a9c52010-02-22 08:11:01 -0800399/* If http_should_keep_alive() in the on_headers_complete or
Ben Noordhuis2b7e87c2012-07-27 01:34:57 +0200400 * on_message_complete callback returns 0, then this should be
Ryan Dahld37a9c52010-02-22 08:11:01 -0800401 * the last message on the connection.
402 * If you are the server, respond with the "Connection: close" header.
403 * If you are the client, close the connection.
Ryan Dahl5b00b6a2009-11-20 15:36:55 +0100404 */
Ben Noordhuis9f923472012-08-29 23:13:04 +0200405int http_should_keep_alive(const http_parser *parser);
Ryan97d45722009-04-25 13:33:16 +0200406
Ryan Dahlddbd5c32010-06-20 13:21:36 -0700407/* Returns a string version of the HTTP method. */
Peter Griess9114e582011-05-23 14:09:19 -0500408const char *http_method_str(enum http_method m);
409
Stefano Sambi2ed75272018-05-23 15:30:53 +0200410/* Returns a string version of the HTTP status code. */
411const char *http_status_str(enum http_status s);
412
Peter Griess9114e582011-05-23 14:09:19 -0500413/* Return a string name of the given error */
414const char *http_errno_name(enum http_errno err);
415
416/* Return a string description of the given error */
417const char *http_errno_description(enum http_errno err);
Ryan Dahle07e0b92010-02-27 21:23:53 -0800418
Fedor Indutny777ba4e2015-02-24 12:50:20 +0300419/* Initialize all http_parser_url members to 0 */
420void http_parser_url_init(struct http_parser_url *u);
421
Peter Griessd7675cd2011-07-21 18:36:56 -0500422/* Parse a URL; return nonzero on failure */
423int http_parser_parse_url(const char *buf, size_t buflen,
424 int is_connect,
425 struct http_parser_url *u);
426
Peter Griessd0bb8672012-01-06 18:13:41 -0600427/* Pause or un-pause the parser; a nonzero value pauses */
428void http_parser_pause(http_parser *parser, int paused);
429
BogDan Vatra1ca7de52012-08-31 09:07:55 +0300430/* Checks if this is the final chunk of the body. */
431int http_body_is_final(const http_parser *parser);
432
cjihrig0ae8d932018-12-03 09:35:31 -0500433/* Change the maximum header size provided at compile time. */
434void http_parser_set_max_header_size(uint32_t size);
435
Ryan97d45722009-04-25 13:33:16 +0200436#ifdef __cplusplus
437}
Ryane244dbf2009-08-20 12:45:41 +0200438#endif
Ryan97d45722009-04-25 13:33:16 +0200439#endif