summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/client_https.hpp
blob: 3300ea6865f05439faf327be74dba1cb8b856e8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// license:MIT
// copyright-holders:Ole Christian Eidheim, Miodrag Milanovic
#ifndef CLIENT_HTTPS_HPP
#define CLIENT_HTTPS_HPP

#include "client_http.hpp"
#include "asio/ssl.hpp"

namespace webpp {
	using HTTPS = asio::ssl::stream<asio::ip::tcp::socket>;

	template<>
	class Client<HTTPS> : public ClientBase<HTTPS> {
	public:
		explicit Client(const std::string& server_port_path, bool verify_certificate=true,
				const std::string& cert_file=std::string(), const std::string& private_key_file=std::string(),
				const std::string& verify_file=std::string()) :
				ClientBase(server_port_path, 443), m_context(asio::ssl::context::tlsv12) {
			if(cert_file.size()>0 && private_key_file.size()>0) {
				m_context.use_certificate_chain_file(cert_file);
				m_context.use_private_key_file(private_key_file, asio::ssl::context::pem);
			}

			if(verify_certificate)
				m_context.set_verify_callback(asio::ssl::rfc2818_verification(host));

			if(verify_file.size()>0)
				m_context.load_verify_file(verify_file);
			else
				m_context.set_default_verify_paths();

			if(verify_file.size()>0 || verify_certificate)
				m_context.set_verify_mode(asio::ssl::verify_peer);
			else
				m_context.set_verify_mode(asio::ssl::verify_none);
		}

	protected:
		asio::ssl::context m_context;

		void connect() override {
			if(!socket || !socket->lowest_layer().is_open()) {
				std::unique_ptr<asio::ip::tcp::resolver::query> query;
				if (config.proxy_server.empty()) {
					query = std::make_unique<asio::ip::tcp::resolver::query>(host, std::to_string(port));
				}
				else {
					auto proxy_host_port = parse_host_port(config.proxy_server, 8080);
					query = std::make_unique<asio::ip::tcp::resolver::query>(proxy_host_port.first, std::to_string(proxy_host_port.second));
				}
				resolver.async_resolve(*query, [this]
							(const std::error_code &ec, asio::ip::tcp::resolver::iterator it) {
					if (!ec) {
						{
							std::lock_guard<std::mutex> lock(socket_mutex);
							socket = std::make_unique<HTTPS>(io_context, m_context);
						}

						auto timer = get_timeout_timer(config.timeout_connect);
						asio::async_connect(socket->lowest_layer(), it, [this, timer]
								(const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) {
							if (timer)
								timer->cancel();
							if (!ec) {
								asio::ip::tcp::no_delay option(true);
								socket->lowest_layer().set_option(option);
							}
							else {
								std::lock_guard<std::mutex> lock(socket_mutex);
								socket = nullptr;
								throw std::system_error(ec);
							}
						});
					}
					else {
						std::lock_guard<std::mutex> lock(socket_mutex);
						socket = nullptr;
						throw std::system_error(ec);
					}
				});
				io_context.reset();
				io_context.run();

				if (!config.proxy_server.empty()) {
					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();
					asio::async_write(socket->next_layer(), write_buffer,
						[this, timer](const std::error_code &ec, size_t /*bytes_transferred*/) {
						if (timer)
							timer->cancel();
						if (ec) {
							std::lock_guard<std::mutex> lock(socket_mutex);
							socket = nullptr;
							throw std::system_error(ec);
						}
					});
					io_context.reset();
					io_context.run();

					std::shared_ptr<Response> response(new Response());
					timer=get_timeout_timer();
					asio::async_read_until(socket->next_layer(), response->content_buffer, "\r\n\r\n",
												[this, timer](const std::error_code& ec, size_t /*bytes_transferred*/) {
						if(timer)
							timer->cancel();
						if(ec) {
							std::lock_guard<std::mutex> lock(socket_mutex);
							socket=nullptr;
							throw std::system_error(ec);
						}
					});
					io_context.reset();
					io_context.run();
					parse_response_header(response);

					if (response->status_code.size()>0 && response->status_code.compare(0, 3, "200") != 0) {
						std::lock_guard<std::mutex> lock(socket_mutex);
						socket = nullptr;
						throw std::system_error(std::error_code(int(std::errc::permission_denied), std::generic_category()));
					}
				}

				auto timer = get_timeout_timer();
				this->socket->async_handshake(asio::ssl::stream_base::client,
					[this, timer](const std::error_code& ec) {
					if (timer)
						timer->cancel();
					if (ec) {
						std::lock_guard<std::mutex> lock(socket_mutex);
						socket = nullptr;
						throw std::system_error(ec);
					}
				});
				io_context.reset();
				io_context.run();
			}
		}
	};

	class https_client : public Client<HTTPS> {
	public:
		explicit https_client(const std::string& server_port_path, bool verify_certificate = true,
			const std::string& cert_file = std::string(), const std::string& private_key_file = std::string(),
			const std::string& verify_file = std::string()) : Client<HTTPS>::Client(server_port_path, verify_certificate, cert_file, private_key_file, verify_file) {}
	};

}

#endif  /* CLIENT_HTTPS_HPP */