From 2b5ae3d0d1b4e1204c67214b87f85aee9dad7ad0 Mon Sep 17 00:00:00 2001 From: eidheim Date: Wed, 19 Feb 2020 11:58:54 +0100 Subject: [PATCH] Client: removed duplicate code --- client_http.hpp | 153 +++++++++++++++++------------------------------- 1 file changed, 55 insertions(+), 98 deletions(-) diff --git a/client_http.hpp b/client_http.hpp index afd1f64..2b790b8 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -218,55 +218,7 @@ namespace SimpleWeb { /// If you reuse the io_service for other tasks, use the asynchronous request functions instead. /// When requesting Server-Sent Events: will throw on error::eof, please use asynchronous request functions instead. std::shared_ptr request(const std::string &method, const std::string &path = {"/"}, string_view content = {}, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { - { - LockGuard lock(synchronous_request_mutex); - if(!synchronous_request_called) { - if(io_service) // Throw if io_service already set - throw make_error_code::make_error_code(errc::operation_not_permitted); - io_service = std::make_shared(); - internal_io_service = true; - auto io_service_ = io_service; - std::thread thread([io_service_] { - auto work = make_work_guard(*io_service_); - io_service_->run(); - }); - thread.detach(); - synchronous_request_called = true; - } - } - - std::shared_ptr response; - std::promise> response_promise; - bool stop_future_handlers = false; - request(method, path, content, header, [&response, &response_promise, &stop_future_handlers](std::shared_ptr response_, error_code ec) { - if(stop_future_handlers) - return; - - if(!response) - response = response_; - else if(!ec) { - if(response_->streambuf.size() + response->streambuf.size() > response->streambuf.max_size()) { - ec = make_error_code::make_error_code(errc::message_size); - response->close(); - } - else { - // Move partial response_ content to response: - auto &source = response_->streambuf; - auto &target = response->streambuf; - target.commit(asio::buffer_copy(target.prepare(source.size()), source.data())); - source.consume(source.size()); - } - } - - if(ec) { - response_promise.set_exception(std::make_exception_ptr(system_error(ec))); - stop_future_handlers = true; - } - else if(response_->content.end) - response_promise.set_value(response); - }); - - return response_promise.get_future().get(); + return sync_request(method, path, content, header); } /// Convenience function to perform synchronous request. The io_service is started in this function. @@ -274,55 +226,7 @@ namespace SimpleWeb { /// If you reuse the io_service for other tasks, use the asynchronous request functions instead. /// When requesting Server-Sent Events: will throw on error::eof, please use asynchronous request functions instead. std::shared_ptr request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header = CaseInsensitiveMultimap()) { - { - LockGuard lock(synchronous_request_mutex); - if(!synchronous_request_called) { - if(io_service) // Throw if io_service already set - throw make_error_code::make_error_code(errc::operation_not_permitted); - io_service = std::make_shared(); - internal_io_service = true; - auto io_service_ = io_service; - std::thread thread([io_service_] { - auto work = make_work_guard(*io_service_); - io_service_->run(); - }); - thread.detach(); - synchronous_request_called = 1; - } - } - - std::shared_ptr response; - std::promise> response_promise; - bool stop_future_handlers = false; - request(method, path, content, header, [&response, &response_promise, &stop_future_handlers](std::shared_ptr response_, error_code ec) { - if(stop_future_handlers) - return; - - if(!response) - response = response_; - else if(!ec) { - if(response_->streambuf.size() + response->streambuf.size() > response->streambuf.max_size()) { - ec = make_error_code::make_error_code(errc::message_size); - response->close(); - } - else { - // Move partial response_ content to response: - auto &source = response_->streambuf; - auto &target = response->streambuf; - target.commit(asio::buffer_copy(target.prepare(source.size()), source.data())); - source.consume(source.size()); - } - } - - if(ec) { - response_promise.set_exception(std::make_exception_ptr(system_error(ec))); - stop_future_handlers = true; - } - else if(response_->content.end) - response_promise.set_value(response); - }); - - return response_promise.get_future().get(); + return sync_request(method, path, content, header); } /// Asynchronous request where running Client's io_service is required. @@ -507,6 +411,59 @@ namespace SimpleWeb { port = parsed_host_port.second; } + template + std::shared_ptr sync_request(const std::string &method, const std::string &path, ContentType &content, const CaseInsensitiveMultimap &header) { + { + LockGuard lock(synchronous_request_mutex); + if(!synchronous_request_called) { + if(io_service) // Throw if io_service already set + throw make_error_code::make_error_code(errc::operation_not_permitted); + io_service = std::make_shared(); + internal_io_service = true; + auto io_service_ = io_service; + std::thread thread([io_service_] { + auto work = make_work_guard(*io_service_); + io_service_->run(); + }); + thread.detach(); + synchronous_request_called = true; + } + } + + std::shared_ptr response; + std::promise> response_promise; + bool stop_future_handlers = false; + request(method, path, content, header, [&response, &response_promise, &stop_future_handlers](std::shared_ptr response_, error_code ec) { + if(stop_future_handlers) + return; + + if(!response) + response = response_; + else if(!ec) { + if(response_->streambuf.size() + response->streambuf.size() > response->streambuf.max_size()) { + ec = make_error_code::make_error_code(errc::message_size); + response->close(); + } + else { + // Move partial response_ content to response: + auto &source = response_->streambuf; + auto &target = response->streambuf; + target.commit(asio::buffer_copy(target.prepare(source.size()), source.data())); + source.consume(source.size()); + } + } + + if(ec) { + response_promise.set_exception(std::make_exception_ptr(system_error(ec))); + stop_future_handlers = true; + } + else if(response_->content.end) + response_promise.set_value(response); + }); + + return response_promise.get_future().get(); + } + std::shared_ptr get_connection() noexcept { std::shared_ptr connection; LockGuard lock(connections_mutex);