Server/Response.pm

filter: Statementstmt 85.8% 91 / 106 Branchbran 73.5% 25 / 34 Conditioncond 60.0% 12 / 20 MC/DCmcdc 45.4% 5 / 11 Subroutinesub 94.4% 17 / 18 Podpod 100% 8 / 8 totaltotal 80.2% 158 / 197 CC 25 SCAR 20.3
CC
25
Cov
80%
sendfile
16.6
__fix_headers
9.0
error
6.0
CRAP 7.6
↑ Summary Server.pm »
1 # $Id: Response.pm 909 2012-07-13 15:38:39Z fil $
2 # Copyright 2010 Philip Gwyn
3  
4 package POEx::HTTP::Server::Response;
5  
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use HTTP::Status qw( RC_NOT_FOUND RC_FORBIDDEN
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 RC_NOT_MODIFIED RC_INTERNAL_SERVER_ERROR );
15  
16 15 ▶use base qw( HTTP::Response );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 sub DEBUG () { 0 }
19  
20 #######################################
21 # Get/set streaming status
22 sub streaming
23 67 ▶{
Subroutine streaming: called CC=2 SCAR=6.9
Pod documented
24 67 my $self = shift;
25 67 my $rv = $self->{__streaming};
26 67 ▶ if (@_) { $self->{__streaming} = !!$_[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 63 67
27 67 return $rv;
28 }
29  
30 #######################################
31 # Get/set if the response header has been sent or not
32 sub headers_sent
33 82 ▶{
Subroutine headers_sent: called CC=2 SCAR=6.9
Pod documented
34 82 my $self = shift;
35 82 my $rv = $self->{__headers_sent};
36 82 ▶ if (@_) { $self->{__headers_sent} = !!$_[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 20 62 82
37 82 return $rv;
38 }
39  
40 #######################################
41 # End the request
42 sub done
43 15 ▶{
Subroutine done: called CC=2 SCAR=7.9
Pod documented
44 15 my( $self ) = @_;
45 15 ▶ unless( $self->{__done} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"__done"}) 0 15 15
46 0 carp "Only call ", ref($self), "->done once";
47 0 return;
48 }
49  
50 15 $poe_kernel->post( @{ delete $self->{__done} } );
51 }
52  
53 13 ▶sub finished { not exists $_[0]->{__done} }
Subroutine finished: called CC=1 SCAR=0.0
Pod documented
54  
55 #######################################
56 # Send some data. But not all the data
57 sub send
58 11 ▶{
Subroutine send: called CC=1 SCAR=0.0
Pod documented
59 11 my( $self, $something ) = @_;
60 11 $self->__fix_headers;
61 11 $poe_kernel->post( @{ $self->{__send} }, $something );
62 }
63  
64 #######################################
65 # Send the response
66 sub respond
67 14 ▶{
Subroutine respond: called CC=2 SCAR=7.0
Pod documented
68 14 my( $self ) = @_;
69  
70 14 ▶ croak "Responding more then once to a request" unless $self->{__respond};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"__respond"} 0 14 14
71  
72 14 $self->__fix_headers;
73 14 $poe_kernel->post( @{ delete $self->{__respond} } );
74 }
75  
76 sub __fix_headers
77 31 ▶{
Subroutine __fix_headers: called CC=9 SCAR=22.0
78 31 my( $self ) = @_;
79 31 ▶ return if $self->headers_sent;
Branch: 1 branch
BranchTrueFalseTotal
if $self->headers_sent 10 21 31
80 21 my $req = $self->request;
81  
82 21 ▶ unless( $self->protocol ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->protocol) 19 2 21
83 19 $self->protocol( $req->protocol );
84 }
85  
86 21 ▶ unless( $self->header('Date') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->header("Date")) 19 2 21
87 19 $self->header( 'Date', time2str(time) );
88 }
89  
90 21 ▶ if( not defined $self->header( 'Content-Length' ) and
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->header("Content-Length") || $self->streaming and $req->method ne "HEAD") 15 6 21
Condition100%6/6
not defined $self->header("Content-Length") || $self->streaming and $req->method ne "HEAD"
!ll&&!rl&&r
5115
defined $self->header('Content-Length') || $self->streaming
l!l&&r!l&&!r
3216
MC/DC100%3/3
not defined $self->header("Content-Length") || $self->streaming and $req->method ne "HEAD"
defined $self->header('Content-Length') $self->streaming $req->method ne "HEAD"
Truth table100%4/4
not(A || B) and C
A= defined $self->header('Content-Length')B= $self->streamingC= $req->method ne "HEAD"
ABCresult
1XX0
01X0
0000
0011
91 not $self->streaming and $req->method ne 'HEAD' ) {
92 15 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
93 15 my $c = $self->content;
94 15 ▶ if( defined $c and $c ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c and $c ne "") 13 2 15
Condition66%2/3
defined $c and $c ne ""
!ll&&!rl&&r
0213
MC/DC50%1/2
defined $c and $c ne ""
defined $c $c ne ""
Truth table66%2/3
A and B
A= defined $cB= $c ne ""
ABresult
0X0
100
111
95 13 $self->header( 'Content-Length' => length $c );
96 }
97 }
98 }
99  
100 #######################################
101 # Helper routine for generating an error
102 sub error
103 0 ▶{
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
104 0 my( $self, $rc, $text ) = @_;
105  
106 0 $self->code( $rc );
107 0 ▶ $self->content_type( 'text/plain' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->content_type 0 0 0
108 unless defined $self->content_type;
109 0 $self->content( $text );
110  
111 0 $self->respond;
112 0 $self->done;
113  
114 }
115  
116 #######################################
117 # Send a file to the client
118 sub sendfile
119 3 ▶{
Subroutine sendfile: called CC=12 SCAR=28.1
Pod documented
120 3 my( $self, $file, $ct ) = @_;
121  
122 3 DEBUG and warn "file=$file";
123  
124 3 ▶ my $path = $self->request->uri ?
Branch: 1 branch
BranchTrueFalseTotal
$self->request->uri ? : 3 0 3
125 $self->request->uri->path : basename $file;
126 3 ▶ unless( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 3 3
127 0 $self->error( RC_NOT_FOUND, "No such file or directory $path" );
128 0 return;
129 }
130 3 ▶ unless( -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 0 3 3
131 0 $self->error( RC_FORBIDDEN, "Denied $path: $!" );
132 0 return;
133 }
134  
135 # Info about the file
136 3 my $lastmod = (stat $file)[9];
137 3 my $size = (stat $file)[7];
138 3 DEBUG and warn "lastmod=$lastmod size=$size";
139  
140 # some required headers
141 3 $self->header( 'Last-Modified' => time2str( $lastmod ) );
142 3 ▶ unless( defined $self->content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->content_type) 0 3 3
143 0 ▶ $ct ||= 'application/octet-stream';
Condition0%0/2
$ct ||= "application/octet-stream"
l!l
00
MC/DC0%0/1
$ct ||= "application/octet-stream"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
144 0 DEBUG and warn "ct=$ct";
145 0 $self->content_type( $ct );
146 }
147  
148 # Bail early for HEAD requests
149 3 ▶ if ( $self->request->method eq 'HEAD' and $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->request->method eq "HEAD" and $size) 1 2 3
Condition66%2/3
$self->request->method eq "HEAD" and $size
!ll&&!rl&&r
201
MC/DC50%1/2
$self->request->method eq "HEAD" and $size
$self->request->method eq "HEAD" $size
Truth table66%2/3
A and B
A= $self->request->method eq "HEAD"B= $size
ABresult
0X0
100
111
150 1 $self->header( 'Content-Length' => $size );
151 1 $self->respond;
152 1 $self->done;
153 1 return;
154 }
155  
156 # Bail early for If-Modified-Since
157 2 my $since = $self->request->header( 'If-Modified-Since' );
158 2 ▶ if( $since ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($since) 1 1 2
159 1 $since = str2time( $since );
160 1 ▶ if ( $lastmod && $since && $since >= $lastmod ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastmod and $since and $since >= $lastmod) 1 0 1
Condition33%2/6
$lastmod and $since and $since >= $lastmod
!ll&&!rl&&r
001
$lastmod and $since
!ll&&!rl&&r
001
MC/DC0%0/3
$lastmod and $since and $since >= $lastmod
$lastmod $since $since >= $lastmod
Truth table25%1/4
A and B and C
A= $lastmodB= $sinceC= $since >= $lastmod
ABCresult
0XX0
10X0
1100
1111
161 1 $self->remove_header( 'Last-Modified' );
162 ## RFC 2616 section 4.3 says no content-length for 403
163 # $response->content_length( $size );
164 1 $self->code( RC_NOT_MODIFIED );
165 1 $self->respond;
166 1 $self->done;
167 1 return;
168 }
169 }
170  
171 1 $self->header( 'Content-Length' => $size );
172 1 $self->__fix_headers;
173  
174 1 $poe_kernel->post( @{ $self->{__sendfile} }, $path, $file, $size );
175 }
176  
177  
178 15 1;
179  
180 __END__
↑ Summary Server.pm »