WWW/Handler/static.pm

filter: Statementstmt 45.1% 14 / 31 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 57.1% 4 / 7 Podpod 66.6% 2 / 3 totaltotal 27.4% 20 / 73 CC 10 SCAR 30.8
CC
10
Cov
26%
contentType
42.0
run
20.0
new
6.0
CRAP 21.8
« WWW/Handler.pm ↑ Summary WWW.pm »
1 ##-*- Mode: CPerl -*-
2  
3 ## File: DiaColloDB::WWW::Handler::static.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description:
6 ## + DiaColloDB::WWW::Server URI handler for static files
7 ## + adapted from DTA::CAB::Server::HTTP::Handler::File ( svn+ssh://odo.dwds.de/home/svn/dev/DTA-CAB/trunk/CAB/Server/HTTP/Handler/File.pm )
8 ##======================================================================
9  
10 package DiaColloDB::WWW::Handler::static;
11 1 ▶use DiaColloDB::WWW::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 our @ISA = qw(DiaColloDB::WWW::Handler);
17  
18 ##--------------------------------------------------------------
19 ## $h = $class_or_obj->new(%options)
20 ## + options:
21 ## contentType => $mimeType, ##-- override MIME type e.g. 'text/plain; charset=utf8'; default via MIME::Types::mimeTypeOf()
22 ## charset => $charset, ##-- default charset for text types (default='utf8')
23 ## file => $filename, ##-- filename to return
24 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
25 0 my $that = shift;
26 0 ▶ return bless { file=>'', contentType=>undef, charset=>'utf8', @_ }, ref($that)||$that;
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
27 }
28  
29 ## $rsp = $h->run($server, $clientConn, $httpRequest)
30 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
31 0 my ($h,$srv,$csock,$hreq) = @_;
32 0 ▶ return $h->error($csock,(-e $h->{file} ? RC_FORBIDDEN : RC_NOT_FOUND)) if (!-r $h->{file});
Branch: 2 branches
BranchTrueFalseTotal
unless -r $h->{"file"} 0 0 0
-e $h->{'file'} ? : 0 0 0
33  
34 0 open(my $fh, "<:raw", $h->{file});
35 0 ▶ return $h->error($csock,RC_NOT_FOUND) if (!defined($fh));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
36 0 local $/=undef;
37 0 my $data = <$fh>;
38 0 close($fh);
39 0 my $rsp = HTTP::Response->new(RC_OK, status_message(RC_OK), ['Content-Type' => $h->contentType($srv)]);
40 0 $rsp->content_ref(\$data);
41 0 return $rsp;
42 }
43  
44 ##--------------------------------------------------------------
45 ## local utilities
46  
47 ## $contentType = $h->contentType($srv)
48 ## $contentType = $h->contentType($srv,$file)
49 0 ▶sub contentType {
Subroutine contentType: not called CC=6 SCAR=37.4
Pod undocumented
50 0 my ($h,$srv,$file) = @_;
51 0 ▶ $file //= $h->{file};
Condition0%0/3
$file //= $h->{"file"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$file //= $h->{"file"}
$file $h->{"file"}
Truth table0%0/3
A //= B
A= $fileB= $h->{"file"}
ABresult
1X1
011
000
52 0 ▶ my $ctype = $h->{contentType} // $srv->mimetype($file);
Condition0%0/3
$h->{"contentType"} // $srv->mimetype($file)
l!l&&r!l&&!r
000
MC/DC0%0/2
$h->{"contentType"} // $srv->mimetype($file)
$h->{"contentType"} $srv->mimetype($file)
Truth table0%0/3
A // B
A= $h->{"contentType"}B= $srv->mimetype($file)
ABresult
1X1
011
000
53 0 ▶ $ctype .= "; charset=$h->{charset}" if ($ctype =~ /^text/ && $ctype !~ /\bcharset\b/ && defined($h->{charset}));
Branch: 1 branch
BranchTrueFalseTotal
if $ctype =~ /^text/ and not $ctype =~ /\bcharset\b/ and defined $h->{"charset"} 0 0 0
Condition0%0/6
$ctype =~ /^text/ and not $ctype =~ /\bcharset\b/ and defined $h->{"charset"}
!ll&&!rl&&r
000
$ctype =~ /^text/ and not $ctype =~ /\bcharset\b/
!ll&&!rl&&r
000
MC/DC0%0/3
$ctype =~ /^text/ and not $ctype =~ /\bcharset\b/ and defined $h->{"charset"}
$ctype =~ /^text/ not $ctype =~ /\bcharset\b/ defined $h->{"charset"}
Truth table0%0/4
A and not(B) and C
A= $ctype =~ /^text/B= not $ctype =~ /\bcharset\b/C= defined $h->{"charset"}
ABCresult
0XX0
10X0
1100
1111
54 0 return $ctype;
55 }
56  
57 1 1; ##-- be happy
58  
59 __END__
« WWW/Handler.pm ↑ Summary WWW.pm »