Skip to content

Commit

Permalink
Fix bug preventing GET params from being read on parametrized URLs. (#…
Browse files Browse the repository at this point in the history
…326)

This also introduces tests to verify the bug doesn't return.

The bug was due to the populate_args method checking whether the
cache was empty before executing. By doing so, it was ignoring that
in case of parametrized URLs, the webserver class was loading the
params as arguments.

Co-authored-by: Merlino <[email protected]>
  • Loading branch information
etr and Merlino authored Jun 30, 2023
1 parent c130666 commit d249ba6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,15 @@ const http::header_view_map http_request::get_cookies() const {
}

void http_request::populate_args() const {
if (!cache->unescaped_args.empty()) {
if (cache->args_populated) {
return;
}
arguments_accumulator aa;
aa.unescaper = unescaper;
aa.arguments = &cache->unescaped_args;
MHD_get_connection_values(underlying_connection, MHD_GET_ARGUMENT_KIND, &build_request_args, reinterpret_cast<void*>(&aa));

cache->args_populated = true;
}

http_arg_value http_request::get_arg(std::string_view key) const {
Expand Down
2 changes: 2 additions & 0 deletions src/httpserver/http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ class http_request {
std::string requestor_ip;
std::string digested_user;
std::map<std::string, std::vector<std::string>, http::arg_comparator> unescaped_args;

bool args_populated = false;
};
std::unique_ptr<http_request_data_cache> cache = std::make_unique<http_request_data_cache>();
// Populate the data cache unescaped_args
Expand Down
18 changes: 18 additions & 0 deletions test/integ/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,24 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg)
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(regex_matching_arg)

LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_with_url_pars)
args_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("this/captures/{arg}/passed/in/input", &resource));
curl_global_init(CURL_GLOBAL_ALL);

string s;
CURL *curl = curl_easy_init();
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/whatever/passed/in/input?arg2=second_argument");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
res = curl_easy_perform(curl);
LT_ASSERT_EQ(res, 0);
LT_CHECK_EQ(s, "whateversecond_argument");
curl_easy_cleanup(curl);
LT_END_AUTO_TEST(regex_matching_arg_with_url_pars)

LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_custom)
args_resource resource;
LT_ASSERT_EQ(true, ws->register_resource("this/captures/numeric/{arg|([0-9]+)}/passed/in/input", &resource));
Expand Down

0 comments on commit d249ba6

Please sign in to comment.