Skip to content

Instantly share code, notes, and snippets.

@pmakholm
Created May 10, 2012 09:47
Show Gist options
  • Save pmakholm/2652188 to your computer and use it in GitHub Desktop.
Save pmakholm/2652188 to your computer and use it in GitHub Desktop.
package PlackX::Util::Middelware;
use Plack::Util;
sub handle_response {
my ($res, %callback) = @_;
$callback{on_chunk} = sub { 1 };
$callback{on_close} = sub { 1 };
Plack::Util::response_cb( $res, sub {
my $res = shift;
my ($status, $header, $body) = @$res;
if (!defined $body) {
return sub {
my $chunk = shift;
return $callback{on_chunk}->($chunk)
if defined $chunk;
$callback{on_close}->();
return;
};
};
my $get_chunk = ref $body eq 'ARRAY' ? sub { shift @$body } : sub { $body->getline };
my $wrapped_body = Plack::Util::inline_object(
getline => sub {
my $chunk = $getchunk->();
return $callback{on_chunk}->($chunk)
if defined $chunk;
return;
},
close => sub {
$body->close() if ref $body ne 'ARRAY';
$callback{on_close};
},
);
@$res( $status, $header, $wraped_body);
});
}
1;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment