From c19eed028535b847489ca03a43fc4dc42e7451cf Mon Sep 17 00:00:00 2001 From: ipknHama Date: Fri, 2 May 2014 21:54:25 +0900 Subject: [PATCH] add support for "Expect: 100-continue" --- http_connection.h | 16 ++++++++++++++-- parser.h | 13 ++++++++++++- unittest.cpp | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/http_connection.h b/http_connection.h index 5da294d9b..867196cf2 100644 --- a/http_connection.h +++ b/http_connection.h @@ -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 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") diff --git a/parser.h b/parser.h index 5e686b278..87d956241 100644 --- a/parser.h +++ b/parser.h @@ -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; diff --git a/unittest.cpp b/unittest.cpp index 40903c51c..000e2f570 100644 --- a/unittest.cpp +++ b/unittest.cpp @@ -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();