mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: The node.js HTTP parser library that we use has been unmaintained for some time. We should move to the maintained replacement, which is llhttp. This patch will update our dependency and bring us over to the new library, as well as make any changes we need. Modifications: This patch comes in 4 parts, each contained in a separate commit in the PR. The first commit drops the existing http_parser code and updates some of the repo state for using llhttp. The second commit rewrites the update script to bring in llhttp instead of http_parser. The third runs the actual script. You can skip reviewing this except to sanity check the outcome. The fourth commit updates the NIO code and the tests to get everything working. In general the substance of the product modifications was minimal. The logic around keeping track of where we are in the buffer and how upgrades work has changed a bit, so that required some fiddling. I also had to add an error reporting path for the delegates to be able to throw specific errors that llhttp no longer checks for. Finally, I removed two tests that were a little overzealous and that llhttp does not police. Result: Back on the supported path.
154 lines
4.6 KiB
C
154 lines
4.6 KiB
C
/* Additional changes for SwiftNIO:
|
|
- prefixed all symbols by 'c_nio_'
|
|
*/
|
|
#include <stdio.h>
|
|
#ifndef LLHTTP__TEST
|
|
# include "include/c_nio_llhttp.h"
|
|
#else
|
|
# define llhttp_t llparse_t
|
|
#endif /* */
|
|
|
|
int c_nio_llhttp_message_needs_eof(const llhttp_t* parser);
|
|
int c_nio_llhttp_should_keep_alive(const llhttp_t* parser);
|
|
|
|
int c_nio_llhttp__before_headers_complete(llhttp_t* parser, const char* p,
|
|
const char* endp) {
|
|
/* Set this here so that on_headers_complete() callbacks can see it */
|
|
if ((parser->flags & F_UPGRADE) &&
|
|
(parser->flags & F_CONNECTION_UPGRADE)) {
|
|
/* For responses, "Upgrade: foo" and "Connection: upgrade" are
|
|
* mandatory only when it is a 101 Switching Protocols response,
|
|
* otherwise it is purely informational, to announce support.
|
|
*/
|
|
parser->upgrade =
|
|
(parser->type == HTTP_REQUEST || parser->status_code == 101);
|
|
} else {
|
|
parser->upgrade = (parser->method == HTTP_CONNECT);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/* Return values:
|
|
* 0 - No body, `restart`, message_complete
|
|
* 1 - CONNECT request, `restart`, message_complete, and pause
|
|
* 2 - chunk_size_start
|
|
* 3 - body_identity
|
|
* 4 - body_identity_eof
|
|
* 5 - invalid transfer-encoding for request
|
|
*/
|
|
int c_nio_llhttp__after_headers_complete(llhttp_t* parser, const char* p,
|
|
const char* endp) {
|
|
int hasBody;
|
|
|
|
hasBody = parser->flags & F_CHUNKED || parser->content_length > 0;
|
|
if (parser->upgrade && (parser->method == HTTP_CONNECT ||
|
|
(parser->flags & F_SKIPBODY) || !hasBody)) {
|
|
/* Exit, the rest of the message is in a different protocol. */
|
|
return 1;
|
|
}
|
|
|
|
if (parser->flags & F_SKIPBODY) {
|
|
return 0;
|
|
} else if (parser->flags & F_CHUNKED) {
|
|
/* chunked encoding - ignore Content-Length header, prepare for a chunk */
|
|
return 2;
|
|
} else if (parser->flags & F_TRANSFER_ENCODING) {
|
|
if (parser->type == HTTP_REQUEST &&
|
|
(parser->lenient_flags & LENIENT_CHUNKED_LENGTH) == 0 &&
|
|
(parser->lenient_flags & LENIENT_TRANSFER_ENCODING) == 0) {
|
|
/* RFC 7230 3.3.3 */
|
|
|
|
/* If a Transfer-Encoding header field
|
|
* is present in a request and the chunked transfer coding is not
|
|
* the final encoding, the message body length cannot be determined
|
|
* reliably; the server MUST respond with the 400 (Bad Request)
|
|
* status code and then close the connection.
|
|
*/
|
|
return 5;
|
|
} else {
|
|
/* RFC 7230 3.3.3 */
|
|
|
|
/* If a Transfer-Encoding header field is present in a response and
|
|
* the chunked transfer coding is not the final encoding, the
|
|
* message body length is determined by reading the connection until
|
|
* it is closed by the server.
|
|
*/
|
|
return 4;
|
|
}
|
|
} else {
|
|
if (!(parser->flags & F_CONTENT_LENGTH)) {
|
|
if (!c_nio_llhttp_message_needs_eof(parser)) {
|
|
/* Assume content-length 0 - read the next */
|
|
return 0;
|
|
} else {
|
|
/* Read body until EOF */
|
|
return 4;
|
|
}
|
|
} else if (parser->content_length == 0) {
|
|
/* Content-Length header given but zero: Content-Length: 0\r\n */
|
|
return 0;
|
|
} else {
|
|
/* Content-Length header given and non-zero */
|
|
return 3;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
int c_nio_llhttp__after_message_complete(llhttp_t* parser, const char* p,
|
|
const char* endp) {
|
|
int should_keep_alive;
|
|
|
|
should_keep_alive = c_nio_llhttp_should_keep_alive(parser);
|
|
parser->finish = HTTP_FINISH_SAFE;
|
|
parser->flags = 0;
|
|
|
|
/* NOTE: this is ignored in loose parsing mode */
|
|
return should_keep_alive;
|
|
}
|
|
|
|
|
|
int c_nio_llhttp_message_needs_eof(const llhttp_t* parser) {
|
|
if (parser->type == HTTP_REQUEST) {
|
|
return 0;
|
|
}
|
|
|
|
/* See RFC 2616 section 4.4 */
|
|
if (parser->status_code / 100 == 1 || /* 1xx e.g. Continue */
|
|
parser->status_code == 204 || /* No Content */
|
|
parser->status_code == 304 || /* Not Modified */
|
|
(parser->flags & F_SKIPBODY)) { /* response to a HEAD request */
|
|
return 0;
|
|
}
|
|
|
|
/* RFC 7230 3.3.3, see `c_nio_llhttp__after_headers_complete` */
|
|
if ((parser->flags & F_TRANSFER_ENCODING) &&
|
|
(parser->flags & F_CHUNKED) == 0) {
|
|
return 1;
|
|
}
|
|
|
|
if (parser->flags & (F_CHUNKED | F_CONTENT_LENGTH)) {
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
|
|
int c_nio_llhttp_should_keep_alive(const llhttp_t* parser) {
|
|
if (parser->http_major > 0 && parser->http_minor > 0) {
|
|
/* HTTP/1.1 */
|
|
if (parser->flags & F_CONNECTION_CLOSE) {
|
|
return 0;
|
|
}
|
|
} else {
|
|
/* HTTP/1.0 or earlier */
|
|
if (!(parser->flags & F_CONNECTION_KEEP_ALIVE)) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
return !c_nio_llhttp_message_needs_eof(parser);
|
|
}
|