5 # define llhttp_t llparse_t
8 int llhttp_message_needs_eof(const llhttp_t* parser);
9 int llhttp_should_keep_alive(const llhttp_t* parser);
11 int llhttp__before_headers_complete(llhttp_t* parser, const char* p,
13 /* Set this here so that on_headers_complete() callbacks can see it */
14 if ((parser->flags & F_UPGRADE) &&
15 (parser->flags & F_CONNECTION_UPGRADE)) {
16 /* For responses, "Upgrade: foo" and "Connection: upgrade" are
17 * mandatory only when it is a 101 Switching Protocols response,
18 * otherwise it is purely informational, to announce support.
21 (parser->type == HTTP_REQUEST || parser->status_code == 101);
23 parser->upgrade = (parser->method == HTTP_CONNECT);
30 * 0 - No body, `restart`, message_complete
31 * 1 - CONNECT request, `restart`, message_complete, and pause
32 * 2 - chunk_size_start
34 * 4 - body_identity_eof
35 * 5 - invalid transfer-encoding for request
37 int llhttp__after_headers_complete(llhttp_t* parser, const char* p,
41 hasBody = parser->flags & F_CHUNKED || parser->content_length > 0;
42 if (parser->upgrade && (parser->method == HTTP_CONNECT ||
43 (parser->flags & F_SKIPBODY) || !hasBody)) {
44 /* Exit, the rest of the message is in a different protocol. */
48 if (parser->flags & F_SKIPBODY) {
50 } else if (parser->flags & F_CHUNKED) {
51 /* chunked encoding - ignore Content-Length header, prepare for a chunk */
53 } else if (parser->flags & F_TRANSFER_ENCODING) {
54 if (parser->type == HTTP_REQUEST &&
55 (parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0) {
58 /* If a Transfer-Encoding header field
59 * is present in a request and the chunked transfer coding is not
60 * the final encoding, the message body length cannot be determined
61 * reliably; the server MUST respond with the 400 (Bad Request)
62 * status code and then close the connection.
68 /* If a Transfer-Encoding header field is present in a response and
69 * the chunked transfer coding is not the final encoding, the
70 * message body length is determined by reading the connection until
71 * it is closed by the server.
76 if (!(parser->flags & F_CONTENT_LENGTH)) {
77 if (!llhttp_message_needs_eof(parser)) {
78 /* Assume content-length 0 - read the next */
81 /* Read body until EOF */
84 } else if (parser->content_length == 0) {
85 /* Content-Length header given but zero: Content-Length: 0\r\n */
88 /* Content-Length header given and non-zero */
95 int llhttp__after_message_complete(llhttp_t* parser, const char* p,
97 int should_keep_alive;
99 should_keep_alive = llhttp_should_keep_alive(parser);
100 parser->finish = HTTP_FINISH_SAFE;
103 /* NOTE: this is ignored in loose parsing mode */
104 return should_keep_alive;
108 int llhttp_message_needs_eof(const llhttp_t* parser) {
109 if (parser->type == HTTP_REQUEST) {
113 /* See RFC 2616 section 4.4 */
114 if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
115 parser->status_code == 204 || /* No Content */
116 parser->status_code == 304 || /* Not Modified */
117 (parser->flags & F_SKIPBODY)) { /* response to a HEAD request */
121 /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */
122 if ((parser->flags & F_TRANSFER_ENCODING) &&
123 (parser->flags & F_CHUNKED) == 0) {
127 if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
135 int llhttp_should_keep_alive(const llhttp_t* parser) {
136 if (parser->http_major > 0 && parser->http_minor > 0) {
138 if (parser->flags & F_CONNECTION_CLOSE) {
142 /* HTTP/1.0 or earlier */
143 if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
148 return !llhttp_message_needs_eof(parser);