| 1 | ##-*- Mode: CPerl -*- | ||
| 2 | |||
| 3 | ## File: DTA::CAB::Server::HTTP::Handler::Directory.pm | ||
| 4 | ## Author: Bryan Jurish <moocow@cpan.org> | ||
| 5 | ## Description: | ||
| 6 | ## + CAB HTTP Server: request handler: directory | ||
| 7 | ##====================================================================== | ||
| 8 | |||
| 9 | package DTA::CAB::Server::HTTP::Handler::Directory; | ||
| 10 | use DTA::CAB::Server::HTTP::Handler; | ||
| 11 | use HTTP::Status; | ||
| 12 | use Carp; | ||
| 13 | use strict; | ||
| 14 | |||
| 15 | our @ISA = qw(DTA::CAB::Server::HTTP::Handler); | ||
| 16 | |||
| 17 | ##-------------------------------------------------------------- | ||
| 18 | ## Aliases | ||
| 19 | BEGIN { | ||
| 20 | DTA::CAB::Server::HTTP::Handler->registerAlias( | ||
| 21 | 'DTA::CAB::Server::HTTP::Handler::directory'=>__PACKAGE__, | ||
| 22 | 'DTA::CAB::Server::HTTP::Handler::dir'=>__PACKAGE__, | ||
| 23 | 'DTA::CAB::Server::HTTP::Handler::Dir'=>__PACKAGE__, | ||
| 24 | 'directory' => __PACKAGE__, | ||
| 25 | 'dir' => __PACKAGE__, | ||
| 26 | 'Dir' => __PACKAGE__, | ||
| 27 | ); | ||
| 28 | } | ||
| 29 | |||
| 30 | ##-------------------------------------------------------------- | ||
| 31 | ## Methods | ||
| 32 | |||
| 33 | ## $h = $class_or_obj->new(%options) | ||
| 34 | ## + options: | ||
| 35 | ## dir => $baseDirectory, ##-- default='.' | ||
| 36 | ## logLevel => $level, ##-- debug log level (defaul=undef (none)) | ||
| 37 | sub new { | ||
| 38 | my $that = shift; | ||
| 39 | my $h = bless { dir=>'.', @_ }, ref($that)||$that; | ||
| 40 | $h->{dir} =~ s|/$||g; | ||
| 41 | return $h; | ||
| 42 | } | ||
| 43 | |||
| 44 | ## $bool = $h->prepare($server) | ||
| 45 | sub prepare { return (-d $_[0]{dir} && -r $_[0]{dir}); } | ||
| 46 | |||
| 47 | ## $rsp = $h->run($server, $localPath, $clientConn, $httpRequest) | ||
| 48 | sub run { | ||
| 49 | my ($h,$srv,$path,$csock,$hreq) = @_; | ||
| 50 | my $path_matched = $path; | ||
| 51 | my $path_full = $hreq->uri->path(); | ||
| 52 | my $file = $path_full; | ||
| 53 | $file =~ s/^\Q$path_matched\E\/?//; | ||
| 54 | $file = $h->{dir}.'/'.$file; | ||
| 55 | $h->vlog($h->{logLevel}, "run(", $csock->peerhost, "): file=$file"); | ||
| 56 | return $h->error($csock,(-e $file ? RC_FORBIDDEN : RC_NOT_FOUND)) if (!-r $file); | ||
| 57 | $csock->send_file_response($file); | ||
| 58 | $csock->shutdown(2); | ||
| 59 | $csock->close(); | ||
| 60 | return undef; | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; ##-- be happy | ||
| 64 | |||
| 65 | __END__ |