Skip to content

Commit 0b541ff

Browse files
Rockybillyata.yardimci
and
ata.yardimci
authored
Add get_socket_fd method to Client and ClientImpl, add according unit… (#1341)
* Add get_socket_fd method to Client and ClientImpl, add according unit test * Change name get_socket_fd to get_socket * Change name get_socket to socket Co-authored-by: ata.yardimci <[email protected]>
1 parent 106be19 commit 0b541ff

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

httplib.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,8 @@ class ClientImpl {
992992

993993
size_t is_socket_open() const;
994994

995+
socket_t socket() const;
996+
995997
void stop();
996998

997999
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
@@ -1344,6 +1346,8 @@ class Client {
13441346

13451347
size_t is_socket_open() const;
13461348

1349+
socket_t socket() const;
1350+
13471351
void stop();
13481352

13491353
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
@@ -6944,6 +6948,10 @@ inline size_t ClientImpl::is_socket_open() const {
69446948
return socket_.is_open();
69456949
}
69466950

6951+
inline socket_t ClientImpl::socket() const {
6952+
return socket_.sock;
6953+
}
6954+
69476955
inline void ClientImpl::stop() {
69486956
std::lock_guard<std::mutex> guard(socket_mutex_);
69496957

@@ -8151,6 +8159,8 @@ inline Result Client::send(const Request &req) { return cli_->send(req); }
81518159

81528160
inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
81538161

8162+
inline socket_t Client::socket() const { return cli_->socket(); }
8163+
81548164
inline void Client::stop() { cli_->stop(); }
81558165

81568166
inline void

test/test.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4750,6 +4750,43 @@ TEST(SendAPI, SimpleInterface_Online) {
47504750
EXPECT_EQ(301, res->status);
47514751
}
47524752

4753+
TEST(ClientImplMethods, GetSocketTest) {
4754+
httplib::Server svr;
4755+
svr.Get( "/", [&](const httplib::Request& req, httplib::Response& res) {
4756+
res.status = 200;
4757+
});
4758+
4759+
auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); });
4760+
4761+
while (!svr.is_running()) {
4762+
std::this_thread::sleep_for(std::chrono::milliseconds(5));
4763+
}
4764+
4765+
{
4766+
httplib::Client cli("http://127.0.0.1:3333");
4767+
cli.set_keep_alive(true);
4768+
4769+
// Use the behavior of cpp-httplib of opening the connection
4770+
// only when the first request happens. If that changes,
4771+
// this test would be obsolete.
4772+
4773+
EXPECT_EQ(cli.socket(), INVALID_SOCKET);
4774+
4775+
// This also implicitly tests the server. But other tests would fail much
4776+
// earlier than this one to be considered.
4777+
4778+
auto res = cli.Get("/");
4779+
ASSERT_TRUE(res);
4780+
4781+
EXPECT_EQ(200, res->status);
4782+
ASSERT_TRUE(cli.socket() != INVALID_SOCKET);
4783+
}
4784+
4785+
svr.stop();
4786+
thread.join();
4787+
ASSERT_FALSE(svr.is_running());
4788+
}
4789+
47534790
// Disabled due to out-of-memory problem on GitHub Actions
47544791
#ifdef _WIN64
47554792
TEST(ServerLargeContentTest, DISABLED_SendLargeContent) {

0 commit comments

Comments
 (0)