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 9 30 #define HTTP_PARSER_VERSION_PATCH 4 31 32 #include <stddef.h> 33 #if defined(_WIN32) && !defined(__MINGW32__) && \ 34 (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) 35 #include <BaseTsd.h> 36 typedef __int8 int8_t; 37 typedef unsigned __int8 uint8_t; 38 typedef __int16 int16_t; 39 typedef unsigned __int16 uint16_t; 40 typedef __int32 int32_t; 41 typedef unsigned __int32 uint32_t; 42 typedef __int64 int64_t; 43 typedef unsigned __int64 uint64_t; 44 #elif (defined(__sun) || defined(__sun__)) && defined(__SunOS_5_9) 45 #include <sys/inttypes.h> 46 #else 47 #include <stdint.h> 48 #endif 49 50 /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run 51 * faster 52 */ 53 #ifndef HTTP_PARSER_STRICT 54 # define HTTP_PARSER_STRICT 1 55 #endif 56 57 /* Maximium header size allowed. If the macro is not defined 58 * before including this header then the default is used. To 59 * change the maximum header size, define the macro in the build 60 * environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove 61 * the effective limit on the size of the header, define the macro 62 * to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff) 63 */ 64 #ifndef HTTP_MAX_HEADER_SIZE 65 # define HTTP_MAX_HEADER_SIZE (80*1024) 66 #endif 67 68 typedef struct http_parser http_parser; 69 typedef struct http_parser_settings http_parser_settings; 70 71 72 /* Callbacks should return non-zero to indicate an error. The parser will 73 * then halt execution. 74 * 75 * The one exception is on_headers_complete. In a HTTP_RESPONSE parser 76 * returning '1' from on_headers_complete will tell the parser that it 77 * should not expect a body. This is used when receiving a response to a 78 * HEAD request which may contain 'Content-Length' or 'Transfer-Encoding: 79 * chunked' headers that indicate the presence of a body. 80 * 81 * Returning `2` from on_headers_complete will tell parser that it should not 82 * expect neither a body nor any futher responses on this connection. This is 83 * useful for handling responses to a CONNECT request which may not contain 84 * `Upgrade` or `Connection: upgrade` headers. 85 * 86 * http_data_cb does not return data chunks. It will be called arbitrarily 87 * many times for each string. E.G. you might get 10 callbacks for "on_url" 88 * each providing just a few characters more data. 89 */ 90 typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); 91 typedef int (*http_cb) (http_parser*); 92 93 94 /* Status Codes */ 95 #define HTTP_STATUS_MAP(XX) \ 96 XX(100, CONTINUE, Continue) \ 97 XX(101, SWITCHING_PROTOCOLS, Switching Protocols) \ 98 XX(102, PROCESSING, Processing) \ 99 XX(200, OK, OK) \ 100 XX(201, CREATED, Created) \ 101 XX(202, ACCEPTED, Accepted) \ 102 XX(203, NON_AUTHORITATIVE_INFORMATION, Non-Authoritative Information) \ 103 XX(204, NO_CONTENT, No Content) \ 104 XX(205, RESET_CONTENT, Reset Content) \ 105 XX(206, PARTIAL_CONTENT, Partial Content) \ 106 XX(207, MULTI_STATUS, Multi-Status) \ 107 XX(208, ALREADY_REPORTED, Already Reported) \ 108 XX(226, IM_USED, IM Used) \ 109 XX(300, MULTIPLE_CHOICES, Multiple Choices) \ 110 XX(301, MOVED_PERMANENTLY, Moved Permanently) \ 111 XX(302, FOUND, Found) \ 112 XX(303, SEE_OTHER, See Other) \ 113 XX(304, NOT_MODIFIED, Not Modified) \ 114 XX(305, USE_PROXY, Use Proxy) \ 115 XX(307, TEMPORARY_REDIRECT, Temporary Redirect) \ 116 XX(308, PERMANENT_REDIRECT, Permanent Redirect) \ 117 XX(400, BAD_REQUEST, Bad Request) \ 118 XX(401, UNAUTHORIZED, Unauthorized) \ 119 XX(402, PAYMENT_REQUIRED, Payment Required) \ 120 XX(403, FORBIDDEN, Forbidden) \ 121 XX(404, NOT_FOUND, Not Found) \ 122 XX(405, METHOD_NOT_ALLOWED, Method Not Allowed) \ 123 XX(406, NOT_ACCEPTABLE, Not Acceptable) \ 124 XX(407, PROXY_AUTHENTICATION_REQUIRED, Proxy Authentication Required) \ 125 XX(408, REQUEST_TIMEOUT, Request Timeout) \ 126 XX(409, CONFLICT, Conflict) \ 127 XX(410, GONE, Gone) \ 128 XX(411, LENGTH_REQUIRED, Length Required) \ 129 XX(412, PRECONDITION_FAILED, Precondition Failed) \ 130 XX(413, PAYLOAD_TOO_LARGE, Payload Too Large) \ 131 XX(414, URI_TOO_LONG, URI Too Long) \ 132 XX(415, UNSUPPORTED_MEDIA_TYPE, Unsupported Media Type) \ 133 XX(416, RANGE_NOT_SATISFIABLE, Range Not Satisfiable) \ 134 XX(417, EXPECTATION_FAILED, Expectation Failed) \ 135 XX(421, MISDIRECTED_REQUEST, Misdirected Request) \ 136 XX(422, UNPROCESSABLE_ENTITY, Unprocessable Entity) \ 137 XX(423, LOCKED, Locked) \ 138 XX(424, FAILED_DEPENDENCY, Failed Dependency) \ 139 XX(426, UPGRADE_REQUIRED, Upgrade Required) \ 140 XX(428, PRECONDITION_REQUIRED, Precondition Required) \ 141 XX(429, TOO_MANY_REQUESTS, Too Many Requests) \ 142 XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, Request Header Fields Too Large) \ 143 XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, Unavailable For Legal Reasons) \ 144 XX(500, INTERNAL_SERVER_ERROR, Internal Server Error) \ 145 XX(501, NOT_IMPLEMENTED, Not Implemented) \ 146 XX(502, BAD_GATEWAY, Bad Gateway) \ 147 XX(503, SERVICE_UNAVAILABLE, Service Unavailable) \ 148 XX(504, GATEWAY_TIMEOUT, Gateway Timeout) \ 149 XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP Version Not Supported) \ 150 XX(506, VARIANT_ALSO_NEGOTIATES, Variant Also Negotiates) \ 151 XX(507, INSUFFICIENT_STORAGE, Insufficient Storage) \ 152 XX(508, LOOP_DETECTED, Loop Detected) \ 153 XX(510, NOT_EXTENDED, Not Extended) \ 154 XX(511, NETWORK_AUTHENTICATION_REQUIRED, Network Authentication Required) \ 155 156 enum http_status 157 { 158 #define XX(num, name, string) HTTP_STATUS_##name = num, 159 HTTP_STATUS_MAP(XX) 160 #undef XX 161 }; 162 163 164 /* Request Methods */ 165 #define HTTP_METHOD_MAP(XX) \ 166 XX(0, DELETE, DELETE) \ 167 XX(1, GET, GET) \ 168 XX(2, HEAD, HEAD) \ 169 XX(3, POST, POST) \ 170 XX(4, PUT, PUT) \ 171 /* pathological */ \ 172 XX(5, CONNECT, CONNECT) \ 173 XX(6, OPTIONS, OPTIONS) \ 174 XX(7, TRACE, TRACE) \ 175 /* WebDAV */ \ 176 XX(8, COPY, COPY) \ 177 XX(9, LOCK, LOCK) \ 178 XX(10, MKCOL, MKCOL) \ 179 XX(11, MOVE, MOVE) \ 180 XX(12, PROPFIND, PROPFIND) \ 181 XX(13, PROPPATCH, PROPPATCH) \ 182 XX(14, SEARCH, SEARCH) \ 183 XX(15, UNLOCK, UNLOCK) \ 184 XX(16, BIND, BIND) \ 185 XX(17, REBIND, REBIND) \ 186 XX(18, UNBIND, UNBIND) \ 187 XX(19, ACL, ACL) \ 188 /* subversion */ \ 189 XX(20, REPORT, REPORT) \ 190 XX(21, MKACTIVITY, MKACTIVITY) \ 191 XX(22, CHECKOUT, CHECKOUT) \ 192 XX(23, MERGE, MERGE) \ 193 /* upnp */ \ 194 XX(24, MSEARCH, M-SEARCH) \ 195 XX(25, NOTIFY, NOTIFY) \ 196 XX(26, SUBSCRIBE, SUBSCRIBE) \ 197 XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ 198 /* RFC-5789 */ \ 199 XX(28, PATCH, PATCH) \ 200 XX(29, PURGE, PURGE) \ 201 /* CalDAV */ \ 202 XX(30, MKCALENDAR, MKCALENDAR) \ 203 /* RFC-2068, section 19.6.1.2 */ \ 204 XX(31, LINK, LINK) \ 205 XX(32, UNLINK, UNLINK) \ 206 /* icecast */ \ 207 XX(33, SOURCE, SOURCE) \ 208 209 enum http_method 210 { 211 #define XX(num, name, string) HTTP_##name = num, 212 HTTP_METHOD_MAP(XX) 213 #undef XX 214 }; 215 216 217 enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; 218 219 220 /* Flag values for http_parser.flags field */ 221 enum flags 222 { F_CHUNKED = 1 << 0 223 , F_CONNECTION_KEEP_ALIVE = 1 << 1 224 , F_CONNECTION_CLOSE = 1 << 2 225 , F_CONNECTION_UPGRADE = 1 << 3 226 , F_TRAILING = 1 << 4 227 , F_UPGRADE = 1 << 5 228 , F_SKIPBODY = 1 << 6 229 , F_CONTENTLENGTH = 1 << 7 230 }; 231 232 233 /* Map for errno-related constants 234 * 235 * The provided argument should be a macro that takes 2 arguments. 236 */ 237 #define HTTP_ERRNO_MAP(XX) \ 238 /* No error */ \ 239 XX(OK, "success") \ 240 \ 241 /* Callback-related errors */ \ 242 XX(CB_message_begin, "the on_message_begin callback failed") \ 243 XX(CB_url, "the on_url callback failed") \ 244 XX(CB_header_field, "the on_header_field callback failed") \ 245 XX(CB_header_value, "the on_header_value callback failed") \ 246 XX(CB_headers_complete, "the on_headers_complete callback failed") \ 247 XX(CB_body, "the on_body callback failed") \ 248 XX(CB_message_complete, "the on_message_complete callback failed") \ 249 XX(CB_status, "the on_status callback failed") \ 250 XX(CB_chunk_header, "the on_chunk_header callback failed") \ 251 XX(CB_chunk_complete, "the on_chunk_complete callback failed") \ 252 \ 253 /* Parsing-related errors */ \ 254 XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ 255 XX(HEADER_OVERFLOW, \ 256 "too many header bytes seen; overflow detected") \ 257 XX(CLOSED_CONNECTION, \ 258 "data received after completed connection: close message") \ 259 XX(INVALID_VERSION, "invalid HTTP version") \ 260 XX(INVALID_STATUS, "invalid HTTP status code") \ 261 XX(INVALID_METHOD, "invalid HTTP method") \ 262 XX(INVALID_URL, "invalid URL") \ 263 XX(INVALID_HOST, "invalid host") \ 264 XX(INVALID_PORT, "invalid port") \ 265 XX(INVALID_PATH, "invalid path") \ 266 XX(INVALID_QUERY_STRING, "invalid query string") \ 267 XX(INVALID_FRAGMENT, "invalid fragment") \ 268 XX(LF_EXPECTED, "LF character expected") \ 269 XX(INVALID_HEADER_TOKEN, "invalid character in header") \ 270 XX(INVALID_CONTENT_LENGTH, \ 271 "invalid character in content-length header") \ 272 XX(UNEXPECTED_CONTENT_LENGTH, \ 273 "unexpected content-length header") \ 274 XX(INVALID_CHUNK_SIZE, \ 275 "invalid character in chunk size header") \ 276 XX(INVALID_CONSTANT, "invalid constant string") \ 277 XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ 278 XX(STRICT, "strict mode assertion failed") \ 279 XX(PAUSED, "parser is paused") \ 280 XX(UNKNOWN, "an unknown error occurred") \ 281 XX(INVALID_TRANSFER_ENCODING, \ 282 "request has invalid transfer-encoding") \ 283 284 285 /* Define HPE_* values for each errno value above */ 286 #define HTTP_ERRNO_GEN(n, s) HPE_##n, 287 enum http_errno { 288 HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) 289 }; 290 #undef HTTP_ERRNO_GEN 291 292 293 /* Get an http_errno value from an http_parser */ 294 #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) 295 296 297 struct http_parser { 298 /** PRIVATE **/ 299 unsigned int type : 2; /* enum http_parser_type */ 300 unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */ 301 unsigned int state : 7; /* enum state from http_parser.c */ 302 unsigned int header_state : 7; /* enum header_state from http_parser.c */ 303 unsigned int index : 5; /* index into current matcher */ 304 unsigned int uses_transfer_encoding : 1; /* Transfer-Encoding header is present */ 305 unsigned int allow_chunked_length : 1; /* Allow headers with both 306 * `Content-Length` and 307 * `Transfer-Encoding: chunked` set */ 308 unsigned int lenient_http_headers : 1; 309 310 uint32_t nread; /* # bytes read in various scenarios */ 311 uint64_t content_length; /* # bytes in body. `(uint64_t) -1` (all bits one) 312 * if no Content-Length header. 313 */ 314 315 /** READ-ONLY **/ 316 unsigned short http_major; 317 unsigned short http_minor; 318 unsigned int status_code : 16; /* responses only */ 319 unsigned int method : 8; /* requests only */ 320 unsigned int http_errno : 7; 321 322 /* 1 = Upgrade header was present and the parser has exited because of that. 323 * 0 = No upgrade header present. 324 * Should be checked when http_parser_execute() returns in addition to 325 * error checking. 326 */ 327 unsigned int upgrade : 1; 328 329 /** PUBLIC **/ 330 void *data; /* A pointer to get hook to the "connection" or "socket" object */ 331 }; 332 333 334 struct http_parser_settings { 335 http_cb on_message_begin; 336 http_data_cb on_url; 337 http_data_cb on_status; 338 http_data_cb on_header_field; 339 http_data_cb on_header_value; 340 http_cb on_headers_complete; 341 http_data_cb on_body; 342 http_cb on_message_complete; 343 /* When on_chunk_header is called, the current chunk length is stored 344 * in parser->content_length. 345 */ 346 http_cb on_chunk_header; 347 http_cb on_chunk_complete; 348 }; 349 350 351 enum http_parser_url_fields 352 { UF_SCHEMA = 0 353 , UF_HOST = 1 354 , UF_PORT = 2 355 , UF_PATH = 3 356 , UF_QUERY = 4 357 , UF_FRAGMENT = 5 358 , UF_USERINFO = 6 359 , UF_MAX = 7 360 }; 361 362 363 /* Result structure for http_parser_parse_url(). 364 * 365 * Callers should index into field_data[] with UF_* values iff field_set 366 * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and 367 * because we probably have padding left over), we convert any port to 368 * a uint16_t. 369 */ 370 struct http_parser_url { 371 uint16_t field_set; /* Bitmask of (1 << UF_*) values */ 372 uint16_t port; /* Converted UF_PORT string */ 373 374 struct { 375 uint16_t off; /* Offset into buffer in which field starts */ 376 uint16_t len; /* Length of run in buffer */ 377 } field_data[UF_MAX]; 378 }; 379 380 381 /* Returns the library version. Bits 16-23 contain the major version number, 382 * bits 8-15 the minor version number and bits 0-7 the patch level. 383 * Usage example: 384 * 385 * unsigned long version = http_parser_version(); 386 * unsigned major = (version >> 16) & 255; 387 * unsigned minor = (version >> 8) & 255; 388 * unsigned patch = version & 255; 389 * printf("http_parser v%u.%u.%u\n", major, minor, patch); 390 */ 391 unsigned long http_parser_version(void); 392 393 void http_parser_init(http_parser *parser, enum http_parser_type type); 394 395 396 /* Initialize http_parser_settings members to 0 397 */ 398 void http_parser_settings_init(http_parser_settings *settings); 399 400 401 /* Executes the parser. Returns number of parsed bytes. Sets 402 * `parser->http_errno` on error. */ 403 size_t http_parser_execute(http_parser *parser, 404 const http_parser_settings *settings, 405 const char *data, 406 size_t len); 407 408 409 /* If http_should_keep_alive() in the on_headers_complete or 410 * on_message_complete callback returns 0, then this should be 411 * the last message on the connection. 412 * If you are the server, respond with the "Connection: close" header. 413 * If you are the client, close the connection. 414 */ 415 int http_should_keep_alive(const http_parser *parser); 416 417 /* Returns a string version of the HTTP method. */ 418 const char *http_method_str(enum http_method m); 419 420 /* Returns a string version of the HTTP status code. */ 421 const char *http_status_str(enum http_status s); 422 423 /* Return a string name of the given error */ 424 const char *http_errno_name(enum http_errno err); 425 426 /* Return a string description of the given error */ 427 const char *http_errno_description(enum http_errno err); 428 429 /* Initialize all http_parser_url members to 0 */ 430 void http_parser_url_init(struct http_parser_url *u); 431 432 /* Parse a URL; return nonzero on failure */ 433 int http_parser_parse_url(const char *buf, size_t buflen, 434 int is_connect, 435 struct http_parser_url *u); 436 437 /* Pause or un-pause the parser; a nonzero value pauses */ 438 void http_parser_pause(http_parser *parser, int paused); 439 440 /* Checks if this is the final chunk of the body. */ 441 int http_body_is_final(const http_parser *parser); 442 443 /* Change the maximum header size provided at compile time. */ 444 void http_parser_set_max_header_size(uint32_t size); 445 446 #ifdef __cplusplus 447 } 448 #endif 449 #endif 450