Skip to content

Commit 63a96ae

Browse files
committed
Improved Client2 interface
1 parent bbb83d1 commit 63a96ae

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

httplib.h

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5354,63 +5354,88 @@ class Client2 {
53545354

53555355
void stop() { cli_->stop(); }
53565356

5357-
void set_timeout_sec(time_t timeout_sec) {
5357+
Client2 &set_timeout_sec(time_t timeout_sec) {
53585358
cli_->set_timeout_sec(timeout_sec);
5359+
return *this;
53595360
}
53605361

5361-
void set_read_timeout(time_t sec, time_t usec) {
5362+
Client2 &set_read_timeout(time_t sec, time_t usec) {
53625363
cli_->set_read_timeout(sec, usec);
5364+
return *this;
53635365
}
53645366

5365-
void set_keep_alive_max_count(size_t count) {
5367+
Client2 &set_keep_alive_max_count(size_t count) {
53665368
cli_->set_keep_alive_max_count(count);
5369+
return *this;
53675370
}
53685371

5369-
void set_basic_auth(const char *username, const char *password) {
5372+
Client2 &set_basic_auth(const char *username, const char *password) {
53705373
cli_->set_basic_auth(username, password);
5374+
return *this;
53715375
}
53725376

53735377
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
5374-
void set_digest_auth(const char *username, const char *password) {
5378+
Client2 &set_digest_auth(const char *username, const char *password) {
53755379
cli_->set_digest_auth(username, password);
5380+
return *this;
53765381
}
53775382
#endif
53785383

5379-
void set_follow_location(bool on) { cli_->set_follow_location(on); }
5384+
Client2 &set_follow_location(bool on) {
5385+
cli_->set_follow_location(on);
5386+
return *this;
5387+
}
53805388

5381-
void set_compress(bool on) { cli_->set_compress(on); }
5389+
Client2 &set_compress(bool on) {
5390+
cli_->set_compress(on);
5391+
return *this;
5392+
}
53825393

5383-
void set_interface(const char *intf) { cli_->set_interface(intf); }
5394+
Client2 &set_interface(const char *intf) {
5395+
cli_->set_interface(intf);
5396+
return *this;
5397+
}
53845398

5385-
void set_proxy(const char *host, int port) { cli_->set_proxy(host, port); }
5399+
Client2 &set_proxy(const char *host, int port) {
5400+
cli_->set_proxy(host, port);
5401+
return *this;
5402+
}
53865403

5387-
void set_proxy_basic_auth(const char *username, const char *password) {
5404+
Client2 &set_proxy_basic_auth(const char *username, const char *password) {
53885405
cli_->set_proxy_basic_auth(username, password);
5406+
return *this;
53895407
}
53905408

53915409
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
5392-
void set_proxy_digest_auth(const char *username, const char *password) {
5410+
Client2 &set_proxy_digest_auth(const char *username, const char *password) {
53935411
cli_->set_proxy_digest_auth(username, password);
5412+
return *this;
53945413
}
53955414
#endif
53965415

5397-
void set_logger(Logger logger) { cli_->set_logger(logger); }
5416+
Client2 &set_logger(Logger logger) {
5417+
cli_->set_logger(logger);
5418+
return *this;
5419+
}
53985420

53995421
// SSL
54005422
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
5401-
void set_ca_cert_path(const char *ca_cert_file_path,
5402-
const char *ca_cert_dir_path = nullptr) {
5423+
Client2 &set_ca_cert_path(const char *ca_cert_file_path,
5424+
const char *ca_cert_dir_path = nullptr) {
54035425
dynamic_cast<SSLClient &>(*cli_).set_ca_cert_path(ca_cert_file_path,
54045426
ca_cert_dir_path);
5427+
return *this;
54055428
}
54065429

5407-
void set_ca_cert_store(X509_STORE *ca_cert_store) {
5430+
Client2 &set_ca_cert_store(X509_STORE *ca_cert_store) {
54085431
dynamic_cast<SSLClient &>(*cli_).set_ca_cert_store(ca_cert_store);
5432+
return *this;
54095433
}
54105434

5411-
void enable_server_certificate_verification(bool enabled) {
5435+
Client2 &enable_server_certificate_verification(bool enabled) {
54125436
dynamic_cast<SSLClient &>(*cli_).enable_server_certificate_verification(
54135437
enabled);
5438+
return *this;
54145439
}
54155440

54165441
long get_openssl_verify_result() const {

test/test.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,11 @@ TEST(HttpsToHttpRedirectTest, Redirect) {
674674
}
675675

676676
TEST(HttpsToHttpRedirectTest2, Redirect) {
677-
httplib::Client2 cli("https://httpbin.org");
678-
cli.set_follow_location(true);
679677
auto res =
680-
cli.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
678+
httplib::Client2("https://httpbin.org")
679+
.set_follow_location(true)
680+
.Get("/redirect-to?url=http%3A%2F%2Fwww.google.com&status_code=302");
681+
681682
ASSERT_TRUE(res != nullptr);
682683
EXPECT_EQ(200, res->status);
683684
}

0 commit comments

Comments
 (0)