yhirose 2 years ago
parent
commit
f1daa5b88b
2 changed files with 47 additions and 15 deletions
  1. 27 15
      httplib.h
  2. 20 0
      test/test.cc

+ 27 - 15
httplib.h

@@ -1066,11 +1066,13 @@ public:
   bool send(Request &req, Response &res, Error &error);
   Result send(const Request &req);
 
-  size_t is_socket_open() const;
+  void stop();
 
-  socket_t socket() const;
+  std::string host() const;
+  int port() const;
 
-  void stop();
+  size_t is_socket_open() const;
+  socket_t socket() const;
 
   void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
 
@@ -1439,11 +1441,13 @@ public:
   bool send(Request &req, Response &res, Error &error);
   Result send(const Request &req);
 
-  size_t is_socket_open() const;
+  void stop();
 
-  socket_t socket() const;
+  std::string host() const;
+  int port() const;
 
-  void stop();
+  size_t is_socket_open() const;
+  socket_t socket() const;
 
   void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
 
@@ -7477,13 +7481,6 @@ inline Result ClientImpl::Options(const std::string &path,
   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() {
   std::lock_guard<std::mutex> guard(socket_mutex_);
 
@@ -7507,6 +7504,17 @@ inline void ClientImpl::stop() {
   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) {
   connection_timeout_sec_ = sec;
   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 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 socket_t Client::socket() const { return cli_->socket(); }
 
-inline void Client::stop() { cli_->stop(); }
-
 inline void
 Client::set_hostname_addr_map(std::map<std::string, std::string> addr_map) {
   cli_->set_hostname_addr_map(std::move(addr_map));

+ 20 - 0
test/test.cc

@@ -4693,6 +4693,26 @@ TEST_F(PayloadMaxLengthTest, ExceedLimit) {
   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
 TEST(SSLClientTest, UpdateCAStore) {
   httplib::SSLClient httplib_client("www.google.com");