Ryan Dahl | 63daf22 | 2011-03-22 13:29:58 -0700 | [diff] [blame] | 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. |
Ryan Dahl | 79947a7 | 2010-01-06 18:54:51 -0800 | [diff] [blame] | 2 | * |
Ryan Dahl | b283cd9 | 2009-11-19 21:11:06 +0100 | [diff] [blame] | 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: |
Ryan Dahl | 79947a7 | 2010-01-06 18:54:51 -0800 | [diff] [blame] | 9 | * |
Ryan Dahl | b283cd9 | 2009-11-19 21:11:06 +0100 | [diff] [blame] | 10 | * The above copyright notice and this permission notice shall be included in |
| 11 | * all copies or substantial portions of the Software. |
Ryan Dahl | 79947a7 | 2010-01-06 18:54:51 -0800 | [diff] [blame] | 12 | * |
Ryan Dahl | b283cd9 | 2009-11-19 21:11:06 +0100 | [diff] [blame] | 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 |
Ryan Dahl | 79947a7 | 2010-01-06 18:54:51 -0800 | [diff] [blame] | 19 | * IN THE SOFTWARE. |
Ryan Dahl | b283cd9 | 2009-11-19 21:11:06 +0100 | [diff] [blame] | 20 | */ |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 21 | #ifndef http_parser_h |
| 22 | #define http_parser_h |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
Ryan | e244dbf | 2009-08-20 12:45:41 +0200 | [diff] [blame] | 25 | #endif |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 26 | |
Ben Noordhuis | 6df37aa | 2013-06-13 19:06:41 +0200 | [diff] [blame] | 27 | /* Also update SONAME in the Makefile whenever you change these. */ |
Ben Noordhuis | 3fb4e06 | 2012-10-10 22:38:20 +0200 | [diff] [blame] | 28 | #define HTTP_PARSER_VERSION_MAJOR 2 |
Fedor Indutny | 9b0d5b3 | 2016-04-19 10:41:48 -0400 | [diff] [blame^] | 29 | #define HTTP_PARSER_VERSION_MINOR 7 |
| 30 | #define HTTP_PARSER_VERSION_PATCH 0 |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 31 | |
Ryan | e244dbf | 2009-08-20 12:45:41 +0200 | [diff] [blame] | 32 | #include <sys/types.h> |
Andrew Udvare | b36c2a9 | 2015-07-11 23:56:25 -0700 | [diff] [blame] | 33 | #if defined(_WIN32) && !defined(__MINGW32__) && \ |
| 34 | (!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__) |
James McLaughlin | 8ee3b0d | 2012-07-14 12:38:56 +0100 | [diff] [blame] | 35 | #include <BaseTsd.h> |
James McLaughlin | 2b77aba | 2012-12-10 18:18:36 +0000 | [diff] [blame] | 36 | #include <stddef.h> |
Ewen Cheslack-Postava | 24be793 | 2010-11-03 00:51:37 -0700 | [diff] [blame] | 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; |
Ewen Cheslack-Postava | 24be793 | 2010-11-03 00:51:37 -0700 | [diff] [blame] | 45 | #else |
| 46 | #include <stdint.h> |
Ewen Cheslack-Postava | 4afe80a | 2010-06-27 23:14:29 -0700 | [diff] [blame] | 47 | #endif |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 48 | |
Ryan Dahl | fb6dc67 | 2009-11-20 14:24:05 +0100 | [diff] [blame] | 49 | /* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run |
Ryan Dahl | 79947a7 | 2010-01-06 18:54:51 -0800 | [diff] [blame] | 50 | * faster |
| 51 | */ |
Ryan Dahl | fb6dc67 | 2009-11-20 14:24:05 +0100 | [diff] [blame] | 52 | #ifndef HTTP_PARSER_STRICT |
| 53 | # define HTTP_PARSER_STRICT 1 |
Ryan Dahl | fb6dc67 | 2009-11-20 14:24:05 +0100 | [diff] [blame] | 54 | #endif |
| 55 | |
Vinnie Falco | 24e2d2d | 2014-03-15 19:17:48 -0700 | [diff] [blame] | 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 |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 66 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 67 | typedef struct http_parser http_parser; |
Ryan Dahl | dbd2dad | 2010-02-27 20:23:29 -0800 | [diff] [blame] | 68 | typedef struct http_parser_settings http_parser_settings; |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 69 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 70 | |
Ryan Dahl | d37a9c5 | 2010-02-22 08:11:01 -0800 | [diff] [blame] | 71 | /* Callbacks should return non-zero to indicate an error. The parser will |
Ryan | e244dbf | 2009-08-20 12:45:41 +0200 | [diff] [blame] | 72 | * then halt execution. |
| 73 | * |
Ryan Dahl | c2acc21 | 2010-05-25 16:25:26 -0700 | [diff] [blame] | 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 | * |
Fedor Indutny | 04d28a7 | 2016-04-17 00:14:00 +0200 | [diff] [blame] | 80 | * Returning `2` from on_headers_complete will tell parser that it should not |
| 81 | * expect neither a body nor any futher responses on this connection. This is |
| 82 | * useful for handling responses to a CONNECT request which may not contain |
| 83 | * `Upgrade` or `Connection: upgrade` headers. |
| 84 | * |
George Miroshnykov | 17ed7de | 2014-08-21 12:42:10 +0300 | [diff] [blame] | 85 | * http_data_cb does not return data chunks. It will be called arbitrarily |
Peter Faiman | b4fc777 | 2012-10-28 13:23:13 -0700 | [diff] [blame] | 86 | * many times for each string. E.G. you might get 10 callbacks for "on_url" |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 87 | * each providing just a few characters more data. |
| 88 | */ |
| 89 | typedef int (*http_data_cb) (http_parser*, const char *at, size_t length); |
| 90 | typedef int (*http_cb) (http_parser*); |
| 91 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 92 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 93 | /* Request Methods */ |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 94 | #define HTTP_METHOD_MAP(XX) \ |
| 95 | XX(0, DELETE, DELETE) \ |
| 96 | XX(1, GET, GET) \ |
| 97 | XX(2, HEAD, HEAD) \ |
| 98 | XX(3, POST, POST) \ |
| 99 | XX(4, PUT, PUT) \ |
| 100 | /* pathological */ \ |
| 101 | XX(5, CONNECT, CONNECT) \ |
| 102 | XX(6, OPTIONS, OPTIONS) \ |
| 103 | XX(7, TRACE, TRACE) \ |
Ivan Enderlin | 8b1d652 | 2015-04-24 17:36:26 +0200 | [diff] [blame] | 104 | /* WebDAV */ \ |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 105 | XX(8, COPY, COPY) \ |
| 106 | XX(9, LOCK, LOCK) \ |
| 107 | XX(10, MKCOL, MKCOL) \ |
| 108 | XX(11, MOVE, MOVE) \ |
| 109 | XX(12, PROPFIND, PROPFIND) \ |
| 110 | XX(13, PROPPATCH, PROPPATCH) \ |
| 111 | XX(14, SEARCH, SEARCH) \ |
| 112 | XX(15, UNLOCK, UNLOCK) \ |
Ivan Enderlin | 8b1d652 | 2015-04-24 17:36:26 +0200 | [diff] [blame] | 113 | XX(16, BIND, BIND) \ |
| 114 | XX(17, REBIND, REBIND) \ |
| 115 | XX(18, UNBIND, UNBIND) \ |
Ivan Enderlin | eb5e992 | 2015-04-24 17:55:26 +0200 | [diff] [blame] | 116 | XX(19, ACL, ACL) \ |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 117 | /* subversion */ \ |
Ivan Enderlin | eb5e992 | 2015-04-24 17:55:26 +0200 | [diff] [blame] | 118 | XX(20, REPORT, REPORT) \ |
| 119 | XX(21, MKACTIVITY, MKACTIVITY) \ |
| 120 | XX(22, CHECKOUT, CHECKOUT) \ |
| 121 | XX(23, MERGE, MERGE) \ |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 122 | /* upnp */ \ |
Ivan Enderlin | eb5e992 | 2015-04-24 17:55:26 +0200 | [diff] [blame] | 123 | XX(24, MSEARCH, M-SEARCH) \ |
| 124 | XX(25, NOTIFY, NOTIFY) \ |
| 125 | XX(26, SUBSCRIBE, SUBSCRIBE) \ |
| 126 | XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \ |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 127 | /* RFC-5789 */ \ |
Ivan Enderlin | eb5e992 | 2015-04-24 17:55:26 +0200 | [diff] [blame] | 128 | XX(28, PATCH, PATCH) \ |
| 129 | XX(29, PURGE, PURGE) \ |
Helge HeĂ | 1317eec | 2014-06-24 11:50:13 +0200 | [diff] [blame] | 130 | /* CalDAV */ \ |
Ivan Enderlin | eb5e992 | 2015-04-24 17:55:26 +0200 | [diff] [blame] | 131 | XX(30, MKCALENDAR, MKCALENDAR) \ |
Olivier Mengué | e557b62 | 2015-10-12 19:37:28 +0200 | [diff] [blame] | 132 | /* RFC-2068, section 19.6.1.2 */ \ |
| 133 | XX(31, LINK, LINK) \ |
| 134 | XX(32, UNLINK, UNLINK) \ |
Ben Noordhuis | 36808f4 | 2012-02-27 23:35:47 +0100 | [diff] [blame] | 135 | |
Ryan Dahl | ce38189 | 2009-10-27 13:50:54 +0100 | [diff] [blame] | 136 | enum http_method |
Ben Noordhuis | 36808f4 | 2012-02-27 23:35:47 +0100 | [diff] [blame] | 137 | { |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 138 | #define XX(num, name, string) HTTP_##name = num, |
Ben Noordhuis | 36808f4 | 2012-02-27 23:35:47 +0100 | [diff] [blame] | 139 | HTTP_METHOD_MAP(XX) |
Ben Noordhuis | 8bec3ea | 2012-03-12 02:13:51 +0100 | [diff] [blame] | 140 | #undef XX |
Ryan Dahl | ce38189 | 2009-10-27 13:50:54 +0100 | [diff] [blame] | 141 | }; |
Ryan | c288a96 | 2009-04-26 20:52:53 +0200 | [diff] [blame] | 142 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 143 | |
Cliff Frey | b8c3336 | 2010-05-26 19:30:13 -0700 | [diff] [blame] | 144 | enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH }; |
Ryan Dahl | 1a67704 | 2010-01-08 21:38:17 -0800 | [diff] [blame] | 145 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 146 | |
Peter Griess | b1c2cf8 | 2011-05-23 11:42:13 -0500 | [diff] [blame] | 147 | /* Flag values for http_parser.flags field */ |
| 148 | enum flags |
| 149 | { F_CHUNKED = 1 << 0 |
| 150 | , F_CONNECTION_KEEP_ALIVE = 1 << 1 |
| 151 | , F_CONNECTION_CLOSE = 1 << 2 |
Jon Kolb | 091ebb8 | 2012-03-01 19:09:42 -0500 | [diff] [blame] | 152 | , F_CONNECTION_UPGRADE = 1 << 3 |
| 153 | , F_TRAILING = 1 << 4 |
| 154 | , F_UPGRADE = 1 << 5 |
| 155 | , F_SKIPBODY = 1 << 6 |
James M Snell | e2e467b | 2016-02-10 11:33:49 +1100 | [diff] [blame] | 156 | , F_CONTENTLENGTH = 1 << 7 |
Peter Griess | b1c2cf8 | 2011-05-23 11:42:13 -0500 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 160 | /* Map for errno-related constants |
James M Snell | bee4817 | 2015-10-28 21:47:12 -0700 | [diff] [blame] | 161 | * |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 162 | * The provided argument should be a macro that takes 2 arguments. |
| 163 | */ |
| 164 | #define HTTP_ERRNO_MAP(XX) \ |
| 165 | /* No error */ \ |
| 166 | XX(OK, "success") \ |
| 167 | \ |
| 168 | /* Callback-related errors */ \ |
| 169 | XX(CB_message_begin, "the on_message_begin callback failed") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 170 | XX(CB_url, "the on_url callback failed") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 171 | XX(CB_header_field, "the on_header_field callback failed") \ |
| 172 | XX(CB_header_value, "the on_header_value callback failed") \ |
| 173 | XX(CB_headers_complete, "the on_headers_complete callback failed") \ |
Peter Griess | 761a5ea | 2011-07-09 11:47:34 -0500 | [diff] [blame] | 174 | XX(CB_body, "the on_body callback failed") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 175 | XX(CB_message_complete, "the on_message_complete callback failed") \ |
Patrik Stutz | d7b938b | 2013-01-17 08:38:34 -0600 | [diff] [blame] | 176 | XX(CB_status, "the on_status callback failed") \ |
Kjell Schubert | d767545 | 2015-04-17 17:36:02 -0700 | [diff] [blame] | 177 | XX(CB_chunk_header, "the on_chunk_header callback failed") \ |
| 178 | XX(CB_chunk_complete, "the on_chunk_complete callback failed") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 179 | \ |
| 180 | /* Parsing-related errors */ \ |
| 181 | XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \ |
| 182 | XX(HEADER_OVERFLOW, \ |
| 183 | "too many header bytes seen; overflow detected") \ |
| 184 | XX(CLOSED_CONNECTION, \ |
| 185 | "data received after completed connection: close message") \ |
| 186 | XX(INVALID_VERSION, "invalid HTTP version") \ |
| 187 | XX(INVALID_STATUS, "invalid HTTP status code") \ |
| 188 | XX(INVALID_METHOD, "invalid HTTP method") \ |
| 189 | XX(INVALID_URL, "invalid URL") \ |
| 190 | XX(INVALID_HOST, "invalid host") \ |
| 191 | XX(INVALID_PORT, "invalid port") \ |
| 192 | XX(INVALID_PATH, "invalid path") \ |
| 193 | XX(INVALID_QUERY_STRING, "invalid query string") \ |
| 194 | XX(INVALID_FRAGMENT, "invalid fragment") \ |
| 195 | XX(LF_EXPECTED, "LF character expected") \ |
| 196 | XX(INVALID_HEADER_TOKEN, "invalid character in header") \ |
| 197 | XX(INVALID_CONTENT_LENGTH, \ |
| 198 | "invalid character in content-length header") \ |
James M Snell | e2e467b | 2016-02-10 11:33:49 +1100 | [diff] [blame] | 199 | XX(UNEXPECTED_CONTENT_LENGTH, \ |
| 200 | "unexpected content-length header") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 201 | XX(INVALID_CHUNK_SIZE, \ |
| 202 | "invalid character in chunk size header") \ |
| 203 | XX(INVALID_CONSTANT, "invalid constant string") \ |
| 204 | XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\ |
| 205 | XX(STRICT, "strict mode assertion failed") \ |
Peter Griess | d0bb867 | 2012-01-06 18:13:41 -0600 | [diff] [blame] | 206 | XX(PAUSED, "parser is paused") \ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 207 | XX(UNKNOWN, "an unknown error occurred") |
| 208 | |
| 209 | |
| 210 | /* Define HPE_* values for each errno value above */ |
| 211 | #define HTTP_ERRNO_GEN(n, s) HPE_##n, |
| 212 | enum http_errno { |
| 213 | HTTP_ERRNO_MAP(HTTP_ERRNO_GEN) |
| 214 | }; |
| 215 | #undef HTTP_ERRNO_GEN |
| 216 | |
| 217 | |
| 218 | /* Get an http_errno value from an http_parser */ |
Fouad Mardini | 2b2ba2d | 2011-07-24 18:49:54 +0300 | [diff] [blame] | 219 | #define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno) |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 220 | |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 221 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 222 | struct http_parser { |
| 223 | /** PRIVATE **/ |
Charlie Somerville | 11419c8 | 2013-11-19 01:53:05 -0800 | [diff] [blame] | 224 | unsigned int type : 2; /* enum http_parser_type */ |
James M Snell | e2e467b | 2016-02-10 11:33:49 +1100 | [diff] [blame] | 225 | unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */ |
Fedor Indutny | 7ba3123 | 2015-02-15 17:44:48 +0100 | [diff] [blame] | 226 | unsigned int state : 7; /* enum state from http_parser.c */ |
James M Snell | e2e467b | 2016-02-10 11:33:49 +1100 | [diff] [blame] | 227 | unsigned int header_state : 7; /* enum header_state from http_parser.c */ |
| 228 | unsigned int index : 7; /* index into current matcher */ |
| 229 | unsigned int lenient_http_headers : 1; |
Ryan Dahl | 88d11b3 | 2010-04-14 03:17:11 -0700 | [diff] [blame] | 230 | |
Peter Griess | d0bb867 | 2012-01-06 18:13:41 -0600 | [diff] [blame] | 231 | uint32_t nread; /* # bytes read in various scenarios */ |
Ben Noordhuis | f668e72 | 2012-01-22 01:20:35 +0100 | [diff] [blame] | 232 | uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */ |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 233 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 234 | /** READ-ONLY **/ |
Ryan Dahl | fb6dc67 | 2009-11-20 14:24:05 +0100 | [diff] [blame] | 235 | unsigned short http_major; |
| 236 | unsigned short http_minor; |
Charlie Somerville | 11419c8 | 2013-11-19 01:53:05 -0800 | [diff] [blame] | 237 | unsigned int status_code : 16; /* responses only */ |
| 238 | unsigned int method : 8; /* requests only */ |
| 239 | unsigned int http_errno : 7; |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 240 | |
Cliff Frey | 5502cdd | 2010-05-27 15:04:24 -0700 | [diff] [blame] | 241 | /* 1 = Upgrade header was present and the parser has exited because of that. |
| 242 | * 0 = No upgrade header present. |
| 243 | * Should be checked when http_parser_execute() returns in addition to |
| 244 | * error checking. |
| 245 | */ |
Charlie Somerville | 11419c8 | 2013-11-19 01:53:05 -0800 | [diff] [blame] | 246 | unsigned int upgrade : 1; |
Cliff Frey | 5502cdd | 2010-05-27 15:04:24 -0700 | [diff] [blame] | 247 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 248 | /** PUBLIC **/ |
| 249 | void *data; /* A pointer to get hook to the "connection" or "socket" object */ |
Ryan Dahl | dbd2dad | 2010-02-27 20:23:29 -0800 | [diff] [blame] | 250 | }; |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 251 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 252 | |
Ryan Dahl | dbd2dad | 2010-02-27 20:23:29 -0800 | [diff] [blame] | 253 | struct http_parser_settings { |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 254 | http_cb on_message_begin; |
Ryan Dahl | 3834853 | 2009-11-19 21:10:39 +0100 | [diff] [blame] | 255 | http_data_cb on_url; |
Patrik Stutz | d7b938b | 2013-01-17 08:38:34 -0600 | [diff] [blame] | 256 | http_data_cb on_status; |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 257 | http_data_cb on_header_field; |
| 258 | http_data_cb on_header_value; |
| 259 | http_cb on_headers_complete; |
| 260 | http_data_cb on_body; |
| 261 | http_cb on_message_complete; |
Kjell Schubert | d767545 | 2015-04-17 17:36:02 -0700 | [diff] [blame] | 262 | /* When on_chunk_header is called, the current chunk length is stored |
| 263 | * in parser->content_length. |
| 264 | */ |
| 265 | http_cb on_chunk_header; |
| 266 | http_cb on_chunk_complete; |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 267 | }; |
| 268 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 269 | |
Peter Griess | d7675cd | 2011-07-21 18:36:56 -0500 | [diff] [blame] | 270 | enum http_parser_url_fields |
| 271 | { UF_SCHEMA = 0 |
| 272 | , UF_HOST = 1 |
| 273 | , UF_PORT = 2 |
| 274 | , UF_PATH = 3 |
| 275 | , UF_QUERY = 4 |
| 276 | , UF_FRAGMENT = 5 |
Bertrand Paquet | 7965096 | 2012-07-08 01:44:31 +0200 | [diff] [blame] | 277 | , UF_USERINFO = 6 |
| 278 | , UF_MAX = 7 |
Peter Griess | d7675cd | 2011-07-21 18:36:56 -0500 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | |
| 282 | /* Result structure for http_parser_parse_url(). |
| 283 | * |
| 284 | * Callers should index into field_data[] with UF_* values iff field_set |
| 285 | * has the relevant (1 << UF_*) bit set. As a courtesy to clients (and |
| 286 | * because we probably have padding left over), we convert any port to |
| 287 | * a uint16_t. |
| 288 | */ |
| 289 | struct http_parser_url { |
| 290 | uint16_t field_set; /* Bitmask of (1 << UF_*) values */ |
| 291 | uint16_t port; /* Converted UF_PORT string */ |
| 292 | |
| 293 | struct { |
| 294 | uint16_t off; /* Offset into buffer in which field starts */ |
| 295 | uint16_t len; /* Length of run in buffer */ |
| 296 | } field_data[UF_MAX]; |
| 297 | }; |
| 298 | |
| 299 | |
Ben Noordhuis | d326431 | 2012-11-15 17:05:40 +0100 | [diff] [blame] | 300 | /* Returns the library version. Bits 16-23 contain the major version number, |
| 301 | * bits 8-15 the minor version number and bits 0-7 the patch level. |
| 302 | * Usage example: |
| 303 | * |
| 304 | * unsigned long version = http_parser_version(); |
| 305 | * unsigned major = (version >> 16) & 255; |
| 306 | * unsigned minor = (version >> 8) & 255; |
| 307 | * unsigned patch = version & 255; |
Marc O'Morain | 11ecb42 | 2014-09-08 11:24:18 +0100 | [diff] [blame] | 308 | * printf("http_parser v%u.%u.%u\n", major, minor, patch); |
Ben Noordhuis | d326431 | 2012-11-15 17:05:40 +0100 | [diff] [blame] | 309 | */ |
| 310 | unsigned long http_parser_version(void); |
| 311 | |
Ryan Dahl | 1a67704 | 2010-01-08 21:38:17 -0800 | [diff] [blame] | 312 | void http_parser_init(http_parser *parser, enum http_parser_type type); |
Ryan Dahl | d37a9c5 | 2010-02-22 08:11:01 -0800 | [diff] [blame] | 313 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 314 | |
Romain Giraud | 53063b7 | 2015-01-23 16:25:31 +0100 | [diff] [blame] | 315 | /* Initialize http_parser_settings members to 0 |
| 316 | */ |
| 317 | void http_parser_settings_init(http_parser_settings *settings); |
| 318 | |
| 319 | |
Maciej MaĆecki | 7bbb774 | 2014-08-14 03:22:20 +0200 | [diff] [blame] | 320 | /* Executes the parser. Returns number of parsed bytes. Sets |
| 321 | * `parser->http_errno` on error. */ |
Ryan Dahl | dbd2dad | 2010-02-27 20:23:29 -0800 | [diff] [blame] | 322 | size_t http_parser_execute(http_parser *parser, |
Cliff Frey | 7239788 | 2010-05-11 11:33:24 -0700 | [diff] [blame] | 323 | const http_parser_settings *settings, |
Ryan Dahl | dbd2dad | 2010-02-27 20:23:29 -0800 | [diff] [blame] | 324 | const char *data, |
| 325 | size_t len); |
Ryan Dahl | d37a9c5 | 2010-02-22 08:11:01 -0800 | [diff] [blame] | 326 | |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 327 | |
Ryan Dahl | d37a9c5 | 2010-02-22 08:11:01 -0800 | [diff] [blame] | 328 | /* If http_should_keep_alive() in the on_headers_complete or |
Ben Noordhuis | 2b7e87c | 2012-07-27 01:34:57 +0200 | [diff] [blame] | 329 | * on_message_complete callback returns 0, then this should be |
Ryan Dahl | d37a9c5 | 2010-02-22 08:11:01 -0800 | [diff] [blame] | 330 | * the last message on the connection. |
| 331 | * If you are the server, respond with the "Connection: close" header. |
| 332 | * If you are the client, close the connection. |
Ryan Dahl | 5b00b6a | 2009-11-20 15:36:55 +0100 | [diff] [blame] | 333 | */ |
Ben Noordhuis | 9f92347 | 2012-08-29 23:13:04 +0200 | [diff] [blame] | 334 | int http_should_keep_alive(const http_parser *parser); |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 335 | |
Ryan Dahl | ddbd5c3 | 2010-06-20 13:21:36 -0700 | [diff] [blame] | 336 | /* Returns a string version of the HTTP method. */ |
Peter Griess | 9114e58 | 2011-05-23 14:09:19 -0500 | [diff] [blame] | 337 | const char *http_method_str(enum http_method m); |
| 338 | |
| 339 | /* Return a string name of the given error */ |
| 340 | const char *http_errno_name(enum http_errno err); |
| 341 | |
| 342 | /* Return a string description of the given error */ |
| 343 | const char *http_errno_description(enum http_errno err); |
Ryan Dahl | e07e0b9 | 2010-02-27 21:23:53 -0800 | [diff] [blame] | 344 | |
Fedor Indutny | 777ba4e | 2015-02-24 12:50:20 +0300 | [diff] [blame] | 345 | /* Initialize all http_parser_url members to 0 */ |
| 346 | void http_parser_url_init(struct http_parser_url *u); |
| 347 | |
Peter Griess | d7675cd | 2011-07-21 18:36:56 -0500 | [diff] [blame] | 348 | /* Parse a URL; return nonzero on failure */ |
| 349 | int http_parser_parse_url(const char *buf, size_t buflen, |
| 350 | int is_connect, |
| 351 | struct http_parser_url *u); |
| 352 | |
Peter Griess | d0bb867 | 2012-01-06 18:13:41 -0600 | [diff] [blame] | 353 | /* Pause or un-pause the parser; a nonzero value pauses */ |
| 354 | void http_parser_pause(http_parser *parser, int paused); |
| 355 | |
BogDan Vatra | 1ca7de5 | 2012-08-31 09:07:55 +0300 | [diff] [blame] | 356 | /* Checks if this is the final chunk of the body. */ |
| 357 | int http_body_is_final(const http_parser *parser); |
| 358 | |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 359 | #ifdef __cplusplus |
| 360 | } |
Ryan | e244dbf | 2009-08-20 12:45:41 +0200 | [diff] [blame] | 361 | #endif |
Ryan | 97d4572 | 2009-04-25 13:33:16 +0200 | [diff] [blame] | 362 | #endif |