<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Plack::Middleware::ContentLength;
use strict;
use warnings;
use parent qw( Plack::Middleware );

use Plack::Util;

sub call {
    my $self = shift;
    my $res  = $self-&gt;app-&gt;(@_);

    return $self-&gt;response_cb($res, sub {
        my $res = shift;
        my $h = Plack::Util::headers($res-&gt;[1]);
        if (!Plack::Util::status_with_no_entity_body($res-&gt;[0]) &amp;&amp;
            !$h-&gt;exists('Content-Length') &amp;&amp;
            !$h-&gt;exists('Transfer-Encoding') &amp;&amp;
            defined(my $content_length = Plack::Util::content_length($res-&gt;[2]))) {
            $h-&gt;push('Content-Length' =&gt; $content_length);
        }
    });
}

1;

__END__

=head1 NAME

Plack::Middleware::ContentLength - Adds Content-Length header automatically

=head1 SYNOPSIS

  # in app.psgi

  builder {
      enable "Plack::Middleware::ContentLength";
      $app;
  }

  # Or in Plack::Handler::*
  $app = Plack::Middleware::ContentLength-&gt;wrap($app);

=head1 DESCRIPTION

Plack::Middleware::ContentLength is a middleware that automatically
adds C&lt;Content-Length&gt; header when it's appropriate i.e. the response
has a content body with calculable size (array of chunks or a real
filehandle).

This middleware can also be used as a library from PSGI server
implementations to automatically set C&lt;Content-Length&gt; rather than in
the end user level.

=head1 AUTHOR

Tatsuhiko Miyagawa

=head1 SEE ALSO

Rack::ContentLength

=cut

</pre></body></html>