| 1 | ##-*- Mode: CPerl -*- | ||
| 2 | |||
| 3 | ## File: DTA::CAB::Server::HTTP::Handler::File.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: | ||
| 6 | ## + DTA::CAB::Server::HTTP::Handler class: static files | ||
| 7 | ##====================================================================== | ||
| 8 | |||
| 9 | package DTA::CAB::Server::HTTP::Handler::File; | ||
| 10 | use DTA::CAB::Server::HTTP::Handler; | ||
| 11 | use HTTP::Status; | ||
| 12 | use HTTP::Date qw(); | ||
| 13 | use IO::File; | ||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | our @ISA = qw(DTA::CAB::Server::HTTP::Handler); | ||
| 18 | |||
| 19 | ##-------------------------------------------------------------- | ||
| 20 | ## Aliases | ||
| 21 | BEGIN { | ||
| 22 | DTA::CAB::Server::HTTP::Handler->registerAlias( | ||
| 23 | 'DTA::CAB::Server::Server::HTTP::Handler::file' => __PACKAGE__, | ||
| 24 | 'file' => __PACKAGE__, | ||
| 25 | ); | ||
| 26 | } | ||
| 27 | |||
| 28 | ##-------------------------------------------------------------- | ||
| 29 | ## $h = $class_or_obj->new(%options) | ||
| 30 | ## + options: | ||
| 31 | ## contentType => $mimeType, ##-- default: text/plain ; alt. e.g. 'text/plain; charset="UTF-8"' | ||
| 32 | ## file => $filename, ##-- filename to return | ||
| 33 | sub new { | ||
| 34 | my $that = shift; | ||
| 35 | return bless { file=>'', contentType=>undef, @_ }, ref($that)||$that; | ||
| 36 | } | ||
| 37 | |||
| 38 | ## $bool = $obj->prepare($srv) | ||
| 39 | sub prepare { return (-r $_[0]{file}); } | ||
| 40 | |||
| 41 | ## $rsp = $h->run($server, $localPath, $clientConn, $httpRequest) | ||
| 42 | sub run { | ||
| 43 | my ($h,$srv,$path,$csock,$hreq) = @_; | ||
| 44 | return $h->error($csock,(-e $h->{file} ? RC_FORBIDDEN : RC_NOT_FOUND)) if (!-r $h->{file}); | ||
| 45 | |||
| 46 | if (!$h->{contentType}) { | ||
| 47 | $csock->send_file_response($h->{file}); | ||
| 48 | $csock->shutdown(2); | ||
| 49 | $csock->close; | ||
| 50 | return undef; | ||
| 51 | } | ||
| 52 | elsif (0) { | ||
| 53 | ##-- weirdness | ||
| 54 | my $CRLF = "\015\012"; ##-- "\r\n" is not portable | ||
| 55 | my $rsp = HTTP::Response->new(RC_OK, status_message(RC_OK)); | ||
| 56 | my ($size,$mtime) = (stat $h->{file})[7,9]; | ||
| 57 | $rsp->header('Content-Type' => $h->{contentType}) if ($h->{contentType}); | ||
| 58 | $rsp->header('Content-Length' => $size); | ||
| 59 | $rsp->header('Last-Modified' => HTTP::Date::time2str($mtime)); | ||
| 60 | $csock->send_basic_header; | ||
| 61 | $csock->print($rsp->headers->as_string($CRLF), $CRLF); | ||
| 62 | $csock->send_file($h->{file}); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | my $ioh = IO::File->new("<$h->{file}"); | ||
| 66 | return $h->error($csock,RC_NOT_FOUND) if (!defined($ioh)); | ||
| 67 | my $data = join('', $ioh->getlines); | ||
| 68 | return HTTP::Response->new(RC_OK, status_message(RC_OK), | ||
| 69 | [ | ||
| 70 | 'Content-Type' => $h->{contentType}, | ||
| 71 | ], | ||
| 72 | $data); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | 1; ##-- be happy | ||
| 78 | |||
| 79 | __END__ |