Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 04002d5

Browse files
committed
Added set_default_headers (Fix yhirose#600)
1 parent 38a7706 commit 04002d5

File tree

2 files changed

+93
-16
lines changed

2 files changed

+93
-16
lines changed

httplib.h

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,16 @@ class ClientImpl {
702702
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
703703
ContentReceiver content_receiver,
704704
Progress progress);
705+
std::shared_ptr<Response> Get(const char *path,
706+
ResponseHandler response_handler,
707+
ContentReceiver content_receiver);
705708
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
706709
ResponseHandler response_handler,
707710
ContentReceiver content_receiver);
711+
std::shared_ptr<Response> Get(const char *path,
712+
ResponseHandler response_handler,
713+
ContentReceiver content_receiver,
714+
Progress progress);
708715
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
709716
ResponseHandler response_handler,
710717
ContentReceiver content_receiver,
@@ -781,6 +788,8 @@ class ClientImpl {
781788

782789
void stop();
783790

791+
void set_default_headers(Headers headers);
792+
784793
void set_tcp_nodelay(bool on);
785794
void set_socket_options(SocketOptions socket_options);
786795

@@ -838,6 +847,9 @@ class ClientImpl {
838847
mutable std::mutex socket_mutex_;
839848
std::recursive_mutex request_mutex_;
840849

850+
// Default headers
851+
Headers default_headers_;
852+
841853
// Settings
842854
std::string client_cert_path_;
843855
std::string client_key_path_;
@@ -967,13 +979,20 @@ class Client {
967979
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
968980
ContentReceiver content_receiver,
969981
Progress progress);
982+
std::shared_ptr<Response> Get(const char *path,
983+
ResponseHandler response_handler,
984+
ContentReceiver content_receiver);
970985
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
971986
ResponseHandler response_handler,
972987
ContentReceiver content_receiver);
973988
std::shared_ptr<Response> Get(const char *path, const Headers &headers,
974989
ResponseHandler response_handler,
975990
ContentReceiver content_receiver,
976991
Progress progress);
992+
std::shared_ptr<Response> Get(const char *path,
993+
ResponseHandler response_handler,
994+
ContentReceiver content_receiver,
995+
Progress progress);
977996

978997
std::shared_ptr<Response> Head(const char *path);
979998
std::shared_ptr<Response> Head(const char *path, const Headers &headers);
@@ -1044,6 +1063,8 @@ class Client {
10441063

10451064
void stop();
10461065

1066+
void set_default_headers(Headers headers);
1067+
10471068
void set_tcp_nodelay(bool on);
10481069
void set_socket_options(SocketOptions socket_options);
10491070

@@ -3285,7 +3306,7 @@ make_basic_authentication_header(const std::string &username,
32853306

32863307
inline std::pair<std::string, std::string>
32873308
make_bearer_token_authentication_header(const std::string &token,
3288-
bool is_proxy = false) {
3309+
bool is_proxy = false) {
32893310
auto field = "Bearer " + token;
32903311
auto key = is_proxy ? "Proxy-Authorization" : "Authorization";
32913312
return std::make_pair(key, field);
@@ -4788,7 +4809,8 @@ inline std::shared_ptr<Response> ClientImpl::send_with_content_provider(
47884809
ContentProvider content_provider, const char *content_type) {
47894810
Request req;
47904811
req.method = method;
4791-
req.headers = headers;
4812+
req.headers = default_headers_;
4813+
req.headers.insert(headers.begin(), headers.end());
47924814
req.path = path;
47934815

47944816
if (content_type) { req.headers.emplace("Content-Type", content_type); }
@@ -4928,7 +4950,8 @@ ClientImpl::Get(const char *path, const Headers &headers, Progress progress) {
49284950
Request req;
49294951
req.method = "GET";
49304952
req.path = path;
4931-
req.headers = headers;
4953+
req.headers = default_headers_;
4954+
req.headers.insert(headers.begin(), headers.end());
49324955
req.progress = std::move(progress);
49334956

49344957
auto res = std::make_shared<Response>();
@@ -4960,6 +4983,13 @@ ClientImpl::Get(const char *path, const Headers &headers,
49604983
std::move(progress));
49614984
}
49624985

4986+
inline std::shared_ptr<Response>
4987+
ClientImpl::Get(const char *path, ResponseHandler response_handler,
4988+
ContentReceiver content_receiver) {
4989+
return Get(path, Headers(), std::move(response_handler), content_receiver,
4990+
Progress());
4991+
}
4992+
49634993
inline std::shared_ptr<Response>
49644994
ClientImpl::Get(const char *path, const Headers &headers,
49654995
ResponseHandler response_handler,
@@ -4968,14 +4998,22 @@ ClientImpl::Get(const char *path, const Headers &headers,
49684998
Progress());
49694999
}
49705000

5001+
inline std::shared_ptr<Response>
5002+
ClientImpl::Get(const char *path, ResponseHandler response_handler,
5003+
ContentReceiver content_receiver, Progress progress) {
5004+
return Get(path, Headers(), std::move(response_handler), content_receiver,
5005+
progress);
5006+
}
5007+
49715008
inline std::shared_ptr<Response>
49725009
ClientImpl::Get(const char *path, const Headers &headers,
49735010
ResponseHandler response_handler,
49745011
ContentReceiver content_receiver, Progress progress) {
49755012
Request req;
49765013
req.method = "GET";
49775014
req.path = path;
4978-
req.headers = headers;
5015+
req.headers = default_headers_;
5016+
req.headers.insert(headers.begin(), headers.end());
49795017
req.response_handler = std::move(response_handler);
49805018
req.content_receiver = std::move(content_receiver);
49815019
req.progress = std::move(progress);
@@ -4992,7 +5030,8 @@ inline std::shared_ptr<Response> ClientImpl::Head(const char *path,
49925030
const Headers &headers) {
49935031
Request req;
49945032
req.method = "HEAD";
4995-
req.headers = headers;
5033+
req.headers = default_headers_;
5034+
req.headers.insert(headers.begin(), headers.end());
49965035
req.path = path;
49975036

49985037
auto res = std::make_shared<Response>();
@@ -5171,7 +5210,8 @@ inline std::shared_ptr<Response> ClientImpl::Delete(const char *path,
51715210
const char *content_type) {
51725211
Request req;
51735212
req.method = "DELETE";
5174-
req.headers = headers;
5213+
req.headers = default_headers_;
5214+
req.headers.insert(headers.begin(), headers.end());
51755215
req.path = path;
51765216

51775217
if (content_type) { req.headers.emplace("Content-Type", content_type); }
@@ -5190,8 +5230,9 @@ inline std::shared_ptr<Response> ClientImpl::Options(const char *path,
51905230
const Headers &headers) {
51915231
Request req;
51925232
req.method = "OPTIONS";
5233+
req.headers = default_headers_;
5234+
req.headers.insert(headers.begin(), headers.end());
51935235
req.path = path;
5194-
req.headers = headers;
51955236

51965237
auto res = std::make_shared<Response>();
51975238

@@ -5250,6 +5291,10 @@ inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; }
52505291

52515292
inline void ClientImpl::set_follow_location(bool on) { follow_location_ = on; }
52525293

5294+
inline void ClientImpl::set_default_headers(Headers headers) {
5295+
default_headers_ = std::move(headers);
5296+
}
5297+
52535298
inline void ClientImpl::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
52545299

52555300
inline void ClientImpl::set_socket_options(SocketOptions socket_options) {
@@ -6001,12 +6046,24 @@ inline std::shared_ptr<Response> Client::Get(const char *path,
60016046
Progress progress) {
60026047
return cli_->Get(path, headers, content_receiver, progress);
60036048
}
6049+
inline std::shared_ptr<Response> Client::Get(const char *path,
6050+
ResponseHandler response_handler,
6051+
ContentReceiver content_receiver) {
6052+
return cli_->Get(path, Headers(), response_handler, content_receiver);
6053+
}
60046054
inline std::shared_ptr<Response> Client::Get(const char *path,
60056055
const Headers &headers,
60066056
ResponseHandler response_handler,
60076057
ContentReceiver content_receiver) {
60086058
return cli_->Get(path, headers, response_handler, content_receiver);
60096059
}
6060+
inline std::shared_ptr<Response> Client::Get(const char *path,
6061+
ResponseHandler response_handler,
6062+
ContentReceiver content_receiver,
6063+
Progress progress) {
6064+
return cli_->Get(path, Headers(), response_handler, content_receiver,
6065+
progress);
6066+
}
60106067
inline std::shared_ptr<Response> Client::Get(const char *path,
60116068
const Headers &headers,
60126069
ResponseHandler response_handler,
@@ -6157,6 +6214,10 @@ inline size_t Client::is_socket_open() const { return cli_->is_socket_open(); }
61576214

61586215
inline void Client::stop() { cli_->stop(); }
61596216

6217+
inline void Client::set_default_headers(Headers headers) {
6218+
cli_->set_default_headers(std::move(headers));
6219+
}
6220+
61606221
inline void Client::set_tcp_nodelay(bool on) { cli_->set_tcp_nodelay(on); }
61616222
inline void Client::set_socket_options(SocketOptions socket_options) {
61626223
cli_->set_socket_options(socket_options);

test/test.cc

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
354354

355355
std::string body;
356356
auto res = cli.Get(
357-
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137", Headers(),
357+
"/httpgallery/chunked/chunkedimage.aspx?0.4153841143030137",
358358
[&](const Response &response) {
359359
EXPECT_EQ(200, response.status);
360360
return true;
@@ -372,6 +372,26 @@ TEST(ChunkedEncodingTest, WithResponseHandlerAndContentReceiver) {
372372
EXPECT_EQ(out, body);
373373
}
374374

375+
TEST(DefaultHeadersTest, FromHTTPBin) {
376+
Client cli("httpbin.org");
377+
cli.set_default_headers({make_range_header({{1, 10}})});
378+
cli.set_connection_timeout(5);
379+
380+
{
381+
auto res = cli.Get("/range/32");
382+
ASSERT_TRUE(res != nullptr);
383+
EXPECT_EQ("bcdefghijk", res->body);
384+
EXPECT_EQ(206, res->status);
385+
}
386+
387+
{
388+
auto res = cli.Get("/range/32");
389+
ASSERT_TRUE(res != nullptr);
390+
EXPECT_EQ("bcdefghijk", res->body);
391+
EXPECT_EQ(206, res->status);
392+
}
393+
}
394+
375395
TEST(RangeTest, FromHTTPBin) {
376396
auto host = "httpbin.org";
377397

@@ -385,8 +405,7 @@ TEST(RangeTest, FromHTTPBin) {
385405
cli.set_connection_timeout(5);
386406

387407
{
388-
Headers headers;
389-
auto res = cli.Get("/range/32", headers);
408+
auto res = cli.Get("/range/32");
390409
ASSERT_TRUE(res != nullptr);
391410
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
392411
EXPECT_EQ(200, res->status);
@@ -541,8 +560,7 @@ TEST(CancelTest, WithCancelLargePayload) {
541560
cli.set_connection_timeout(5);
542561

543562
uint32_t count = 0;
544-
Headers headers;
545-
auto res = cli.Get("/range/65536", headers,
563+
auto res = cli.Get("/range/65536",
546564
[&count](uint64_t, uint64_t) { return (count++ == 0); });
547565
ASSERT_TRUE(res == nullptr);
548566
}
@@ -2319,8 +2337,7 @@ TEST_F(ServerTest, Gzip) {
23192337
}
23202338

23212339
TEST_F(ServerTest, GzipWithoutAcceptEncoding) {
2322-
Headers headers;
2323-
auto res = cli_.Get("/compress", headers);
2340+
auto res = cli_.Get("/compress");
23242341

23252342
ASSERT_TRUE(res != nullptr);
23262343
EXPECT_TRUE(res->get_header_value("Content-Encoding").empty());
@@ -2369,9 +2386,8 @@ TEST_F(ServerTest, GzipWithoutDecompressing) {
23692386
}
23702387

23712388
TEST_F(ServerTest, GzipWithContentReceiverWithoutAcceptEncoding) {
2372-
Headers headers;
23732389
std::string body;
2374-
auto res = cli_.Get("/compress", headers,
2390+
auto res = cli_.Get("/compress",
23752391
[&](const char *data, uint64_t data_length) {
23762392
EXPECT_EQ(data_length, 100);
23772393
body.append(data, data_length);

0 commit comments

Comments
 (0)