Skip to content

Commit e421287

Browse files
authored
Merge pull request #1223 from gitpaladin/master
Add `primary_ip` `primary_port` to `Response`
2 parents 0292b5a + 12a511e commit e421287

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

cpr/response.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ Response::Response(std::shared_ptr<CurlHolder> curl, std::string&& p_text, std::
3535
uploaded_bytes = uploaded_bytes_double;
3636
#endif
3737
curl_easy_getinfo(curl_->handle, CURLINFO_REDIRECT_COUNT, &redirect_count);
38+
#if LIBCURL_VERSION_NUM >= 0x071300 // 7.19.0
39+
char* ip_ptr{nullptr};
40+
if (curl_easy_getinfo(curl_->handle, CURLINFO_PRIMARY_IP, &ip_ptr) == CURLE_OK && ip_ptr) {
41+
primary_ip = ip_ptr;
42+
}
43+
#endif
44+
#if LIBCURL_VERSION_NUM >= 0x071500 // 7.21.0
45+
// Ignored here since libcurl uses a long for this.
46+
// NOLINTNEXTLINE(google-runtime-int)
47+
long port = 0;
48+
if (curl_easy_getinfo(curl_->handle, CURLINFO_PRIMARY_PORT, &port) == CURLE_OK) {
49+
primary_port = port;
50+
}
51+
#endif
3852
}
3953

4054
std::vector<CertInfo> Response::GetCertInfos() const {

include/cpr/response.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Response {
4242
// Ignored here since libcurl uses a long for this.
4343
// NOLINTNEXTLINE(google-runtime-int)
4444
long redirect_count{};
45+
std::string primary_ip{};
46+
std::uint16_t primary_port{};
4547

4648
Response() = default;
4749
Response(std::shared_ptr<CurlHolder> curl, std::string&& p_text, std::string&& p_header_string, Cookies&& p_cookies, Error&& p_error);

test/async_tests.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ TEST(AsyncTests, AsyncGetTest) {
2727
EXPECT_EQ(url, response.url);
2828
EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
2929
EXPECT_EQ(200, response.status_code);
30+
EXPECT_EQ(response.primary_ip, "127.0.0.1");
31+
EXPECT_EQ(response.primary_port, server->GetPort());
3032
}
3133

3234
TEST(AsyncTests, AsyncGetMultipleTest) {

0 commit comments

Comments
 (0)