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