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 && (parser->flags & F_LENIENT) == 0) {
57 /* If a Transfer-Encoding header field
58 * is present in a request and the chunked transfer coding is not
59 * the final encoding, the message body length cannot be determined
60 * reliably; the server MUST respond with the 400 (Bad Request)
61 * status code and then close the connection.
67 /* If a Transfer-Encoding header field is present in a response and
68 * the chunked transfer coding is not the final encoding, the
69 * message body length is determined by reading the connection until
70 * it is closed by the server.
75 if (!(parser->flags & F_CONTENT_LENGTH)) {
76 if (!llhttp_message_needs_eof(parser)) {
77 /* Assume content-length 0 - read the next */
80 /* Read body until EOF */
83 } else if (parser->content_length == 0) {
84 /* Content-Length header given but zero: Content-Length: 0\r\n */
87 /* Content-Length header given and non-zero */
94 int llhttp__after_message_complete(llhttp_t* parser, const char* p,
96 int should_keep_alive;
98 should_keep_alive = llhttp_should_keep_alive(parser);
99 parser->finish = HTTP_FINISH_SAFE;
101 /* Keep `F_LENIENT` flag between messages, but reset every other flag */
102 parser->flags &= F_LENIENT;
104 /* NOTE: this is ignored in loose parsing mode */
105 return should_keep_alive;
109 int llhttp_message_needs_eof(const llhttp_t* parser) {
110 if (parser->type == HTTP_REQUEST) {
114 /* See RFC 2616 section 4.4 */
115 if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
116 parser->status_code == 204 || /* No Content */
117 parser->status_code == 304 || /* Not Modified */
118 (parser->flags & F_SKIPBODY)) { /* response to a HEAD request */
122 /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */
123 if ((parser->flags & F_TRANSFER_ENCODING) &&
124 (parser->flags & F_CHUNKED) == 0) {
128 if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
136 int llhttp_should_keep_alive(const llhttp_t* parser) {
137 if (parser->http_major > 0 && parser->http_minor > 0) {
139 if (parser->flags & F_CONNECTION_CLOSE) {
143 /* HTTP/1.0 or earlier */
144 if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
149 return !llhttp_message_needs_eof(parser);