Skip to content

Commit

Permalink
Add option to bypass URL encode of path (yhirose#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalex authored May 15, 2021
1 parent 5cfb70c commit e00ad37
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,8 @@ class ClientImpl {
void set_keep_alive(bool on);
void set_follow_location(bool on);

void set_url_encode(bool on);

void set_compress(bool on);

void set_decompress(bool on);
Expand Down Expand Up @@ -1101,6 +1103,8 @@ class ClientImpl {
bool keep_alive_ = false;
bool follow_location_ = false;

bool url_encode_ = true;

int address_family_ = AF_UNSPEC;
bool tcp_nodelay_ = CPPHTTPLIB_TCP_NODELAY;
SocketOptions socket_options_ = nullptr;
Expand Down Expand Up @@ -1318,6 +1322,8 @@ class Client {
void set_keep_alive(bool on);
void set_follow_location(bool on);

void set_url_encode(bool on);

void set_compress(bool on);

void set_decompress(bool on);
Expand Down Expand Up @@ -5276,6 +5282,7 @@ inline void ClientImpl::copy_settings(const ClientImpl &rhs) {
#endif
keep_alive_ = rhs.keep_alive_;
follow_location_ = rhs.follow_location_;
url_encode_ = rhs.url_encode_;
tcp_nodelay_ = rhs.tcp_nodelay_;
socket_options_ = rhs.socket_options_;
compress_ = rhs.compress_;
Expand Down Expand Up @@ -5703,7 +5710,7 @@ inline bool ClientImpl::write_request(Stream &strm, Request &req,
{
detail::BufferStream bstrm;

const auto &path = detail::encode_url(req.path);
const auto &path = url_encode_ ? detail::encode_url(req.path) : req.path;
bstrm.write_format("%s %s HTTP/1.1\r\n", req.method.c_str(), path.c_str());

detail::write_headers(bstrm, req.headers);
Expand Down Expand Up @@ -6432,6 +6439,8 @@ inline void ClientImpl::set_keep_alive(bool on) { keep_alive_ = on; }

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

inline void ClientImpl::set_url_encode(bool on) { url_encode_ = on; }

inline void ClientImpl::set_default_headers(Headers headers) {
default_headers_ = std::move(headers);
}
Expand Down Expand Up @@ -7560,6 +7569,8 @@ inline void Client::set_follow_location(bool on) {
cli_->set_follow_location(on);
}

inline void Client::set_url_encode(bool on) { cli_->set_url_encode(on); }

inline void Client::set_compress(bool on) { cli_->set_compress(on); }

inline void Client::set_decompress(bool on) { cli_->set_decompress(on); }
Expand Down
36 changes: 36 additions & 0 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,42 @@ TEST(RedirectFromPageWithContent, Redirect) {

#endif

TEST(PathUrlEncodeTest, PathUrlEncode) {
Server svr;

svr.Get("/foo", [](const Request & req, Response &res) {
auto a = req.params.find("a");
if (a != req.params.end()) {
res.set_content((*a).second, "text/plain");
res.status = 200;
} else {
res.status = 400;
}
});

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

// Give GET time to get a few messages.
std::this_thread::sleep_for(std::chrono::seconds(1));

{
Client cli(HOST, PORT);
cli.set_url_encode(false);

auto res = cli.Get("/foo?a=explicitly+encoded");
ASSERT_TRUE(res);
EXPECT_EQ(200, res->status);
// This expects it back with a space, as the `+` won't have been
// url-encoded, and server-side the params get decoded turning `+`
// into spaces.
EXPECT_EQ("explicitly encoded", res->body);
}

svr.stop();
thread.join();
ASSERT_FALSE(svr.is_running());
}

TEST(BindServerTest, BindDualStack) {
Server svr;

Expand Down

0 comments on commit e00ad37

Please sign in to comment.