This commit is contained in:
yhirose 2023-07-04 20:27:11 -04:00
parent fe9a1949a6
commit f1daa5b88b
2 changed files with 49 additions and 17 deletions

View File

@ -1066,12 +1066,14 @@ public:
bool send(Request &req, Response &res, Error &error); bool send(Request &req, Response &res, Error &error);
Result send(const Request &req); Result send(const Request &req);
size_t is_socket_open() const;
socket_t socket() const;
void stop(); void stop();
std::string host() const;
int port() const;
size_t is_socket_open() const;
socket_t socket() const;
void set_hostname_addr_map(std::map<std::string, std::string> addr_map); void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
void set_default_headers(Headers headers); void set_default_headers(Headers headers);
@ -1439,12 +1441,14 @@ public:
bool send(Request &req, Response &res, Error &error); bool send(Request &req, Response &res, Error &error);
Result send(const Request &req); Result send(const Request &req);
size_t is_socket_open() const;
socket_t socket() const;
void stop(); void stop();
std::string host() const;
int port() const;
size_t is_socket_open() const;
socket_t socket() const;
void set_hostname_addr_map(std::map<std::string, std::string> addr_map); void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
void set_default_headers(Headers headers); void set_default_headers(Headers headers);
@ -7477,13 +7481,6 @@ inline Result ClientImpl::Options(const std::string &path,
return send_(std::move(req)); return send_(std::move(req));
} }
inline size_t ClientImpl::is_socket_open() const {
std::lock_guard<std::mutex> guard(socket_mutex_);
return socket_.is_open();
}
inline socket_t ClientImpl::socket() const { return socket_.sock; }
inline void ClientImpl::stop() { inline void ClientImpl::stop() {
std::lock_guard<std::mutex> guard(socket_mutex_); std::lock_guard<std::mutex> guard(socket_mutex_);
@ -7507,6 +7504,17 @@ inline void ClientImpl::stop() {
close_socket(socket_); close_socket(socket_);
} }
inline std::string ClientImpl::host() const { return host_; }
inline int ClientImpl::port() const { return port_; }
inline size_t ClientImpl::is_socket_open() const {
std::lock_guard<std::mutex> guard(socket_mutex_);
return socket_.is_open();
}
inline socket_t ClientImpl::socket() const { return socket_.sock; }
inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) { inline void ClientImpl::set_connection_timeout(time_t sec, time_t usec) {
connection_timeout_sec_ = sec; connection_timeout_sec_ = sec;
connection_timeout_usec_ = usec; connection_timeout_usec_ = usec;
@ -8719,12 +8727,16 @@ inline bool Client::send(Request &req, Response &res, Error &error) {
inline Result Client::send(const Request &req) { return cli_->send(req); } inline Result Client::send(const Request &req) { return cli_->send(req); }
inline void Client::stop() { cli_->stop(); }
inline std::string Client::host() const { return cli_->host(); }
inline int Client::port() const { return cli_->port(); }
inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); } inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
inline socket_t Client::socket() const { return cli_->socket(); } inline socket_t Client::socket() const { return cli_->socket(); }
inline void Client::stop() { cli_->stop(); }
inline void inline void
Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) { Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
cli_->set_hostname_addr_map(std::move(addr_map)); cli_->set_hostname_addr_map(std::move(addr_map));

View File

@ -4693,6 +4693,26 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) {
EXPECT_EQ(200, res->status); EXPECT_EQ(200, res->status);
} }
TEST(HostAndPortPropertiesTest, NoSSL) {
httplib::Client cli("www.google.com", 1234);
ASSERT_EQ("www.google.com", cli.host());
ASSERT_EQ(1234, cli.port());
}
TEST(HostAndPortPropertiesTest, NoSSLWithSimpleAPI) {
httplib::Client cli("www.google.com:1234");
ASSERT_EQ("www.google.com", cli.host());
ASSERT_EQ(1234, cli.port());
}
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(HostAndPortPropertiesTest, SSL) {
httplib::SSLClient cli("www.google.com");
ASSERT_EQ("www.google.com", cli.host());
ASSERT_EQ(443, cli.port());
}
#endif
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
TEST(SSLClientTest, UpdateCAStore) { TEST(SSLClientTest, UpdateCAStore) {
httplib::SSLClient httplib_client("www.google.com"); httplib::SSLClient httplib_client("www.google.com");