diff --git a/http_parser.c b/http_parser.c index 263eaeb..bb99d76 100644 --- a/http_parser.c +++ b/http_parser.c @@ -1516,16 +1516,29 @@ size_t http_parser_execute (http_parser *parser, switch (h_state) { case h_general: - for (; p != data + len; p++) { - ch = *p; - if (ch == CR || ch == LF) { - --p; - break; - } + { + const char* p_cr; + const char* p_lf; + size_t limit = data + len - p; + + limit = MIN(limit, HTTP_MAX_HEADER_SIZE); + + p_cr = memchr(p, CR, limit); + p_lf = memchr(p, LF, limit); + if (p_cr != NULL) { + if (p_lf != NULL && p_cr >= p_lf) + p = p_lf; + else + p = p_cr; + } else if (p_lf != NULL) { + p = p_lf; + } else { + p = data + len; } - if (p == data + len) - --p; + --p; + break; + } case h_connection: case h_transfer_encoding: