Skip to content

Commit

Permalink
Prevent HTTP pipelining which Crow doesn't support.
Browse files Browse the repository at this point in the history
(cherry picked from commit fba01dc)
The-EDev committed Aug 24, 2022
1 parent 62dae4c commit 0aff1bc
Showing 3 changed files with 17 additions and 12 deletions.
6 changes: 5 additions & 1 deletion include/crow/http_connection.h
Original file line number Diff line number Diff line change
@@ -83,6 +83,7 @@ namespace crow
if (!ec)
{
start_deadline();
parser_.clear();

do_read();
}
@@ -127,7 +128,7 @@ namespace crow
is_invalid_request = true;
res = response(400);
}
if (req.upgrade)
else if (req.upgrade)
{
// h2 or h2c headers
if (req.get_header_value("upgrade").substr(0, 2) == "h2")
@@ -395,6 +396,7 @@ namespace crow
res.end();
res.clear();
buffers_.clear();
parser_.clear();
}

void do_write_general()
@@ -452,6 +454,7 @@ namespace crow
res.end();
res.clear();
buffers_.clear();
parser_.clear();
}
}

@@ -513,6 +516,7 @@ namespace crow
is_writing = false;
res.clear();
res_body_copy_.clear();
parser_.clear();
if (!ec)
{
if (close_connection_)
8 changes: 0 additions & 8 deletions include/crow/http_parser_merged.h
Original file line number Diff line number Diff line change
@@ -1576,7 +1576,6 @@ static const int8_t unhex[256] =

if (parser->flags & F_TRAILING) {
/* End of a chunked request */
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
break;
}
@@ -1652,14 +1651,12 @@ static const int8_t unhex[256] =

/* Exit, the rest of the connect is in a different protocol. */
if (parser->upgrade) {
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
parser->nread = nread;
return (p - data) + 1;
}

if (parser->flags & F_SKIPBODY) {
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
} else if (parser->flags & F_CHUNKED) {
/* chunked encoding - ignore Content-Length header,
@@ -1699,7 +1696,6 @@ static const int8_t unhex[256] =
if (parser->content_length == 0)
{
/* Content-Length header given but zero: Content-Length: 0\r\n */
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
}
else if (parser->content_length != CROW_ULLONG_MAX)
@@ -1710,7 +1706,6 @@ static const int8_t unhex[256] =
else
{
/* Assume content-length 0 - read the next */
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
}
}
@@ -1762,7 +1757,6 @@ static const int8_t unhex[256] =
break;

case s_message_done:
parser->state = CROW_NEW_MESSAGE();
CROW_CALLBACK_NOTIFY(message_complete);
break;

@@ -2007,9 +2001,7 @@ http_parser_set_max_header_size(uint32_t size) {
#undef CROW_TOKEN
#undef CROW_IS_URL_CHAR
//#undef CROW_IS_HOST_CHAR
#undef CROW_start_state
#undef CROW_STRICT_CHECK
#undef CROW_NEW_MESSAGE

}
}
15 changes: 12 additions & 3 deletions include/crow/parser.h
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@
#include <boost/algorithm/string.hpp>
#include <algorithm>

#include "crow/http_parser_merged.h"
#include "crow/http_request.h"
#include "crow/http_parser_merged.h"

namespace crow
{
@@ -20,7 +20,6 @@ namespace crow
static int on_message_begin(http_parser* self_)
{
HTTPParser* self = static_cast<HTTPParser*>(self_);
self->clear();
return 0;
}
static int on_url(http_parser* self_, const char* at, size_t length)
@@ -85,7 +84,8 @@ namespace crow
static int on_message_complete(http_parser* self_)
{
HTTPParser* self = static_cast<HTTPParser*>(self_);


self->message_complete = true;
// url params
self->url = self->raw_url.substr(0, self->qs_point != 0 ? self->qs_point : std::string::npos);
self->url_params = query_string(self->raw_url);
@@ -103,6 +103,9 @@ namespace crow
/// Parse a buffer into the different sections of an HTTP request.
bool feed(const char* buffer, int length)
{
if (message_complete)
return true;

const static http_parser_settings settings_{
on_message_begin,
on_url,
@@ -139,6 +142,8 @@ namespace crow
qs_point = 0;
http_major = 0;
http_minor = 0;
message_complete = false;
state = CROW_NEW_MESSAGE();
keep_alive = false;
close_connection = false;
}
@@ -178,6 +183,7 @@ namespace crow
std::string url;

int header_building_state = 0;
bool message_complete = false;
std::string header_field;
std::string header_value;
ci_map headers;
@@ -189,3 +195,6 @@ namespace crow
Handler* handler_; ///< This is currently an HTTP connection object (\ref crow.Connection).
};
} // namespace crow

#undef CROW_NEW_MESSAGE
#undef CROW_start_state

0 comments on commit 0aff1bc

Please sign in to comment.