Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose authored Jan 23, 2021
1 parent 59f5fdb commit 0308d60
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 9 deletions.
80 changes: 75 additions & 5 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class Server {
Headers headers = Headers());
bool remove_mount_point(const char *mount_point);
Server &set_file_extension_and_mimetype_mapping(const char *ext,
const char *mime);
const char *mime);
Server &set_file_request_handler(Handler handler);

Server &set_error_handler(HandlerWithReturn handler);
Expand Down Expand Up @@ -835,6 +835,14 @@ class ClientImpl {
ResponseHandler response_handler, ContentReceiver content_receiver,
Progress progress);

Result Get(const char *path, const Params &params, const Headers &headers,
Progress progress = nullptr);
Result Get(const char *path, const Params &params, const Headers &headers,
ContentReceiver content_receiver, Progress progress = nullptr);
Result Get(const char *path, const Params &params, const Headers &headers,
ResponseHandler response_handler, ContentReceiver content_receiver,
Progress progress = nullptr);

Result Head(const char *path);
Result Head(const char *path, const Headers &headers);

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

Result Get(const char *path, const Params &params, const Headers &headers,
Progress progress = nullptr);
Result Get(const char *path, const Params &params, const Headers &headers,
ContentReceiver content_receiver, Progress progress = nullptr);
Result Get(const char *path, const Params &params, const Headers &headers,
ResponseHandler response_handler, ContentReceiver content_receiver,
Progress progress = nullptr);

Result Head(const char *path);
Result Head(const char *path, const Headers &headers);

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

inline std::string append_query_params(const char *path, const Params &params) {
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 &params) {
split(s.data(), s.data() + s.size(), '&', [&](const char *b, const char *e) {
std::string key;
Expand Down Expand Up @@ -4222,8 +4246,9 @@ inline bool Server::remove_mount_point(const char *mount_point) {
return false;
}

inline Server &Server::set_file_extension_and_mimetype_mapping(const char *ext,
const char *mime) {
inline Server &
Server::set_file_extension_and_mimetype_mapping(const char *ext,
const char *mime) {
file_extension_and_mimetype_map_[ext] = mime;

return *this;
Expand Down Expand Up @@ -4264,8 +4289,8 @@ inline Server &Server::set_logger(Logger logger) {
return *this;
}

inline Server
&Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
inline Server &
Server::set_expect_100_continue_handler(Expect100ContinueHandler handler) {
expect_100_continue_handler_ = std::move(handler);

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

inline Result ClientImpl::Get(const char *path, const Params &params,
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 &params,
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 &params,
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) {
return Head(path, Headers());
}
Expand Down Expand Up @@ -7020,6 +7074,22 @@ inline Result Client::Get(const char *path, const Headers &headers,
return cli_->Get(path, headers, std::move(response_handler),
std::move(content_receiver), std::move(progress));
}
inline Result Client::Get(const char *path, const Params &params,
const Headers &headers, Progress progress) {
return cli_->Get(path, params, headers, progress);
}
inline Result Client::Get(const char *path, const Params &params,
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 &params,
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, const Headers &headers) {
Expand Down
57 changes: 53 additions & 4 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,31 @@ TEST(HttpsToHttpRedirectTest, Redirect) {
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) {
Server svr8080;
Server svr8081;
Expand Down Expand Up @@ -956,9 +981,8 @@ TEST(ErrorHandlerTest, ContentLength) {
TEST(NoContentTest, ContentLength) {
Server svr;

svr.Get("/hi", [](const Request & /*req*/, Response &res) {
res.status = 204;
});
svr.Get("/hi",
[](const Request & /*req*/, Response &res) { res.status = 204; });
auto thread = std::thread([&]() { svr.listen(HOST, PORT); });

// Give GET time to get a few messages.
Expand Down Expand Up @@ -3979,7 +4003,7 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding) {
}
#endif

TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
TEST(HttpsToHttpRedirectTest, SimpleInterface) {
Client cli("https://nghttp2.org");
cli.set_follow_location(true);
auto res =
Expand All @@ -3989,4 +4013,29 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface) {
ASSERT_TRUE(res);
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

0 comments on commit 0308d60

Please sign in to comment.