mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-17 10:59:39 +00:00
Implement setup_proxy_connection method for SSLClient and refactor proxy handling in open_stream
This commit is contained in:
1 parent
f6ed5fc60f
commit
b1bb2b7ecc
1 file changed
+46
-36
@@ -2191,6 +2191,10 @@ protected:
|
||||
|
||||
virtual bool create_and_connect_socket(Socket &socket, Error &error);
|
||||
virtual bool ensure_socket_connection(Socket &socket, Error &error);
|
||||
virtual bool setup_proxy_connection(
|
||||
Socket &socket,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
Response &res, bool &success, Error &error);
|
||||
|
||||
// All of:
|
||||
// shutdown_ssl
|
||||
@@ -2717,6 +2721,10 @@ private:
|
||||
std::function<bool(Stream &strm)> callback) override;
|
||||
bool is_ssl() const override;
|
||||
|
||||
bool setup_proxy_connection(
|
||||
Socket &socket,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
Response &res, bool &success, Error &error) override;
|
||||
bool connect_with_proxy(
|
||||
Socket &sock,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
@@ -12140,6 +12148,13 @@ inline bool ClientImpl::ensure_socket_connection(Socket &socket, Error &error) {
|
||||
return create_and_connect_socket(socket, error);
|
||||
}
|
||||
|
||||
inline bool ClientImpl::setup_proxy_connection(
|
||||
Socket & /*socket*/,
|
||||
std::chrono::time_point<std::chrono::steady_clock> /*start_time*/,
|
||||
Response & /*res*/, bool & /*success*/, Error & /*error*/) {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void ClientImpl::shutdown_ssl(Socket & /*socket*/,
|
||||
bool /*shutdown_gracefully*/) {
|
||||
// If there are any requests in flight from threads other than us, then it's
|
||||
@@ -12261,27 +12276,14 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
// TODO: refactoring
|
||||
if (is_ssl()) {
|
||||
auto &scli = static_cast<SSLClient &>(*this);
|
||||
if (!proxy_host_.empty() && proxy_port_ != -1) {
|
||||
auto success = false;
|
||||
if (!scli.connect_with_proxy(socket_, req.start_time_, res, success,
|
||||
error)) {
|
||||
if (!success) { output_error_log(error, &req); }
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
if (!proxy_host_.empty() && proxy_port_ != -1) {
|
||||
if (!scli.initialize_ssl(socket_, error)) {
|
||||
output_error_log(error, &req);
|
||||
return false;
|
||||
}
|
||||
{
|
||||
auto success = true;
|
||||
if (!setup_proxy_connection(socket_, req.start_time_, res, success,
|
||||
error)) {
|
||||
if (!success) { output_error_log(error, &req); }
|
||||
return success;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Mark the current socket as being in use so that it cannot be closed by
|
||||
@@ -12413,7 +12415,6 @@ ClientImpl::open_stream(const std::string &method, const std::string &path,
|
||||
handle.response = detail::make_unique<Response>();
|
||||
handle.error = Error::Success;
|
||||
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
auto query_path = params.empty() ? path : append_query_params(path, params);
|
||||
handle.connection_ = detail::make_unique<ClientConnection>();
|
||||
|
||||
@@ -12443,24 +12444,15 @@ ClientImpl::open_stream(const std::string &method, const std::string &path,
|
||||
return handle;
|
||||
}
|
||||
|
||||
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||
if (is_ssl()) {
|
||||
auto &scli = static_cast<SSLClient &>(*this);
|
||||
if (!proxy_host_.empty() && proxy_port_ != -1) {
|
||||
auto success = false;
|
||||
if (!scli.connect_with_proxy(socket_, start_time, *handle.response,
|
||||
success, handle.error)) {
|
||||
if (!success) { handle.response.reset(); }
|
||||
return handle;
|
||||
}
|
||||
|
||||
if (!scli.initialize_ssl(socket_, handle.error)) {
|
||||
handle.response.reset();
|
||||
return handle;
|
||||
}
|
||||
{
|
||||
auto success = true;
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
if (!setup_proxy_connection(socket_, start_time, *handle.response,
|
||||
success, handle.error)) {
|
||||
if (!success) { handle.response.reset(); }
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
transfer_socket_ownership_to_handle(handle);
|
||||
@@ -15437,6 +15429,24 @@ inline bool SSLClient::create_and_connect_socket(Socket &socket, Error &error) {
|
||||
return ClientImpl::create_and_connect_socket(socket, error);
|
||||
}
|
||||
|
||||
inline bool SSLClient::setup_proxy_connection(
|
||||
Socket &socket,
|
||||
std::chrono::time_point<std::chrono::steady_clock> start_time,
|
||||
Response &res, bool &success, Error &error) {
|
||||
if (proxy_host_.empty() || proxy_port_ == -1) { return true; }
|
||||
|
||||
if (!connect_with_proxy(socket, start_time, res, success, error)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!initialize_ssl(socket, error)) {
|
||||
success = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Assumes that socket_mutex_ is locked and that there are no requests in
|
||||
// flight
|
||||
inline bool SSLClient::connect_with_proxy(
|
||||
|
||||
Reference in new issue
Block a user