Created
May 10, 2012 09:47
-
-
Save pmakholm/2652188 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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