11#ifndef INCLUDE_LLHTTP_H_
22#define INCLUDE_LLHTTP_H_
33
4- #define LLHTTP_VERSION_MAJOR 2
5- #define LLHTTP_VERSION_MINOR 2
6- #define LLHTTP_VERSION_PATCH 1
4+ #define LLHTTP_VERSION_MAJOR 5
5+ #define LLHTTP_VERSION_MINOR 0
6+ #define LLHTTP_VERSION_PATCH 0
77
88#ifndef LLHTTP_STRICT_MODE
99# define LLHTTP_STRICT_MODE 0
@@ -33,10 +33,11 @@ struct llhttp__internal_s {
3333 uint8_t http_major ;
3434 uint8_t http_minor ;
3535 uint8_t header_state ;
36- uint16_t flags ;
36+ uint8_t lenient_flags ;
3737 uint8_t upgrade ;
38- uint16_t status_code ;
3938 uint8_t finish ;
39+ uint16_t flags ;
40+ uint16_t status_code ;
4041 void * settings ;
4142};
4243
@@ -91,11 +92,17 @@ enum llhttp_flags {
9192 F_CONTENT_LENGTH = 0x20 ,
9293 F_SKIPBODY = 0x40 ,
9394 F_TRAILING = 0x80 ,
94- F_LENIENT = 0x100 ,
9595 F_TRANSFER_ENCODING = 0x200
9696};
9797typedef enum llhttp_flags llhttp_flags_t ;
9898
99+ enum llhttp_lenient_flags {
100+ LENIENT_HEADERS = 0x1 ,
101+ LENIENT_CHUNKED_LENGTH = 0x2 ,
102+ LENIENT_KEEP_ALIVE = 0x4
103+ };
104+ typedef enum llhttp_lenient_flags llhttp_lenient_flags_t ;
105+
99106enum llhttp_type {
100107 HTTP_BOTH = 0 ,
101108 HTTP_REQUEST = 1 ,
@@ -249,6 +256,12 @@ extern "C" {
249256#endif
250257#include <stddef.h>
251258
259+ #if defined(__wasm__ )
260+ #define LLHTTP_EXPORT __attribute__((visibility("default")))
261+ #else
262+ #define LLHTTP_EXPORT
263+ #endif
264+
252265typedef llhttp__internal_t llhttp_t ;
253266typedef struct llhttp_settings_s llhttp_settings_t ;
254267
@@ -286,6 +299,11 @@ struct llhttp_settings_s {
286299 */
287300 llhttp_cb on_chunk_header ;
288301 llhttp_cb on_chunk_complete ;
302+
303+ llhttp_cb on_url_complete ;
304+ llhttp_cb on_status_complete ;
305+ llhttp_cb on_header_field_complete ;
306+ llhttp_cb on_header_value_complete ;
289307};
290308
291309/* Initialize the parser with specific type and user settings.
@@ -294,10 +312,46 @@ struct llhttp_settings_s {
294312 * the `parser` here. In practice, `settings` has to be either a static
295313 * variable or be allocated with `malloc`, `new`, etc.
296314 */
315+ LLHTTP_EXPORT
297316void llhttp_init (llhttp_t * parser , llhttp_type_t type ,
298317 const llhttp_settings_t * settings );
299318
319+ #if defined(__wasm__ )
320+
321+ LLHTTP_EXPORT
322+ llhttp_t * llhttp_alloc (llhttp_type_t type );
323+
324+ LLHTTP_EXPORT
325+ void llhttp_free (llhttp_t * parser );
326+
327+ LLHTTP_EXPORT
328+ uint8_t llhttp_get_type (llhttp_t * parser );
329+
330+ LLHTTP_EXPORT
331+ uint8_t llhttp_get_http_major (llhttp_t * parser );
332+
333+ LLHTTP_EXPORT
334+ uint8_t llhttp_get_http_minor (llhttp_t * parser );
335+
336+ LLHTTP_EXPORT
337+ uint8_t llhttp_get_method (llhttp_t * parser );
338+
339+ LLHTTP_EXPORT
340+ int llhttp_get_status_code (llhttp_t * parser );
341+
342+ LLHTTP_EXPORT
343+ uint8_t llhttp_get_upgrade (llhttp_t * parser );
344+
345+ #endif // defined(__wasm__)
346+
347+ /* Reset an already initialized parser back to the start state, preserving the
348+ * existing parser type, callback settings, user data, and lenient flags.
349+ */
350+ LLHTTP_EXPORT
351+ void llhttp_reset (llhttp_t * parser );
352+
300353/* Initialize the settings object */
354+ LLHTTP_EXPORT
301355void llhttp_settings_init (llhttp_settings_t * settings );
302356
303357/* Parse full or partial request/response, invoking user callbacks along the
@@ -316,6 +370,7 @@ void llhttp_settings_init(llhttp_settings_t* settings);
316370 * to return the same error upon each successive call up until `llhttp_init()`
317371 * is called.
318372 */
373+ LLHTTP_EXPORT
319374llhttp_errno_t llhttp_execute (llhttp_t * parser , const char * data , size_t len );
320375
321376/* This method should be called when the other side has no further bytes to
@@ -326,16 +381,19 @@ llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
326381 * connection. This method will invoke `on_message_complete()` callback if the
327382 * request was terminated safely. Otherwise a error code would be returned.
328383 */
384+ LLHTTP_EXPORT
329385llhttp_errno_t llhttp_finish (llhttp_t * parser );
330386
331387/* Returns `1` if the incoming message is parsed until the last byte, and has
332388 * to be completed by calling `llhttp_finish()` on EOF
333389 */
390+ LLHTTP_EXPORT
334391int llhttp_message_needs_eof (const llhttp_t * parser );
335392
336393/* Returns `1` if there might be any other messages following the last that was
337394 * successfully parsed.
338395 */
396+ LLHTTP_EXPORT
339397int llhttp_should_keep_alive (const llhttp_t * parser );
340398
341399/* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
@@ -344,50 +402,59 @@ int llhttp_should_keep_alive(const llhttp_t* parser);
344402 * Important: do not call this from user callbacks! User callbacks must return
345403 * `HPE_PAUSED` if pausing is required.
346404 */
405+ LLHTTP_EXPORT
347406void llhttp_pause (llhttp_t * parser );
348407
349408/* Might be called to resume the execution after the pause in user's callback.
350409 * See `llhttp_execute()` above for details.
351410 *
352411 * Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
353412 */
413+ LLHTTP_EXPORT
354414void llhttp_resume (llhttp_t * parser );
355415
356416/* Might be called to resume the execution after the pause in user's callback.
357417 * See `llhttp_execute()` above for details.
358418 *
359419 * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
360420 */
421+ LLHTTP_EXPORT
361422void llhttp_resume_after_upgrade (llhttp_t * parser );
362423
363424/* Returns the latest return error */
425+ LLHTTP_EXPORT
364426llhttp_errno_t llhttp_get_errno (const llhttp_t * parser );
365427
366428/* Returns the verbal explanation of the latest returned error.
367429 *
368430 * Note: User callback should set error reason when returning the error. See
369431 * `llhttp_set_error_reason()` for details.
370432 */
433+ LLHTTP_EXPORT
371434const char * llhttp_get_error_reason (const llhttp_t * parser );
372435
373436/* Assign verbal description to the returned error. Must be called in user
374437 * callbacks right before returning the errno.
375438 *
376439 * Note: `HPE_USER` error code might be useful in user callbacks.
377440 */
441+ LLHTTP_EXPORT
378442void llhttp_set_error_reason (llhttp_t * parser , const char * reason );
379443
380444/* Returns the pointer to the last parsed byte before the returned error. The
381445 * pointer is relative to the `data` argument of `llhttp_execute()`.
382446 *
383447 * Note: this method might be useful for counting the number of parsed bytes.
384448 */
449+ LLHTTP_EXPORT
385450const char * llhttp_get_error_pos (const llhttp_t * parser );
386451
387452/* Returns textual name of error code */
453+ LLHTTP_EXPORT
388454const char * llhttp_errno_name (llhttp_errno_t err );
389455
390456/* Returns textual name of HTTP method */
457+ LLHTTP_EXPORT
391458const char * llhttp_method_name (llhttp_method_t method );
392459
393460
@@ -400,7 +467,36 @@ const char* llhttp_method_name(llhttp_method_t method);
400467 *
401468 * **(USE AT YOUR OWN RISK)**
402469 */
403- void llhttp_set_lenient (llhttp_t * parser , int enabled );
470+ LLHTTP_EXPORT
471+ void llhttp_set_lenient_headers (llhttp_t * parser , int enabled );
472+
473+
474+ /* Enables/disables lenient handling of conflicting `Transfer-Encoding` and
475+ * `Content-Length` headers (disabled by default).
476+ *
477+ * Normally `llhttp` would error when `Transfer-Encoding` is present in
478+ * conjunction with `Content-Length`. This error is important to prevent HTTP
479+ * request smuggling, but may be less desirable for small number of cases
480+ * involving legacy servers.
481+ *
482+ * **(USE AT YOUR OWN RISK)**
483+ */
484+ LLHTTP_EXPORT
485+ void llhttp_set_lenient_chunked_length (llhttp_t * parser , int enabled );
486+
487+
488+ /* Enables/disables lenient handling of `Connection: close` and HTTP/1.0
489+ * requests responses.
490+ *
491+ * Normally `llhttp` would error on (in strict mode) or discard (in loose mode)
492+ * the HTTP request/response after the request/response with `Connection: close`
493+ * and `Content-Length`. This is important to prevent cache poisoning attacks,
494+ * but might interact badly with outdated and insecure clients. With this flag
495+ * the extra request/response will be parsed normally.
496+ *
497+ * **(USE AT YOUR OWN RISK)**
498+ */
499+ void llhttp_set_lenient_keep_alive (llhttp_t * parser , int enabled );
404500
405501#ifdef __cplusplus
406502} /* extern "C" */
0 commit comments