| 1 | #include <stdio.h> |
| 2 | #ifndef LLHTTP__TEST |
| 3 | # include "llhttp.h" |
| 4 | #else |
| 5 | # define llhttp_t llparse_t |
| 6 | #endif /* */ |
| 7 | |
| 8 | int llhttp_message_needs_eof(const llhttp_t* parser); |
| 9 | int llhttp_should_keep_alive(const llhttp_t* parser); |
| 10 | |
| 11 | int (llhttp_t* parser, const char* p, |
| 12 | const char* endp) { |
| 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. |
| 19 | */ |
| 20 | parser->upgrade = |
| 21 | (parser->type == HTTP_REQUEST || parser->status_code == 101); |
| 22 | } else { |
| 23 | parser->upgrade = (parser->method == HTTP_CONNECT); |
| 24 | } |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | /* Return values: |
| 30 | * 0 - No body, `restart`, message_complete |
| 31 | * 1 - CONNECT request, `restart`, message_complete, and pause |
| 32 | * 2 - chunk_size_start |
| 33 | * 3 - body_identity |
| 34 | * 4 - body_identity_eof |
| 35 | * 5 - invalid transfer-encoding for request |
| 36 | */ |
| 37 | int (llhttp_t* parser, const char* p, |
| 38 | const char* endp) { |
| 39 | int hasBody; |
| 40 | |
| 41 | hasBody = parser->flags & F_CHUNKED || parser->content_length > 0; |
| 42 | if ( |
| 43 | (parser->upgrade && (parser->method == HTTP_CONNECT || |
| 44 | (parser->flags & F_SKIPBODY) || !hasBody)) || |
| 45 | /* See RFC 2616 section 4.4 - 1xx e.g. Continue */ |
| 46 | (parser->type == HTTP_RESPONSE && parser->status_code == 101) |
| 47 | ) { |
| 48 | /* Exit, the rest of the message is in a different protocol. */ |
| 49 | return 1; |
| 50 | } |
| 51 | |
| 52 | if (parser->type == HTTP_RESPONSE && parser->status_code == 100) { |
| 53 | /* No body, restart as the message is complete */ |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* See RFC 2616 section 4.4 */ |
| 58 | if ( |
| 59 | parser->flags & F_SKIPBODY || /* response to a HEAD request */ |
| 60 | ( |
| 61 | parser->type == HTTP_RESPONSE && ( |
| 62 | parser->status_code == 102 || /* Processing */ |
| 63 | parser->status_code == 103 || /* Early Hints */ |
| 64 | parser->status_code == 204 || /* No Content */ |
| 65 | parser->status_code == 304 /* Not Modified */ |
| 66 | ) |
| 67 | ) |
| 68 | ) { |
| 69 | return 0; |
| 70 | } else if (parser->flags & F_CHUNKED) { |
| 71 | /* chunked encoding - ignore Content-Length header, prepare for a chunk */ |
| 72 | return 2; |
| 73 | } else if (parser->flags & F_TRANSFER_ENCODING) { |
| 74 | if (parser->type == HTTP_REQUEST && |
| 75 | (parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 && |
| 76 | (parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) { |
| 77 | /* RFC 7230 3.3.3 */ |
| 78 | |
| 79 | /* If a Transfer-Encoding header field |
| 80 | * is present in a request and the chunked transfer coding is not |
| 81 | * the final encoding, the message body length cannot be determined |
| 82 | * reliably; the server MUST respond with the 400 (Bad Request) |
| 83 | * status code and then close the connection. |
| 84 | */ |
| 85 | return 5; |
| 86 | } else { |
| 87 | /* RFC 7230 3.3.3 */ |
| 88 | |
| 89 | /* If a Transfer-Encoding header field is present in a response and |
| 90 | * the chunked transfer coding is not the final encoding, the |
| 91 | * message body length is determined by reading the connection until |
| 92 | * it is closed by the server. |
| 93 | */ |
| 94 | return 4; |
| 95 | } |
| 96 | } else { |
| 97 | if (!(parser->flags & F_CONTENT_LENGTH)) { |
| 98 | if (!llhttp_message_needs_eof(parser)) { |
| 99 | /* Assume content-length 0 - read the next */ |
| 100 | return 0; |
| 101 | } else { |
| 102 | /* Read body until EOF */ |
| 103 | return 4; |
| 104 | } |
| 105 | } else if (parser->content_length == 0) { |
| 106 | /* Content-Length header given but zero: Content-Length: 0\r\n */ |
| 107 | return 0; |
| 108 | } else { |
| 109 | /* Content-Length header given and non-zero */ |
| 110 | return 3; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | int llhttp__after_message_complete(llhttp_t* parser, const char* p, |
| 117 | const char* endp) { |
| 118 | int should_keep_alive; |
| 119 | |
| 120 | should_keep_alive = llhttp_should_keep_alive(parser); |
| 121 | parser->finish = HTTP_FINISH_SAFE; |
| 122 | parser->flags = 0; |
| 123 | |
| 124 | /* NOTE: this is ignored in loose parsing mode */ |
| 125 | return should_keep_alive; |
| 126 | } |
| 127 | |
| 128 | |
| 129 | int llhttp_message_needs_eof(const llhttp_t* parser) { |
| 130 | if (parser->type == HTTP_REQUEST) { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* See RFC 2616 section 4.4 */ |
| 135 | if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */ |
| 136 | parser->status_code == 204 || /* No Content */ |
| 137 | parser->status_code == 304 || /* Not Modified */ |
| 138 | (parser->flags & F_SKIPBODY)) { /* response to a HEAD request */ |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* RFC 7230 3.3.3, see `llhttp__after_headers_complete` */ |
| 143 | if ((parser->flags & F_TRANSFER_ENCODING) && |
| 144 | (parser->flags & F_CHUNKED) == 0) { |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) { |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | return 1; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | int llhttp_should_keep_alive(const llhttp_t* parser) { |
| 157 | if (parser->http_major > 0 && parser->http_minor > 0) { |
| 158 | /* HTTP/1.1 */ |
| 159 | if (parser->flags & F_CONNECTION_CLOSE) { |
| 160 | return 0; |
| 161 | } |
| 162 | } else { |
| 163 | /* HTTP/1.0 or earlier */ |
| 164 | if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) { |
| 165 | return 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return !llhttp_message_needs_eof(parser); |
| 170 | } |
| 171 | |