NAME

PAGI::Middleware::ContentLength - Auto Content-Length header middleware

SYNOPSIS

use PAGI::Middleware::Builder;

my $app = builder {
    enable 'ContentLength';
    $my_app;
};

DESCRIPTION

PAGI::Middleware::ContentLength automatically adds a Content-Length header to responses that don't already have one. It buffers the response body to calculate the length, then sends the complete response.

This middleware is useful when the application doesn't know the body length upfront, but you want to avoid chunked encoding.

CONFIGURATION

  • auto_chunked (default: 0)

    If true, skip adding Content-Length and let chunked encoding be used instead. This is useful for large responses where buffering would be expensive.

NOTES

  • For streaming responses (multiple body events with more => 1), this middleware switches to pass-through mode to avoid buffering.

  • Responses that already have Content-Length are passed through unchanged.

  • Responses with Transfer-Encoding: chunked are passed through unchanged.

  • SSE and WebSocket responses should not use this middleware.

SEE ALSO

PAGI::Middleware - Base class for middleware