Skip to content

Commit 192f298

Browse files
committed
Added Get2
1 parent 143b2dd commit 192f298

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

httplib.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,13 @@ class Result {
717717
Error err_;
718718
};
719719

720+
struct ClientOptions {
721+
Headers headers;
722+
Progress progress;
723+
ResponseHandler response_handler;
724+
ContentReceiver content_receiver;
725+
};
726+
720727
class ClientImpl {
721728
public:
722729
explicit ClientImpl(const std::string &host);
@@ -731,6 +738,8 @@ class ClientImpl {
731738

732739
virtual bool is_valid() const;
733740

741+
Result Get2(std::string_view path, ClientOptions options);
742+
734743
Result Get(const char *path);
735744
Result Get(const char *path, const Headers &headers);
736745
Result Get(const char *path, Progress progress);
@@ -966,6 +975,8 @@ class Client {
966975

967976
bool is_valid() const;
968977

978+
Result Get2(std::string_view path, ClientOptions options);
979+
969980
Result Get(const char *path);
970981
Result Get(const char *path, const Headers &headers);
971982
Result Get(const char *path, Progress progress);
@@ -5106,6 +5117,13 @@ ClientImpl::process_socket(Socket &socket,
51065117

51075118
inline bool ClientImpl::is_ssl() const { return false; }
51085119

5120+
inline Result ClientImpl::Get2(std::string_view path, ClientOptions options) {
5121+
auto path2 = std::string(path);
5122+
return Get(path2.c_str(), std::move(options.headers),
5123+
std::move(options.response_handler),
5124+
std::move(options.content_receiver), std::move(options.progress));
5125+
}
5126+
51095127
inline Result ClientImpl::Get(const char *path) {
51105128
return Get(path, Headers(), Progress());
51115129
}
@@ -5844,7 +5862,7 @@ inline void SSLClient::set_ca_cert_path(const char *ca_cert_file_path,
58445862

58455863
inline void SSLClient::set_ca_cert_store(X509_STORE *ca_cert_store) {
58465864
if (ca_cert_store) {
5847-
if(ctx_) {
5865+
if (ctx_) {
58485866
if (SSL_CTX_get_cert_store(ctx_) != ca_cert_store) {
58495867
// Free memory allocated for old cert and use new store `ca_cert_store`
58505868
SSL_CTX_set_cert_store(ctx_, ca_cert_store);
@@ -6205,6 +6223,10 @@ inline bool Client::is_valid() const {
62056223
return cli_ != nullptr && cli_->is_valid();
62066224
}
62076225

6226+
inline Result Client::Get2(std::string_view path, ClientOptions options) {
6227+
return cli_->Get2(path, options);
6228+
}
6229+
62086230
inline Result Client::Get(const char *path) { return cli_->Get(path); }
62096231
inline Result Client::Get(const char *path, const Headers &headers) {
62106232
return cli_->Get(path, headers);

test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#CXX = clang++
3-
CXXFLAGS = -ggdb -O0 -std=c++11 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion -fsanitize=address
3+
CXXFLAGS = -ggdb -O0 -std=c++17 -DGTEST_USE_OWN_TR1_TUPLE -I.. -I. -Wall -Wextra -Wtype-limits -Wconversion -fsanitize=address
44

55
OPENSSL_DIR = /usr/local/opt/[email protected]
66
OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto

test/test.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3443,4 +3443,57 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
34433443
EXPECT_EQ(200, res->status);
34443444
}
34453445
#endif
3446+
3447+
/**
3448+
* C++17 Interface Examples
3449+
*/
3450+
3451+
TEST(ChunkedEncodingTest, Cpp17_WithResponseHandlerAndContentReceiver) {
3452+
auto host = "www.httpwatch.com";
3453+
3454+
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
3455+
auto port = 443;
3456+
SSLClient cli(host, port);
3457+
#else
3458+
auto port = 80;
3459+
Client cli(host, port);
3460+
#endif
3461+
cli.set_connection_timeout(2);
3462+
3463+
std::string path =
3464+
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137";
3465+
3466+
std::string body;
3467+
auto res = cli.Get2(path, {.response_handler =
3468+
[&](const Response &response) {
3469+
EXPECT_EQ(200, response.status);
3470+
return true;
3471+
},
3472+
.content_receiver =
3473+
[&](const char *data, size_t data_length) {
3474+
body.append(data, data_length);
3475+
return true;
3476+
}});
3477+
ASSERT_TRUE(res);
3478+
3479+
std::string out;
3480+
detail::read_file("./image.jpg", out);
3481+
3482+
EXPECT_EQ(200, res->status);
3483+
EXPECT_EQ(out, body);
3484+
}
3485+
3486+
TEST(DecodeWithChunkedEncoding, Cpp17_BrotliEncoding) {
3487+
Client cli("https://cdnjs.cloudflare.com");
3488+
3489+
const char *path = "/ajax/libs/jquery/3.5.1/jquery.js";
3490+
3491+
auto res = cli.Get2(path, {.headers = {{"Accept-Encoding", "brotli"}}});
3492+
3493+
ASSERT_TRUE(res);
3494+
EXPECT_EQ(200, res->status);
3495+
EXPECT_EQ(287630, res->body.size());
3496+
EXPECT_EQ("application/javascript; charset=utf-8",
3497+
res->get_header_value("Content-Type"));
3498+
}
34463499
#endif

0 commit comments

Comments
 (0)