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