diff options
| author | Roland Reichwein <mail@reichwein.it> | 2020-04-27 19:02:35 +0200 | 
|---|---|---|
| committer | Roland Reichwein <mail@reichwein.it> | 2020-04-27 19:02:35 +0200 | 
| commit | 37da545bc5f1bd5c3d810699cc685c36cdcd27fa (patch) | |
| tree | 19d3f521d59dd2be660bb0c1bf99b72a279ff859 | |
| parent | 0d8329bd3aea1874f6fd41c066ec45fd73607504 (diff) | |
Fix broken pipe
| -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_));  | 
