Made protocol() const, added default proxy ports, and added handshake on https proxy (based on #83)

This commit is contained in:
eidheim 2016-11-25 12:36:49 +01:00
commit 599775c83f
3 changed files with 47 additions and 18 deletions

View file

@ -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<boost::asio::deadline_timer> get_timeout_timer() {
@ -366,7 +366,7 @@ namespace SimpleWeb {
Client(const std::string& server_port_path) : ClientBase<HTTP>::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<boost::asio::ip::tcp::resolver::query>(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<boost::asio::ip::tcp::resolver::query>(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,

View file

@ -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<boost::asio::ip::tcp::resolver::query>(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<boost::asio::ip::tcp::resolver::query>(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<std::mutex> lock(socket_mutex);
this->socket=nullptr;
throw boost::system::system_error(ec);
}
});
}
else {
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> lock(socket_mutex);
socket=nullptr;
throw boost::system::system_error(ec);
}
});
io_service.reset();
io_service.run();
}
}
};

View file

@ -48,7 +48,7 @@ class ClientTest : public ClientBase<HTTP> {
public:
ClientTest(const std::string& server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
std::string protocol() {
std::string protocol() const {
return "http";
}