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