| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | package Plack::Middleware::AdaptFilehandleRead; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use base 'Plack::Middleware'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Plack::Middleware::AdaptFilehandleRead::Proxy; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Plack::Util::Accessor 'always_adapt', 'chunksize'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Plack::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub looks_like_a_readable_fh { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
looks_like_a_readable_fh: called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | my $body = shift || return; # If there's a body | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | return Scalar::Util::blessed($body) && # and its an object | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%3/9
builtin::blessed($body) && $body->can('read') && ($self->always_adapt || !$body->can('getline'))
builtin::blessed($body) and $body->can("read")
$self->always_adapt or not $body->can("getline")
MC/DC0%0/4
builtin::blessed($body) && $body->can('read') && ($self->always_adapt || !$body->can('getline'))
builtin::blessed($body)
$body->can("read")
$self->always_adapt
not $body->can("getline")
Truth table20%1/5
A and B && C or not(D)
A= builtin::blessed($body)B= $body->can("read")C= $self->always_adaptD= not $body->can("getline")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | $body->can('read') && # which can 'read' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | ($self->always_adapt || # and either ->always_adapt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | !$body->can('getline')); # or doesn't 'getline' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | my($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my $res = $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | return Plack::Util::response_cb($res, sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | if( $self->looks_like_a_readable_fh((my $r = shift)->[2]) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # We have an filehandle like object that doesn't do ->getline | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # so Plack can't do anything with it. Wrap it in a proxy object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # that adds the ->getline method by adapting read. We assume | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # this ->read works like $fh->read(BUF, LEN, [OFFSET]). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | $r->[2] = Plack::Middleware::AdaptFilehandleRead::Proxy->new($r->[2], ($self->chunksize || 65536)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->chunksize || 65536
MC/DC0%0/1
$self->chunksize || 65536
$self->chunksize
Truth table50%1/2
A
A= $self->chunksize
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | Plack::Middleware::AdaptFilehandleRead - Give a filehandle a getline method when its missing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | use Plack::Builder; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | use AppReturnsFileHandleWithRead; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | my $app = AppReturnsFileHandleWithRead->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | builder { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | enable 'AdaptFilehandleRead'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | $app; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | L<PSGI> allows for the body content to be a glob filehandle or a Filehandle | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | like object. For the later case the object must have a method C<getline> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | which works as described in L<IO::Handle>. However sometimes you may have | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | a custom filehandle like object that support the common C<read> method. For | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | example many versions of L<Catalyst> allowed the body of a response to be a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | glob or object that does C<read>. People might have written custom streaming | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | applications that had this C<read> method but not C<getline>. As a result | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | these custom Filehandle like objects are not compatible with the expectations | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | of L<PSGI>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | This middleware exists to help you convert such a custom made filehandle like | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | object. If you have created something like this (or for example using some | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | shared code like L<MogileFS::Client> that returns a filehandle that does C<read> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | but not C<getline>) you can use this middleware to wrap said object in a proxy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | that does the C<getline> method by reading from the exising C<read> method. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | By default, if this middleware is enabled, it will examine any body values and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | check if they are 1) an object, 2) that does C<read> and 3) doesn't do C<getline> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | If such a case exists it will create an instance of L<Plack::Middleware::AdaptFilehandleRead::Proxy> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | which had the C<getline> method. It also will delegate any other method calls | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | to the wrapped object via AUTOLOAD so if you have some additional custom methods | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | it will still work as expected. It does not currently proxy any L<overload>ing. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | If for some reason your custom filehandle like object does C<getline> but its | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | faulty and the C<read> method is correct, you can set C<always_adapt> to true | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | and the proxy will be applied even if a C<getline> method is detected. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | builder { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | enable 'AdaptFilehandleRead', always_adapt=>1; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | $app; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | This middleware will do its best to respect the various allowed values of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | C<$/> for deciding how to return content from C<getline> Currently we support | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | C<$/> values of scalar ref (like \8192 for reading fixed length chunks) or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | simple scalars (like \n for reading newline delimited records). Currently | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | we don't support C<$/> as undef (for slurping full content) and some of the other | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | more esoteric values of C<$/> as the author percieves that support was not needed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | withing the context of adapting C<read> for L<PSGI> uses (all exampled L<Plack> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | handlers seemed to use the scalar ref fixed length chunk value for C<$/>, but | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | we choose to also support the scalar record deliminator option since its very | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | commonly seen elsewhere). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | This middleware has the following attributes: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | =head2 always_adapt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | Defaults to false, Optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | Set this to any true value and the proxy will always wrap any filehandle like | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | object, as long as it has a C<read> method (even it if already has a C<getline> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | method.) Use this if you have a custom filehandle like object that you are using | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | as the body of a L<PSGI> reponse that has both C<read> and C<getline> but the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | C<getline> is broken in some way (but C<read> isn't). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =head2 chunksize | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | Defaults to 65536, Optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | When adapting C<read>, we call for chunks of data 65536 in length. This may not be the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | most efficient way to read your files based on your specific requirements. If so, you | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | may override the size of the chunks: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | builder { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | enable 'AdaptFilehandleRead', chunksize=>65536; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | $app; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | B<NOTE>: Be aware that the chunk is read into memory as each chunk is read. Should a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | chunk fail to find the deliminator indicated by C<$/>, another chunk would be read. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | If you entire file contains no match, it is not impossible for the entire file to be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | thus read into memory should C<getline> be used. In these cases you might wish to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | make sure your underlying L<Plack> server has other ways to handle these types of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | files (for example using XSendfile or via some other optimization.) or instead be sure | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | to use the fixed chunk sized option for C<$/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | B<NOTE>: For most L<Plack> handlers, "$/" is set to a scalar refer, such as: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | local $/ = \'4096' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | which is a flag indicating we'd prefer ->getline to return fixed length chunks | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | instead of variable length lines. In this case the 'chunksize' attribute is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | ignored. Which means if you are using this with L<Plack> chances are this | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | attribute will not be respected :) You probably should not worry about this! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | L<PSGI>, L<Plack>, L<Plack::Middleware>, L<IO::File>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | L<Plack::Middleware::AdaptFilehandleRead::Proxy>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | See 'perlvar' docs for more on the possible values of C<$/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | Copyright 2014, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||