Filter/FastCGI.pm

filter: Statementstmt 30.0% 42 / 140 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 54.1% 13 / 24 Podpod 0.0% 0 / 6 totaltotal 22.5% 55 / 244 SCAR 66.6
CC
38
Cov
20%
_do_record
110.0
get_one
90.0
put
56.0
CRAP 777.3
↑ Summary Component/FastCGI/Stream.pm »
1 package POE::Filter::FastCGI;
2 BEGIN {
3 2 ▶ $POE::Filter::FastCGI::VERSION = '0.19';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our(@ROLE, @TYPE);
10  
11 BEGIN {
12 # Some normal constants
13 2 ▶ use constant FCGI_VERSION_1 => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶ use constant HEADER_LENGTH => 8;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶ use constant STATE_WAIT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use constant STATE_DATA => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶ use constant REQUEST_COMPLETE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶ use constant CANT_MPX_CONN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶ use constant OVERLOADED => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use constant UNKNOWN_ROLE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # Request flag constants
24 2 ▶ use constant FCGI_KEEP_CONN => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Constant maps
27 2 ▶ @TYPE = qw(
Subroutine BEGIN: called CC=3 SCAR=11.0
28 NULL
29 BEGIN_REQUEST
30 ABORT_REQUEST
31 END_REQUEST
32 PARAMS
33 FCGI_STDIN
34 FCGI_STDOUT
35 FCGI_STDERR
36 );
37 2 my $c = 1;
38 2 constant->import($_ => $c++) for @TYPE[1 .. $#TYPE];
39  
40 2 @ROLE = qw(
41 NULL
42 RESPONDER
43 AUTHORIZER
44 FILTER
45 );
46 2 $c = 1;
47 2 constant->import($_ => $c++) for @ROLE[1 .. $#ROLE];
48 }
49  
50 sub new {
51 0 ▶ my($class) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my $self = bless {
53 buffer => "",
54 conn => [ ],
55 state => STATE_WAIT,
56 }, $class;
57 0 return $self;
58 }
59  
60 sub get {
61 0 ▶ my($self, $stream) = @_;
Subroutine get: not called CC=3 SCAR=24.8
Pod undocumented
62 0 $self->get_one_start($stream);
63 0 my(@out, $conn);
64 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
@$conn and do { do { $conn = $self->get_one; push @out, @$conn if @$conn } } 0 0 0
65 0 $conn = $self->get_one;
66 0 ▶ push @out => @$conn if @$conn;
Branch: 1 branch
BranchTrueFalseTotal
if @$conn 0 0 0
67 }while(@$conn);
68 0 return \@out;
69 }
70  
71 sub get_pending {
72 0 ▶ my($self) = @_;
Subroutine get_pending: not called CC=2 SCAR=17.9
Pod undocumented
73 0 ▶ return $self->{buffer} ? $self->{buffer} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'buffer'} ? : 0 0 0
74 }
75  
76 sub get_one {
77 0 ▶ my($self) = @_;
Subroutine get_one: not called CC=9 SCAR=45.0
Pod undocumented
78  
79 0 ▶ while($self->{buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"buffer"}) 0 0 0
80 0 ▶ if($self->{state} == STATE_WAIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} == 1) 0 0 0
81 0 ▶ return [ ] unless length $self->{buffer} >= HEADER_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"buffer"} >= 8 0 0 0
82  
83 # Remove FastCGI header from buffer
84 0 my $header = substr $self->{buffer}, 0, HEADER_LENGTH, "";
85  
86 0 @$self{qw/version type requestid contentlen padlen/} =
87 unpack "CCnnC", $header;
88  
89 warn "Wrong version, or direct request from a browser"
90 0 ▶ if $self->{version} != FCGI_VERSION_1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"version"} != 1 0 0 0
91  
92 0 ▶ if($self->{contentlen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'contentlen'}) { } 0 0 0
93 0 $self->{state} = STATE_DATA;
94 }else{
95 0 my $conn = $self->_do_record;
96 0 ▶ return [$conn] if defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conn 0 0 0
97 0 next;
98 }
99 }
100  
101 0 ▶ if(length $self->{buffer} >= ($self->{contentlen} + $self->{padlen})) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'buffer'} >= $self->{'contentlen'} + $self->{'padlen'}) { } 0 0 0
102 # Remove content from buffer
103 0 my $content = substr $self->{buffer}, 0, $self->{contentlen}, "";
104 # Remove padding
105 0 substr $self->{buffer}, 0, $self->{padlen}, "";
106  
107 0 my $conn = $self->_do_record($content);
108 0 ▶ return [$conn] if defined $conn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $conn 0 0 0
109 } else {
110 0 return [ ];
111 }
112 }
113 0 return [ ];
114 }
115  
116 sub get_one_start {
117 0 ▶ my($self, $stream) = @_;
Subroutine get_one_start: not called CC=1 SCAR=6.9
Pod undocumented
118 0 $self->{buffer} .= join '', @$stream;
119 }
120  
121 # Process FastCGI record
122 sub _do_record {
123 0 ▶ my($self, $content) = @_;
Subroutine _do_record: not called CC=10 SCAR=47.0
124  
125 0 ▶ if($self->{type} == BEGIN_REQUEST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"type"} == 1) 0 0 0
126 0 my($role, $flags) = unpack "nC", $content;
127 0 $self->{conn}->[$self->{requestid}] = {
128 state => BEGIN_REQUEST,
129 flags => $flags,
130 role => $ROLE[$role],
131 cgi => { },
132 };
133  
134 0 ▶ $self->{conn}->[$self->{requestid}]{keepconn} = $flags & FCGI_KEEP_CONN ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 1 ? : 0 0 0
135 0 return $self->_cleanup;
136 }
137  
138 0 ▶ return $self->_cleanup if not defined $self->{conn}->[$self->{requestid}];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"conn"}[$self->{"requestid"}] 0 0 0
139  
140 0 my $conn = $self->{conn}->[$self->{requestid}];
141 0 $conn->{state} = $self->{type};
142  
143 0 ▶ if($self->{type} == PARAMS) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'type'} == 4) { } 0 0 0
elsif ($self->{'type'} == 5) { } 0 0 0
144 0 ▶ if(defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 0 0 0
145 0 my $offset = 0;
146 0 my($nlen, $vlen);
147 0 ▶ while(defined($nlen = _read_nv_len(\$content, \$offset)) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined($nlen = _read_nv_len(\$content, \$offset)) and defined($vlen = _read_nv_len(\$content, \$offset))) 0 0 0
Condition0%0/3
defined($nlen = _read_nv_len(\$content, \$offset)) and defined($vlen = _read_nv_len(\$content, \$offset))
!ll&&!rl&&r
000
MC/DC0%0/2
defined($nlen = _read_nv_len(\$content, \$offset)) and defined($vlen = _read_nv_len(\$content, \$offset))
defined($nlen = _read_nv_len(\$content, \$offset)) defined($vlen = _read_nv_len(\$content, \$offset))
Truth table0%0/3
A and B
A= defined($nlen = _read_nv_len(\$content, \$offset))B= defined($vlen = _read_nv_len(\$content, \$offset))
ABresult
0X0
100
111
148 defined($vlen = _read_nv_len(\$content, \$offset))) {
149 0 my($name, $value) = (substr($content, $offset, $nlen),
150 substr($content, $offset + $nlen, $vlen));
151 0 $conn->{cgi}->{$name} = $value;
152 0 $offset += $nlen + $vlen;
153 }
154 }
155 }elsif($self->{type} == FCGI_STDIN) {
156 0 ▶ if(defined $content) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 0 0 0
157 0 $conn->{postdata} .= $content;
158 }else{
159 0 my $cgi = delete $conn->{cgi};
160 0 return [$self->{requestid}, $conn, $cgi];
161 }
162 }
163  
164 0 return $self->_cleanup;
165 }
166  
167 sub _cleanup {
168 0 ▶ my($self, $request) = @_;
Subroutine _cleanup: not called CC=1 SCAR=6.9
169 0 delete @$self{qw/version type requestid contentlen padlen/};
170 0 $self->{state} = STATE_WAIT;
171 0 return $request;
172 }
173  
174 sub _read_nv_len {
175 0 ▶ my($dataref, $offsetref) = @_;
Subroutine _read_nv_len: not called CC=4 SCAR=30.0
176 0 my $buf = substr($$dataref, $$offsetref++, 1);
177 0 ▶ return undef unless length $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless length $buf 0 0 0
178 0 my $len = unpack("C", $buf);
179  
180 0 ▶ if($len & 0x80) { # High order bit set
Branch: 1 branch
BranchTrueFalseTotal
if ($len & 128) 0 0 0
181 0 $buf = substr($$dataref, $$offsetref, 3);
182 0 ▶ return undef unless $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless $buf 0 0 0
183 0 $$offsetref += 3;
184 0 $len = unpack("N", (pack("C", $len & 0x7f) . $buf));
185 }
186  
187 0 return $len;
188 }
189  
190 sub put {
191 0 ▶ my($self, $input) = @_;
Subroutine put: not called CC=7 SCAR=40.3
Pod undocumented
192 0 my @output;
193  
194 0 for my $response(@$input) {
195 0 ▶ if(UNIVERSAL::isa($response, "POE::Component::FastCGI::Response")) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($response, 'POE::Component::FastCGI::Response')) { } 0 0 0
elsif (ref $response eq 'HASH') { } 0 0 0
196 $self->_write(\@output, $response->{requestid},
197 0 FCGI_STDOUT, $response->as_string);
198 0 $self->_close(\@output, $response->{requestid});
199 }elsif(ref $response eq "HASH") {
200 0 ▶ if(length $response->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $response->{"content"}) 0 0 0
201 $self->_write(\@output, $response->{requestid},
202 0 FCGI_STDOUT, $response->{content});
203 }
204 0 ▶ if(exists $response->{close} and $response->{close}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $response->{"close"} and $response->{"close"}) 0 0 0
Condition0%0/3
exists $response->{"close"} and $response->{"close"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $response->{"close"} and $response->{"close"}
exists $response->{"close"} $response->{"close"}
Truth table0%0/3
A and B
A= exists $response->{"close"}B= $response->{"close"}
ABresult
0X0
100
111
205 0 $self->_close(\@output, $response->{requestid});
206 }
207 }else{
208 0 warn "Unhandled put";
209 }
210 }
211  
212 0 return [ join '', @output ];
213 }
214  
215 # Close a connection
216 sub _close {
217 0 ▶ my($self, $output, $id, $status, $appstatus) = @_;
Subroutine _close: not called CC=4 SCAR=30.0
218 0 ▶ $status = REQUEST_COMPLETE unless defined $status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $status 0 0 0
219 0 ▶ $self->_write($output, $id, FCGI_STDOUT, "") if $status == REQUEST_COMPLETE;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 0 0 0 0
220 0 ▶ $self->_write($output, $id, END_REQUEST,
Branch: 1 branch
BranchTrueFalseTotal
defined $appstatus ? : 0 0 0
221 pack("NCx3", (defined $appstatus ? $appstatus : 0), $status, 0));
222 0 delete $self->{conn}->[$id];
223 }
224  
225 # Append FastCGI packets to @$output.
226 sub _write {
227 0 ▶ my ($self, $output, $id, $type, $content) = @_;
Subroutine _write: not called CC=4 SCAR=30.0
228 0 my $length = length $content;
229 0 my $offset = 0;
230  
231 0 ▶ if($length == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length == 0) 0 0 0
232 # Null packet
233 0 push @$output, pack("CCnnCx", FCGI_VERSION_1, $type, $id, 0, 0);
234 0 return;
235 }
236  
237 # Create as many 32KiB packets as needed
238 0 ▶ while ($length > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 0) 0 0 0
239 0 ▶ my $len = $length > 32*1024 ? 32*1024 : $length;
Branch: 1 branch
BranchTrueFalseTotal
$length > 32768 ? : 0 0 0
240 0 my $padlen = (8 - ($len % 8)) % 8;
241 0 push @$output, pack("CCnnCxa${len}x$padlen",
242 FCGI_VERSION_1, $type, $id, $len, $padlen,
243 substr($content, $offset, $len));
244  
245 0 $length -= $len;
246 0 $offset += $len;
247 }
248 }
249  
250 2 1;
251  
252 =head1 NAME
253  
254 POE::Filter::FastCGI - Parse and create FastCGI requests
255  
256 =head1 SYNOPSIS
257  
258 $fastcgi = POE::Filter::FastCGI->new;
259 $arrayref_with_binary_fastcgi_response = $fastcgi->put($put);
260 $arrayref_with_fastcgi_request_array = $fastcgi->get($chunks);
261  
262 =head1 DESCRIPTION
263  
264 Parses the FastCGI binary protocol into a perl array with the CGI
265 environment and any POST or other data that is sent.
266  
267 Accepts either L<POE::Component::FastCGI::Response> objects or a
268 simple hash reference via C<put> and converts into the FastCGI
269 binary protocol. The hash reference should have keys of requestid
270 and content and an optional key of close to end the FastCGI
271 request.
272  
273 =head1 AUTHOR
274  
275 Copyright 2005, David Leadbeater L<http://dgl.cx/contact>. All rights reserved.
276  
277 This library is free software; you can redistribute it and/or modify
278 it under the same terms as Perl itself.
279  
280 Some parts taken from FCGI's Pure Perl implementation.
281  
282 =head1 BUGS
283  
284 This is rather tightly coupled with L<POE::Component::FastCGI>, ideally
285 there would be some form of intermediate perl object to use for FastCGI
286 like L<POE::Filter::HTTPD> can make use of L<HTTP::Request>.
287  
288 This code is pure perl, it's probably slow compared to L<FCGI> (which is
289 mostly C) and it doesn't handle as many record types as L<FCGI>. However
290 L<FCGI> doesn't allow more than one concurrent request.
291  
292 =head1 SEE ALSO
293  
294 L<POE::Component::FastCGI>, L<POE::Filter::HTTPD>, L<POE::Filter>.
295  
296 =cut
↑ Summary Component/FastCGI/Stream.pm »