1 
2 #ifndef INCLUDE_LLHTTP_H_
3 #define INCLUDE_LLHTTP_H_
4 
5 #define LLHTTP_VERSION_MAJOR 9
6 #define LLHTTP_VERSION_MINOR 0
7 #define LLHTTP_VERSION_PATCH 1
8 
9 #ifndef INCLUDE_LLHTTP_ITSELF_H_
10 #define INCLUDE_LLHTTP_ITSELF_H_
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #include <stdint.h>
16 
17 typedef struct llhttp__internal_s llhttp__internal_t;
18 struct llhttp__internal_s {
19   int32_t _index;
20   void* _span_pos0;
21   void* _span_cb0;
22   int32_t error;
23   const char* reason;
24   const char* error_pos;
25   void* data;
26   void* _current;
27   uint64_t content_length;
28   uint8_t type;
29   uint8_t method;
30   uint8_t http_major;
31   uint8_t http_minor;
32   uint8_t header_state;
33   uint8_t lenient_flags;
34   uint8_t upgrade;
35   uint8_t finish;
36   uint16_t flags;
37   uint16_t status_code;
38   uint8_t initial_message_completed;
39   void* settings;
40 };
41 
42 int llhttp__internal_init(llhttp__internal_t* s);
43 int llhttp__internal_execute(llhttp__internal_t* s, const char* p, const char* endp);
44 
45 #ifdef __cplusplus
46 }  /* extern "C" */
47 #endif
48 #endif  /* INCLUDE_LLHTTP_ITSELF_H_ */
49 
50 
51 #ifndef LLLLHTTP_C_HEADERS_
52 #define LLLLHTTP_C_HEADERS_
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 enum llhttp_errno {
58   HPE_OK = 0,
59   HPE_INTERNAL = 1,
60   HPE_STRICT = 2,
61   HPE_CR_EXPECTED = 25,
62   HPE_LF_EXPECTED = 3,
63   HPE_UNEXPECTED_CONTENT_LENGTH = 4,
64   HPE_UNEXPECTED_SPACE = 30,
65   HPE_CLOSED_CONNECTION = 5,
66   HPE_INVALID_METHOD = 6,
67   HPE_INVALID_URL = 7,
68   HPE_INVALID_CONSTANT = 8,
69   HPE_INVALID_VERSION = 9,
70   HPE_INVALID_HEADER_TOKEN = 10,
71   HPE_INVALID_CONTENT_LENGTH = 11,
72   HPE_INVALID_CHUNK_SIZE = 12,
73   HPE_INVALID_STATUS = 13,
74   HPE_INVALID_EOF_STATE = 14,
75   HPE_INVALID_TRANSFER_ENCODING = 15,
76   HPE_CB_MESSAGE_BEGIN = 16,
77   HPE_CB_HEADERS_COMPLETE = 17,
78   HPE_CB_MESSAGE_COMPLETE = 18,
79   HPE_CB_CHUNK_HEADER = 19,
80   HPE_CB_CHUNK_COMPLETE = 20,
81   HPE_PAUSED = 21,
82   HPE_PAUSED_UPGRADE = 22,
83   HPE_PAUSED_H2_UPGRADE = 23,
84   HPE_USER = 24,
85   HPE_CB_URL_COMPLETE = 26,
86   HPE_CB_STATUS_COMPLETE = 27,
87   HPE_CB_METHOD_COMPLETE = 32,
88   HPE_CB_VERSION_COMPLETE = 33,
89   HPE_CB_HEADER_FIELD_COMPLETE = 28,
90   HPE_CB_HEADER_VALUE_COMPLETE = 29,
91   HPE_CB_CHUNK_EXTENSION_NAME_COMPLETE = 34,
92   HPE_CB_CHUNK_EXTENSION_VALUE_COMPLETE = 35,
93   HPE_CB_RESET = 31
94 };
95 typedef enum llhttp_errno llhttp_errno_t;
96 
97 enum llhttp_flags {
98   F_CONNECTION_KEEP_ALIVE = 0x1,
99   F_CONNECTION_CLOSE = 0x2,
100   F_CONNECTION_UPGRADE = 0x4,
101   F_CHUNKED = 0x8,
102   F_UPGRADE = 0x10,
103   F_CONTENT_LENGTH = 0x20,
104   F_SKIPBODY = 0x40,
105   F_TRAILING = 0x80,
106   F_TRANSFER_ENCODING = 0x200
107 };
108 typedef enum llhttp_flags llhttp_flags_t;
109 
110 enum llhttp_lenient_flags {
111   LENIENT_HEADERS = 0x1,
112   LENIENT_CHUNKED_LENGTH = 0x2,
113   LENIENT_KEEP_ALIVE = 0x4,
114   LENIENT_TRANSFER_ENCODING = 0x8,
115   LENIENT_VERSION = 0x10,
116   LENIENT_DATA_AFTER_CLOSE = 0x20,
117   LENIENT_OPTIONAL_LF_AFTER_CR = 0x40,
118   LENIENT_OPTIONAL_CRLF_AFTER_CHUNK = 0x80
119 };
120 typedef enum llhttp_lenient_flags llhttp_lenient_flags_t;
121 
122 enum llhttp_type {
123   HTTP_BOTH = 0,
124   HTTP_REQUEST = 1,
125   HTTP_RESPONSE = 2
126 };
127 typedef enum llhttp_type llhttp_type_t;
128 
129 enum llhttp_finish {
130   HTTP_FINISH_SAFE = 0,
131   HTTP_FINISH_SAFE_WITH_CB = 1,
132   HTTP_FINISH_UNSAFE = 2
133 };
134 typedef enum llhttp_finish llhttp_finish_t;
135 
136 enum llhttp_method {
137   HTTP_DELETE = 0,
138   HTTP_GET = 1,
139   HTTP_HEAD = 2,
140   HTTP_POST = 3,
141   HTTP_PUT = 4,
142   HTTP_CONNECT = 5,
143   HTTP_OPTIONS = 6,
144   HTTP_TRACE = 7,
145   HTTP_COPY = 8,
146   HTTP_LOCK = 9,
147   HTTP_MKCOL = 10,
148   HTTP_MOVE = 11,
149   HTTP_PROPFIND = 12,
150   HTTP_PROPPATCH = 13,
151   HTTP_SEARCH = 14,
152   HTTP_UNLOCK = 15,
153   HTTP_BIND = 16,
154   HTTP_REBIND = 17,
155   HTTP_UNBIND = 18,
156   HTTP_ACL = 19,
157   HTTP_REPORT = 20,
158   HTTP_MKACTIVITY = 21,
159   HTTP_CHECKOUT = 22,
160   HTTP_MERGE = 23,
161   HTTP_MSEARCH = 24,
162   HTTP_NOTIFY = 25,
163   HTTP_SUBSCRIBE = 26,
164   HTTP_UNSUBSCRIBE = 27,
165   HTTP_PATCH = 28,
166   HTTP_PURGE = 29,
167   HTTP_MKCALENDAR = 30,
168   HTTP_LINK = 31,
169   HTTP_UNLINK = 32,
170   HTTP_SOURCE = 33,
171   HTTP_PRI = 34,
172   HTTP_DESCRIBE = 35,
173   HTTP_ANNOUNCE = 36,
174   HTTP_SETUP = 37,
175   HTTP_PLAY = 38,
176   HTTP_PAUSE = 39,
177   HTTP_TEARDOWN = 40,
178   HTTP_GET_PARAMETER = 41,
179   HTTP_SET_PARAMETER = 42,
180   HTTP_REDIRECT = 43,
181   HTTP_RECORD = 44,
182   HTTP_FLUSH = 45
183 };
184 typedef enum llhttp_method llhttp_method_t;
185 
186 enum llhttp_status {
187   HTTP_STATUS_CONTINUE = 100,
188   HTTP_STATUS_SWITCHING_PROTOCOLS = 101,
189   HTTP_STATUS_PROCESSING = 102,
190   HTTP_STATUS_EARLY_HINTS = 103,
191   HTTP_STATUS_RESPONSE_IS_STALE = 110,
192   HTTP_STATUS_REVALIDATION_FAILED = 111,
193   HTTP_STATUS_DISCONNECTED_OPERATION = 112,
194   HTTP_STATUS_HEURISTIC_EXPIRATION = 113,
195   HTTP_STATUS_MISCELLANEOUS_WARNING = 199,
196   HTTP_STATUS_OK = 200,
197   HTTP_STATUS_CREATED = 201,
198   HTTP_STATUS_ACCEPTED = 202,
199   HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION = 203,
200   HTTP_STATUS_NO_CONTENT = 204,
201   HTTP_STATUS_RESET_CONTENT = 205,
202   HTTP_STATUS_PARTIAL_CONTENT = 206,
203   HTTP_STATUS_MULTI_STATUS = 207,
204   HTTP_STATUS_ALREADY_REPORTED = 208,
205   HTTP_STATUS_TRANSFORMATION_APPLIED = 214,
206   HTTP_STATUS_IM_USED = 226,
207   HTTP_STATUS_MISCELLANEOUS_PERSISTENT_WARNING = 299,
208   HTTP_STATUS_MULTIPLE_CHOICES = 300,
209   HTTP_STATUS_MOVED_PERMANENTLY = 301,
210   HTTP_STATUS_FOUND = 302,
211   HTTP_STATUS_SEE_OTHER = 303,
212   HTTP_STATUS_NOT_MODIFIED = 304,
213   HTTP_STATUS_USE_PROXY = 305,
214   HTTP_STATUS_SWITCH_PROXY = 306,
215   HTTP_STATUS_TEMPORARY_REDIRECT = 307,
216   HTTP_STATUS_PERMANENT_REDIRECT = 308,
217   HTTP_STATUS_BAD_REQUEST = 400,
218   HTTP_STATUS_UNAUTHORIZED = 401,
219   HTTP_STATUS_PAYMENT_REQUIRED = 402,
220   HTTP_STATUS_FORBIDDEN = 403,
221   HTTP_STATUS_NOT_FOUND = 404,
222   HTTP_STATUS_METHOD_NOT_ALLOWED = 405,
223   HTTP_STATUS_NOT_ACCEPTABLE = 406,
224   HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED = 407,
225   HTTP_STATUS_REQUEST_TIMEOUT = 408,
226   HTTP_STATUS_CONFLICT = 409,
227   HTTP_STATUS_GONE = 410,
228   HTTP_STATUS_LENGTH_REQUIRED = 411,
229   HTTP_STATUS_PRECONDITION_FAILED = 412,
230   HTTP_STATUS_PAYLOAD_TOO_LARGE = 413,
231   HTTP_STATUS_URI_TOO_LONG = 414,
232   HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
233   HTTP_STATUS_RANGE_NOT_SATISFIABLE = 416,
234   HTTP_STATUS_EXPECTATION_FAILED = 417,
235   HTTP_STATUS_IM_A_TEAPOT = 418,
236   HTTP_STATUS_PAGE_EXPIRED = 419,
237   HTTP_STATUS_ENHANCE_YOUR_CALM = 420,
238   HTTP_STATUS_MISDIRECTED_REQUEST = 421,
239   HTTP_STATUS_UNPROCESSABLE_ENTITY = 422,
240   HTTP_STATUS_LOCKED = 423,
241   HTTP_STATUS_FAILED_DEPENDENCY = 424,
242   HTTP_STATUS_TOO_EARLY = 425,
243   HTTP_STATUS_UPGRADE_REQUIRED = 426,
244   HTTP_STATUS_PRECONDITION_REQUIRED = 428,
245   HTTP_STATUS_TOO_MANY_REQUESTS = 429,
246   HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL = 430,
247   HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431,
248   HTTP_STATUS_LOGIN_TIMEOUT = 440,
249   HTTP_STATUS_NO_RESPONSE = 444,
250   HTTP_STATUS_RETRY_WITH = 449,
251   HTTP_STATUS_BLOCKED_BY_PARENTAL_CONTROL = 450,
252   HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451,
253   HTTP_STATUS_CLIENT_CLOSED_LOAD_BALANCED_REQUEST = 460,
254   HTTP_STATUS_INVALID_X_FORWARDED_FOR = 463,
255   HTTP_STATUS_REQUEST_HEADER_TOO_LARGE = 494,
256   HTTP_STATUS_SSL_CERTIFICATE_ERROR = 495,
257   HTTP_STATUS_SSL_CERTIFICATE_REQUIRED = 496,
258   HTTP_STATUS_HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497,
259   HTTP_STATUS_INVALID_TOKEN = 498,
260   HTTP_STATUS_CLIENT_CLOSED_REQUEST = 499,
261   HTTP_STATUS_INTERNAL_SERVER_ERROR = 500,
262   HTTP_STATUS_NOT_IMPLEMENTED = 501,
263   HTTP_STATUS_BAD_GATEWAY = 502,
264   HTTP_STATUS_SERVICE_UNAVAILABLE = 503,
265   HTTP_STATUS_GATEWAY_TIMEOUT = 504,
266   HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED = 505,
267   HTTP_STATUS_VARIANT_ALSO_NEGOTIATES = 506,
268   HTTP_STATUS_INSUFFICIENT_STORAGE = 507,
269   HTTP_STATUS_LOOP_DETECTED = 508,
270   HTTP_STATUS_BANDWIDTH_LIMIT_EXCEEDED = 509,
271   HTTP_STATUS_NOT_EXTENDED = 510,
272   HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511,
273   HTTP_STATUS_WEB_SERVER_UNKNOWN_ERROR = 520,
274   HTTP_STATUS_WEB_SERVER_IS_DOWN = 521,
275   HTTP_STATUS_CONNECTION_TIMEOUT = 522,
276   HTTP_STATUS_ORIGIN_IS_UNREACHABLE = 523,
277   HTTP_STATUS_TIMEOUT_OCCURED = 524,
278   HTTP_STATUS_SSL_HANDSHAKE_FAILED = 525,
279   HTTP_STATUS_INVALID_SSL_CERTIFICATE = 526,
280   HTTP_STATUS_RAILGUN_ERROR = 527,
281   HTTP_STATUS_SITE_IS_OVERLOADED = 529,
282   HTTP_STATUS_SITE_IS_FROZEN = 530,
283   HTTP_STATUS_IDENTITY_PROVIDER_AUTHENTICATION_ERROR = 561,
284   HTTP_STATUS_NETWORK_READ_TIMEOUT = 598,
285   HTTP_STATUS_NETWORK_CONNECT_TIMEOUT = 599
286 };
287 typedef enum llhttp_status llhttp_status_t;
288 
289 #define HTTP_ERRNO_MAP(XX) \
290   XX(0, OK, OK) \
291   XX(1, INTERNAL, INTERNAL) \
292   XX(2, STRICT, STRICT) \
293   XX(25, CR_EXPECTED, CR_EXPECTED) \
294   XX(3, LF_EXPECTED, LF_EXPECTED) \
295   XX(4, UNEXPECTED_CONTENT_LENGTH, UNEXPECTED_CONTENT_LENGTH) \
296   XX(30, UNEXPECTED_SPACE, UNEXPECTED_SPACE) \
297   XX(5, CLOSED_CONNECTION, CLOSED_CONNECTION) \
298   XX(6, INVALID_METHOD, INVALID_METHOD) \
299   XX(7, INVALID_URL, INVALID_URL) \
300   XX(8, INVALID_CONSTANT, INVALID_CONSTANT) \
301   XX(9, INVALID_VERSION, INVALID_VERSION) \
302   XX(10, INVALID_HEADER_TOKEN, INVALID_HEADER_TOKEN) \
303   XX(11, INVALID_CONTENT_LENGTH, INVALID_CONTENT_LENGTH) \
304   XX(12, INVALID_CHUNK_SIZE, INVALID_CHUNK_SIZE) \
305   XX(13, INVALID_STATUS, INVALID_STATUS) \
306   XX(14, INVALID_EOF_STATE, INVALID_EOF_STATE) \
307   XX(15, INVALID_TRANSFER_ENCODING, INVALID_TRANSFER_ENCODING) \
308   XX(16, CB_MESSAGE_BEGIN, CB_MESSAGE_BEGIN) \
309   XX(17, CB_HEADERS_COMPLETE, CB_HEADERS_COMPLETE) \
310   XX(18, CB_MESSAGE_COMPLETE, CB_MESSAGE_COMPLETE) \
311   XX(19, CB_CHUNK_HEADER, CB_CHUNK_HEADER) \
312   XX(20, CB_CHUNK_COMPLETE, CB_CHUNK_COMPLETE) \
313   XX(21, PAUSED, PAUSED) \
314   XX(22, PAUSED_UPGRADE, PAUSED_UPGRADE) \
315   XX(23, PAUSED_H2_UPGRADE, PAUSED_H2_UPGRADE) \
316   XX(24, USER, USER) \
317   XX(26, CB_URL_COMPLETE, CB_URL_COMPLETE) \
318   XX(27, CB_STATUS_COMPLETE, CB_STATUS_COMPLETE) \
319   XX(32, CB_METHOD_COMPLETE, CB_METHOD_COMPLETE) \
320   XX(33, CB_VERSION_COMPLETE, CB_VERSION_COMPLETE) \
321   XX(28, CB_HEADER_FIELD_COMPLETE, CB_HEADER_FIELD_COMPLETE) \
322   XX(29, CB_HEADER_VALUE_COMPLETE, CB_HEADER_VALUE_COMPLETE) \
323   XX(34, CB_CHUNK_EXTENSION_NAME_COMPLETE, CB_CHUNK_EXTENSION_NAME_COMPLETE) \
324   XX(35, CB_CHUNK_EXTENSION_VALUE_COMPLETE, CB_CHUNK_EXTENSION_VALUE_COMPLETE) \
325   XX(31, CB_RESET, CB_RESET) \
326 
327 
328 #define HTTP_METHOD_MAP(XX) \
329   XX(0, DELETE, DELETE) \
330   XX(1, GET, GET) \
331   XX(2, HEAD, HEAD) \
332   XX(3, POST, POST) \
333   XX(4, PUT, PUT) \
334   XX(5, CONNECT, CONNECT) \
335   XX(6, OPTIONS, OPTIONS) \
336   XX(7, TRACE, TRACE) \
337   XX(8, COPY, COPY) \
338   XX(9, LOCK, LOCK) \
339   XX(10, MKCOL, MKCOL) \
340   XX(11, MOVE, MOVE) \
341   XX(12, PROPFIND, PROPFIND) \
342   XX(13, PROPPATCH, PROPPATCH) \
343   XX(14, SEARCH, SEARCH) \
344   XX(15, UNLOCK, UNLOCK) \
345   XX(16, BIND, BIND) \
346   XX(17, REBIND, REBIND) \
347   XX(18, UNBIND, UNBIND) \
348   XX(19, ACL, ACL) \
349   XX(20, REPORT, REPORT) \
350   XX(21, MKACTIVITY, MKACTIVITY) \
351   XX(22, CHECKOUT, CHECKOUT) \
352   XX(23, MERGE, MERGE) \
353   XX(24, MSEARCH, M-SEARCH) \
354   XX(25, NOTIFY, NOTIFY) \
355   XX(26, SUBSCRIBE, SUBSCRIBE) \
356   XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
357   XX(28, PATCH, PATCH) \
358   XX(29, PURGE, PURGE) \
359   XX(30, MKCALENDAR, MKCALENDAR) \
360   XX(31, LINK, LINK) \
361   XX(32, UNLINK, UNLINK) \
362   XX(33, SOURCE, SOURCE) \
363 
364 
365 #define RTSP_METHOD_MAP(XX) \
366   XX(1, GET, GET) \
367   XX(3, POST, POST) \
368   XX(6, OPTIONS, OPTIONS) \
369   XX(35, DESCRIBE, DESCRIBE) \
370   XX(36, ANNOUNCE, ANNOUNCE) \
371   XX(37, SETUP, SETUP) \
372   XX(38, PLAY, PLAY) \
373   XX(39, PAUSE, PAUSE) \
374   XX(40, TEARDOWN, TEARDOWN) \
375   XX(41, GET_PARAMETER, GET_PARAMETER) \
376   XX(42, SET_PARAMETER, SET_PARAMETER) \
377   XX(43, REDIRECT, REDIRECT) \
378   XX(44, RECORD, RECORD) \
379   XX(45, FLUSH, FLUSH) \
380 
381 
382 #define HTTP_ALL_METHOD_MAP(XX) \
383   XX(0, DELETE, DELETE) \
384   XX(1, GET, GET) \
385   XX(2, HEAD, HEAD) \
386   XX(3, POST, POST) \
387   XX(4, PUT, PUT) \
388   XX(5, CONNECT, CONNECT) \
389   XX(6, OPTIONS, OPTIONS) \
390   XX(7, TRACE, TRACE) \
391   XX(8, COPY, COPY) \
392   XX(9, LOCK, LOCK) \
393   XX(10, MKCOL, MKCOL) \
394   XX(11, MOVE, MOVE) \
395   XX(12, PROPFIND, PROPFIND) \
396   XX(13, PROPPATCH, PROPPATCH) \
397   XX(14, SEARCH, SEARCH) \
398   XX(15, UNLOCK, UNLOCK) \
399   XX(16, BIND, BIND) \
400   XX(17, REBIND, REBIND) \
401   XX(18, UNBIND, UNBIND) \
402   XX(19, ACL, ACL) \
403   XX(20, REPORT, REPORT) \
404   XX(21, MKACTIVITY, MKACTIVITY) \
405   XX(22, CHECKOUT, CHECKOUT) \
406   XX(23, MERGE, MERGE) \
407   XX(24, MSEARCH, M-SEARCH) \
408   XX(25, NOTIFY, NOTIFY) \
409   XX(26, SUBSCRIBE, SUBSCRIBE) \
410   XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
411   XX(28, PATCH, PATCH) \
412   XX(29, PURGE, PURGE) \
413   XX(30, MKCALENDAR, MKCALENDAR) \
414   XX(31, LINK, LINK) \
415   XX(32, UNLINK, UNLINK) \
416   XX(33, SOURCE, SOURCE) \
417   XX(34, PRI, PRI) \
418   XX(35, DESCRIBE, DESCRIBE) \
419   XX(36, ANNOUNCE, ANNOUNCE) \
420   XX(37, SETUP, SETUP) \
421   XX(38, PLAY, PLAY) \
422   XX(39, PAUSE, PAUSE) \
423   XX(40, TEARDOWN, TEARDOWN) \
424   XX(41, GET_PARAMETER, GET_PARAMETER) \
425   XX(42, SET_PARAMETER, SET_PARAMETER) \
426   XX(43, REDIRECT, REDIRECT) \
427   XX(44, RECORD, RECORD) \
428   XX(45, FLUSH, FLUSH) \
429 
430 
431 #define HTTP_STATUS_MAP(XX) \
432   XX(100, CONTINUE, CONTINUE) \
433   XX(101, SWITCHING_PROTOCOLS, SWITCHING_PROTOCOLS) \
434   XX(102, PROCESSING, PROCESSING) \
435   XX(103, EARLY_HINTS, EARLY_HINTS) \
436   XX(110, RESPONSE_IS_STALE, RESPONSE_IS_STALE) \
437   XX(111, REVALIDATION_FAILED, REVALIDATION_FAILED) \
438   XX(112, DISCONNECTED_OPERATION, DISCONNECTED_OPERATION) \
439   XX(113, HEURISTIC_EXPIRATION, HEURISTIC_EXPIRATION) \
440   XX(199, MISCELLANEOUS_WARNING, MISCELLANEOUS_WARNING) \
441   XX(200, OK, OK) \
442   XX(201, CREATED, CREATED) \
443   XX(202, ACCEPTED, ACCEPTED) \
444   XX(203, NON_AUTHORITATIVE_INFORMATION, NON_AUTHORITATIVE_INFORMATION) \
445   XX(204, NO_CONTENT, NO_CONTENT) \
446   XX(205, RESET_CONTENT, RESET_CONTENT) \
447   XX(206, PARTIAL_CONTENT, PARTIAL_CONTENT) \
448   XX(207, MULTI_STATUS, MULTI_STATUS) \
449   XX(208, ALREADY_REPORTED, ALREADY_REPORTED) \
450   XX(214, TRANSFORMATION_APPLIED, TRANSFORMATION_APPLIED) \
451   XX(226, IM_USED, IM_USED) \
452   XX(299, MISCELLANEOUS_PERSISTENT_WARNING, MISCELLANEOUS_PERSISTENT_WARNING) \
453   XX(300, MULTIPLE_CHOICES, MULTIPLE_CHOICES) \
454   XX(301, MOVED_PERMANENTLY, MOVED_PERMANENTLY) \
455   XX(302, FOUND, FOUND) \
456   XX(303, SEE_OTHER, SEE_OTHER) \
457   XX(304, NOT_MODIFIED, NOT_MODIFIED) \
458   XX(305, USE_PROXY, USE_PROXY) \
459   XX(306, SWITCH_PROXY, SWITCH_PROXY) \
460   XX(307, TEMPORARY_REDIRECT, TEMPORARY_REDIRECT) \
461   XX(308, PERMANENT_REDIRECT, PERMANENT_REDIRECT) \
462   XX(400, BAD_REQUEST, BAD_REQUEST) \
463   XX(401, UNAUTHORIZED, UNAUTHORIZED) \
464   XX(402, PAYMENT_REQUIRED, PAYMENT_REQUIRED) \
465   XX(403, FORBIDDEN, FORBIDDEN) \
466   XX(404, NOT_FOUND, NOT_FOUND) \
467   XX(405, METHOD_NOT_ALLOWED, METHOD_NOT_ALLOWED) \
468   XX(406, NOT_ACCEPTABLE, NOT_ACCEPTABLE) \
469   XX(407, PROXY_AUTHENTICATION_REQUIRED, PROXY_AUTHENTICATION_REQUIRED) \
470   XX(408, REQUEST_TIMEOUT, REQUEST_TIMEOUT) \
471   XX(409, CONFLICT, CONFLICT) \
472   XX(410, GONE, GONE) \
473   XX(411, LENGTH_REQUIRED, LENGTH_REQUIRED) \
474   XX(412, PRECONDITION_FAILED, PRECONDITION_FAILED) \
475   XX(413, PAYLOAD_TOO_LARGE, PAYLOAD_TOO_LARGE) \
476   XX(414, URI_TOO_LONG, URI_TOO_LONG) \
477   XX(415, UNSUPPORTED_MEDIA_TYPE, UNSUPPORTED_MEDIA_TYPE) \
478   XX(416, RANGE_NOT_SATISFIABLE, RANGE_NOT_SATISFIABLE) \
479   XX(417, EXPECTATION_FAILED, EXPECTATION_FAILED) \
480   XX(418, IM_A_TEAPOT, IM_A_TEAPOT) \
481   XX(419, PAGE_EXPIRED, PAGE_EXPIRED) \
482   XX(420, ENHANCE_YOUR_CALM, ENHANCE_YOUR_CALM) \
483   XX(421, MISDIRECTED_REQUEST, MISDIRECTED_REQUEST) \
484   XX(422, UNPROCESSABLE_ENTITY, UNPROCESSABLE_ENTITY) \
485   XX(423, LOCKED, LOCKED) \
486   XX(424, FAILED_DEPENDENCY, FAILED_DEPENDENCY) \
487   XX(425, TOO_EARLY, TOO_EARLY) \
488   XX(426, UPGRADE_REQUIRED, UPGRADE_REQUIRED) \
489   XX(428, PRECONDITION_REQUIRED, PRECONDITION_REQUIRED) \
490   XX(429, TOO_MANY_REQUESTS, TOO_MANY_REQUESTS) \
491   XX(430, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL, REQUEST_HEADER_FIELDS_TOO_LARGE_UNOFFICIAL) \
492   XX(431, REQUEST_HEADER_FIELDS_TOO_LARGE, REQUEST_HEADER_FIELDS_TOO_LARGE) \
493   XX(440, LOGIN_TIMEOUT, LOGIN_TIMEOUT) \
494   XX(444, NO_RESPONSE, NO_RESPONSE) \
495   XX(449, RETRY_WITH, RETRY_WITH) \
496   XX(450, BLOCKED_BY_PARENTAL_CONTROL, BLOCKED_BY_PARENTAL_CONTROL) \
497   XX(451, UNAVAILABLE_FOR_LEGAL_REASONS, UNAVAILABLE_FOR_LEGAL_REASONS) \
498   XX(460, CLIENT_CLOSED_LOAD_BALANCED_REQUEST, CLIENT_CLOSED_LOAD_BALANCED_REQUEST) \
499   XX(463, INVALID_X_FORWARDED_FOR, INVALID_X_FORWARDED_FOR) \
500   XX(494, REQUEST_HEADER_TOO_LARGE, REQUEST_HEADER_TOO_LARGE) \
501   XX(495, SSL_CERTIFICATE_ERROR, SSL_CERTIFICATE_ERROR) \
502   XX(496, SSL_CERTIFICATE_REQUIRED, SSL_CERTIFICATE_REQUIRED) \
503   XX(497, HTTP_REQUEST_SENT_TO_HTTPS_PORT, HTTP_REQUEST_SENT_TO_HTTPS_PORT) \
504   XX(498, INVALID_TOKEN, INVALID_TOKEN) \
505   XX(499, CLIENT_CLOSED_REQUEST, CLIENT_CLOSED_REQUEST) \
506   XX(500, INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR) \
507   XX(501, NOT_IMPLEMENTED, NOT_IMPLEMENTED) \
508   XX(502, BAD_GATEWAY, BAD_GATEWAY) \
509   XX(503, SERVICE_UNAVAILABLE, SERVICE_UNAVAILABLE) \
510   XX(504, GATEWAY_TIMEOUT, GATEWAY_TIMEOUT) \
511   XX(505, HTTP_VERSION_NOT_SUPPORTED, HTTP_VERSION_NOT_SUPPORTED) \
512   XX(506, VARIANT_ALSO_NEGOTIATES, VARIANT_ALSO_NEGOTIATES) \
513   XX(507, INSUFFICIENT_STORAGE, INSUFFICIENT_STORAGE) \
514   XX(508, LOOP_DETECTED, LOOP_DETECTED) \
515   XX(509, BANDWIDTH_LIMIT_EXCEEDED, BANDWIDTH_LIMIT_EXCEEDED) \
516   XX(510, NOT_EXTENDED, NOT_EXTENDED) \
517   XX(511, NETWORK_AUTHENTICATION_REQUIRED, NETWORK_AUTHENTICATION_REQUIRED) \
518   XX(520, WEB_SERVER_UNKNOWN_ERROR, WEB_SERVER_UNKNOWN_ERROR) \
519   XX(521, WEB_SERVER_IS_DOWN, WEB_SERVER_IS_DOWN) \
520   XX(522, CONNECTION_TIMEOUT, CONNECTION_TIMEOUT) \
521   XX(523, ORIGIN_IS_UNREACHABLE, ORIGIN_IS_UNREACHABLE) \
522   XX(524, TIMEOUT_OCCURED, TIMEOUT_OCCURED) \
523   XX(525, SSL_HANDSHAKE_FAILED, SSL_HANDSHAKE_FAILED) \
524   XX(526, INVALID_SSL_CERTIFICATE, INVALID_SSL_CERTIFICATE) \
525   XX(527, RAILGUN_ERROR, RAILGUN_ERROR) \
526   XX(529, SITE_IS_OVERLOADED, SITE_IS_OVERLOADED) \
527   XX(530, SITE_IS_FROZEN, SITE_IS_FROZEN) \
528   XX(561, IDENTITY_PROVIDER_AUTHENTICATION_ERROR, IDENTITY_PROVIDER_AUTHENTICATION_ERROR) \
529   XX(598, NETWORK_READ_TIMEOUT, NETWORK_READ_TIMEOUT) \
530   XX(599, NETWORK_CONNECT_TIMEOUT, NETWORK_CONNECT_TIMEOUT) \
531 
532 
533 #ifdef __cplusplus
534 }  /* extern "C" */
535 #endif
536 #endif  /* LLLLHTTP_C_HEADERS_ */
537 
538 
539 #ifndef INCLUDE_LLHTTP_API_H_
540 #define INCLUDE_LLHTTP_API_H_
541 #ifdef __cplusplus
542 extern "C" {
543 #endif
544 #include <stddef.h>
545 
546 #if defined(__wasm__)
547 #define LLHTTP_EXPORT __attribute__((visibility("default")))
548 #else
549 #define LLHTTP_EXPORT
550 #endif
551 
552 typedef llhttp__internal_t llhttp_t;
553 typedef struct llhttp_settings_s llhttp_settings_t;
554 
555 typedef int (*llhttp_data_cb)(llhttp_t*, const char *at, size_t length);
556 typedef int (*llhttp_cb)(llhttp_t*);
557 
558 struct llhttp_settings_s {
559   /* Possible return values 0, -1, `HPE_PAUSED` */
560   llhttp_cb      on_message_begin;
561 
562   /* Possible return values 0, -1, HPE_USER */
563   llhttp_data_cb on_url;
564   llhttp_data_cb on_status;
565   llhttp_data_cb on_method;
566   llhttp_data_cb on_version;
567   llhttp_data_cb on_header_field;
568   llhttp_data_cb on_header_value;
569   llhttp_data_cb      on_chunk_extension_name;
570   llhttp_data_cb      on_chunk_extension_value;
571 
572   /* Possible return values:
573    * 0  - Proceed normally
574    * 1  - Assume that request/response has no body, and proceed to parsing the
575    *      next message
576    * 2  - Assume absence of body (as above) and make `llhttp_execute()` return
577    *      `HPE_PAUSED_UPGRADE`
578    * -1 - Error
579    * `HPE_PAUSED`
580    */
581   llhttp_cb      on_headers_complete;
582 
583   /* Possible return values 0, -1, HPE_USER */
584   llhttp_data_cb on_body;
585 
586   /* Possible return values 0, -1, `HPE_PAUSED` */
587   llhttp_cb      on_message_complete;
588   llhttp_cb      on_url_complete;
589   llhttp_cb      on_status_complete;
590   llhttp_cb      on_method_complete;
591   llhttp_cb      on_version_complete;
592   llhttp_cb      on_header_field_complete;
593   llhttp_cb      on_header_value_complete;
594   llhttp_cb      on_chunk_extension_name_complete;
595   llhttp_cb      on_chunk_extension_value_complete;
596 
597   /* When on_chunk_header is called, the current chunk length is stored
598    * in parser->content_length.
599    * Possible return values 0, -1, `HPE_PAUSED`
600    */
601   llhttp_cb      on_chunk_header;
602   llhttp_cb      on_chunk_complete;
603   llhttp_cb      on_reset;
604 };
605 
606 /* Initialize the parser with specific type and user settings.
607  *
608  * NOTE: lifetime of `settings` has to be at least the same as the lifetime of
609  * the `parser` here. In practice, `settings` has to be either a static
610  * variable or be allocated with `malloc`, `new`, etc.
611  */
612 LLHTTP_EXPORT
613 void llhttp_init(llhttp_t* parser, llhttp_type_t type,
614                  const llhttp_settings_t* settings);
615 
616 LLHTTP_EXPORT
617 llhttp_t* llhttp_alloc(llhttp_type_t type);
618 
619 LLHTTP_EXPORT
620 void llhttp_free(llhttp_t* parser);
621 
622 LLHTTP_EXPORT
623 uint8_t llhttp_get_type(llhttp_t* parser);
624 
625 LLHTTP_EXPORT
626 uint8_t llhttp_get_http_major(llhttp_t* parser);
627 
628 LLHTTP_EXPORT
629 uint8_t llhttp_get_http_minor(llhttp_t* parser);
630 
631 LLHTTP_EXPORT
632 uint8_t llhttp_get_method(llhttp_t* parser);
633 
634 LLHTTP_EXPORT
635 int llhttp_get_status_code(llhttp_t* parser);
636 
637 LLHTTP_EXPORT
638 uint8_t llhttp_get_upgrade(llhttp_t* parser);
639 
640 /* Reset an already initialized parser back to the start state, preserving the
641  * existing parser type, callback settings, user data, and lenient flags.
642  */
643 LLHTTP_EXPORT
644 void llhttp_reset(llhttp_t* parser);
645 
646 /* Initialize the settings object */
647 LLHTTP_EXPORT
648 void llhttp_settings_init(llhttp_settings_t* settings);
649 
650 /* Parse full or partial request/response, invoking user callbacks along the
651  * way.
652  *
653  * If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing
654  * interrupts, and such errno is returned from `llhttp_execute()`. If
655  * `HPE_PAUSED` was used as a errno, the execution can be resumed with
656  * `llhttp_resume()` call.
657  *
658  * In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE`
659  * is returned after fully parsing the request/response. If the user wishes to
660  * continue parsing, they need to invoke `llhttp_resume_after_upgrade()`.
661  *
662  * NOTE: if this function ever returns a non-pause type error, it will continue
663  * to return the same error upon each successive call up until `llhttp_init()`
664  * is called.
665  */
666 LLHTTP_EXPORT
667 llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
668 
669 /* This method should be called when the other side has no further bytes to
670  * send (e.g. shutdown of readable side of the TCP connection.)
671  *
672  * Requests without `Content-Length` and other messages might require treating
673  * all incoming bytes as the part of the body, up to the last byte of the
674  * connection. This method will invoke `on_message_complete()` callback if the
675  * request was terminated safely. Otherwise a error code would be returned.
676  */
677 LLHTTP_EXPORT
678 llhttp_errno_t llhttp_finish(llhttp_t* parser);
679 
680 /* Returns `1` if the incoming message is parsed until the last byte, and has
681  * to be completed by calling `llhttp_finish()` on EOF
682  */
683 LLHTTP_EXPORT
684 int llhttp_message_needs_eof(const llhttp_t* parser);
685 
686 /* Returns `1` if there might be any other messages following the last that was
687  * successfully parsed.
688  */
689 LLHTTP_EXPORT
690 int llhttp_should_keep_alive(const llhttp_t* parser);
691 
692 /* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
693  * appropriate error reason.
694  *
695  * Important: do not call this from user callbacks! User callbacks must return
696  * `HPE_PAUSED` if pausing is required.
697  */
698 LLHTTP_EXPORT
699 void llhttp_pause(llhttp_t* parser);
700 
701 /* Might be called to resume the execution after the pause in user's callback.
702  * See `llhttp_execute()` above for details.
703  *
704  * Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
705  */
706 LLHTTP_EXPORT
707 void llhttp_resume(llhttp_t* parser);
708 
709 /* Might be called to resume the execution after the pause in user's callback.
710  * See `llhttp_execute()` above for details.
711  *
712  * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
713  */
714 LLHTTP_EXPORT
715 void llhttp_resume_after_upgrade(llhttp_t* parser);
716 
717 /* Returns the latest return error */
718 LLHTTP_EXPORT
719 llhttp_errno_t llhttp_get_errno(const llhttp_t* parser);
720 
721 /* Returns the verbal explanation of the latest returned error.
722  *
723  * Note: User callback should set error reason when returning the error. See
724  * `llhttp_set_error_reason()` for details.
725  */
726 LLHTTP_EXPORT
727 const char* llhttp_get_error_reason(const llhttp_t* parser);
728 
729 /* Assign verbal description to the returned error. Must be called in user
730  * callbacks right before returning the errno.
731  *
732  * Note: `HPE_USER` error code might be useful in user callbacks.
733  */
734 LLHTTP_EXPORT
735 void llhttp_set_error_reason(llhttp_t* parser, const char* reason);
736 
737 /* Returns the pointer to the last parsed byte before the returned error. The
738  * pointer is relative to the `data` argument of `llhttp_execute()`.
739  *
740  * Note: this method might be useful for counting the number of parsed bytes.
741  */
742 LLHTTP_EXPORT
743 const char* llhttp_get_error_pos(const llhttp_t* parser);
744 
745 /* Returns textual name of error code */
746 LLHTTP_EXPORT
747 const char* llhttp_errno_name(llhttp_errno_t err);
748 
749 /* Returns textual name of HTTP method */
750 LLHTTP_EXPORT
751 const char* llhttp_method_name(llhttp_method_t method);
752 
753 /* Returns textual name of HTTP status */
754 LLHTTP_EXPORT
755 const char* llhttp_status_name(llhttp_status_t status);
756 
757 /* Enables/disables lenient header value parsing (disabled by default).
758  *
759  * Lenient parsing disables header value token checks, extending llhttp's
760  * protocol support to highly non-compliant clients/server. No
761  * `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
762  * lenient parsing is "on".
763  *
764  * **Enabling this flag can pose a security issue since you will be exposed to
765  * request smuggling attacks. USE WITH CAUTION!**
766  */
767 LLHTTP_EXPORT
768 void llhttp_set_lenient_headers(llhttp_t* parser, int enabled);
769 
770 
771 /* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
772  * `Content-Length` headers (disabled by default).
773  *
774  * Normally `llhttp` would error when `Transfer-Encoding` is present in
775  * conjunction with `Content-Length`. This error is important to prevent HTTP
776  * request smuggling, but may be less desirable for small number of cases
777  * involving legacy servers.
778  *
779  * **Enabling this flag can pose a security issue since you will be exposed to
780  * request smuggling attacks. USE WITH CAUTION!**
781  */
782 LLHTTP_EXPORT
783 void llhttp_set_lenient_chunked_length(llhttp_t* parser, int enabled);
784 
785 
786 /* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
787  * requests responses.
788  *
789  * Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
790  * the HTTP request/response after the request/response with `Connection: close`
791  * and `Content-Length`. This is important to prevent cache poisoning attacks,
792  * but might interact badly with outdated and insecure clients. With this flag
793  * the extra request/response will be parsed normally.
794  *
795  * **Enabling this flag can pose a security issue since you will be exposed to
796  * poisoning attacks. USE WITH CAUTION!**
797  */
798 LLHTTP_EXPORT
799 void llhttp_set_lenient_keep_alive(llhttp_t* parser, int enabled);
800 
801 /* Enables/disables lenient handling of `Transfer-Encoding` header.
802  *
803  * Normally `llhttp` would error when a `Transfer-Encoding` has `chunked` value
804  * and another value after it (either in a single header or in multiple
805  * headers whose value are internally joined using `, `).
806  * This is mandated by the spec to reliably determine request body size and thus
807  * avoid request smuggling.
808  * With this flag the extra value will be parsed normally.
809  *
810  * **Enabling this flag can pose a security issue since you will be exposed to
811  * request smuggling attacks. USE WITH CAUTION!**
812  */
813 LLHTTP_EXPORT
814 void llhttp_set_lenient_transfer_encoding(llhttp_t* parser, int enabled);
815 
816 /* Enables/disables lenient handling of HTTP version.
817  *
818  * Normally `llhttp` would error when the HTTP version in the request or status line
819  * is not `0.9`, `1.0`, `1.1` or `2.0`.
820  * With this flag the invalid value will be parsed normally.
821  *
822  * **Enabling this flag can pose a security issue since you will allow unsupported
823  * HTTP versions. USE WITH CAUTION!**
824  */
825 LLHTTP_EXPORT
826 void llhttp_set_lenient_version(llhttp_t* parser, int enabled);
827 
828 /* Enables/disables lenient handling of additional data received after a message ends
829  * and keep-alive is disabled.
830  *
831  * Normally `llhttp` would error when additional unexpected data is received if the message
832  * contains the `Connection` header with `close` value.
833  * With this flag the extra data will discarded without throwing an error.
834  *
835  * **Enabling this flag can pose a security issue since you will be exposed to
836  * poisoning attacks. USE WITH CAUTION!**
837  */
838 LLHTTP_EXPORT
839 void llhttp_set_lenient_data_after_close(llhttp_t* parser, int enabled);
840 
841 /* Enables/disables lenient handling of incomplete CRLF sequences.
842  *
843  * Normally `llhttp` would error when a CR is not followed by LF when terminating the
844  * request line, the status line, the headers or a chunk header.
845  * With this flag only a CR is required to terminate such sections.
846  *
847  * **Enabling this flag can pose a security issue since you will be exposed to
848  * request smuggling attacks. USE WITH CAUTION!**
849  */
850 LLHTTP_EXPORT
851 void llhttp_set_lenient_optional_lf_after_cr(llhttp_t* parser, int enabled);
852 
853 /* Enables/disables lenient handling of chunks not separated via CRLF.
854  *
855  * Normally `llhttp` would error when after a chunk data a CRLF is missing before
856  * starting a new chunk.
857  * With this flag the new chunk can start immediately after the previous one.
858  *
859  * **Enabling this flag can pose a security issue since you will be exposed to
860  * request smuggling attacks. USE WITH CAUTION!**
861  */
862 LLHTTP_EXPORT
863 void llhttp_set_lenient_optional_crlf_after_chunk(llhttp_t* parser, int enabled);
864 
865 #ifdef __cplusplus
866 }  /* extern "C" */
867 #endif
868 #endif  /* INCLUDE_LLHTTP_API_H_ */
869 
870 
871 #endif  /* INCLUDE_LLHTTP_H_ */
872