| 1 | package HTTP::Server::EV::ServeStatic; | ||
| 2 | use utf8; | ||
| 3 | use strict; | ||
| 4 | use MIME::Types; | ||
| 5 | |||
| 6 | my $types = MIME::Types->new; | ||
| 7 | |||
| 8 | our $VERSION = '0.1'; | ||
| 9 | |||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | HTTP::Server::EV::ServeStatic - Simple static file server. | ||
| 14 | |||
| 15 | =head1 DESCRIPTION | ||
| 16 | |||
| 17 | Simple static file server for L<HTTP::Server::EV>. This module good for testing apps without frontend server. It's blocking and reads entire file into memory, so if you going to serve big files and need good performance - use frontend servers like nginx or lighttpd. | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | use HTTP::Server::EV; | ||
| 22 | use HTTP::Server::EV::ServeStatic; | ||
| 23 | |||
| 24 | my $static_server = HTTP::Server::EV::ServeStatic->new({ | ||
| 25 | basedir => './html', | ||
| 26 | indexes => [qw/index.html index.xhml default.html/], | ||
| 27 | forbidden_ext => [qw/pm pl php conf ini/], | ||
| 28 | rewrite_cb => sub { | ||
| 29 | local($_) = @_; | ||
| 30 | |||
| 31 | s/foo/bar/; # /path/foo/file.txt -> /path/bar/file.txt | ||
| 32 | |||
| 33 | return $_; | ||
| 34 | } | ||
| 35 | }); | ||
| 36 | # or defaults | ||
| 37 | my $static_server = HTTP::Server::EV::ServeStatic->new; | ||
| 38 | |||
| 39 | |||
| 40 | HTTP::Server::EV->new->listen( 8080 , sub{ | ||
| 41 | my ($cgi) = @_; | ||
| 42 | $static_server->serve($cgi); # try serve file. | ||
| 43 | |||
| 44 | # or give control to your app if file not found | ||
| 45 | |||
| 46 | $cgi->header({ STATUS => '404 Not Found' }); | ||
| 47 | $cgi->print('404 - File not found'); | ||
| 48 | }); | ||
| 49 | |||
| 50 | EV::run; | ||
| 51 | |||
| 52 | |||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 new( { options } ) | ||
| 57 | |||
| 58 | =over | ||
| 59 | |||
| 60 | =item basedir | ||
| 61 | |||
| 62 | Directory with files. Dafault './' | ||
| 63 | |||
| 64 | =item indexes | ||
| 65 | |||
| 66 | Files to search for if no file specified in url. Arrayref. Default [qw/index.html/] | ||
| 67 | |||
| 68 | =item forbidden_ext | ||
| 69 | |||
| 70 | Don't serve files with those extensions. Arrayref. Default [qw/pm pl/] | ||
| 71 | |||
| 72 | =item rewrite_cb | ||
| 73 | |||
| 74 | Callback for rewriting uri before processing request. Gets uri as first arg and need to return new uri. | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =head2 serve( $http_server_ev_cgi_obj ) | ||
| 79 | |||
| 80 | Process request | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | sub new { | ||
| 91 | my ($pkgname, $self) = @_; | ||
| 92 | |||
| 93 | $self->{basedir} //= '.'; | ||
| 94 | $self->{indexes} //= ['index.html']; | ||
| 95 | $self->{forbidden_ext} //= [qw/pm pl/]; | ||
| 96 | |||
| 97 | if($self->{forbidden_ext}){ | ||
| 98 | $self->{forbidden}{$_} = 1 for @{$self->{forbidden_ext}}; | ||
| 99 | } | ||
| 100 | |||
| 101 | bless $self, $pkgname; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub serve { # self, cgi_obj | ||
| 106 | my $path = $_[1]->{headers}{REQUEST_URI}; | ||
| 107 | |||
| 108 | $path = $_[0]->{rewrite_cb}->($path) if $_[0]->{rewrite_cb}; | ||
| 109 | |||
| 110 | $path =~s/\?.*$//g; | ||
| 111 | $path =~s/\.\.+//g; | ||
| 112 | $path =~s/[:<!>]//g; | ||
| 113 | |||
| 114 | $path = $_[0]->_find_real_path($path) or return; | ||
| 115 | |||
| 116 | $path =~m/\.([^.]*)$/; | ||
| 117 | return if $_[0]->{forbidden}{$1}; | ||
| 118 | my $ext = $1; | ||
| 119 | |||
| 120 | local( $/ ); # enable file slurp | ||
| 121 | open( my $fh, '<', $path ) or return; | ||
| 122 | binmode $fh; | ||
| 123 | |||
| 124 | $_[1]->header({ | ||
| 125 | 'Content-Type' => ( ($_ = $types->mimeTypeOf($ext)) ? $_->type : undef ) | ||
| 126 | }); | ||
| 127 | |||
| 128 | $_[1]->print(<$fh>); | ||
| 129 | |||
| 130 | close $fh; | ||
| 131 | |||
| 132 | $_[1]->next; | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | sub _find_real_path { | ||
| 138 | my ($self, $path) = @_; | ||
| 139 | |||
| 140 | if(-f ($self->{basedir}.$path) ){ | ||
| 141 | return $self->{basedir}.$path; | ||
| 142 | }elsif( $self->{indexes} ){ | ||
| 143 | for ( @{ $self->{indexes} }){ | ||
| 144 | return $self->{basedir}.$path.'/'.$_ if( -f $self->{basedir}.$path.'/'.$_); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } |