Skip to content

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

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

Open
1 task done
dimitriadis-georgios opened this issue Jan 7, 2025 · 0 comments
Assignees
Labels
bug Confirmed bugs or reports that are very likely to be bugs.

Comments

@dimitriadis-georgios
Copy link

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;
}
@dimitriadis-georgios dimitriadis-georgios added the bug Confirmed bugs or reports that are very likely to be bugs. label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Confirmed bugs or reports that are very likely to be bugs.
Projects
None yet
Development

No branches or pull requests

2 participants