etr / libhttpserver

C++ library for creating an embedded Rest HTTP server (and more)
GNU Lesser General Public License v2.1
894 stars 187 forks source link

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

Open JavierJF opened 2 years ago

JavierJF commented 2 years ago

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:

Actual behavior:

Reproduces how often: 100%

Versions

Additional Information

I'm submitting a PR with the potential fix.