diff --git a/client_http.hpp b/client_http.hpp index 080e5a3..9a7d981 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -127,26 +127,15 @@ namespace SimpleWeb { virtual ~ClientBase() {} - /// Synchronous request. The io_service is run within this function. + /// Convenience function to perform synchronous request. The io_service is run within this function. + /// If reusing the io_service for other tasks, please use the asynchronous request functions instead. std::shared_ptr request(const std::string& method, const std::string& path=std::string("/"), string_view content="", const CaseInsensitiveMultimap& header=CaseInsensitiveMultimap()) { - auto session=std::make_shared(io_service, get_connection(), create_request_header(method, path, header)); std::shared_ptr response; - session->callback=[this, &response, session](const error_code &ec) { - { - std::lock_guard lock(*connections_mutex); - session->connection->in_use=false; - } - response=session->response; + request(method, path, content, header, [&response](std::shared_ptr response_, const error_code &ec) { + response=response_; if(ec) throw system_error(ec); - }; - - std::ostream write_stream(session->request_buffer.get()); - if(content.size()>0) - write_stream << "Content-Length: " << content.size() << "\r\n"; - write_stream << "\r\n" << content; - - Client::connect(session); + }); io_service->reset(); io_service->run(); @@ -154,31 +143,15 @@ namespace SimpleWeb { return response; } - /// Synchronous request. The io_service is run within this function. + /// Convenience function to perform synchronous request. The io_service is run within this function. + /// If reusing the io_service for other tasks, please use the asynchronous request functions instead. std::shared_ptr request(const std::string& method, const std::string& path, std::iostream& content, const CaseInsensitiveMultimap& header=CaseInsensitiveMultimap()) { - auto session=std::make_shared(io_service, get_connection(), create_request_header(method, path, header)); std::shared_ptr response; - session->callback=[this, &response, session](const error_code &ec) { - { - std::lock_guard lock(*connections_mutex); - session->connection->in_use=false; - } - response=session->response; + request(method, path, content, header, [&response](std::shared_ptr response_, const error_code &ec) { + response=response_; if(ec) throw system_error(ec); - }; - - content.seekp(0, std::ios::end); - auto content_length=content.tellp(); - content.seekp(0, std::ios::beg); - std::ostream write_stream(session->request_buffer.get()); - if(content_length>0) - write_stream << "Content-Length: " << content_length << "\r\n"; - write_stream << "\r\n"; - if(content_length>0) - write_stream << content.rdbuf(); - - Client::connect(session); + }); io_service->reset(); io_service->run();