| 1 | package POEx::Role::PSGIServer::Streamer; | ||
| 2 | BEGIN { | ||
| 3 | $POEx::Role::PSGIServer::Streamer::VERSION = '1.110670'; | ||
| 4 | } | ||
| 5 | |||
| 6 | #ABSTRACT: Provides streaming filehandle PSGI implementation | ||
| 7 | use MooseX::Declare; | ||
| 8 | |||
| 9 | class POEx::Role::PSGIServer::Streamer { | ||
| 10 | use POE::Filter::Map; | ||
| 11 | use POE::Filter::Stream; | ||
| 12 | use MooseX::Types::Moose(':all'); | ||
| 13 | use POEx::Types::PSGIServer(':all'); | ||
| 14 | |||
| 15 | |||
| 16 | has server_context => (is => 'ro', isa => PSGIServerContext, required => 1); | ||
| 17 | |||
| 18 | |||
| 19 | has closed_chunk => ( is => 'rw', isa => Bool, default => 0 ); | ||
| 20 | |||
| 21 | with 'POEx::Role::Streaming'; | ||
| 22 | |||
| 23 | |||
| 24 | method _build_filter { | ||
| 25 | if($self->server_context->{chunked}) { | ||
| 26 | POE::Filter::Map->new( | ||
| 27 | Get => sub { $_ }, | ||
| 28 | Put => sub { | ||
| 29 | my $data = shift; | ||
| 30 | return $data if $data =~ /0\r\n\r\n/; | ||
| 31 | my $len = sprintf "%X", do { use bytes; length($data) }; | ||
| 32 | return "$len\r\n$data\r\n"; | ||
| 33 | } | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | return POE::Filter::Stream->new(); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | around done_writing { | ||
| 43 | if($self->server_context->{chunked} && !$self->closed_chunk) { | ||
| 44 | $self->closed_chunk(1); | ||
| 45 | $self->put("0\r\n\r\n"); | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | $self->$orig; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | |||
| 56 | =pod | ||
| 57 | |||
| 58 | =head1 NAME | ||
| 59 | |||
| 60 | POEx::Role::PSGIServer::Streamer - Provides streaming filehandle PSGI implementation | ||
| 61 | |||
| 62 | =head1 VERSION | ||
| 63 | |||
| 64 | version 1.110670 | ||
| 65 | |||
| 66 | =head1 PUBLIC_ATTRIBUTES | ||
| 67 | |||
| 68 | =head2 server_context | ||
| 69 | |||
| 70 | is: ro, isa: PSGIServerContext, required: 1 | ||
| 71 | |||
| 72 | This is the server context from POEx::Role::PSGIServer. It is needed to determine the semantics of the current request | ||
| 73 | |||
| 74 | =head1 PRIVATE_ATTRIBUTES | ||
| 75 | |||
| 76 | =head2 closed_chunk | ||
| 77 | |||
| 78 | is: rw, isa: Bool, default: 0 | ||
| 79 | |||
| 80 | closed_chunk is a flag used by the advised L</around done_writing> to know whether the chunked transfer encoding needs a terminator or if the terminator has already been written to the output buffer | ||
| 81 | |||
| 82 | =head1 PROTECTED_METHODS | ||
| 83 | |||
| 84 | =head2 around done_writing | ||
| 85 | |||
| 86 | done_writing is advised to check if the context demands a chunked terminator and if one hasn't been sent yet. If so, it marks L</closed_chunk>, puts the terminator into the output buffer, and returns. Upon second invocation when the buffers are flushed, it will execute the original method. | ||
| 87 | |||
| 88 | =head1 PRIVATE_METHODS | ||
| 89 | |||
| 90 | =head2 _build_filter | ||
| 91 | |||
| 92 | _build_filter is overridden to return a L<POE::Filter::Map> filter if the current response is to be chunk transfer encoded. | ||
| 93 | |||
| 94 | =head1 AUTHOR | ||
| 95 | |||
| 96 | Nicholas Perez <nperez@cpan.org> | ||
| 97 | |||
| 98 | =head1 COPYRIGHT AND LICENSE | ||
| 99 | |||
| 100 | This software is copyright (c) 2011 by Infinity Interactive. | ||
| 101 | |||
| 102 | This is free software; you can redistribute it and/or modify it under | ||
| 103 | the same terms as the Perl 5 programming language system itself. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | |||
| 108 | __END__ |