| 1 | package Plack::Middleware::ContentMD5; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent qw( Plack::Middleware ); | ||
| 6 | |||
| 7 | use Plack::Util; | ||
| 8 | use Digest::MD5 qw/md5_hex/; | ||
| 9 | |||
| 10 | sub call { | ||
| 11 | my $self = shift; | ||
| 12 | my $res = $self->app->(@_); | ||
| 13 | |||
| 14 | $self->response_cb($res, sub { | ||
| 15 | my $res = shift; | ||
| 16 | |||
| 17 | return unless defined $res->[2]; | ||
| 18 | return if (Plack::Util::status_with_no_entity_body($res->[0])); | ||
| 19 | |||
| 20 | my $h = Plack::Util::headers($res->[1]); | ||
| 21 | return if ( $h->exists('Content-MD5') ); | ||
| 22 | |||
| 23 | my $body = $res->[2]; | ||
| 24 | if (ref $body eq 'ARRAY') { | ||
| 25 | $h->set('Content-MD5', md5_hex(@$body)); | ||
| 26 | } | ||
| 27 | # Do we need support $fh? | ||
| 28 | |||
| 29 | return; | ||
| 30 | }); | ||
| 31 | } | ||
| 32 | |||
| 33 | 1; | ||
| 34 | |||
| 35 | __END__ |