Skip to content

Commit d249ba6

Browse files
etrMerlino
andauthored
Fix bug preventing GET params from being read on parametrized URLs. (#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]>
1 parent c130666 commit d249ba6

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/http_request.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ const http::header_view_map http_request::get_cookies() const {
105105
}
106106

107107
void http_request::populate_args() const {
108-
if (!cache->unescaped_args.empty()) {
108+
if (cache->args_populated) {
109109
return;
110110
}
111111
arguments_accumulator aa;
112112
aa.unescaper = unescaper;
113113
aa.arguments = &cache->unescaped_args;
114114
MHD_get_connection_values(underlying_connection, MHD_GET_ARGUMENT_KIND, &build_request_args, reinterpret_cast<void*>(&aa));
115+
116+
cache->args_populated = true;
115117
}
116118

117119
http_arg_value http_request::get_arg(std::string_view key) const {

src/httpserver/http_request.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ class http_request {
410410
std::string requestor_ip;
411411
std::string digested_user;
412412
std::map<std::string, std::vector<std::string>, http::arg_comparator> unescaped_args;
413+
414+
bool args_populated = false;
413415
};
414416
std::unique_ptr<http_request_data_cache> cache = std::make_unique<http_request_data_cache>();
415417
// Populate the data cache unescaped_args

test/integ/basic.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,24 @@ LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg)
948948
curl_easy_cleanup(curl);
949949
LT_END_AUTO_TEST(regex_matching_arg)
950950

951+
LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_with_url_pars)
952+
args_resource resource;
953+
LT_ASSERT_EQ(true, ws->register_resource("this/captures/{arg}/passed/in/input", &resource));
954+
curl_global_init(CURL_GLOBAL_ALL);
955+
956+
string s;
957+
CURL *curl = curl_easy_init();
958+
CURLcode res;
959+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:" PORT_STRING "/this/captures/whatever/passed/in/input?arg2=second_argument");
960+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
961+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
962+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
963+
res = curl_easy_perform(curl);
964+
LT_ASSERT_EQ(res, 0);
965+
LT_CHECK_EQ(s, "whateversecond_argument");
966+
curl_easy_cleanup(curl);
967+
LT_END_AUTO_TEST(regex_matching_arg_with_url_pars)
968+
951969
LT_BEGIN_AUTO_TEST(basic_suite, regex_matching_arg_custom)
952970
args_resource resource;
953971
LT_ASSERT_EQ(true, ws->register_resource("this/captures/numeric/{arg|([0-9]+)}/passed/in/input", &resource));

0 commit comments

Comments
 (0)