From 094447545ce23f8f3e2e6a7ac9ef563720b42357 Mon Sep 17 00:00:00 2001 From: Tatsuhiko Miyagawa Date: Wed, 14 Jul 2010 00:54:02 -0700 Subject: [PATCH] Moved response_cb() to Plack::Util as a function so the inlined middleware components, either with enable sub {} or with the plain perl code ref can make use of it. --- lib/Plack/Component.pm | 54 +-------------------------------------- lib/Plack/Util.pm | 57 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/lib/Plack/Component.pm b/lib/Plack/Component.pm index 82a7efcec..61c87d105 100644 --- a/lib/Plack/Component.pm +++ b/lib/Plack/Component.pm @@ -42,59 +42,7 @@ sub to_app { sub response_cb { my($self, $res, $cb) = @_; - - my $body_filter = sub { - my($cb, $res) = @_; - my $filter_cb = $cb->($res); - # If response_cb returns a callback, treat it as a $body filter - if (defined $filter_cb && ref $filter_cb eq 'CODE') { - Plack::Util::header_remove($res->[1], 'Content-Length'); - if (defined $res->[2]) { - if (ref $res->[2] eq 'ARRAY') { - for my $line (@{$res->[2]}) { - $line = $filter_cb->($line); - } - # Send EOF. - my $eof = $filter_cb->( undef ); - push @{ $res->[2] }, $eof if defined $eof; - } else { - my $body = $res->[2]; - my $getline = sub { $body->getline }; - $res->[2] = Plack::Util::inline_object - getline => sub { $filter_cb->($getline->()) }, - close => sub { $body->close }; - } - } else { - return $filter_cb; - } - } - }; - - if (ref $res eq 'ARRAY') { - $body_filter->($cb, $res); - return $res; - } elsif (ref $res eq 'CODE') { - return sub { - my $respond = shift; - my $cb = $cb; # To avoid the nested closure leak for 5.8.x - $res->(sub { - my $res = shift; - my $filter_cb = $body_filter->($cb, $res); - if ($filter_cb) { - my $writer = $respond->($res); - if ($writer) { - return Plack::Util::inline_object - write => sub { $writer->write($filter_cb->(@_)) }, - close => sub { $writer->write($filter_cb->(undef)); $writer->close }; - } - } else { - return $respond->($res); - } - }); - }; - } - - return $res; + Plack::Util::response_cb($res, $cb); } 1; diff --git a/lib/Plack/Util.pm b/lib/Plack/Util.pm index b41d34360..f869b283b 100644 --- a/lib/Plack/Util.pm +++ b/lib/Plack/Util.pm @@ -236,6 +236,63 @@ sub inline_object { bless {%args}, 'Plack::Util::Prototype'; } +sub response_cb { + my($res, $cb) = @_; + + my $body_filter = sub { + my($cb, $res) = @_; + my $filter_cb = $cb->($res); + # If response_cb returns a callback, treat it as a $body filter + if (defined $filter_cb && ref $filter_cb eq 'CODE') { + Plack::Util::header_remove($res->[1], 'Content-Length'); + if (defined $res->[2]) { + if (ref $res->[2] eq 'ARRAY') { + for my $line (@{$res->[2]}) { + $line = $filter_cb->($line); + } + # Send EOF. + my $eof = $filter_cb->( undef ); + push @{ $res->[2] }, $eof if defined $eof; + } else { + my $body = $res->[2]; + my $getline = sub { $body->getline }; + $res->[2] = Plack::Util::inline_object + getline => sub { $filter_cb->($getline->()) }, + close => sub { $body->close }; + } + } else { + return $filter_cb; + } + } + }; + + if (ref $res eq 'ARRAY') { + $body_filter->($cb, $res); + return $res; + } elsif (ref $res eq 'CODE') { + return sub { + my $respond = shift; + my $cb = $cb; # To avoid the nested closure leak for 5.8.x + $res->(sub { + my $res = shift; + my $filter_cb = $body_filter->($cb, $res); + if ($filter_cb) { + my $writer = $respond->($res); + if ($writer) { + return Plack::Util::inline_object + write => sub { $writer->write($filter_cb->(@_)) }, + close => sub { $writer->write($filter_cb->(undef)); $writer->close }; + } + } else { + return $respond->($res); + } + }); + }; + } + + return $res; +} + package Plack::Util::Prototype; our $AUTOLOAD;