add support for "Expect: 100-continue"

This commit is contained in:
ipknHama 2014-05-02 21:54:25 +09:00
parent 23f9b52858
commit c19eed0285
3 changed files with 27 additions and 4 deletions

View File

@ -34,6 +34,18 @@ namespace crow
do_read();
}
void handle_header()
{
// HTTP 1.1 Expect: 100-continue
if (parser_.check_version(1, 1) && parser_.headers.count("expect") && parser_.headers["expect"] == "100-continue")
{
buffers_.clear();
static std::string expect_100_continue = "HTTP/1.1 100 Continue\r\n\r\n";
buffers_.emplace_back(expect_100_continue.data(), expect_100_continue.size());
do_write();
}
}
void handle()
{
static std::unordered_map<int, std::string> statusCodes = {
@ -61,13 +73,13 @@ namespace crow
bool is_invalid_request = false;
request req = parser_.to_request();
if (parser_.http_major == 1 && parser_.http_minor == 0)
if (parser_.check_version(1, 0))
{
// HTTP/1.0
if (!(req.headers.count("connection") && boost::iequals(req.headers["connection"],"Keep-Alive")))
close_connection_ = true;
}
else if (parser_.http_major == 1 && parser_.http_minor == 1)
else if (parser_.check_version(1, 1))
{
// HTTP/1.1
if (req.headers.count("connection") && req.headers["connection"] == "close")

View File

@ -66,6 +66,7 @@ namespace crow
boost::algorithm::to_lower(self->header_field);
self->headers.emplace(std::move(self->header_field), std::move(self->header_value));
}
self->process_header();
return 0;
}
static int on_body(http_parser* self_, const char* at, size_t length)
@ -118,16 +119,26 @@ namespace crow
body.clear();
}
void process_header()
{
handler_->handle_header();
}
void process_message()
{
handler_->handle();
}
request to_request()
request to_request() const
{
return request{(HTTPMethod)method, std::move(url), std::move(headers), std::move(body)};
}
bool check_version(int major, int minor) const
{
return http_major == major && http_minor == minor;
}
std::string url;
int header_building_state = 0;
std::string header_field;

View File

@ -241,7 +241,7 @@ TEST(server_handling_error_request)
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
//std::cerr << e.what() << std::endl;
}
}
server.stop();