etr/libhttpserver

[BUG] Query parameters missing in get_args() result if path arguments are used.

dimitriadis-georgios opened this issue · 0 comments

Prerequisites

Description

The result from calling http_request::get_args() will not contain URL query parameters if the resource URL has path parameters. This is due to webserver::finalize_answer(...) calling http_server::set_arg(...) for each URL path argument, thus rendering http_request::cache::unescaped_args non-empty. http_request::get_args() will call http_request::populate_args(), which will do nothing if unescaped_args is non-empty.

Steps to Reproduce

  1. Change line examples/hello_with_get_arg.cpp:26 from
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello: " + std::string(req.get_arg("name"))));

to

return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(std::string(req.get_arg("greeting")) + ", " + std::string(req.get_arg("name"))));
  1. Change line examples/hello_with_get_arg.cpp:34 from
ws.register_resource("/hello", &hwr);

to lines

hwr.disallow_all();
hwr.set_allowing("GET", true);
ws.register_resource("/{greeting}", &hwr);
  1. Build and start example server hello_with_get_arg in a terminal.
  2. In a second terminal, run curl http://localhost:8080/Howdy?name=Bob

Expected behavior: Server responds with text "Howdy, Bob"

Actual behavior: Server responds with "Howdy, "

Reproduces how often: 100%

Versions

  • OS version Linux gd 6.8.0-50-generic #51~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 21 12:03:03 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
  • libhttpserver version 0.19.0, compiled
  • libmicrohttpd version 1.0.1, compiled

Additional Information

Full example program, modified as specified in steps to reproduce.

#include <httpserver.hpp>

class hello_world_resource : public httpserver::http_resource {
 public:
     std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req) {
         return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(std::string(req.get_arg("greeting")) + ", " + std::string(req.get_arg("name"))));
     }
};

int main() {
    httpserver::webserver ws = httpserver::create_webserver(8080);

    hello_world_resource hwr;
    hwr.disallow_all();
    hwr.set_allowing("GET", true);
    ws.register_resource("/{greeting}", &hwr);
    ws.start(true);

    return 0;
}