From 37da545bc5f1bd5c3d810699cc685c36cdcd27fa Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Mon, 27 Apr 2020 19:02:35 +0200 Subject: Fix broken pipe --- http.cpp | 16 ++++++++++------ https.cpp | 16 ++++++++++------ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/http.cpp b/http.cpp index ce3309f..6a488ed 100644 --- a/http.cpp +++ b/http.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -56,7 +57,7 @@ class session : public std::enable_shared_from_this #endif beast::flat_buffer buffer_; Server& m_server; - http::request_parser parser_; + std::optional> parser_; request_type req_; std::shared_ptr res_; @@ -107,7 +108,6 @@ public: #endif , m_server(server) { - parser_.body_limit(1000000000); // 1GB limit } // Start the asynchronous operation @@ -132,19 +132,24 @@ public: // Make the request empty before reading, // otherwise the operation behavior is undefined. req_ = {}; + + // this is the way to reset the parser. it's necessary. + // https://github.com/boostorg/beast/issues/927 + parser_.emplace(); + parser_->body_limit(1000000000); // 1GB limit #ifdef BOOST_LATEST // Set the timeout. stream_.expires_after(std::chrono::seconds(30)); // Read a request - http::async_read(stream_, buffer_, parser_, + http::async_read(stream_, buffer_, *parser_, beast::bind_front_handler( &session::on_read, shared_from_this())); #else - http::async_read(socket_, buffer_, parser_, + http::async_read(socket_, buffer_, *parser_, boost::asio::bind_executor( strand_, std::bind( @@ -175,8 +180,7 @@ public: if(ec) return fail(ec, "read"); - req_ = parser_.get(); - parser_.release(); + req_ = parser_->get(); // Send the response handle_request(m_server, std::move(req_)); diff --git a/https.cpp b/https.cpp index f02a1b7..cec0e4b 100644 --- a/https.cpp +++ b/https.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -90,7 +91,7 @@ class session : public std::enable_shared_from_this #endif beast::flat_buffer buffer_; Server& m_server; - http::request_parser parser_; + std::optional> parser_; // need to reset parser every time, no other mechanism currently http::request req_; std::shared_ptr res_; @@ -143,7 +144,6 @@ public: #endif , m_server(server) { - parser_.body_limit(1000000000); // 1GB limit } // Start the asynchronous operation @@ -208,18 +208,23 @@ public: // Make the request empty before reading, // otherwise the operation behavior is undefined. req_ = {}; + + // this is the way to reset the parser. it's necessary. + // https://github.com/boostorg/beast/issues/927 + parser_.emplace(); + parser_->body_limit(1000000000); // 1GB limit #ifdef BOOST_LATEST // Set the timeout. beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(30)); // Read a request - http::async_read(stream_, buffer_, parser_, + http::async_read(stream_, buffer_, *parser_, beast::bind_front_handler( &session::on_read, shared_from_this())); #else - http::async_read(stream_, buffer_, parser_, + http::async_read(stream_, buffer_, *parser_, boost::asio::bind_executor( strand_, std::bind( @@ -248,8 +253,7 @@ public: if(ec) return fail(ec, "read"); - req_ = parser_.get(); - parser_.release(); + req_ = parser_->get(); // Send the response handle_request(m_server, std::move(req_)); -- cgit v1.2.3