From c6f830f54dfaaf95250cd2bc4f097caf96ef45d9 Mon Sep 17 00:00:00 2001 From: Seva Date: Wed, 14 Aug 2019 02:30:19 -0400 Subject: [PATCH] http: fix refactoring memory issues --- src/http.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/http.cpp b/src/http.cpp index b69eec7c..cf76e88c 100644 --- a/src/http.cpp +++ b/src/http.cpp @@ -26,7 +26,7 @@ constexpr char HTTP_HEADER_CONTENT_LENGTH[] = "Content-Length"; constexpr char HTTP_HEADER_CONTENT_TYPE[] = "Content-Type"; constexpr char HTTP_HEADER_CONTENT_TYPE_JSON[] = "application/json"; constexpr char HTTP_HEADER_DELIM[] = "\r\n\r\n"; -constexpr char JSON_VALUE_DELIM = '\n'; +constexpr char JSON_VALUE_DELIM[] = "\n"; // connection @@ -65,7 +65,7 @@ Connection::Connection(asio::io_context& ctx, std::shared_ptrd("[http:connection:%i] [ssl] using certificate:\n%s", id_, cert.c_str()); + logger_->d("[http:connection:%i] [ssl] using %s certificate", id_, certificate->getUID().c_str()); ssl_ctx_->set_verify_mode(asio::ssl::verify_peer | asio::ssl::verify_fail_if_no_peer_cert); ssl_socket_ = std::make_unique(ctx_, ssl_ctx_); @@ -75,15 +75,20 @@ Connection::Connection(asio::io_context& ctx, std::shared_ptrcancel(); - ssl_socket_->close(); + ssl_socket_->cancel(ec); + ssl_socket_->close(ec); } else { - socket_->close(); + socket_->cancel(ec); + socket_->close(ec); } + if (ec and logger_) + logger_->e("[http:connection:%i] error closing: %s", id_, ec.message().c_str()); } + ssl_socket_.reset(); socket_.reset(); } @@ -816,7 +821,7 @@ Request::handle_request(const asio::error_code& ec) // read response notify_state_change(State::RECEIVING); conn_->async_read_until(HTTP_HEADER_DELIM, std::bind(&Request::handle_response_header, - this, std::placeholders::_1)); + this, std::placeholders::_1)); } void @@ -875,7 +880,7 @@ Request::handle_response_header(const asio::error_code& ec) // server wants to keep sending or we have content-length defined else if (response_.headers[HTTP_HEADER_CONNECTION] == HTTP_HEADER_CONNECTION_KEEP_ALIVE) { - conn_->async_read_until(&JSON_VALUE_DELIM, + conn_->async_read_until(JSON_VALUE_DELIM, std::bind(&Request::handle_response_body, this, std::placeholders::_1, std::placeholders::_2)); } else if (connection_type_ == restinio::http_connection_header_t::close) @@ -917,7 +922,7 @@ Request::handle_response_body(const asio::error_code& ec, const size_t bytes) } // read and parse the chunked encoding fragment else { - auto body = conn_->read_until(JSON_VALUE_DELIM) + '\n'; + auto body = conn_->read_until(JSON_VALUE_DELIM[0]) + '\n'; response_.body = body; parse_request(body); } @@ -934,7 +939,7 @@ Request::handle_response_body(const asio::error_code& ec, const size_t bytes) std::bind(&Request::handle_response_body, this, std::placeholders::_1, std::placeholders::_2)); // server wants to keep sending else if (response_.headers[HTTP_HEADER_CONNECTION] == HTTP_HEADER_CONNECTION_KEEP_ALIVE){ - conn_->async_read_until(&JSON_VALUE_DELIM, + conn_->async_read_until(JSON_VALUE_DELIM, std::bind(&Request::handle_response_body, this, std::placeholders::_1, std::placeholders::_2)); } else if (connection_type_ == restinio::http_connection_header_t::close)