File Coverage

blib/lib/DiaColloDB/WWW/Handler/static.pm
Criterion Covered Total %
statement 12 29 41.3
branch 0 8 0.0
condition 0 15 0.0
subroutine 4 7 57.1
pod 2 3 66.6
total 18 62 29.0


line stmt bran cond sub pod time code
1             ##-*- Mode: CPerl -*-
2              
3             ## File: DiaColloDB::WWW::Handler::static.pm
4             ## Author: Bryan Jurish
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     1   7 use DiaColloDB::WWW::Handler;
  1         3  
  1         7  
12 1     1   38 use HTTP::Status;
  1         2  
  1         283  
13 1     1   11 use Carp;
  1         3  
  1         67  
14 1     1   6 use strict;
  1         2  
  1         268  
15              
16             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             sub new {
25 0     0 1   my $that = shift;
26 0   0       return bless { file=>'', contentType=>undef, charset=>'utf8', @_ }, ref($that)||$that;
27             }
28              
29             ## $rsp = $h->run($server, $clientConn, $httpRequest)
30             sub run {
31 0     0 1   my ($h,$srv,$csock,$hreq) = @_;
32 0 0         return $h->error($csock,(-e $h->{file} ? RC_FORBIDDEN : RC_NOT_FOUND)) if (!-r $h->{file});
    0          
33              
34 0           open(my $fh, "<:raw", $h->{file});
35 0 0         return $h->error($csock,RC_NOT_FOUND) if (!defined($fh));
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             sub contentType {
50 0     0 0   my ($h,$srv,$file) = @_;
51 0   0       $file //= $h->{file};
52 0   0       my $ctype = $h->{contentType} // $srv->mimetype($file);
53 0 0 0       $ctype .= "; charset=$h->{charset}" if ($ctype =~ /^text/ && $ctype !~ /\bcharset\b/ && defined($h->{charset}));
      0        
54 0           return $ctype;
55             }
56              
57             1; ##-- be happy
58              
59             __END__