Skip to content
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

Pistache not support absolute URI with HTTP/1.1 protocol #1144

Open
wufanqqfsc opened this issue Jul 6, 2023 · 1 comment
Open

Pistache not support absolute URI with HTTP/1.1 protocol #1144

wufanqqfsc opened this issue Jul 6, 2023 · 1 comment

Comments

@wufanqqfsc
Copy link

According to HTTP/1.1: Request (w3.org)

"The absoluteURI form is REQUIRED when the request is being made to a proxy."
...
"all HTTP/1.1 servers MUST accept the absolute URI form in requests, even though HTTP/1.1 clients will only generate them in requests to proxies."

But seems Pistache just can handler the relative path and not support the absolute URI.

Just like the curl http request CMD:

curl -X PATCH http://127.0.0.1:8080/ -v --request-target http://127.0.0.1:8080/xxx --path-as-is -H "Host: 127.0.0.1:8080" -H "Content-Type:"

And Pistache will can't find the route path for this URI: "http://127.0.0.1:8080/xxx" and using the default handler.

@ljluestc
Copy link

ljluestc commented Nov 3, 2024


#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
#include <iostream>
#include <string>

using namespace Pistache;

class CustomHandler : public Http::Handler {
public:
    HTTP_PROTOTYPE(CustomHandler)

    void onRequest(const Http::Request& request, Http::ResponseWriter response) override {
        // Check if the request is using absolute URI
        if (request.resource().empty()) {
            // If the resource is empty, we might be dealing with absolute URI
            auto hostHeader = request.headers().get<Http::Header::Host>();
            if (hostHeader) {
                // Extract the absolute URI from the request
                std::string absoluteUri = request.header(Http::Header::RequestTarget).value();
                std::cout << "Received absolute URI: " << absoluteUri << std::endl;

                // Extract the path from the absolute URI
                std::string path = absoluteUri.substr(absoluteUri.find("/", 7)); // 7 to skip "http://"
                std::cout << "Extracted path: " << path << std::endl;

                // Handle the extracted path
                if (path == "/xxx") {
                    response.send(Http::Code::Ok, "Handled request for /xxx");
                    return;
                }
            }
        }

        // Fallback for unknown paths
        response.send(Http::Code::Not_Found, "Route not found");
    }
};

class TimeoutServer {
public:
    TimeoutServer() : router(), handler(new CustomHandler()) {}

    void init(size_t threads = 2) {
        auto opts = Http::Endpoint::options()
                        .threads(threads);
        
        server.init(opts);

        // Set up the router
        Rest::Routes::Get(router, "/xxx", Rest::Routes::bind(&CustomHandler::onRequest, handler));
        server.setHandler(router.handler());
    }

    void start() {
        server.serve();
    }

private:
    Http::Endpoint server{Address(Ipv4::any(), Port(8080))};
    Rest::Router router;
    std::shared_ptr<CustomHandler> handler;
};

int main() {
    TimeoutServer server;
    server.init();
    std::cout << "Starting server on port 8080..." << std::endl;
    server.start();
    return 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants