Skip to content

Add get_socket_fd method to Client and ClientImpl, add according unit… #1341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,8 @@ class ClientImpl {

size_t is_socket_open() const;

socket_t socket() const;

void stop();

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

size_t is_socket_open() const;

socket_t socket() const;

void stop();

void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
Expand Down Expand Up @@ -6944,6 +6948,10 @@ inline size_t ClientImpl::is_socket_open() const {
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_);

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

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
Expand Down
37 changes: 37 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4750,6 +4750,43 @@ TEST(SendAPI, SimpleInterface_Online) {
EXPECT_EQ(301, res->status);
}

TEST(ClientImplMethods, GetSocketTest) {
httplib::Server svr;
svr.Get( "/", [&](const httplib::Request& req, httplib::Response& res) {
res.status = 200;
});

auto thread = std::thread([&]() { svr.listen("127.0.0.1", 3333); });

while (!svr.is_running()) {
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}

{
httplib::Client cli("http://127.0.0.1:3333");
cli.set_keep_alive(true);

// Use the behavior of cpp-httplib of opening the connection
// only when the first request happens. If that changes,
// this test would be obsolete.

EXPECT_EQ(cli.socket(), INVALID_SOCKET);

// This also implicitly tests the server. But other tests would fail much
// earlier than this one to be considered.

auto res = cli.Get("/");
ASSERT_TRUE(res);

EXPECT_EQ(200, res->status);
ASSERT_TRUE(cli.socket() != INVALID_SOCKET);
}

svr.stop();
thread.join();
ASSERT_FALSE(svr.is_running());
}

// Disabled due to out-of-memory problem on GitHub Actions
#ifdef _WIN64
TEST(ServerLargeContentTest, DISABLED_SendLargeContent) {
Expand Down