ClientBase now derives from and use std::enable_shared_from_this
This commit is contained in:
parent
1992bbb4b4
commit
026f47f195
3 changed files with 94 additions and 92 deletions
132
client_http.hpp
132
client_http.hpp
|
|
@ -33,7 +33,7 @@ namespace SimpleWeb {
|
||||||
class Client;
|
class Client;
|
||||||
|
|
||||||
template <class socket_type>
|
template <class socket_type>
|
||||||
class ClientBase {
|
class ClientBase : public std::enable_shared_from_this<ClientBase<socket_type>> {
|
||||||
ClientBase(const ClientBase &) = delete;
|
ClientBase(const ClientBase &) = delete;
|
||||||
ClientBase &operator=(const ClientBase &) = delete;
|
ClientBase &operator=(const ClientBase &) = delete;
|
||||||
|
|
||||||
|
|
@ -92,32 +92,18 @@ namespace SimpleWeb {
|
||||||
protected:
|
protected:
|
||||||
class Connection {
|
class Connection {
|
||||||
public:
|
public:
|
||||||
Connection(const std::string &host, unsigned short port, const Config &config, std::unique_ptr<socket_type> &&socket)
|
Connection(std::unique_ptr<socket_type> &&socket) : socket(std::move(socket)) {}
|
||||||
: host(host), port(port), config(config), socket(std::move(socket)) {
|
|
||||||
if(config.proxy_server.empty())
|
|
||||||
query = std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(host, std::to_string(port)));
|
|
||||||
else {
|
|
||||||
auto proxy_host_port = parse_host_port(config.proxy_server, 8080);
|
|
||||||
query = std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string host;
|
|
||||||
unsigned short port;
|
|
||||||
Config config;
|
|
||||||
|
|
||||||
std::unique_ptr<socket_type> socket;
|
std::unique_ptr<socket_type> socket;
|
||||||
bool in_use = false;
|
bool in_use = false;
|
||||||
bool reconnecting = false;
|
bool reconnecting = false;
|
||||||
|
|
||||||
std::unique_ptr<asio::ip::tcp::resolver::query> query;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Session {
|
class Session {
|
||||||
public:
|
public:
|
||||||
Session(const std::shared_ptr<asio::io_service> &io_service, const std::shared_ptr<Connection> &connection, std::unique_ptr<asio::streambuf> &&request_buffer)
|
Session(std::shared_ptr<ClientBase<socket_type>> &&self, const std::shared_ptr<Connection> &connection, std::unique_ptr<asio::streambuf> &&request_buffer)
|
||||||
: io_service(io_service), connection(connection), request_buffer(std::move(request_buffer)), response(new Response()) {}
|
: self(std::move(self)), connection(connection), request_buffer(std::move(request_buffer)), response(new Response()) {}
|
||||||
std::shared_ptr<asio::io_service> io_service;
|
std::shared_ptr<ClientBase<socket_type>> self;
|
||||||
std::shared_ptr<Connection> connection;
|
std::shared_ptr<Connection> connection;
|
||||||
std::unique_ptr<asio::streambuf> request_buffer;
|
std::unique_ptr<asio::streambuf> request_buffer;
|
||||||
std::shared_ptr<Response> response;
|
std::shared_ptr<Response> response;
|
||||||
|
|
@ -171,7 +157,7 @@ namespace SimpleWeb {
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, string_view content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
||||||
auto connection = session->connection;
|
auto connection = session->connection;
|
||||||
auto response = session->response;
|
auto response = session->response;
|
||||||
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
|
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
|
||||||
|
|
@ -207,7 +193,7 @@ namespace SimpleWeb {
|
||||||
write_stream << "\r\n"
|
write_stream << "\r\n"
|
||||||
<< content;
|
<< content;
|
||||||
|
|
||||||
Client<socket_type>::connect(session);
|
connect(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
|
@ -230,7 +216,7 @@ namespace SimpleWeb {
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
void request(const std::string &method, const std::string &path, std::istream &content, const CaseInsensitiveMultimap &header,
|
||||||
std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
|
std::function<void(std::shared_ptr<Response> &, const error_code &)> &&request_callback_) {
|
||||||
auto session = std::make_shared<Session>(io_service, get_connection(), create_request_header(method, path, header));
|
auto session = std::make_shared<Session>(this->shared_from_this(), get_connection(), create_request_header(method, path, header));
|
||||||
auto connection = session->connection;
|
auto connection = session->connection;
|
||||||
auto response = session->response;
|
auto response = session->response;
|
||||||
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
|
auto request_callback = std::make_shared<std::function<void(std::shared_ptr<Response> &, const error_code &)>>(std::move(request_callback_));
|
||||||
|
|
@ -270,7 +256,7 @@ namespace SimpleWeb {
|
||||||
if(content_length > 0)
|
if(content_length > 0)
|
||||||
write_stream << content.rdbuf();
|
write_stream << content.rdbuf();
|
||||||
|
|
||||||
Client<socket_type>::connect(session);
|
connect(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asynchronous request where setting and/or running Client's io_service is required.
|
/// Asynchronous request where setting and/or running Client's io_service is required.
|
||||||
|
|
@ -283,6 +269,8 @@ namespace SimpleWeb {
|
||||||
std::string host;
|
std::string host;
|
||||||
unsigned short port;
|
unsigned short port;
|
||||||
|
|
||||||
|
std::unique_ptr<asio::ip::tcp::resolver::query> query;
|
||||||
|
|
||||||
std::shared_ptr<std::vector<std::shared_ptr<Connection>>> connections;
|
std::shared_ptr<std::vector<std::shared_ptr<Connection>>> connections;
|
||||||
std::shared_ptr<std::mutex> connections_mutex;
|
std::shared_ptr<std::mutex> connections_mutex;
|
||||||
|
|
||||||
|
|
@ -308,10 +296,21 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
connection->reconnecting = false;
|
connection->reconnecting = false;
|
||||||
connection->in_use = true;
|
connection->in_use = true;
|
||||||
|
|
||||||
|
if(!query) {
|
||||||
|
if(config.proxy_server.empty())
|
||||||
|
query = std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(host, std::to_string(port)));
|
||||||
|
else {
|
||||||
|
auto proxy_host_port = parse_host_port(config.proxy_server, 8080);
|
||||||
|
query = std::unique_ptr<asio::ip::tcp::resolver::query>(new asio::ip::tcp::resolver::query(proxy_host_port.first, std::to_string(proxy_host_port.second)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual std::shared_ptr<Connection> create_connection() = 0;
|
virtual std::shared_ptr<Connection> create_connection() = 0;
|
||||||
|
virtual void connect(std::shared_ptr<Session> &) = 0;
|
||||||
|
|
||||||
std::unique_ptr<asio::streambuf> create_request_header(const std::string &method, const std::string &path, const CaseInsensitiveMultimap &header) const {
|
std::unique_ptr<asio::streambuf> create_request_header(const std::string &method, const std::string &path, const CaseInsensitiveMultimap &header) const {
|
||||||
auto corrected_path = path;
|
auto corrected_path = path;
|
||||||
|
|
@ -329,7 +328,7 @@ namespace SimpleWeb {
|
||||||
return request_buffer;
|
return request_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::pair<std::string, unsigned short> parse_host_port(const std::string &host_port, unsigned short default_port) {
|
std::pair<std::string, unsigned short> parse_host_port(const std::string &host_port, unsigned short default_port) const {
|
||||||
std::pair<std::string, unsigned short> parsed_host_port;
|
std::pair<std::string, unsigned short> parsed_host_port;
|
||||||
size_t host_end = host_port.find(':');
|
size_t host_end = host_port.find(':');
|
||||||
if(host_end == std::string::npos) {
|
if(host_end == std::string::npos) {
|
||||||
|
|
@ -343,22 +342,22 @@ namespace SimpleWeb {
|
||||||
return parsed_host_port;
|
return parsed_host_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<asio::deadline_timer> get_timeout_timer(std::shared_ptr<Session> &session, size_t timeout = 0) {
|
std::shared_ptr<asio::deadline_timer> get_timeout_timer(std::shared_ptr<Session> &session, size_t timeout = 0) {
|
||||||
if(timeout == 0)
|
if(timeout == 0)
|
||||||
timeout = session->connection->config.timeout;
|
timeout = config.timeout;
|
||||||
if(timeout == 0)
|
if(timeout == 0)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
auto timer = std::make_shared<asio::deadline_timer>(*session->io_service);
|
auto timer = std::make_shared<asio::deadline_timer>(*io_service);
|
||||||
timer->expires_from_now(boost::posix_time::seconds(timeout));
|
timer->expires_from_now(boost::posix_time::seconds(timeout));
|
||||||
timer->async_wait([session](const error_code &ec) mutable {
|
timer->async_wait([this, session](const error_code &ec) mutable {
|
||||||
if(!ec)
|
if(!ec)
|
||||||
close(session);
|
this->close(session);
|
||||||
});
|
});
|
||||||
return timer;
|
return timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_response_header(std::shared_ptr<Response> &response) {
|
void parse_response_header(std::shared_ptr<Response> &response) const {
|
||||||
std::string line;
|
std::string line;
|
||||||
getline(response->content, line);
|
getline(response->content, line);
|
||||||
size_t version_end = line.find(' ');
|
size_t version_end = line.find(' ');
|
||||||
|
|
@ -384,23 +383,23 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write(std::shared_ptr<Session> &session) {
|
void write(std::shared_ptr<Session> &session) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = get_timeout_timer(session);
|
||||||
asio::async_write(*session->connection->socket, session->request_buffer->data(), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_write(*session->connection->socket, session->request_buffer->data(), [this, session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
read(session);
|
this->read(session);
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read(std::shared_ptr<Session> &session) {
|
void read(std::shared_ptr<Session> &session) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = get_timeout_timer(session);
|
||||||
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [session, timer](const error_code &ec, size_t bytes_transferred) mutable {
|
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n\r\n", [this, session, timer](const error_code &ec, size_t bytes_transferred) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
|
|
@ -408,20 +407,20 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
size_t num_additional_bytes = session->response->content_buffer.size() - bytes_transferred;
|
size_t num_additional_bytes = session->response->content_buffer.size() - bytes_transferred;
|
||||||
|
|
||||||
parse_response_header(session->response);
|
this->parse_response_header(session->response);
|
||||||
|
|
||||||
auto header_it = session->response->header.find("Content-Length");
|
auto header_it = session->response->header.find("Content-Length");
|
||||||
if(header_it != session->response->header.end()) {
|
if(header_it != session->response->header.end()) {
|
||||||
auto content_length = stoull(header_it->second);
|
auto content_length = stoull(header_it->second);
|
||||||
if(content_length > num_additional_bytes) {
|
if(content_length > num_additional_bytes) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = this->get_timeout_timer(session);
|
||||||
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(content_length - num_additional_bytes), [this, session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -431,17 +430,17 @@ namespace SimpleWeb {
|
||||||
}
|
}
|
||||||
else if((header_it = session->response->header.find("Transfer-Encoding")) != session->response->header.end() && header_it->second == "chunked") {
|
else if((header_it = session->response->header.find("Transfer-Encoding")) != session->response->header.end() && header_it->second == "chunked") {
|
||||||
auto tmp_streambuf = std::make_shared<asio::streambuf>();
|
auto tmp_streambuf = std::make_shared<asio::streambuf>();
|
||||||
read_chunked(session, tmp_streambuf);
|
this->read_chunked(session, tmp_streambuf);
|
||||||
}
|
}
|
||||||
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
|
else if(session->response->http_version < "1.1" || ((header_it = session->response->header.find("Session")) != session->response->header.end() && header_it->second == "close")) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = this->get_timeout_timer(session);
|
||||||
asio::async_read(*session->connection->socket, session->response->content_buffer, [session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_read(*session->connection->socket, session->response->content_buffer, [this, session, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
if(ec == asio::error::eof) {
|
if(ec == asio::error::eof) {
|
||||||
error_code ec;
|
error_code ec;
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
|
|
@ -457,20 +456,20 @@ namespace SimpleWeb {
|
||||||
else {
|
else {
|
||||||
if(!session->connection->reconnecting) {
|
if(!session->connection->reconnecting) {
|
||||||
session->connection->reconnecting = true;
|
session->connection->reconnecting = true;
|
||||||
close(session);
|
this->close(session);
|
||||||
Client<socket_type>::connect(session);
|
this->connect(session);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_chunked(std::shared_ptr<Session> &session, std::shared_ptr<asio::streambuf> &tmp_streambuf) {
|
void read_chunked(std::shared_ptr<Session> &session, std::shared_ptr<asio::streambuf> &tmp_streambuf) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = get_timeout_timer(session);
|
||||||
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [session, tmp_streambuf, timer](const error_code &ec, size_t bytes_transferred) mutable {
|
asio::async_read_until(*session->connection->socket, session->response->content_buffer, "\r\n", [this, session, tmp_streambuf, timer](const error_code &ec, size_t bytes_transferred) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
|
|
@ -482,7 +481,7 @@ namespace SimpleWeb {
|
||||||
|
|
||||||
auto num_additional_bytes = static_cast<std::streamsize>(session->response->content_buffer.size() - bytes_transferred);
|
auto num_additional_bytes = static_cast<std::streamsize>(session->response->content_buffer.size() - bytes_transferred);
|
||||||
|
|
||||||
auto post_process = [session, tmp_streambuf, length]() mutable {
|
auto post_process = [this, session, tmp_streambuf, length]() mutable {
|
||||||
std::ostream tmp_stream(tmp_streambuf.get());
|
std::ostream tmp_stream(tmp_streambuf.get());
|
||||||
if(length > 0) {
|
if(length > 0) {
|
||||||
std::vector<char> buffer(static_cast<size_t>(length));
|
std::vector<char> buffer(static_cast<size_t>(length));
|
||||||
|
|
@ -495,7 +494,7 @@ namespace SimpleWeb {
|
||||||
session->response->content.get();
|
session->response->content.get();
|
||||||
|
|
||||||
if(length > 0)
|
if(length > 0)
|
||||||
read_chunked(session, tmp_streambuf);
|
this->read_chunked(session, tmp_streambuf);
|
||||||
else {
|
else {
|
||||||
std::ostream response_stream(&session->response->content_buffer);
|
std::ostream response_stream(&session->response->content_buffer);
|
||||||
response_stream << tmp_stream.rdbuf();
|
response_stream << tmp_stream.rdbuf();
|
||||||
|
|
@ -505,14 +504,14 @@ namespace SimpleWeb {
|
||||||
};
|
};
|
||||||
|
|
||||||
if((2 + length) > num_additional_bytes) {
|
if((2 + length) > num_additional_bytes) {
|
||||||
auto timer = get_timeout_timer(session);
|
auto timer = this->get_timeout_timer(session);
|
||||||
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [session, post_process, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_read(*session->connection->socket, session->response->content_buffer, asio::transfer_exactly(2 + length - num_additional_bytes), [this, session, post_process, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
post_process();
|
post_process();
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -521,13 +520,13 @@ namespace SimpleWeb {
|
||||||
post_process();
|
post_process();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static void close(std::shared_ptr<Session> &session) {
|
void close(std::shared_ptr<Session> &session) {
|
||||||
error_code ec;
|
error_code ec;
|
||||||
session->connection->socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
session->connection->socket->lowest_layer().shutdown(asio::ip::tcp::socket::shutdown_both, ec);
|
||||||
session->connection->socket->lowest_layer().close(ec);
|
session->connection->socket->lowest_layer().close(ec);
|
||||||
|
|
@ -554,34 +553,35 @@ namespace SimpleWeb {
|
||||||
Client(const std::string &server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
|
Client(const std::string &server_port_path) : ClientBase<HTTP>::ClientBase(server_port_path, 80) {}
|
||||||
|
|
||||||
std::shared_ptr<Connection> create_connection() override {
|
std::shared_ptr<Connection> create_connection() override {
|
||||||
return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTP>(new HTTP(*io_service)));
|
return std::make_shared<Connection>(std::unique_ptr<HTTP>(new HTTP(*io_service)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void connect(std::shared_ptr<Session> &session) {
|
void connect(std::shared_ptr<Session> &session) override {
|
||||||
if(!session->connection->socket->lowest_layer().is_open()) {
|
if(!session->connection->socket->lowest_layer().is_open()) {
|
||||||
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service);
|
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = get_timeout_timer(session, config.timeout_connect);
|
||||||
resolver->async_resolve(*session->connection->query, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
|
|
||||||
|
resolver->async_resolve(*query, [this, session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = this->get_timeout_timer(session, this->config.timeout_connect);
|
||||||
asio::async_connect(*session->connection->socket, it, [session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
|
asio::async_connect(*session->connection->socket, it, [this, session, timer, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
asio::ip::tcp::no_delay option(true);
|
asio::ip::tcp::no_delay option(true);
|
||||||
session->connection->socket->set_option(option);
|
session->connection->socket->set_option(option);
|
||||||
write(session);
|
this->write(session);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -50,70 +50,70 @@ namespace SimpleWeb {
|
||||||
asio::ssl::context context;
|
asio::ssl::context context;
|
||||||
|
|
||||||
std::shared_ptr<Connection> create_connection() override {
|
std::shared_ptr<Connection> create_connection() override {
|
||||||
return std::make_shared<Connection>(host, port, config, std::unique_ptr<HTTPS>(new HTTPS(*io_service, context)));
|
return std::make_shared<Connection>(std::unique_ptr<HTTPS>(new HTTPS(*io_service, context)));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void connect(std::shared_ptr<Session> &session) {
|
void connect(std::shared_ptr<Session> &session) override {
|
||||||
if(!session->connection->socket->lowest_layer().is_open()) {
|
if(!session->connection->socket->lowest_layer().is_open()) {
|
||||||
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*session->io_service);
|
auto resolver = std::make_shared<asio::ip::tcp::resolver>(*io_service);
|
||||||
resolver->async_resolve(*session->connection->query, [session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
|
resolver->async_resolve(*query, [this, session, resolver](const error_code &ec, asio::ip::tcp::resolver::iterator it) mutable {
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = this->get_timeout_timer(session, this->config.timeout_connect);
|
||||||
asio::async_connect(session->connection->socket->lowest_layer(), it, [session, resolver, timer](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
|
asio::async_connect(session->connection->socket->lowest_layer(), it, [this, session, resolver, timer](const error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
asio::ip::tcp::no_delay option(true);
|
asio::ip::tcp::no_delay option(true);
|
||||||
session->connection->socket->lowest_layer().set_option(option);
|
session->connection->socket->lowest_layer().set_option(option);
|
||||||
|
|
||||||
if(!session->connection->config.proxy_server.empty()) {
|
if(!this->config.proxy_server.empty()) {
|
||||||
auto write_buffer = std::make_shared<asio::streambuf>();
|
auto write_buffer = std::make_shared<asio::streambuf>();
|
||||||
std::ostream write_stream(write_buffer.get());
|
std::ostream write_stream(write_buffer.get());
|
||||||
auto host_port = session->connection->host + ':' + std::to_string(session->connection->port);
|
auto host_port = this->host + ':' + std::to_string(this->port);
|
||||||
write_stream << "CONNECT " + host_port + " HTTP/1.1\r\n"
|
write_stream << "CONNECT " + host_port + " HTTP/1.1\r\n"
|
||||||
<< "Host: " << host_port << "\r\n\r\n";
|
<< "Host: " << host_port << "\r\n\r\n";
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = this->get_timeout_timer(session, this->config.timeout_connect);
|
||||||
asio::async_write(session->connection->socket->next_layer(), *write_buffer, [session, write_buffer, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_write(session->connection->socket->next_layer(), *write_buffer, [this, session, write_buffer, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
std::shared_ptr<Response> response(new Response());
|
std::shared_ptr<Response> response(new Response());
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = this->get_timeout_timer(session, this->config.timeout_connect);
|
||||||
asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [session, response, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
asio::async_read_until(session->connection->socket->next_layer(), response->content_buffer, "\r\n\r\n", [this, session, response, timer](const error_code &ec, size_t /*bytes_transferred*/) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec) {
|
if(!ec) {
|
||||||
parse_response_header(response);
|
this->parse_response_header(response);
|
||||||
if(response->status_code.empty() || response->status_code.compare(0, 3, "200") != 0) {
|
if(response->status_code.empty() || response->status_code.compare(0, 3, "200") != 0) {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(make_error_code::make_error_code(errc::permission_denied));
|
session->callback(make_error_code::make_error_code(errc::permission_denied));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
handshake(session);
|
this->handshake(session);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
handshake(session);
|
this->handshake(session);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -122,15 +122,15 @@ namespace SimpleWeb {
|
||||||
write(session);
|
write(session);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handshake(std::shared_ptr<Session> &session) {
|
void handshake(std::shared_ptr<Session> &session) {
|
||||||
auto timer = get_timeout_timer(session, session->connection->config.timeout_connect);
|
auto timer = get_timeout_timer(session, config.timeout_connect);
|
||||||
session->connection->socket->async_handshake(asio::ssl::stream_base::client, [session, timer](const error_code &ec) mutable {
|
session->connection->socket->async_handshake(asio::ssl::stream_base::client, [this, session, timer](const error_code &ec) mutable {
|
||||||
if(timer)
|
if(timer)
|
||||||
timer->cancel();
|
timer->cancel();
|
||||||
if(!ec)
|
if(!ec)
|
||||||
write(session);
|
this->write(session);
|
||||||
else {
|
else {
|
||||||
close(session);
|
this->close(session);
|
||||||
session->callback(ec);
|
session->callback(ec);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ class ServerTest : public ServerBase<HTTP> {
|
||||||
public:
|
public:
|
||||||
ServerTest() : ServerBase<HTTP>::ServerBase(8080) {}
|
ServerTest() : ServerBase<HTTP>::ServerBase(8080) {}
|
||||||
|
|
||||||
void accept() {}
|
void accept() override {}
|
||||||
|
|
||||||
void parse_request_test() {
|
void parse_request_test() {
|
||||||
HTTP socket(*io_service);
|
HTTP socket(*io_service);
|
||||||
|
|
@ -59,6 +59,8 @@ public:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void connect(std::shared_ptr<Session> &) override {}
|
||||||
|
|
||||||
void constructor_parse_test1() {
|
void constructor_parse_test1() {
|
||||||
assert(host == "test.org");
|
assert(host == "test.org");
|
||||||
assert(port == 8080);
|
assert(port == 8080);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue