From b81e76f68ad7834f969518ba6b1e161ecb4a7517 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Tue, 2 Feb 2021 04:57:34 +0300 Subject: [PATCH] Update http_connection.h, http_request.h, and http_response.h --- include/crow/http_connection.h | 24 ++++++++++++++++-------- include/crow/http_request.h | 2 ++ include/crow/http_response.h | 11 +++++++++++ 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index 62d8a91eb..5485bc0d0 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -319,6 +319,7 @@ namespace crow ctx_ = detail::context(); req.middleware_context = (void*)&ctx_; req.io_service = &adaptor_.get_io_service(); + req.adaptor = &adaptor_; detail::middleware_call_helper<0, decltype(ctx_), decltype(*middlewares_), Middlewares...>(*middlewares_, req, res, ctx_); if (!res.completed_) @@ -393,15 +394,22 @@ namespace crow res.set_header("location", location); } - prepare_buffers(); - CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << ' ' << close_connection_; - if (res.is_static_type()) + prepare_buffers(); + CROW_LOG_INFO << "Response: " << this << ' ' << req_.raw_url << ' ' << res.code << (res.manual_ ? " Manual " : " ") << close_connection_; + if (!res.manual_) { - do_write_static(); - }else { - do_write_general(); + if (res.is_static_type()) + { + do_write_static(); + }else { + do_write_general(); + } + } + else + { + is_writing = true; + boost::asio::write(adaptor_.socket(), buffers_); } - } private: @@ -472,7 +480,7 @@ namespace crow } - if (!res.headers.count("content-length")) + if (!res.headers.count("content-length") && !res.manual_) { content_length_ = std::to_string(res.body.size()); static std::string content_length_tag = "Content-Length: "; diff --git a/include/crow/http_request.h b/include/crow/http_request.h index eb7e902ba..026e72cb4 100644 --- a/include/crow/http_request.h +++ b/include/crow/http_request.h @@ -5,6 +5,7 @@ #include "crow/common.h" #include "crow/ci_map.h" #include "crow/query_string.h" +#include "crow/socket_adaptors.h" namespace crow { @@ -35,6 +36,7 @@ namespace crow void* middleware_context{}; boost::asio::io_service* io_service{}; + SocketAdaptor* adaptor; /// Construct an empty request. (sets the method to `GET`) request() diff --git a/include/crow/http_response.h b/include/crow/http_response.h index e4d37d2c5..2e6e77fa9 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -232,10 +232,21 @@ namespace crow } } + template + void send_chunk(Adaptor& adaptor, const std::string& data) + { + manual_ = true; + end(); + std::vector buffers; + buffers.push_back(boost::asio::buffer(data)); + write_buffer_list(buffers, adaptor); + } + private: bool completed_{}; std::function complete_request_handler_; std::function is_alive_helper_; + bool manual_ = false; static_file_info file_info; template