| Plack documentation | Contained in the Plack distribution. |
Plack::Middleware::SimpleContentFilter - Filters response content
use Plack::Builder;
my $app = sub {
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'Hello Foo' ] ];
};
builder {
enable "Plack::Middleware::SimpleContentFilter",
filter => sub { s/Foo/Bar/g; };
$app;
};
This middleware should be considered as a demo. Running this against your application might break your HTML unless you code the filter callback carefully.
Plack::Middleware::SimpleContentFilter is a simple content text filter
to run against response body. This middleware is only enabled against
responses with text/* Content-Type.
Tatsuhiko Miyagawa
| Plack documentation | Contained in the Plack distribution. |
package Plack::Middleware::SimpleContentFilter; use strict; use warnings; use parent qw( Plack::Middleware ); use Plack::Util; use Plack::Util::Accessor qw( filter ); sub call { my $self = shift; my $res = $self->app->(@_); $self->response_cb($res, sub { my $res = shift; my $h = Plack::Util::headers($res->[1]); if ($h->get('Content-Type') =~ m!^text/!) { return sub { my $chunk = shift; return unless defined $chunk; local $_ = $chunk; $self->filter->(); return $_; }; } }); } 1; __END__