diff options
-rw-r--r-- | http.cpp | 16 | ||||
-rw-r--r-- | https.cpp | 16 |
2 files changed, 20 insertions, 12 deletions
@@ -25,6 +25,7 @@ #include <functional> #include <iostream> #include <memory> +#include <optional> #include <string> #include <thread> #include <vector> @@ -56,7 +57,7 @@ class session : public std::enable_shared_from_this<session> #endif beast::flat_buffer buffer_; Server& m_server; - http::request_parser<http::string_body> parser_; + std::optional<http::request_parser<http::string_body>> parser_; request_type req_; std::shared_ptr<response_type> 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_)); @@ -29,6 +29,7 @@ #include <functional> #include <iostream> #include <memory> +#include <optional> #include <string> #include <thread> #include <vector> @@ -90,7 +91,7 @@ class session : public std::enable_shared_from_this<session> #endif beast::flat_buffer buffer_; Server& m_server; - http::request_parser<http::string_body> parser_; + std::optional<http::request_parser<http::string_body>> parser_; // need to reset parser every time, no other mechanism currently http::request<http::string_body> req_; std::shared_ptr<response_type> 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_)); |