1 #ifndef INCLUDE_LLHTTP_H_
2 #define INCLUDE_LLHTTP_H_
4 #define LLHTTP_VERSION_MAJOR 2
5 #define LLHTTP_VERSION_MINOR 0
6 #define LLHTTP_VERSION_PATCH 4
8 #ifndef INCLUDE_LLHTTP_ITSELF_H_
9 #define INCLUDE_LLHTTP_ITSELF_H_
16 typedef struct llhttp__internal_s llhttp__internal_t;
17 struct llhttp__internal_s {
23 const char* error_pos;
26 uint64_t content_length;
39 int llhttp__internal_init(llhttp__internal_t* s);
40 int llhttp__internal_execute(llhttp__internal_t* s, const char* p, const char* endp);
45 #endif /* INCLUDE_LLHTTP_ITSELF_H_ */
47 #ifndef LLLLHTTP_C_HEADERS_
48 #define LLLLHTTP_C_HEADERS_
58 HPE_UNEXPECTED_CONTENT_LENGTH = 4,
59 HPE_CLOSED_CONNECTION = 5,
60 HPE_INVALID_METHOD = 6,
62 HPE_INVALID_CONSTANT = 8,
63 HPE_INVALID_VERSION = 9,
64 HPE_INVALID_HEADER_TOKEN = 10,
65 HPE_INVALID_CONTENT_LENGTH = 11,
66 HPE_INVALID_CHUNK_SIZE = 12,
67 HPE_INVALID_STATUS = 13,
68 HPE_INVALID_EOF_STATE = 14,
69 HPE_INVALID_TRANSFER_ENCODING = 15,
70 HPE_CB_MESSAGE_BEGIN = 16,
71 HPE_CB_HEADERS_COMPLETE = 17,
72 HPE_CB_MESSAGE_COMPLETE = 18,
73 HPE_CB_CHUNK_HEADER = 19,
74 HPE_CB_CHUNK_COMPLETE = 20,
76 HPE_PAUSED_UPGRADE = 22,
79 typedef enum llhttp_errno llhttp_errno_t;
82 F_CONNECTION_KEEP_ALIVE = 0x1,
83 F_CONNECTION_CLOSE = 0x2,
84 F_CONNECTION_UPGRADE = 0x4,
87 F_CONTENT_LENGTH = 0x20,
91 F_TRANSFER_ENCODING = 0x200
93 typedef enum llhttp_flags llhttp_flags_t;
100 typedef enum llhttp_type llhttp_type_t;
103 HTTP_FINISH_SAFE = 0,
104 HTTP_FINISH_SAFE_WITH_CB = 1,
105 HTTP_FINISH_UNSAFE = 2
107 typedef enum llhttp_finish llhttp_finish_t;
131 HTTP_MKACTIVITY = 21,
137 HTTP_UNSUBSCRIBE = 27,
140 HTTP_MKCALENDAR = 30,
145 typedef enum llhttp_method llhttp_method_t;
147 #define HTTP_ERRNO_MAP(XX) \
149 XX(1, INTERNAL, INTERNAL) \
150 XX(2, STRICT, STRICT) \
151 XX(3, LF_EXPECTED, LF_EXPECTED) \
152 XX(4, UNEXPECTED_CONTENT_LENGTH, UNEXPECTED_CONTENT_LENGTH) \
153 XX(5, CLOSED_CONNECTION, CLOSED_CONNECTION) \
154 XX(6, INVALID_METHOD, INVALID_METHOD) \
155 XX(7, INVALID_URL, INVALID_URL) \
156 XX(8, INVALID_CONSTANT, INVALID_CONSTANT) \
157 XX(9, INVALID_VERSION, INVALID_VERSION) \
158 XX(10, INVALID_HEADER_TOKEN, INVALID_HEADER_TOKEN) \
159 XX(11, INVALID_CONTENT_LENGTH, INVALID_CONTENT_LENGTH) \
160 XX(12, INVALID_CHUNK_SIZE, INVALID_CHUNK_SIZE) \
161 XX(13, INVALID_STATUS, INVALID_STATUS) \
162 XX(14, INVALID_EOF_STATE, INVALID_EOF_STATE) \
163 XX(15, INVALID_TRANSFER_ENCODING, INVALID_TRANSFER_ENCODING) \
164 XX(16, CB_MESSAGE_BEGIN, CB_MESSAGE_BEGIN) \
165 XX(17, CB_HEADERS_COMPLETE, CB_HEADERS_COMPLETE) \
166 XX(18, CB_MESSAGE_COMPLETE, CB_MESSAGE_COMPLETE) \
167 XX(19, CB_CHUNK_HEADER, CB_CHUNK_HEADER) \
168 XX(20, CB_CHUNK_COMPLETE, CB_CHUNK_COMPLETE) \
169 XX(21, PAUSED, PAUSED) \
170 XX(22, PAUSED_UPGRADE, PAUSED_UPGRADE) \
174 #define HTTP_METHOD_MAP(XX) \
175 XX(0, DELETE, DELETE) \
180 XX(5, CONNECT, CONNECT) \
181 XX(6, OPTIONS, OPTIONS) \
182 XX(7, TRACE, TRACE) \
185 XX(10, MKCOL, MKCOL) \
187 XX(12, PROPFIND, PROPFIND) \
188 XX(13, PROPPATCH, PROPPATCH) \
189 XX(14, SEARCH, SEARCH) \
190 XX(15, UNLOCK, UNLOCK) \
192 XX(17, REBIND, REBIND) \
193 XX(18, UNBIND, UNBIND) \
195 XX(20, REPORT, REPORT) \
196 XX(21, MKACTIVITY, MKACTIVITY) \
197 XX(22, CHECKOUT, CHECKOUT) \
198 XX(23, MERGE, MERGE) \
199 XX(24, MSEARCH, M-SEARCH) \
200 XX(25, NOTIFY, NOTIFY) \
201 XX(26, SUBSCRIBE, SUBSCRIBE) \
202 XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
203 XX(28, PATCH, PATCH) \
204 XX(29, PURGE, PURGE) \
205 XX(30, MKCALENDAR, MKCALENDAR) \
207 XX(32, UNLINK, UNLINK) \
208 XX(33, SOURCE, SOURCE) \
215 #endif /* LLLLHTTP_C_HEADERS_ */
217 #ifndef INCLUDE_LLHTTP_API_H_
218 #define INCLUDE_LLHTTP_API_H_
224 typedef llhttp__internal_t llhttp_t;
225 typedef struct llhttp_settings_s llhttp_settings_t;
227 typedef int (*llhttp_data_cb)(llhttp_t*, const char *at, size_t length);
228 typedef int (*llhttp_cb)(llhttp_t*);
230 struct llhttp_settings_s {
231 /* Possible return values 0, -1, `HPE_PAUSED` */
232 llhttp_cb on_message_begin;
234 llhttp_data_cb on_url;
235 llhttp_data_cb on_status;
236 llhttp_data_cb on_header_field;
237 llhttp_data_cb on_header_value;
239 /* Possible return values:
240 * 0 - Proceed normally
241 * 1 - Assume that request/response has no body, and proceed to parsing the
243 * 2 - Assume absence of body (as above) and make `llhttp_execute()` return
244 * `HPE_PAUSED_UPGRADE`
248 llhttp_cb on_headers_complete;
250 llhttp_data_cb on_body;
252 /* Possible return values 0, -1, `HPE_PAUSED` */
253 llhttp_cb on_message_complete;
255 /* When on_chunk_header is called, the current chunk length is stored
256 * in parser->content_length.
257 * Possible return values 0, -1, `HPE_PAUSED`
259 llhttp_cb on_chunk_header;
260 llhttp_cb on_chunk_complete;
263 /* Initialize the parser with specific type and user settings */
264 void llhttp_init(llhttp_t* parser, llhttp_type_t type,
265 const llhttp_settings_t* settings);
267 /* Initialize the settings object */
268 void llhttp_settings_init(llhttp_settings_t* settings);
270 /* Parse full or partial request/response, invoking user callbacks along the
273 * If any of `llhttp_data_cb` returns errno not equal to `HPE_OK` - the parsing
274 * interrupts, and such errno is returned from `llhttp_execute()`. If
275 * `HPE_PAUSED` was used as a errno, the execution can be resumed with
276 * `llhttp_resume()` call.
278 * In a special case of CONNECT/Upgrade request/response `HPE_PAUSED_UPGRADE`
279 * is returned after fully parsing the request/response. If the user wishes to
280 * continue parsing, they need to invoke `llhttp_resume_after_upgrade()`.
282 * NOTE: if this function ever returns a non-pause type error, it will continue
283 * to return the same error upon each successive call up until `llhttp_init()`
286 llhttp_errno_t llhttp_execute(llhttp_t* parser, const char* data, size_t len);
288 /* This method should be called when the other side has no further bytes to
289 * send (e.g. shutdown of readable side of the TCP connection.)
291 * Requests without `Content-Length` and other messages might require treating
292 * all incoming bytes as the part of the body, up to the last byte of the
293 * connection. This method will invoke `on_message_complete()` callback if the
294 * request was terminated safely. Otherwise a error code would be returned.
296 llhttp_errno_t llhttp_finish(llhttp_t* parser);
298 /* Returns `1` if the incoming message is parsed until the last byte, and has
299 * to be completed by calling `llhttp_finish()` on EOF
301 int llhttp_message_needs_eof(const llhttp_t* parser);
303 /* Returns `1` if there might be any other messages following the last that was
304 * successfully parsed.
306 int llhttp_should_keep_alive(const llhttp_t* parser);
308 /* Make further calls of `llhttp_execute()` return `HPE_PAUSED` and set
309 * appropriate error reason.
311 * Important: do not call this from user callbacks! User callbacks must return
312 * `HPE_PAUSED` if pausing is required.
314 void llhttp_pause(llhttp_t* parser);
316 /* Might be called to resume the execution after the pause in user's callback.
317 * See `llhttp_execute()` above for details.
319 * Call this only if `llhttp_execute()` returns `HPE_PAUSED`.
321 void llhttp_resume(llhttp_t* parser);
323 /* Might be called to resume the execution after the pause in user's callback.
324 * See `llhttp_execute()` above for details.
326 * Call this only if `llhttp_execute()` returns `HPE_PAUSED_UPGRADE`
328 void llhttp_resume_after_upgrade(llhttp_t* parser);
330 /* Returns the latest return error */
331 llhttp_errno_t llhttp_get_errno(const llhttp_t* parser);
333 /* Returns the verbal explanation of the latest returned error.
335 * Note: User callback should set error reason when returning the error. See
336 * `llhttp_set_error_reason()` for details.
338 const char* llhttp_get_error_reason(const llhttp_t* parser);
340 /* Assign verbal description to the returned error. Must be called in user
341 * callbacks right before returning the errno.
343 * Note: `HPE_USER` error code might be useful in user callbacks.
345 void llhttp_set_error_reason(llhttp_t* parser, const char* reason);
347 /* Returns the pointer to the last parsed byte before the returned error. The
348 * pointer is relative to the `data` argument of `llhttp_execute()`.
350 * Note: this method might be useful for counting the number of parsed bytes.
352 const char* llhttp_get_error_pos(const llhttp_t* parser);
354 /* Returns textual name of error code */
355 const char* llhttp_errno_name(llhttp_errno_t err);
357 /* Returns textual name of HTTP method */
358 const char* llhttp_method_name(llhttp_method_t method);
361 /* Enables/disables lenient header value parsing (disabled by default).
363 * Lenient parsing disables header value token checks, extending llhttp's
364 * protocol support to highly non-compliant clients/server. No
365 * `HPE_INVALID_HEADER_TOKEN` will be raised for incorrect header values when
366 * lenient parsing is "on".
368 * **(USE AT YOUR OWN RISK)**
370 void llhttp_set_lenient(llhttp_t* parser, int enabled);
375 #endif /* INCLUDE_LLHTTP_API_H_ */
377 #endif /* INCLUDE_LLHTTP_H_ */