| 1 | ##-*- Mode: CPerl -*- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | ## File: DiaColloDB::WWW::Handler.pm | ||||||||||||||||||||||||||||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||||||||||||||||||||||||||||
| 5 | ## Description: | ||||||||||||||||||||||||||||
| 6 | ## + abstract handler API class for DiaColloDB::WWW::Server | ||||||||||||||||||||||||||||
| 7 | ## + adapted from DTA::CAB::Server::HTTP::Handler ( svn+ssh://odo.dwds.de/home/svn/dev/DTA-CAB/trunk/CAB/Server/HTTP/Handler.pm ) | ||||||||||||||||||||||||||||
| 8 | ##====================================================================== | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | package DiaColloDB::WWW::Handler; | ||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use HTTP::Status; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use DiaColloDB::Logger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use UNIVERSAL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | *isa = \&UNIVERSAL::isa; | |||||||||||||||||||||||||||
| 18 | 1 | *can = \&UNIVERSAL::can; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | our @ISA = qw(DiaColloDB::Logger); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | ##====================================================================== | ||||||||||||||||||||||||||||
| 24 | ## API | ||||||||||||||||||||||||||||
| 25 | ##====================================================================== | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ## $h = $class_or_obj->new(%options) | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 0 | my $that = shift; | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | return bless { @_ }, 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
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | ## $bool = $h->prepare($server,$path) | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub prepare { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
prepare: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | ## $rsp = $h->run($server, $clientConn, $httpRequest) | ||||||||||||||||||||||||||||
| 37 | ## + perform local processing | ||||||||||||||||||||||||||||
| 38 | ## + should return a HTTP::Response object to pass to the client | ||||||||||||||||||||||||||||
| 39 | ## + if the call die()s or returns undef, an error response will be | ||||||||||||||||||||||||||||
| 40 | ## sent to the client instead if it the connection is still open | ||||||||||||||||||||||||||||
| 41 | ## + this method may return the data to the client itself; if so, | ||||||||||||||||||||||||||||
| 42 | ## it should close the client connection ($csock->shutdown(2); $csock->close()) | ||||||||||||||||||||||||||||
| 43 | ## and return undef to prevent bogus error messages. | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my ($h,$srv,$csock,$hreq) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | $h->logdie("run() method not implemented"); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | ## undef = $h->finish($server, $clientConn) | ||||||||||||||||||||||||||||
| 50 | ## + clean up handler state after run() | ||||||||||||||||||||||||||||
| 51 | ## + default implementation does nothing | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | return; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ##====================================================================== | ||||||||||||||||||||||||||||
| 57 | ## Generic Utilities | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | ## $rsp = $h->headResponse() | ||||||||||||||||||||||||||||
| 60 | ## $rsp = $h->headResponse(\@headers) | ||||||||||||||||||||||||||||
| 61 | ## $rsp = $h->headResponse($httpHeaders) | ||||||||||||||||||||||||||||
| 62 | ## + rudimentary handling for HEAD requests | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | sub headResponse { | ||||||||||||||||||||||||||
|
Subroutine
headResponse: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 0 | my ($h,$hdr) = @_; | |||||||||||||||||||||||||||
| 65 | 0 | return $h->response(RC_OK,undef,$hdr); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | ## $rsp = $CLASS_OR_OBJECT->response($code=RC_OK, $msg=status_message($code), $hdr, $content) | ||||||||||||||||||||||||||||
| 69 | ## + $hdr may be a HTTP::Headers object, an array or hash-ref | ||||||||||||||||||||||||||||
| 70 | ## + wrapper for HTTP::Response->new() | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | sub response { | ||||||||||||||||||||||||||
|
Subroutine
response: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 0 | my $h = shift; | |||||||||||||||||||||||||||
| 73 | 0 | my $code = shift; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | $code = RC_OK if (!defined($code)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ## | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $msg = @_ ? shift : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $msg = status_message($code) if (!defined($msg)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | ## | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $hdr = @_ ? shift : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | $hdr = [] if (!$hdr); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | ## | ||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return HTTP::Response->new($code,$msg,$hdr) if (!@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | return HTTP::Response->new($code,$msg,$hdr,@_); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | ## undef = $h->cerror($csock, $status=RC_INTERNAL_SERVER_ERROR, @msg) | ||||||||||||||||||||||||||||
| 87 | ## + sends an error response and sends it to the client socket | ||||||||||||||||||||||||||||
| 88 | ## + also logs the error at level ($c->{logError}||'warn') and shuts down the socket | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub cerror { | ||||||||||||||||||||||||||
|
Subroutine
cerror: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my ($h,$c,$status,@msg) = @_; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (defined($c) && $c->opened) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $c and $c->opened
MC/DC0%0/2
defined $c and $c->opened
defined $c
$c->opened
Truth table0%0/3
A and B
A= defined $cB= $c->opened
| |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $status = RC_INTERNAL_SERVER_ERROR if (!defined($status)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | my $chost = $c->peerhost(); | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | my $msg = @msg ? join('',@msg) : status_message($status); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | ▶ | $h->vlog(($h->{logError}||'error'), "client=$chost: $msg"); | ||||||||||||||||||||||||||
|
Condition0%0/2
$h->{'logError'} || 'error'
MC/DC0%0/1
$h->{'logError'} || 'error'
$h->{'logError'}
Truth table0%0/2
A
A= $h->{'logError'}
| |||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | 0 | my $_warn=$^W; | |||||||||||||||||||||||||||
| 98 | 0 | $^W=0; | |||||||||||||||||||||||||||
| 99 | 0 | $c->send_error($status, $msg); | |||||||||||||||||||||||||||
| 100 | 0 | $^W=$_warn; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | $c->shutdown(2); | |||||||||||||||||||||||||||
| 103 | 0 | $c->close(); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | return undef; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | ## $rsp = $h->dumpResponse(\$contentRef, %opts) | ||||||||||||||||||||||||||||
| 109 | ## + Create and return a new data-dump response. | ||||||||||||||||||||||||||||
| 110 | ## Known %opts: | ||||||||||||||||||||||||||||
| 111 | ## ( | ||||||||||||||||||||||||||||
| 112 | ## raw => $bool, ##-- return raw data (text/plain) ; defualt=$h->{returnRaw} | ||||||||||||||||||||||||||||
| 113 | ## type => $mimetype, ##-- mime type if not raw mode | ||||||||||||||||||||||||||||
| 114 | ## charset => $enc, ##-- character set, if not raw mode | ||||||||||||||||||||||||||||
| 115 | ## filename => $file, ##-- attachment name, if not raw mode | ||||||||||||||||||||||||||||
| 116 | ## ) | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | sub dumpResponse { | ||||||||||||||||||||||||||
|
Subroutine
dumpResponse: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | my ($h,$dataref,%vars) = @_; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | my $returnRaw = defined($vars{raw}) ? $vars{raw} : $h->{returnRaw}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my $contentType = ($returnRaw || !$vars{type} ? 'text/plain' : $vars{type}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$returnRaw or not $vars{"type"}
MC/DC0%0/2
$returnRaw or not $vars{"type"}
$returnRaw
not $vars{"type"}
Truth table0%0/3
A or not(B)
A= $returnRawB= not $vars{"type"}
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $contentType .= "; charset=$vars{charset}" if ($vars{charset} && $contentType !~ m|application/octet-stream|); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$vars{"charset"} and not $contentType =~ m[application/octet-stream]
MC/DC0%0/2
$vars{"charset"} and not $contentType =~ m[application/octet-stream]
$vars{"charset"}
not $contentType =~ m[application/octet-stream]
Truth table0%0/3
A and not(B)
A= $vars{"charset"}B= not $contentType =~ m[application/octet-stream]
| |||||||||||||||||||||||||||||
| 122 | ## | ||||||||||||||||||||||||||||
| 123 | 0 | my $rsp = $h->response(RC_OK); | |||||||||||||||||||||||||||
| 124 | 0 | $rsp->content_type($contentType); | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | $rsp->content_ref($dataref) if (defined($dataref)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | $rsp->header('Content-Disposition' => "attachment; filename=\"$vars{filename}\"") if ($vars{filename} && !$returnRaw); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$vars{"filename"} and not $returnRaw
MC/DC0%0/2
$vars{"filename"} and not $returnRaw
$vars{"filename"}
not $returnRaw
Truth table0%0/3
A and not(B)
A= $vars{"filename"}B= not $returnRaw
| |||||||||||||||||||||||||||||
| 127 | 0 | return $rsp; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | 1; ##-- be happy | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | __END__ | ||||||||||||||||||||||||||||