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