blib/lib/POE/Filter/HTTPD.pm

filter: Statementstmt 94.0% 207 / 220 Branchbran 72.2% 52 / 72 Conditioncond 62.1% 23 / 37 MC/DCmcdc 28.5% 6 / 21 Subroutinesub 90.3% 28 / 31 Podpod 83.3% 5 / 6 totaltotal 82.9% 321 / 387 CC 56 SCAR 24.2
CC
56
Cov
86%
get_one
27.7
put
7.2
headers_as_strings
6.5
CRAP 11.3
« blib/lib/POE/Resource/Signals.pm ↑ Summary blib/lib/POE/Filter/Reference.pm »
1 # Filter::HTTPD Copyright 1998 Artur Bergman <artur@vogon.se>.
2 # Thanks go to Gisle Aas for his excellent HTTP::Daemon. Some of the
3 # get code was copied out if, unfortunately HTTP::Daemon is not easily
4 # subclassed for POE because of the blocking nature.
5 # 2001-07-27 RCC: This filter will not support the newer get_one()
6 # interface. It gets single things by default, and it does not
7 # support filter switching. If someone absolutely needs to switch to
8 # and from HTTPD filters, they should submit their request as a patch.
9  
10 package POE::Filter::HTTPD;
11  
12 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use POE::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 $VERSION = '1.370';
17 # NOTE - Should be #.### (three decimal places)
18 3 @ISA = qw(POE::Filter);
19  
20 sub DEBUG () { 0 }
21  
22 sub BUFFER () { 0 } # raw data buffer to build requests
23 sub STATE () { 1 } # built a full request
24 sub REQUEST () { 2 } # partial request being built
25 sub CLIENT_PROTO () { 3 } # client protocol version requested
26 sub CONTENT_LEN () { 4 } # expected content length
27 sub CONTENT_ADDED () { 5 } # amount of content added to request
28 sub CONTENT_MAX () { 6 } # max amount of content
29 sub STREAMING () { 7 } # we want to work in streaming mode
30 sub MAX_BUFFER () { 8 } # max size of framing buffer
31 sub FIRST_UNUSED () { 9 }
32  
33 sub ST_HEADERS () { 0x01 } # waiting for complete header block
34 sub ST_CONTENT () { 0x02 } # waiting for complete body
35  
36 3 ▶use Carp qw(croak cluck carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use HTTP::Status qw( status_message RC_BAD_REQUEST RC_OK RC_LENGTH_REQUIRED
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 RC_REQUEST_ENTITY_TOO_LARGE );
39 3 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 3 my $HTTP_1_0 = _http_version("HTTP/1.0");
45 3 my $HTTP_1_1 = _http_version("HTTP/1.1");
46  
47 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 our @EXPORT_OK = qw( FIRST_UNUSED );
49  
50  
51  
52 #------------------------------------------------------------------------------
53 # Set up some routines for convert wide chars (which aren't allowed in HTTP headers)
54 # into MIME encoded equivalents.
55 # See ->headers_as_strings
56 3 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=12.0
57 3 ▶ eval "use utf8";
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 3 3
59 0 DEBUG and warn "We don't have utf8.";
60 0 ▶ *HAVE_UTF8 = sub { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 }
62 else {
63 3 ▶ *HAVE_UTF8 = sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
64 1 ▶ my $downgrade = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 1 my $ret = $_[0];
66 1 utf8::downgrade( $ret );
67 1 return $ret
68 3 };
69 0 ▶ eval "use Email::MIME::RFC2047::Encoder";
Subroutine BEGIN: called CC=1 SCAR=1.2
70 3 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 3 0 3
71 3 DEBUG and warn "We don't have Email::MIME::RFC2047::Encoder";
72 1 ▶ *encode_value = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
73 1 cluck(
74 "Downgrading wide characters in HTTP header. " .
75 "Consider installing Email::MIME::RFC2047::Encoder"
76 );
77 1 $downgrade->( @_ );
78 3 };
79 }
80 else {
81 0 my $encoder = Email::MIME::RFC2047::Encoder->new( encoding => 'iso-8859-1',
82 method => 'Q'
83 );
84 0 ▶ *encode_value = sub { $downgrade->( $encoder->encode_text( @_ ) ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 }
86 }
87 }
88  
89  
90 #------------------------------------------------------------------------------
91  
92 34 ▶sub new {
Subroutine new: called CC=6 SCAR=18.1
Pod documented
93 34 my $type = shift;
94 34 ▶ croak "$type requires an even number of parameters" if @_ and @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and @_ & 1 0 34 34
Condition66%2/3
@_ and @_ & 1
!ll&&!rl&&r
2680
MC/DC0%0/2
@_ and @_ & 1
@_ @_ & 1
Truth table66%2/3
A and B
A= @_B= @_ & 1
ABresult
0X0
100
111
95 34 my %params = @_;
96  
97 34 my $max_content = $type->__param_max( MaxContent => 1024*1024, \%params );
98 32 my $max_buffer = $type->__param_max( MaxBuffer => 512*1024*1024, \%params );
99 30 ▶ my $streaming = $params{Streaming} || 0;
Condition100%2/2
$params{'Streaming'} || 0
l!l
129
MC/DC100%1/1
$params{'Streaming'} || 0
$params{'Streaming'}
Truth table100%2/2
A
A= $params{'Streaming'}
Aresult
11
00
100  
101 30 ▶ croak "MaxBuffer is not large enough for MaxContent"
Branch: 1 branch
BranchTrueFalseTotal
unless $max_buffer >= $max_content + length($max_content) + 1 1 29 30
102 unless $max_buffer >= $max_content + length( $max_content ) + 1;
103  
104 29 delete @params{qw(MaxContent MaxBuffer Streaming)};
105 29 ▶ carp("$type ignores unknown parameters: ", join(', ', sort keys %params))
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %params 0 29 29
106 if scalar keys %params;
107  
108 29 return bless(
109 [
110 '', # BUFFER
111 ST_HEADERS, # STATE
112 undef, # REQUEST
113 undef, # CLIENT_PROTO
114 0, # CONTENT_LEN
115 0, # CONTENT_ADDED
116 $max_content, # CONTENT_MAX
117 $streaming, # STREAMING
118 $max_buffer # MAX_BUFFER
119 ],
120 $type
121 );
122 }
123  
124 #------------------------------------------------------------------------------
125  
126 63 ▶sub get_one_start {
Subroutine get_one_start: called CC=2 SCAR=6.9
Pod documented
127 63 my ($self, $stream) = @_;
128
129 63 $self->[BUFFER] .= join( '', @$stream );
130 63 DEBUG and warn "$$:poe-filter-httpd: Buffered ".length( $self->[BUFFER] )." bytes";
131 63 ▶ die "Framing buffer exceeds the limit"
Branch: 1 branch
BranchTrueFalseTotal
if $self->[8] < length $self->[0] 1 62 63
132 if $self->[MAX_BUFFER] < length( $self->[BUFFER] );
133 }
134  
135 90 ▶sub get_one {
Subroutine get_one: called CC=27 SCAR=33.2
Pod documented
136 90 my ($self) = @_;
137  
138 # Need to check lengths in octets, not characters.
139 3 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 3 0 3
140  
141 # Waiting for a complete suite of headers.
142 90 ▶ if ($self->[STATE] & ST_HEADERS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1] & 1) 84 6 90
143 84 DEBUG and warn "$$:poe-filter-httpd: Looking for headers";
144 # Strip leading whitespace.
145 84 $self->[BUFFER] =~ s/^\s+//;
146  
147 # No blank line yet. Side effect: Raw headers block is extracted
148 # from the input buffer.
149 84 ▶ return [] unless (
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[0] =~ s/^(\S.*?(?:\x0D\x0A?\x0D\x0A?|\x0A\x0D?\x0A\x0D?))//s 56 28 84
150 $self->[BUFFER] =~
151 s/^(\S.*?(?:\x0D\x0A?\x0D\x0A?|\x0A\x0D?\x0A\x0D?))//s
152 );
153  
154 # Raw headers block from the input buffer.
155 28 my $rh = $1;
156  
157 # Parse the request line.
158 28 ▶ if ($rh !~ s/^(\w+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rh =~ s[^(\w+)[ \t]+(\S+)(?:[ \t]+(HTTP/\d+\.\d+))?[^\012]*\012][]) 2 26 28
159 return [
160 2 $self->_build_error(RC_BAD_REQUEST, "Request line parse failure. ($rh)")
161 ];
162 }
163  
164 # Create an HTTP::Request object from values in the request line.
165 26 ▶ my ($method, $request_path, $proto) = ($1, $2, ($3 || "HTTP/0.9"));
Condition100%2/2
$3 || 'HTTP/0.9'
l!l
1313
MC/DC100%1/1
$3 || 'HTTP/0.9'
$3
Truth table100%2/2
A
A= $3
Aresult
11
00
166  
167 # Fix a double starting slash on the path. It happens.
168 26 $request_path =~ s!^//+!/!;
169  
170 26 my $r = HTTP::Request->new($method, URI->new($request_path));
171 26 $r->protocol($proto);
172 26 $self->[CLIENT_PROTO] = $proto = _http_version($proto);
173  
174 # Parse headers.
175  
176 26 my ($key, $val);
177 26 ▶ HEADER: while ($rh =~ s/^([^\012]*)\012//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh =~ s/^([^\012]*)\012//) 51 0 51
178 51 local $_ = $1;
179 51 s/\015$//;
180 51 ▶ if (/^([\w\-~]+)\s*:\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^([\w\-~]+)\s*:\s*(.*)/) { } 25 26 51
elsif (/^\s+(.*)/) { } 0 26 26
181 25 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 13 12 25
182 25 ($key, $val) = ($1, $2);
183 }
184 elsif (/^\s+(.*)/) {
185 0 $val .= " $1";
186 }
187 else {
188 26 last HEADER;
189 }
190 }
191  
192 26 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 12 14 26
193  
194 # We got a full set of headers. Fall through to content if we
195 # have a content length.
196  
197 26 my $cl = $r->content_length();
198 26 ▶ if( defined $cl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cl) 10 16 26
199 10 ▶ unless( $cl =~ /^\s*(\d+)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl =~ /^\s*(\d+)\s*$/) 1 9 10
200 1 $r = $self->_build_error(RC_BAD_REQUEST,
201 "Content-Length is not a number.",
202 $r);
203 1 $self->[BUFFER] = '';
204 1 $self->_reset();
205 1 return [ $r ];
206 }
207 9 ▶ $cl = $1 || 0;
Condition50%1/2
$1 || 0
l!l
90
MC/DC0%0/1
$1 || 0
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
208 }
209 25 my $ce = $r->content_encoding();
210
211 # The presence of a message-body in a request is signaled by the
212 # inclusion of a Content-Length or Transfer-Encoding header field in
213 # the request's message-headers. A message-body MUST NOT be included in
214 # a request if the specification of the request method (section 5.1.1)
215 # does not allow sending an entity-body in requests. A server SHOULD
216 # read and forward a message-body on any request; if the request method
217 # does not include defined semantics for an entity-body, then the
218 # message-body SHOULD be ignored when handling the request.
219 # - RFC2616
220  
221 25 ▶ unless( defined $cl || defined $ce ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cl or defined $ce) 15 10 25
Condition100%3/3
defined $cl or defined $ce
l!l&&r!l&&!r
9115
MC/DC100%2/2
defined $cl or defined $ce
defined $cl defined $ce
Truth table100%3/3
A or B
A= defined $clB= defined $ce
ABresult
1X1
011
000
222 # warn "No body";
223 15 $self->_reset();
224 15 return [ $r ];
225 }
226
227 # PG- GET shouldn't have a body. But RFC2616 talks about Content-Length
228 # for HEAD. And My reading of RFC2616 is that HEAD is the same as GET.
229 # So logically, GET can have a body. And RFC2616 says we SHOULD ignore
230 # it.
231 #
232 # What's more, in apache 1.3.28, a body on a GET or HEAD is read
233 # and discarded. See ap_discard_request_body() in http_protocol.c and
234 # default_handler() in http_core.c
235 #
236 # Neither Firefox 2.0 nor Lynx 2.8.5 set Content-Length on a GET
237  
238 # For compatibility with HTTP/1.0 applications, HTTP/1.1 requests
239 # containing a message-body MUST include a valid Content-Length header
240 # field unless the server is known to be HTTP/1.1 compliant. If a
241 # request contains a message-body and a Content-Length is not given,
242 # the server SHOULD respond with 400 (bad request) if it cannot
243 # determine the length of the message, or with 411 (length required) if
244 # it wishes to insist on receiving a valid Content-Length.
245 # - RFC2616
246 #
247 # PG- This seems to imply that we can either detect the length (but how
248 # would one do that?) or require a Content-Length header. We do the
249 # latter.
250 #
251 # PG- Dispite all the above, I'm not fully sure this implements RFC2616
252 # properly. There's something about transfer-coding that I don't fully
253 # understand.
254  
255 10 ▶ if ( not $cl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cl) 1 9 10
256 # assume a Content-Length of 0 is valid pre 1.1
257 1 ▶ if ($self->[CLIENT_PROTO] >= $HTTP_1_1 and not defined $cl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] >= $HTTP_1_1 and not defined $cl) 1 0 1
Condition33%1/3
$self->[3] >= $HTTP_1_1 and not defined $cl
!ll&&!rl&&r
001
MC/DC0%0/2
$self->[3] >= $HTTP_1_1 and not defined $cl
$self->[3] >= $HTTP_1_1 not defined $cl
Truth table33%1/3
A and not(B)
A= $self->[3] >= $HTTP_1_1B= not defined $cl
ABresult
0X0
100
111
258 # We have Content-Encoding, but not Content-Length.
259 1 $r = $self->_build_error(RC_LENGTH_REQUIRED,
260 "No content length found.",
261 $r);
262 }
263 1 $self->[BUFFER] = '';
264 1 $self->_reset();
265 1 return [ $r ];
266 }
267  
268 # Prevent DOS of a server by malicious clients
269 9 ▶ if( not $self->[STREAMING] and $cl > $self->[CONTENT_MAX] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[7] and $cl > $self->[6]) 2 7 9
Condition100%3/3
not $self->[7] and $cl > $self->[6]
!ll&&!rl&&r
162
MC/DC100%2/2
not $self->[7] and $cl > $self->[6]
not $self->[7] $cl > $self->[6]
Truth table100%3/3
not(A) and B
A= not $self->[7]B= $cl > $self->[6]
ABresult
0X0
100
111
270 2 $r = $self->_build_error(RC_REQUEST_ENTITY_TOO_LARGE,
271 "Content of $cl octets not accepted.",
272 $r);
273 2 $self->[BUFFER] = '';
274 2 $self->_reset();
275 2 return [ $r ];
276 }
277  
278 7 $self->[REQUEST] = $r;
279 7 $self->[CONTENT_LEN] = $cl;
280 7 $self->[STATE] = ST_CONTENT;
281 # Fall through to content.
282 }
283  
284 # Waiting for content.
285 13 ▶ if ($self->[STATE] & ST_CONTENT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1] & 2) 13 0 13
286 13 my $r = $self->[REQUEST];
287 13 my $cl_needed = $self->[CONTENT_LEN] - $self->[CONTENT_ADDED];
288 13 ▶ die "already got enough content ($cl_needed needed)" if $cl_needed < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $cl_needed < 1 0 13 13
289  
290 13 ▶ if( $self->[STREAMING] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[7]) 1 12 13
291 1 DEBUG and warn "$$:poe-filter-httpd: Streaming request content";
292 1 my @ret;
293 # do we have a request?
294 1 ▶ if( $self->[REQUEST] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) 1 0 1
295 1 DEBUG and warn "$$:poe-filter-httpd: Sending request";
296 1 push @ret, $self->[REQUEST]; # send it to the wheel
297 1 $self->[REQUEST] = undef;
298 }
299 # do we have some content ?
300 1 ▶ if( length( $self->[BUFFER] ) ) { # send it to the wheel
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->[0]) 1 0 1
301 1 my $more = substr($self->[BUFFER], 0, $cl_needed);
302 1 DEBUG and warn "$$:poe-filter-httpd: Sending content";
303 1 push @ret, $more;
304 1 $self->[CONTENT_ADDED] += length($more);
305 1 substr( $self->[BUFFER], 0, length($more) ) = "";
306 # is that enough content?
307 1 ▶ if( $self->[CONTENT_ADDED] >= $self->[CONTENT_LEN] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5] >= $self->[4]) 1 0 1
308 1 DEBUG and warn "$$:poe-filter-httpd: All content received ($self->[CONTENT_ADDED] >= $self->[CONTENT_LEN])";
309 # Strip MSIE 5.01's extra CRLFs
310 1 $self->[BUFFER] =~ s/^\s+//;
311 1 $self->_reset;
312 }
313 }
314 1 return \@ret;
315 }
316  
317 # Not enough content to complete the request. Add it to the
318 # request content, and return an incomplete status.
319 12 ▶ if (length($self->[BUFFER]) < $cl_needed) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->[0] < $cl_needed) 6 6 12
320 6 $r->add_content($self->[BUFFER]);
321 6 $self->[CONTENT_ADDED] += length($self->[BUFFER]);
322 6 $self->[BUFFER] = "";
323 6 return [];
324 }
325  
326 # Enough data. Add it to the request content.
327 # PG- CGI.pm only reads Content-Length: bytes from STDIN.
328  
329 # Four-argument substr() would be ideal here, but it's not
330 # entirely backward compatible.
331 6 $r->add_content(substr($self->[BUFFER], 0, $cl_needed));
332 6 substr($self->[BUFFER], 0, $cl_needed) = "";
333  
334 # Some browsers (like MSIE 5.01) send extra CRLFs after the
335 # content. Shame on them.
336 6 $self->[BUFFER] =~ s/^\s+//;
337  
338 # XXX Should we throw the body away on a GET or HEAD? Probably not.
339  
340 # XXX Should we parse Multipart Types bodies?
341  
342 # Prepare for the next request, and return this one.
343 6 $self->_reset();
344 6 return [ $r ];
345 }
346  
347 # What are we waiting for?
348 0 die "unknown state $self->[STATE]";
349 }
350  
351 # Prepare for next request
352 sub _reset
353 26 ▶{
Subroutine _reset: called CC=1 SCAR=0.0
354 26 my($self) = @_;
355 26 $self->[STATE] = ST_HEADERS;
356 26 @$self[REQUEST, CLIENT_PROTO] = (undef, undef);
357 26 @$self[CONTENT_LEN, CONTENT_ADDED] = (0, 0);
358 }
359  
360  
361 #------------------------------------------------------------------------------
362  
363 2 ▶sub put {
Subroutine put: called CC=7 SCAR=19.7
Pod documented
364 2 my ($self, $responses) = @_;
365 2 my @raw;
366  
367 # HTTP::Response's as_string method returns the header lines
368 # terminated by "\n", which does not do the right thing if we want
369 # to send it to a client. Here I've stolen HTTP::Response's
370 # as_string's code and altered it to use network newlines so picky
371 # browsers like lynx get what they expect.
372 # PG- $r->as_string( "\x0D\x0A" ); would accomplish the same thing, no?
373  
374 2 foreach (@$responses) {
375 2 my $code = $_->code;
376 2 ▶ my $status_message = status_message($code) || "Unknown Error";
Condition50%1/2
&status_message($code) || 'Unknown Error'
l!l
20
MC/DC0%0/1
&status_message($code) || 'Unknown Error'
&status_message($code)
Truth table50%1/2
A
A= &status_message($code)
Aresult
11
00
377 2 ▶ my $message = $_->message || "";
Condition50%1/2
$_->message || ''
l!l
20
MC/DC0%0/1
$_->message || ''
$_->message
Truth table50%1/2
A
A= $_->message
Aresult
11
00
378 2 ▶ my $proto = $_->protocol || 'HTTP/1.0';
Condition50%1/2
$_->protocol || 'HTTP/1.0'
l!l
02
MC/DC0%0/1
$_->protocol || 'HTTP/1.0'
$_->protocol
Truth table50%1/2
A
A= $_->protocol
Aresult
11
00
379  
380 2 my $status_line = "$proto $code";
381 2 ▶ $status_line .= " ($status_message)" if $status_message ne $message;
Branch: 1 branch
BranchTrueFalseTotal
if $status_message ne $message 1 1 2
382 2 ▶ $status_line .= " $message" if length($message);
Branch: 1 branch
BranchTrueFalseTotal
if length $message 2 0 2
383  
384 # Use network newlines, and be sure not to mangle newlines in the
385 # response's content.
386  
387 2 my @headers;
388 2 push @headers, $status_line;
389  
390 # Perl can magically promote a string to UTF-8 if it is concatinated
391 # with another UTF-8 string. This behaviour changed between 5.8.8 and
392 # 5.10.1. This is normaly not a problem, but POE::Driver::SysRW uses
393 # syswrite(), which sends POE's internal buffer as-is.
394 # In other words, if the header contains UTF-8, the content will be
395 # promoted to UTF-8 and syswrite() will send those wide bytes, which
396 # will corrupt any images.
397 # For instance, 00 e7 ff 00 00 00 05
398 # will become, 00 c3 a7 c3 bf 00 00 00 05
399 #
400 # The real bug is in HTTP::Message->headers_as_string, which doesn't respect
401 # the following:
402 #
403 # "The TEXT rule is only used for descriptive field contents and values
404 # that are not intended to be interpreted by the message parser. Words
405 # of *TEXT MAY contain characters from character sets other than ISO-
406 # 8859-1 [22] only when encoded according to the rules of RFC 2047
407 # [14]. " -- RFC2616 section 2.2
408 # http://www.ietf.org/rfc/rfc2616.txt
409 # http://www.ietf.org/rfc/rfc2047.txt
410 2 my $endl = "\x0D\x0A";
411 2 push @headers, $self->headers_as_strings( $_->headers, $endl );
412 2 push @raw, join( $endl, @headers, "", "") . $_->content;
413 }
414  
415 2 \@raw;
416 }
417  
418 sub headers_as_strings
419 2 ▶{
Subroutine headers_as_strings: called CC=6 SCAR=18.7
Pod undocumented
420 2 my( $self, $H, $endl ) = @_;
421 2 my @ret;
422 # $H is a HTTP::Headers object
423 2 foreach my $name ( $H->header_field_names ) {
424 # message-header = field-name ":" [ field-value ]
425 # field-name = token
426 # RFC2616 section 4.2
427 #
428 # token = 1*<any CHAR except CTLs or separators>
429 # separators = "(" | ")" | "<" | ">" | "@"
430 # | "," | ";" | ":" | "\" | <">
431 # | "/" | "[" | "]" | "?" | "="
432 # | "{" | "}" | SP | HT
433 # CHAR = <any US-ASCII character (octets 0 - 127)>
434 # CTL = <any US-ASCII control character
435 # (octets 0 - 31) and DEL (127)>
436 # SP = <US-ASCII SP, space (32)>
437 # HT = <US-ASCII HT, horizontal-tab (9)>
438 # RFC2616 section 2.2
439  
440 # In other words, plain ascii text. HTTP::Headers doesn't check for
441 # this, of course. So if we complain here, the cluck ends up in
442 # the wrong place. Doing the simplest thing
443 1 ▶ utf8::downgrade( $name ) if HAVE_UTF8;
Branch: 1 branch
BranchTrueFalseTotal
if HAVE_UTF8() 1 0 1
444  
445 # Deal with header values
446 1 foreach my $value ( $H->header( $name ) ) {
447 1 ▶ if( HAVE_UTF8 and utf8::is_utf8( $value ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (HAVE_UTF8() and utf8::is_utf8($value)) 1 0 1
Condition33%1/3
HAVE_UTF8() and utf8::is_utf8($value)
!ll&&!rl&&r
001
MC/DC0%0/2
HAVE_UTF8() and utf8::is_utf8($value)
HAVE_UTF8() utf8::is_utf8($value)
Truth table33%1/3
A and B
A= HAVE_UTF8()B= utf8::is_utf8($value)
ABresult
0X0
100
111
448 1 DEBUG and warn "$$: Header $name is UTF-8";
449 1 $value = encode_value( $value );
450 }
451
452 1 push @ret, join ": ", $name, _process_newline( $value, $endl );
453 }
454 }
455 2 return @ret;
456 }
457  
458 # This routine is lifted as-is from HTTP::Headers
459 1 ▶sub _process_newline {
Subroutine _process_newline: called CC=1 SCAR=0.0
460 1 local $_ = shift;
461 1 my $endl = shift;
462 # must handle header values with embedded newlines with care
463 1 s/\s+$//; # trailing newlines and space must go
464 1 s/\n(\x0d?\n)+/\n/g; # no empty lines
465 1 s/\n([^\040\t])/\n $1/g; # initial space for continuation
466 1 s/\n/$endl/g; # substitute with requested line ending
467 1 $_;
468 }
469  
470 #------------------------------------------------------------------------------
471  
472 0 ▶sub get_pending {
Subroutine get_pending: not called CC=2 SCAR=17.9
Pod documented
473 0 my $self = shift;
474 0 ▶ return [ $self->[BUFFER] ] if length $self->[BUFFER];
Branch: 1 branch
BranchTrueFalseTotal
if length $self->[0] 0 0 0
475 0 return undef;
476 }
477  
478 #------------------------------------------------------------------------------
479 # Functions specific to HTTPD;
480 #------------------------------------------------------------------------------
481  
482 # Internal function to parse an HTTP status line and return the HTTP
483 # protocol version.
484  
485 32 ▶sub _http_version {
Subroutine _http_version: called CC=2 SCAR=7.1
486 32 local($_) = shift;
487 32 ▶ return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
Branch: 1 branch
BranchTrueFalseTotal
unless m[^(?:HTTP/)?(\d+)\.(\d+)$]i 0 32 32
488 32 $1 * 1000 + $2;
489 }
490  
491 # Build a basic response, given a status, a content type, and some
492 # content.
493  
494 6 ▶sub _build_basic_response {
Subroutine _build_basic_response: called CC=3 SCAR=11.2
495 6 my ($self, $content, $content_type, $status, $message) = @_;
496  
497 # Need to check lengths in octets, not characters.
498 3 ▶ BEGIN { eval { require bytes } and bytes->import; }
Subroutine BEGIN: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require bytes } } and "bytes"->import 3 0 3
499  
500 6 ▶ $content_type ||= 'text/html';
Condition50%1/2
$content_type ||= "text/html"
l!l
60
MC/DC0%0/1
$content_type ||= "text/html"
$content_type
Truth table50%1/2
A
A= $content_type
Aresult
11
00
501 6 ▶ $status ||= RC_OK;
Condition50%1/2
$status ||= 200
l!l
60
MC/DC0%0/1
$status ||= 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
502  
503 6 my $response = HTTP::Response->new($status, $message);
504  
505 6 $response->push_header( 'Content-Type', $content_type );
506 6 $response->push_header( 'Content-Length', length($content) );
507 6 $response->content($content);
508  
509 6 return $response;
510 }
511  
512 6 ▶sub _build_error {
Subroutine _build_error: called CC=5 SCAR=16.5
513 6 my($self, $status, $details, $req) = @_;
514  
515 6 ▶ $status ||= RC_BAD_REQUEST;
Condition50%1/2
$status ||= 400
l!l
60
MC/DC0%0/1
$status ||= 400
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
516 6 ▶ $details ||= '';
Condition50%1/2
$details ||= ""
l!l
60
MC/DC0%0/1
$details ||= ""
$details
Truth table50%1/2
A
A= $details
Aresult
11
00
517 6 ▶ my $message = status_message($status) || "Unknown Error";
Condition50%1/2
&status_message($status) || 'Unknown Error'
l!l
60
MC/DC0%0/1
&status_message($status) || 'Unknown Error'
&status_message($status)
Truth table50%1/2
A
A= &status_message($status)
Aresult
11
00
518  
519 6 my $resp = $self->_build_basic_response(
520 ( "<html>" .
521 "<head>" .
522 "<title>Error $status: $message</title>" .
523 "</head>" .
524 "<body>" .
525 "<h1>Error $status: $message</h1>" .
526 "<p>$details</p>" .
527 "</body>" .
528 "</html>"
529 ),
530 "text/html",
531 $status,
532 $message
533 );
534 6 ▶ $resp->request( $req ) if $req;
Branch: 1 branch
BranchTrueFalseTotal
if $req 4 2 6
535 6 return $resp;
536 }
537  
538 3 1;
539  
540 __END__
« blib/lib/POE/Resource/Signals.pm ↑ Summary blib/lib/POE/Filter/Reference.pm »