Cleanup of synchronous Client::request functions

This commit is contained in:
eidheim 2017-06-22 22:32:51 +02:00
commit 361ac21518

View file

@ -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<Response> request(const std::string& method, const std::string& path=std::string("/"), string_view content="", const CaseInsensitiveMultimap& header=CaseInsensitiveMultimap()) {
auto session=std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
std::shared_ptr<Response> response;
session->callback=[this, &response, session](const error_code &ec) {
{
std::lock_guard<std::mutex> lock(*connections_mutex);
session->connection->in_use=false;
}
response=session->response;
request(method, path, content, header, [&response](std::shared_ptr<Response> 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<socket_type>::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<Response> request(const std::string& method, const std::string& path, std::iostream& content, const CaseInsensitiveMultimap& header=CaseInsensitiveMultimap()) {
auto session=std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
std::shared_ptr<Response> response;
session->callback=[this, &response, session](const error_code &ec) {
{
std::lock_guard<std::mutex> lock(*connections_mutex);
session->connection->in_use=false;
}
response=session->response;
request(method, path, content, header, [&response](std::shared_ptr<Response> 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<socket_type>::connect(session);
});
io_service->reset();
io_service->run();