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

keep alive timeout doesn't add to response header #1189

Open
yangmianmiao opened this issue Jan 18, 2024 · 2 comments
Open

keep alive timeout doesn't add to response header #1189

yangmianmiao opened this issue Jan 18, 2024 · 2 comments

Comments

@yangmianmiao
Copy link

yangmianmiao commented Jan 18, 2024

When set keep alive timeout by options.keepaliveTimeout_, I can't find keep-alive timeout in response header, is there any option need to set or could add this information to response header?

@ljluestc
Copy link

ljluestc commented Nov 3, 2024


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

using namespace Pistache;

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

    void onRequest(const Http::Request& request, Http::ResponseWriter response) override {
        // Extract general request information
        std::cout << "Request URL: " << request.resource() << std::endl;
        std::cout << "Request Method: " << request.method() << std::endl;
        std::cout << "Remote Address: " << request.remoteAddress().host() << std::endl;

        // Print request headers
        std::cout << "Request Headers:" << std::endl;
        for (const auto& header : request.headers()) {
            std::cout << header.first << ": " << header.second << std::endl;
        }

        // Set keep-alive timeout (in seconds)
        const int keepAliveTimeout = 5; // Example timeout value
        response.headers().add<Http::Header::KeepAlive>(std::to_string(keepAliveTimeout));

        // Create a response
        response.headers().add<Http::Header::ContentType>(MIME(Text, Plain));
        response.send(Http::Code::Ok, "Hello, World!");

        // Optionally add other response headers
        response.headers().add<Http::Header::AccessControlAllowOrigin>("*");
        response.headers().add<Http::Header::AccessControlAllowCredentials>("true");
        response.headers().add<Http::Header::ContentLength>(std::to_string(13)); // "Hello, World!" length
        response.headers().add<Http::Header::Date>(Http::Header::Date::formatNow());
    }
};

int main() {
    // Create an HTTP server
    Port port(9080);
    int threads = 2;

    auto opts = Http::Endpoint::options()
                    .threads(threads)
                    .keepAliveTimeout(std::chrono::seconds(5)); // Set keep-alive timeout here

    Http::Endpoint server(Address(Ipv4::any(), port));
    server.init(opts);

    // Set up the router
    Rest::Router router;
    Rest::Routes::Get(router, "/hello", Rest::Routes::bind(&HttpHandler::onRequest));

    server.setHandler(router.handler());

    // Start the server
    server.serve();
    return 0;
}

@kiplingw
Copy link
Member

kiplingw commented Nov 3, 2024

@ljluestc, I think he's asking about server side and not client side.

@yangmianmiao, you want to look at headerTimeout() and bodyTimeout().

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

3 participants