etr/libhttpserver

[BUG] Final keys without values are not processed for POST body

JavierJF opened this issue · 0 comments

Prerequisites

Description

For a POST or PUT request with body format application/x-www-form-urlencoded, if there are final parameters with key, but without value these are left unprocessed and not being reported as arguments in the request.

Steps to Reproduce

1 . Compile and launch this little code snippet:

#include <numeric>
#include <string>
#include <utility>

#include "httpserver.hpp"

using std::string;
using std::pair;

using namespace httpserver;

class hello_world_resource : public http_resource {
public:
    const std::shared_ptr<http_response> render(const http_request& req) {
        const auto& args = req.get_args();
        string recv_args = std::accumulate(args.begin(), args.end(), string {},
                [] (string& res, const pair<string,string>& arg) -> string {
                    const string key_val_str { "\"" + arg.first + "\": " + "'" + arg.second + "'" };

                    if (res.empty()) {
                        res = key_val_str;
                    } else {
                        res = res + ", " + key_val_str;
                    }

                    return res;
                }
            );

        return std::shared_ptr<http_response>(new string_response("Received args: {" + recv_args + "}"));
    }
};

int main(int argc, char** argv) {
    webserver ws = create_webserver(8080);

    hello_world_resource hwr;
    ws.register_resource("/hello", &hwr);
    ws.start(true);
    
    return 0;
}
  1. Perform the following requests to the registered endpoint.

Expected behavior:

  • Response for first request should be: Received args: {arg1=''}
  • Response for second request should be: Received args: {arg1='', arg2=''}

Actual behavior:

  • Response for first request is: Received args: {}
  • Response for second request is: Received args: {arg1=''}

Reproduces how often: 100%

Versions

  • OS version: Linux 5.17.5-arch1-2
  • libhttpserver version: master compiled and 0.18.1 compiled
  • libmicrohttpd version: libmicrohttpd-0.9.68 compiled

Additional Information

I'm submitting a PR with the potential fix.