diff --git a/client_http.hpp b/client_http.hpp index f3cbd38..e371c63 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -104,51 +104,15 @@ namespace SimpleWeb { std::shared_ptr request(const std::string& request_type, const std::string& path="/", boost::string_ref content="", const std::map& header=std::map()) { - auto corrected_path=path; - if(corrected_path=="") - corrected_path="/"; - if(!config.proxy_server.empty() && std::is_same::value) - corrected_path="http://"+host+':'+std::to_string(port)+corrected_path; + auto write_buffer=create_request_header(request_type, path, header); - asio::streambuf write_buffer; - std::ostream write_stream(&write_buffer); - write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; - write_stream << "Host: " << host << "\r\n"; - for(auto& h: header) { - write_stream << h.first << ": " << h.second << "\r\n"; - } + std::ostream write_stream(write_buffer.get()); if(content.size()>0) write_stream << "Content-Length: " << content.size() << "\r\n"; - write_stream << "\r\n"; + write_stream << "\r\n" << content; - connect(); + request_write(write_buffer); - auto timer=get_timeout_timer(); - asio::async_write(*socket, write_buffer, - [this, &content, timer](const error_code &ec, size_t /*bytes_transferred*/) { - if(timer) - timer->cancel(); - if(!ec) { - if(!content.empty()) { - auto timer=get_timeout_timer(); - asio::async_write(*socket, asio::buffer(content.data(), content.size()), - [this, timer](const error_code &ec, size_t /*bytes_transferred*/) { - if(timer) - timer->cancel(); - if(ec) { - std::lock_guard lock(socket_mutex); - this->socket=nullptr; - throw system_error(ec); - } - }); - } - } - else { - std::lock_guard lock(socket_mutex); - socket=nullptr; - throw system_error(ec); - } - }); io_service->reset(); io_service->run(); @@ -157,42 +121,20 @@ namespace SimpleWeb { std::shared_ptr request(const std::string& request_type, const std::string& path, std::iostream& content, const std::map& header=std::map()) { - auto corrected_path=path; - if(corrected_path=="") - corrected_path="/"; - if(!config.proxy_server.empty() && std::is_same::value) - corrected_path="http://"+host+':'+std::to_string(port)+corrected_path; + auto write_buffer=create_request_header(request_type, path, header); content.seekp(0, std::ios::end); auto content_length=content.tellp(); content.seekp(0, std::ios::beg); - - asio::streambuf write_buffer; - std::ostream write_stream(&write_buffer); - write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; - write_stream << "Host: " << host << "\r\n"; - for(auto& h: header) { - write_stream << h.first << ": " << h.second << "\r\n"; - } + std::ostream write_stream(write_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(); - connect(); + request_write(write_buffer); - auto timer=get_timeout_timer(); - asio::async_write(*socket, write_buffer, - [this, timer](const error_code &ec, size_t /*bytes_transferred*/) { - if(timer) - timer->cancel(); - if(ec) { - std::lock_guard lock(socket_mutex); - socket=nullptr; - throw system_error(ec); - } - }); io_service->reset(); io_service->run(); @@ -282,6 +224,37 @@ namespace SimpleWeb { } } + std::shared_ptr create_request_header(const std::string& request_type, const std::string& path, const std::map& header) { + auto corrected_path=path; + if(corrected_path=="") + corrected_path="/"; + if(!config.proxy_server.empty() && std::is_same::value) + corrected_path="http://"+host+':'+std::to_string(port)+corrected_path; + + auto write_buffer=std::make_shared(); + std::ostream write_stream(write_buffer.get()); + write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; + write_stream << "Host: " << host << "\r\n"; + for(auto& h: header) + write_stream << h.first << ": " << h.second << "\r\n"; + return write_buffer; + } + + void request_write(const std::shared_ptr &write_buffer) { + connect(); + + auto timer=get_timeout_timer(); + asio::async_write(*socket, *write_buffer, [this, write_buffer, timer](const error_code &ec, size_t /*bytes_transferred*/) { + if(timer) + timer->cancel(); + if(ec) { + std::lock_guard lock(socket_mutex); + socket=nullptr; + throw system_error(ec); + } + }); + } + std::shared_ptr request_read() { std::shared_ptr response(new Response());