From 599775c83f7313b7ddf7179056c1bedf79a892de Mon Sep 17 00:00:00 2001 From: eidheim Date: Fri, 25 Nov 2016 12:36:49 +0100 Subject: [PATCH] Made protocol() const, added default proxy ports, and added handshake on https proxy (based on #83) --- client_http.hpp | 6 ++--- client_https.hpp | 57 +++++++++++++++++++++++++++++++++----------- tests/parse_test.cpp | 2 +- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/client_http.hpp b/client_http.hpp index efaba65..24997e0 100644 --- a/client_http.hpp +++ b/client_http.hpp @@ -199,7 +199,7 @@ namespace SimpleWeb { return parsed_host_port; } - virtual std::string protocol()=0; + virtual std::string protocol() const =0; virtual void connect()=0; std::shared_ptr get_timeout_timer() { @@ -366,7 +366,7 @@ namespace SimpleWeb { Client(const std::string& server_port_path) : ClientBase::ClientBase(server_port_path, 80) {} protected: - std::string protocol() { + std::string protocol() const { return "http"; } @@ -376,7 +376,7 @@ namespace SimpleWeb { if(config.proxy_server.empty()) query=std::unique_ptr(new boost::asio::ip::tcp::resolver::query(host, std::to_string(port))); else { - auto proxy_host_port=parse_host_port(config.proxy_server, 0); + auto proxy_host_port=parse_host_port(config.proxy_server, 8080); query=std::unique_ptr(new boost::asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second))); } resolver.async_resolve(*query, [this](const boost::system::error_code &ec, diff --git a/client_https.hpp b/client_https.hpp index b06581d..4ebc52d 100644 --- a/client_https.hpp +++ b/client_https.hpp @@ -33,7 +33,7 @@ namespace SimpleWeb { protected: boost::asio::ssl::context context; - std::string protocol() { + std::string protocol() const { return "https"; } @@ -43,7 +43,7 @@ namespace SimpleWeb { if(config.proxy_server.empty()) query=std::unique_ptr(new boost::asio::ip::tcp::resolver::query(host, std::to_string(port))); else { - auto proxy_host_port=parse_host_port(config.proxy_server, 0); + auto proxy_host_port=parse_host_port(config.proxy_server, 8080); query=std::unique_ptr(new boost::asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second))); } resolver.async_resolve(*query, [this] @@ -59,18 +59,6 @@ namespace SimpleWeb { if(!ec) { boost::asio::ip::tcp::no_delay option(true); this->socket->lowest_layer().set_option(option); - - auto timer=get_timeout_timer(); - this->socket->async_handshake(boost::asio::ssl::stream_base::client, - [this, timer](const boost::system::error_code& ec) { - if(timer) - timer->cancel(); - if(ec) { - std::lock_guard lock(socket_mutex); - this->socket=nullptr; - throw boost::system::system_error(ec); - } - }); } else { std::lock_guard lock(socket_mutex); @@ -87,6 +75,47 @@ namespace SimpleWeb { }); io_service.reset(); io_service.run(); + + if(!config.proxy_server.empty()) { + boost::asio::streambuf write_buffer; + std::ostream write_stream(&write_buffer); + auto host_port=host+':'+std::to_string(port); + write_stream << "CONNECT "+host_port+" HTTP/1.1\r\n" << "Host: " << host_port << "\r\n\r\n"; + auto timer=get_timeout_timer(); + boost::asio::async_write(*socket, write_buffer, + [this, timer](const boost::system::error_code &ec, size_t /*bytes_transferred*/) { + if(timer) + timer->cancel(); + if(ec) { + std::lock_guard lock(socket_mutex); + socket=nullptr; + throw boost::system::system_error(ec); + } + }); + io_service.reset(); + io_service.run(); + + auto response=request_read(); + if (response->status_code.size()>0 && response->status_code.substr(0,3) != "200") { + std::lock_guard lock(socket_mutex); + socket=nullptr; + throw boost::system::system_error(boost::system::error_code(boost::system::errc::permission_denied, boost::system::generic_category())); + } + } + + auto timer=get_timeout_timer(); + this->socket->async_handshake(boost::asio::ssl::stream_base::client, + [this, timer](const boost::system::error_code& ec) { + if(timer) + timer->cancel(); + if(ec) { + std::lock_guard lock(socket_mutex); + socket=nullptr; + throw boost::system::system_error(ec); + } + }); + io_service.reset(); + io_service.run(); } } }; diff --git a/tests/parse_test.cpp b/tests/parse_test.cpp index 145270c..d558da9 100644 --- a/tests/parse_test.cpp +++ b/tests/parse_test.cpp @@ -48,7 +48,7 @@ class ClientTest : public ClientBase { public: ClientTest(const std::string& server_port_path) : ClientBase::ClientBase(server_port_path, 80) {} - std::string protocol() { + std::string protocol() const { return "http"; }