PITA/Guest/Server/HTTP.pm

filter: Statementstmt 88.1% 67 / 76 Branchbran 60.0% 12 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 33.3% 1 / 3 totaltotal 78.9% 90 / 114 CC 14 SCAR 16.6
CC
14
Cov
81%
_handler
9.6
new
4.3
handler
3.0
CRAP 5.2
« PITA/Guest/Driver.pm ↑ Summary PITA/Guest/Storage.pm »
1 package PITA::Guest::Server::HTTP;
2  
3 # The HTTP server component of the support server
4  
5 5 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use POE::Declare::HTTP::Server 0.05 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.60';
11 5 our @ISA = 'POE::Declare::HTTP::Server';
12  
13 5 ▶use POE::Declare {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 Mirrors => 'Param',
15 PingEvent => 'Message',
16 MirrorEvent => 'Message',
17 UploadEvent => 'Message',
18 5 };
19  
20  
21  
22  
23  
24 ######################################################################
25 # Constructor and Accessors
26  
27 5 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
28 my $self = shift->SUPER::new(
29 Mirrors => { },
30 @_,
31 9 ▶ Handler => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
32 # Convert to a more convention form
33 9 $_[0]->handler( $_[1]->request, $_[1] );
34 },
35 5 );
36  
37 # Check and normalize
38 5 ▶ unless ( Params::Util::_HASH0($self->Mirrors) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_HASH0(scalar $self->Mirrors)) 0 5 5
39 0 die "Missing or invalid Mirrors param";
40 }
41 5 foreach my $route ( sort keys %{$self->Mirrors} ) {
42 5 my $dir = File::Spec->rel2abs( $self->Mirrors->{$route} );
43 5 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 5 5
44 0 die "Directory '$dir' for mirror '$route' does not exist";
45 }
46 5 $self->Mirrors->{$route} = $dir;
47 }
48  
49 5 return $self;
50 }
51  
52  
53  
54  
55  
56 ######################################################################
57 # Main Methods
58  
59 # Sort of half-assed Process compatibility for testing purposes
60 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
61 0 $_[0]->start;
62 0 POE::Kernel->run;
63 0 return 1;
64 }
65  
66 # Wrapper for doing cleansing of the response
67 9 ▶sub handler {
Subroutine handler: called CC=3 SCAR=11.1
Pod undocumented
68 9 my $self = shift;
69 9 my $response = $_[1];
70  
71 # Call the main handler
72 9 $self->_handler(@_);
73  
74 # Add content length for all responses
75 9 ▶ if ( defined $response->content ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $response->content) 9 0 9
76 9 ▶ unless ( $response->header('Content-Length') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->header("Content-Length")) 9 0 9
77 9 my $bytes = length $response->content;
78 9 $response->header( 'Content-Length' => $bytes );
79 }
80 }
81  
82 9 return;
83 }
84  
85 9 ▶sub _handler {
Subroutine _handler: called CC=9 SCAR=22.6
86 9 my $self = shift;
87 9 my $request = shift;
88 9 my $response = shift;
89 9 my $path = $request->uri->path;
90  
91 9 ▶ if ( $request->method eq 'GET' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->method eq "GET") 6 3 9
92 # Handle a ping
93 6 ▶ if ( $path eq '/' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/") 4 2 6
94 4 $response->code(200);
95 4 $response->header( 'Content-Type' => 'text/plain' );
96 4 $response->content('200 - PONG');
97 4 $self->PingEvent;
98 4 return;
99 }
100  
101 # Handle a mirror file fetch
102 2 my $Mirrors = $self->Mirrors;
103 2 foreach my $route ( sort keys %$Mirrors ) {
104 2 my $escaped = quotemeta $route;
105 2 ▶ next unless $path =~ /^$escaped(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /^$escaped(.+)$/ 0 2 2
106 2 my $file = $1;
107 2 my $root = $Mirrors->{$route};
108 2 my $full = File::Spec->catfile( $root, $file );
109 2 ▶ if ( -f $full and -r _ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $full and -r _) { } 2 0 2
Condition33%1/3
-f $full and -r _
!ll&&!rl&&r
002
MC/DC0%0/2
-f $full and -r _
-f $full -r _
Truth table33%1/3
A and B
A= -f $fullB= -r _
ABresult
0X0
100
111
110 # Load the file
111 2 local $/ = undef;
112 2 ▶ my $io = IO::File->new($full, 'r') or die "open: $full";
Branch: 1 branch
BranchTrueFalseTotal
my $io = "IO::File"->new($full, "r") or die "open: $full" 2 0 2
113 2 $io->binmode;
114 2 my $blob = $io->getline;
115  
116 # Send the file
117 2 $response->code(200);
118 2 $response->header('Content-Type' => 'application/x-gzip');
119 2 $response->content($blob);
120 } else {
121 0 $response->code(404);
122 0 $response->header('Content-Type' => 'text/plain');
123 0 $response->content('404 - File Not Found');
124 }
125  
126 # Report the mirror event
127 2 $self->MirrorEvent( $route, $file, $response->code );
128  
129 2 return;
130 }
131 }
132  
133 3 ▶ if ( $request->method eq 'PUT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->method eq "PUT") 3 0 3
134 # Send the upload message
135 3 $self->UploadEvent( $path => \( $request->content ) );
136  
137 # Send a content-less ok to the client
138 3 $response->code(204);
139 3 $response->message('Upload received');
140  
141 3 return;
142 }
143  
144 0 return;
145 }
146  
147 5 compile;
« PITA/Guest/Driver.pm ↑ Summary PITA/Guest/Storage.pm »