drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.04k stars 1.06k forks source link

`drogon::app().forward(...)` does not perform URL escaping #2013

Open Mis1eader-dev opened 2 months ago

Mis1eader-dev commented 2 months ago

Describe the bug Forwarding requests that contain spaces in the URL all fail to be resolved on the forwarded party. After manually replacing spaces with their escaped equivalences it worked.

string path = req->path();
replaceAll(path, " ", "%20");
req->setPath(std::move(path));
drogon::app().forward(std::move(req), std::move(callback), host);

Solution Perform URL escaping on the path before forwarding.

Alternative Solution Have it use req->getOriginalPath() instead of req->path()

req->setPath(req->getOriginalPath());
drogon::app().forward(std::move(req), std::move(callback), host);
ladaniprem commented 3 weeks ago

include <drogon/drogon.h>

include

void forwardRequest(const std::shared_ptr &req, const std::function<void(const drogon::HttpResponsePtr &)> &callback, const std::string &host) {

req->setPath(req->getOriginalPath());

drogon::app().forward(std::move(req), std::move(callback), host);

}

int main() {

drogon::app().registerHandler("/forward", [](const drogon::HttpRequestPtr &req, std::function<void (const drogon::HttpResponsePtr &)> &&callback) {
    std::string host = "http://example.com";  

    forwardRequest(req, std::move(callback), host);
});

// Start the Drogon application
drogon::app().run();
return 0;

} please check the code I am not understand properly but try this .

Mis1eader-dev commented 3 weeks ago

@ladaniprem the provided alternative solution does this, but I'm awaiting opinions of other maintainers as to whether we should alter request objects or alter the forward function itself.