| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 21 | my $req = $self->request; | |||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 21 | ▶ | unless( $self->protocol ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 19 | $self->protocol( $req->protocol ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 21 | ▶ | unless( $self->header('Date') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 19 | $self->header( 'Date', time2str(time) ); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | ▶ | if( not defined $self->header( 'Content-Length' ) and | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $self->header("Content-Length") || $self->streaming and $req->method ne "HEAD"
defined $self->header('Content-Length') || $self->streaming
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $c and $c ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | $self->request->uri->path : basename $file; | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | ▶ | unless( -f $file ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | $ct ||= 'application/octet-stream'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ct ||= "application/octet-stream"
MC/DC0%0/1
$ct ||= "application/octet-stream"
$ct
Truth table0%0/2
A
A= $ct
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->request->method eq "HEAD" and $size
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | $since = str2time( $since ); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | if ( $lastmod && $since && $since >= $lastmod ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$lastmod and $since and $since >= $lastmod
$lastmod and $since
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||