/
Static.pm
103 lines (87 loc) · 2.14 KB
/
Static.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package Angelos::Middleware::Static;
use Angelos::Class;
extends 'Angelos::Middleware';
use Angelos::Exceptions;
use HTTP::Engine::Response;
use Angelos::MIMETypes;
use Path::Class;
use Angelos::Home;
has 'types' => (
is => 'rw',
default => sub {
Angelos::MIMETypes->new;
}
);
has 'prefix' => (
is => 'rw',
default => 'static',
);
sub wrap {
my ( $self, $next ) = @_;
sub {
my $req = shift;
my $path = $req->path;
if ( $self->is_static_file($path) ) {
my $full_path = Angelos::Home->path_to( 'share', 'root', $path );
return $self->serve_static($full_path);
}
else {
my $res = $next->($req);
return $res;
}
}
}
sub is_static_file {
my ( $self, $path ) = @_;
if ( my $prefix = $self->prefix ) {
return 0 unless $path =~ /^\/$prefix.*/;
}
my $ext = $self->_extract_extension($path);
my $type = $self->_extension_to_type($ext) || 'text/plain';
my $full_path = Angelos::Home->path_to( 'share', 'root', $path );
if ( -f $full_path ) {
return 1;
}
else {
return 0;
}
}
sub serve_static {
my $self = shift;
my $full_path = shift;
my $type = $self->_extension_to_type($full_path);
my $stat = $full_path->stat;
my $res = HTTP::Engine::Response->new;
$res->header( 'Content-Type' => $type );
$res->header( 'Content-Length' => $stat->size );
$res->header( 'Last-Modified' => $stat->mtime );
my $fh = $full_path->openr;
if ( defined $fh ) {
binmode $fh;
$res->code(200);
$res->body($fh);
}
else {
Angelos::Exception->throw(
message => "Unable to open $full_path for reading" );
}
$res;
}
sub _extract_extension {
my ($self, $path) = @_;
$path =~ /\.(\w+)$/;
my $ext = $1;
$ext;
}
sub _extension_to_type {
my ( $self, $full_path ) = @_;
my $type;
if ( $full_path =~ /.*\.(\S{1,})$/xms ) {
my $ext = $1;
$type = $self->types->mime_type_of($ext);
}
$type ||= 'text/plain';
$type;
}
__END_OF_CLASS__
__END__