File: ContentLength.pm

package info (click to toggle)
libplack-perl 0.9941-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,444 kB
  • ctags: 592
  • sloc: perl: 7,155; makefile: 2
file content (64 lines) | stat: -rw-r--r-- 1,401 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package Plack::Middleware::ContentLength;
use strict;
use warnings;
use parent qw( Plack::Middleware );

use Plack::Util;

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

    return $self->response_cb($res, sub {
        my $res = shift;
        my $h = Plack::Util::headers($res->[1]);
        if (!Plack::Util::status_with_no_entity_body($res->[0]) &&
            !$h->exists('Content-Length') &&
            !$h->exists('Transfer-Encoding') &&
            defined(my $content_length = Plack::Util::content_length($res->[2]))) {
            $h->push('Content-Length' => $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->wrap($app);

=head1 DESCRIPTION

Plack::Middleware::ContentLength is a middleware that automatically
adds C<Content-Length> 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<Content-Length> rather than in
the end user level.

=head1 AUTHOR

Tatsuhiko Miyagawa

=head1 SEE ALSO

Rack::ContentLength

=cut