Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1985 #1989

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ using Ranges = std::vector<Range>;
struct Request {
std::string method;
std::string path;
Params params;
Headers headers;
std::string body;

Expand All @@ -623,7 +624,6 @@ struct Request {
// for server
std::string version;
std::string target;
Params params;
MultipartFormDataMap files;
Ranges ranges;
Match matches;
Expand Down Expand Up @@ -7420,7 +7420,7 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
inline bool ClientImpl::is_ssl_peer_could_be_closed(SSL *ssl) const {
char buf[1];
return !SSL_peek(ssl, buf, 1) &&
SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN;
SSL_get_error(ssl, 0) == SSL_ERROR_ZERO_RETURN;
}
#endif

Expand All @@ -7438,9 +7438,7 @@ inline bool ClientImpl::send_(Request &req, Response &res, Error &error) {

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
if (is_alive && is_ssl()) {
if (is_ssl_peer_could_be_closed(socket_.ssl)) {
is_alive = false;
}
if (is_ssl_peer_could_be_closed(socket_.ssl)) { is_alive = false; }
}
#endif

Expand Down Expand Up @@ -7799,7 +7797,13 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
{
detail::BufferStream bstrm;

const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path;
const auto &path_with_query =
req.params.empty() ? req.path
: append_query_params(req.path, req.params);

const auto &path =
url_encode_ ? detail::encode_url(path_with_query) : path_with_query;

detail::write_request_line(bstrm, req.method, path);

header_writer_(bstrm, req.headers);
Expand Down
34 changes: 34 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6542,6 +6542,40 @@ TEST(SendAPI, SimpleInterface_Online) {
EXPECT_EQ(StatusCode::MovedPermanently_301, res->status);
}

TEST(SendAPI, WithParamsInRequest) {
Server svr;

svr.Get("/", [&](const Request &req, Response & /*res*/) {
EXPECT_TRUE(req.has_param("test"));
EXPECT_EQ("test_value", req.get_param_value("test"));
});

auto t = std::thread([&]() { svr.listen(HOST, PORT); });

auto se = detail::scope_exit([&] {
svr.stop();
t.join();
ASSERT_FALSE(svr.is_running());
});

svr.wait_until_ready();

Client cli(HOST, PORT);

{
Request req;
req.method = "GET";
req.path = "/";
req.params.emplace("test", "test_value");
auto res = cli.send(req);
ASSERT_TRUE(res);
}
{
auto res = cli.Get("/", {{"test", "test_value"}}, Headers{});
ASSERT_TRUE(res);
}
}

TEST(ClientImplMethods, GetSocketTest) {
httplib::Server svr;
svr.Get("/", [&](const httplib::Request & /*req*/, httplib::Response &res) {
Expand Down