Skip to content

Commit 0308d60

Browse files
authored
Resolve #831 (#835)
1 parent 59f5fdb commit 0308d60

File tree

2 files changed

+128
-9
lines changed

2 files changed

+128
-9
lines changed

httplib.h

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ class Server {
639639
Headers headers = Headers());
640640
bool remove_mount_point(const char *mount_point);
641641
Server &set_file_extension_and_mimetype_mapping(const char *ext,
642-
const char *mime);
642+
const char *mime);
643643
Server &set_file_request_handler(Handler handler);
644644

645645
Server &set_error_handler(HandlerWithReturn handler);
@@ -835,6 +835,14 @@ class ClientImpl {
835835
ResponseHandler response_handler, ContentReceiver content_receiver,
836836
Progress progress);
837837

838+
Result Get(const char *path, const Params &params, const Headers &headers,
839+
Progress progress = nullptr);
840+
Result Get(const char *path, const Params &params, const Headers &headers,
841+
ContentReceiver content_receiver, Progress progress = nullptr);
842+
Result Get(const char *path, const Params &params, const Headers &headers,
843+
ResponseHandler response_handler, ContentReceiver content_receiver,
844+
Progress progress = nullptr);
845+
838846
Result Head(const char *path);
839847
Result Head(const char *path, const Headers &headers);
840848

@@ -1128,6 +1136,14 @@ class Client {
11281136
Result Get(const char *path, ResponseHandler response_handler,
11291137
ContentReceiver content_receiver, Progress progress);
11301138

1139+
Result Get(const char *path, const Params &params, const Headers &headers,
1140+
Progress progress = nullptr);
1141+
Result Get(const char *path, const Params &params, const Headers &headers,
1142+
ContentReceiver content_receiver, Progress progress = nullptr);
1143+
Result Get(const char *path, const Params &params, const Headers &headers,
1144+
ResponseHandler response_handler, ContentReceiver content_receiver,
1145+
Progress progress = nullptr);
1146+
11311147
Result Head(const char *path);
11321148
Result Head(const char *path, const Headers &headers);
11331149

@@ -3125,6 +3141,14 @@ inline std::string params_to_query_str(const Params &params) {
31253141
return query;
31263142
}
31273143

3144+
inline std::string append_query_params(const char *path, const Params &params) {
3145+
std::string path_with_query = path;
3146+
const static std::regex re("[^?]+\\?.*");
3147+
auto delm = std::regex_match(path, re) ? '&' : '?';
3148+
path_with_query += delm + params_to_query_str(params);
3149+
return path_with_query;
3150+
}
3151+
31283152
inline void parse_query_text(const std::string &s, Params &params) {
31293153
split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
31303154
std::string key;
@@ -4222,8 +4246,9 @@ inline bool Server::remove_mount_point(const char *mount_point) {
42224246
return false;
42234247
}
42244248

4225-
inline Server &Server::set_file_extension_and_mimetype_mapping(const char *ext,
4226-
const char *mime) {
4249+
inline Server &
4250+
Server::set_file_extension_and_mimetype_mapping(const char *ext,
4251+
const char *mime) {
42274252
file_extension_and_mimetype_map_[ext] = mime;
42284253

42294254
return *this;
@@ -4264,8 +4289,8 @@ inline Server &Server::set_logger(Logger logger) {
42644289
return *this;
42654290
}
42664291

4267-
inline Server
4268-
&Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
4292+
inline Server &
4293+
Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
42694294
expect_100_continue_handler_ = std::move(handler);
42704295

42714296
return *this;
@@ -5796,6 +5821,35 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
57965821
return send(req);
57975822
}
57985823

5824+
inline Result ClientImpl::Get(const char *path, const Params &params,
5825+
const Headers &headers, Progress progress) {
5826+
if (params.empty()) { return Get(path, headers); }
5827+
5828+
std::string path_with_query = detail::append_query_params(path, params);
5829+
return Get(path_with_query.c_str(), headers, progress);
5830+
}
5831+
5832+
inline Result ClientImpl::Get(const char *path, const Params &params,
5833+
const Headers &headers,
5834+
ContentReceiver content_receiver,
5835+
Progress progress) {
5836+
return Get(path, params, headers, nullptr, content_receiver, progress);
5837+
}
5838+
5839+
inline Result ClientImpl::Get(const char *path, const Params &params,
5840+
const Headers &headers,
5841+
ResponseHandler response_handler,
5842+
ContentReceiver content_receiver,
5843+
Progress progress) {
5844+
if (params.empty()) {
5845+
return Get(path, headers, response_handler, content_receiver, progress);
5846+
}
5847+
5848+
std::string path_with_query = detail::append_query_params(path, params);
5849+
return Get(path_with_query.c_str(), params, headers, response_handler,
5850+
content_receiver, progress);
5851+
}
5852+
57995853
inline Result ClientImpl::Head(const char *path) {
58005854
return Head(path, Headers());
58015855
}
@@ -7020,6 +7074,22 @@ inline Result Client::Get(const char *path, const Headers &headers,
70207074
return cli_->Get(path, headers, std::move(response_handler),
70217075
std::move(content_receiver), std::move(progress));
70227076
}
7077+
inline Result Client::Get(const char *path, const Params &params,
7078+
const Headers &headers, Progress progress) {
7079+
return cli_->Get(path, params, headers, progress);
7080+
}
7081+
inline Result Client::Get(const char *path, const Params &params,
7082+
const Headers &headers,
7083+
ContentReceiver content_receiver, Progress progress) {
7084+
return cli_->Get(path, params, headers, content_receiver, progress);
7085+
}
7086+
inline Result Client::Get(const char *path, const Params &params,
7087+
const Headers &headers,
7088+
ResponseHandler response_handler,
7089+
ContentReceiver content_receiver, Progress progress) {
7090+
return cli_->Get(path, params, headers, response_handler, content_receiver,
7091+
progress);
7092+
}
70237093

70247094
inline Result Client::Head(const char *path) { return cli_->Head(path); }
70257095
inline Result Client::Head(const char *path, const Headers &headers) {

test/test.cc

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,31 @@ TEST(HttpsToHttpRedirectTest, Redirect) {
816816
EXPECT_EQ(200, res->status);
817817
}
818818

819+
TEST(HttpsToHttpRedirectTest2, Redirect) {
820+
SSLClient cli("nghttp2.org");
821+
cli.set_follow_location(true);
822+
823+
Params params;
824+
params.emplace("url", "http://www.google.com");
825+
params.emplace("status_code", "302");
826+
827+
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
828+
ASSERT_TRUE(res);
829+
EXPECT_EQ(200, res->status);
830+
}
831+
832+
TEST(HttpsToHttpRedirectTest3, Redirect) {
833+
SSLClient cli("nghttp2.org");
834+
cli.set_follow_location(true);
835+
836+
Params params;
837+
params.emplace("url", "http://www.google.com");
838+
839+
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
840+
ASSERT_TRUE(res);
841+
EXPECT_EQ(200, res->status);
842+
}
843+
819844
TEST(RedirectToDifferentPort, Redirect) {
820845
Server svr8080;
821846
Server svr8081;
@@ -956,9 +981,8 @@ TEST(ErrorHandlerTest, ContentLength) {
956981
TEST(NoContentTest, ContentLength) {
957982
Server svr;
958983

959-
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
960-
res.status = 204;
961-
});
984+
svr.Get("/hi",
985+
[](const Request & /*req*/, Response &res) { res.status = 204; });
962986
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
963987

964988
// Give GET time to get a few messages.
@@ -3979,7 +4003,7 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding) {
39794003
}
39804004
#endif
39814005

3982-
TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
4006+
TEST(HttpsToHttpRedirectTest, SimpleInterface) {
39834007
Client cli("https://nghttp2.org");
39844008
cli.set_follow_location(true);
39854009
auto res =
@@ -3989,4 +4013,29 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
39894013
ASSERT_TRUE(res);
39904014
EXPECT_EQ(200, res->status);
39914015
}
4016+
4017+
TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
4018+
Client cli("https://nghttp2.org");
4019+
cli.set_follow_location(true);
4020+
4021+
Params params;
4022+
params.emplace("url", "http://www.google.com");
4023+
params.emplace("status_code", "302");
4024+
4025+
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
4026+
ASSERT_TRUE(res);
4027+
EXPECT_EQ(200, res->status);
4028+
}
4029+
4030+
TEST(HttpsToHttpRedirectTest3, SimpleInterface) {
4031+
Client cli("https://nghttp2.org");
4032+
cli.set_follow_location(true);
4033+
4034+
Params params;
4035+
params.emplace("url", "http://www.google.com");
4036+
4037+
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
4038+
ASSERT_TRUE(res);
4039+
EXPECT_EQ(200, res->status);
4040+
}
39924041
#endif

0 commit comments

Comments
 (0)