| 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
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table0%0/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | open(my $fh, "<:raw", $h->{file}); | |||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | return $h->error($csock,RC_NOT_FOUND) if (!defined($fh)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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"}
MC/DC0%0/2
$file //= $h->{"file"}
$file
$h->{"file"}
Truth table0%0/3
A //= B
A= $fileB= $h->{"file"}
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my $ctype = $h->{contentType} // $srv->mimetype($file); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$h->{"contentType"} // $srv->mimetype($file)
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $ctype .= "; charset=$h->{charset}" if ($ctype =~ /^text/ && $ctype !~ /\bcharset\b/ && defined($h->{charset})); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$ctype =~ /^text/ and not $ctype =~ /\bcharset\b/ and defined $h->{"charset"}
$ctype =~ /^text/ and not $ctype =~ /\bcharset\b/
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $ctype; | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | 1; ##-- be happy | |||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | __END__ | ||||||||||||||||||||||||||||||||||||||||||