mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 09:18:27 +00:00
parent
59f5fdbb33
commit
0308d60cb2
2 changed files with 128 additions and 9 deletions
76
httplib.h
76
httplib.h
|
@ -835,6 +835,14 @@ public:
|
||||||
ResponseHandler response_handler, ContentReceiver content_receiver,
|
ResponseHandler response_handler, ContentReceiver content_receiver,
|
||||||
Progress progress);
|
Progress progress);
|
||||||
|
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
Progress progress = nullptr);
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
ContentReceiver content_receiver, Progress progress = nullptr);
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
ResponseHandler response_handler, ContentReceiver content_receiver,
|
||||||
|
Progress progress = nullptr);
|
||||||
|
|
||||||
Result Head(const char *path);
|
Result Head(const char *path);
|
||||||
Result Head(const char *path, const Headers &headers);
|
Result Head(const char *path, const Headers &headers);
|
||||||
|
|
||||||
|
@ -1128,6 +1136,14 @@ public:
|
||||||
Result Get(const char *path, ResponseHandler response_handler,
|
Result Get(const char *path, ResponseHandler response_handler,
|
||||||
ContentReceiver content_receiver, Progress progress);
|
ContentReceiver content_receiver, Progress progress);
|
||||||
|
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
Progress progress = nullptr);
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
ContentReceiver content_receiver, Progress progress = nullptr);
|
||||||
|
Result Get(const char *path, const Params ¶ms, const Headers &headers,
|
||||||
|
ResponseHandler response_handler, ContentReceiver content_receiver,
|
||||||
|
Progress progress = nullptr);
|
||||||
|
|
||||||
Result Head(const char *path);
|
Result Head(const char *path);
|
||||||
Result Head(const char *path, const Headers &headers);
|
Result Head(const char *path, const Headers &headers);
|
||||||
|
|
||||||
|
@ -3125,6 +3141,14 @@ inline std::string params_to_query_str(const Params ¶ms) {
|
||||||
return query;
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline std::string append_query_params(const char *path, const Params ¶ms) {
|
||||||
|
std::string path_with_query = path;
|
||||||
|
const static std::regex re("[^?]+\\?.*");
|
||||||
|
auto delm = std::regex_match(path, re) ? '&' : '?';
|
||||||
|
path_with_query += delm + params_to_query_str(params);
|
||||||
|
return path_with_query;
|
||||||
|
}
|
||||||
|
|
||||||
inline void parse_query_text(const std::string &s, Params ¶ms) {
|
inline void parse_query_text(const std::string &s, Params ¶ms) {
|
||||||
split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
|
split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
|
||||||
std::string key;
|
std::string key;
|
||||||
|
@ -4222,7 +4246,8 @@ inline bool Server::remove_mount_point(const char *mount_point) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Server &Server::set_file_extension_and_mimetype_mapping(const char *ext,
|
inline Server &
|
||||||
|
Server::set_file_extension_and_mimetype_mapping(const char *ext,
|
||||||
const char *mime) {
|
const char *mime) {
|
||||||
file_extension_and_mimetype_map_[ext] = mime;
|
file_extension_and_mimetype_map_[ext] = mime;
|
||||||
|
|
||||||
|
@ -4264,8 +4289,8 @@ inline Server &Server::set_logger(Logger logger) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Server
|
inline Server &
|
||||||
&Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
|
Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
|
||||||
expect_100_continue_handler_ = std::move(handler);
|
expect_100_continue_handler_ = std::move(handler);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -5796,6 +5821,35 @@ inline Result ClientImpl::Get(const char *path, const Headers &headers,
|
||||||
return send(req);
|
return send(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline Result ClientImpl::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers, Progress progress) {
|
||||||
|
if (params.empty()) { return Get(path, headers); }
|
||||||
|
|
||||||
|
std::string path_with_query = detail::append_query_params(path, params);
|
||||||
|
return Get(path_with_query.c_str(), headers, progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Result ClientImpl::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers,
|
||||||
|
ContentReceiver content_receiver,
|
||||||
|
Progress progress) {
|
||||||
|
return Get(path, params, headers, nullptr, content_receiver, progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Result ClientImpl::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers,
|
||||||
|
ResponseHandler response_handler,
|
||||||
|
ContentReceiver content_receiver,
|
||||||
|
Progress progress) {
|
||||||
|
if (params.empty()) {
|
||||||
|
return Get(path, headers, response_handler, content_receiver, progress);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string path_with_query = detail::append_query_params(path, params);
|
||||||
|
return Get(path_with_query.c_str(), params, headers, response_handler,
|
||||||
|
content_receiver, progress);
|
||||||
|
}
|
||||||
|
|
||||||
inline Result ClientImpl::Head(const char *path) {
|
inline Result ClientImpl::Head(const char *path) {
|
||||||
return Head(path, Headers());
|
return Head(path, Headers());
|
||||||
}
|
}
|
||||||
|
@ -7020,6 +7074,22 @@ inline Result Client::Get(const char *path, const Headers &headers,
|
||||||
return cli_->Get(path, headers, std::move(response_handler),
|
return cli_->Get(path, headers, std::move(response_handler),
|
||||||
std::move(content_receiver), std::move(progress));
|
std::move(content_receiver), std::move(progress));
|
||||||
}
|
}
|
||||||
|
inline Result Client::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers, Progress progress) {
|
||||||
|
return cli_->Get(path, params, headers, progress);
|
||||||
|
}
|
||||||
|
inline Result Client::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers,
|
||||||
|
ContentReceiver content_receiver, Progress progress) {
|
||||||
|
return cli_->Get(path, params, headers, content_receiver, progress);
|
||||||
|
}
|
||||||
|
inline Result Client::Get(const char *path, const Params ¶ms,
|
||||||
|
const Headers &headers,
|
||||||
|
ResponseHandler response_handler,
|
||||||
|
ContentReceiver content_receiver, Progress progress) {
|
||||||
|
return cli_->Get(path, params, headers, response_handler, content_receiver,
|
||||||
|
progress);
|
||||||
|
}
|
||||||
|
|
||||||
inline Result Client::Head(const char *path) { return cli_->Head(path); }
|
inline Result Client::Head(const char *path) { return cli_->Head(path); }
|
||||||
inline Result Client::Head(const char *path, const Headers &headers) {
|
inline Result Client::Head(const char *path, const Headers &headers) {
|
||||||
|
|
57
test/test.cc
57
test/test.cc
|
@ -816,6 +816,31 @@ TEST(HttpsToHttpRedirectTest, Redirect) {
|
||||||
EXPECT_EQ(200, res->status);
|
EXPECT_EQ(200, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(HttpsToHttpRedirectTest2, Redirect) {
|
||||||
|
SSLClient cli("nghttp2.org");
|
||||||
|
cli.set_follow_location(true);
|
||||||
|
|
||||||
|
Params params;
|
||||||
|
params.emplace("url", "http://www.google.com");
|
||||||
|
params.emplace("status_code", "302");
|
||||||
|
|
||||||
|
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(HttpsToHttpRedirectTest3, Redirect) {
|
||||||
|
SSLClient cli("nghttp2.org");
|
||||||
|
cli.set_follow_location(true);
|
||||||
|
|
||||||
|
Params params;
|
||||||
|
params.emplace("url", "http://www.google.com");
|
||||||
|
|
||||||
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
TEST(RedirectToDifferentPort, Redirect) {
|
TEST(RedirectToDifferentPort, Redirect) {
|
||||||
Server svr8080;
|
Server svr8080;
|
||||||
Server svr8081;
|
Server svr8081;
|
||||||
|
@ -956,9 +981,8 @@ TEST(ErrorHandlerTest, ContentLength) {
|
||||||
TEST(NoContentTest, ContentLength) {
|
TEST(NoContentTest, ContentLength) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
svr.Get("/hi", [](const Request & /*req*/, Response &res) {
|
svr.Get("/hi",
|
||||||
res.status = 204;
|
[](const Request & /*req*/, Response &res) { res.status = 204; });
|
||||||
});
|
|
||||||
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });
|
||||||
|
|
||||||
// Give GET time to get a few messages.
|
// Give GET time to get a few messages.
|
||||||
|
@ -3979,7 +4003,7 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
|
TEST(HttpsToHttpRedirectTest, SimpleInterface) {
|
||||||
Client cli("https://nghttp2.org");
|
Client cli("https://nghttp2.org");
|
||||||
cli.set_follow_location(true);
|
cli.set_follow_location(true);
|
||||||
auto res =
|
auto res =
|
||||||
|
@ -3989,4 +4013,29 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
|
||||||
ASSERT_TRUE(res);
|
ASSERT_TRUE(res);
|
||||||
EXPECT_EQ(200, res->status);
|
EXPECT_EQ(200, res->status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
|
||||||
|
Client cli("https://nghttp2.org");
|
||||||
|
cli.set_follow_location(true);
|
||||||
|
|
||||||
|
Params params;
|
||||||
|
params.emplace("url", "http://www.google.com");
|
||||||
|
params.emplace("status_code", "302");
|
||||||
|
|
||||||
|
auto res = cli.Get("/httpbin/redirect-to", params, Headers{});
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(HttpsToHttpRedirectTest3, SimpleInterface) {
|
||||||
|
Client cli("https://nghttp2.org");
|
||||||
|
cli.set_follow_location(true);
|
||||||
|
|
||||||
|
Params params;
|
||||||
|
params.emplace("url", "http://www.google.com");
|
||||||
|
|
||||||
|
auto res = cli.Get("/httpbin/redirect-to?status_code=302", params, Headers{});
|
||||||
|
ASSERT_TRUE(res);
|
||||||
|
EXPECT_EQ(200, res->status);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue