-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.psgi
More file actions
64 lines (57 loc) · 1.55 KB
/
Copy pathapp.psgi
File metadata and controls
64 lines (57 loc) · 1.55 KB
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
#!/usr/bin/perl
use strict;
use warnings;
use Plack::Builder;
use Archive::Zip;
use Data::Dumper;
use List::Util qw(first);
use Plack::MIME;
sub not_found { [404,['Content-type'=>'text/plain'],["notfound"]]; }
my $app = sub {
my $env = shift;
my @pathparts = split '/', $env->{SCRIPT_FILENAME};
my @zipparts;
my $zipfile = join("/",@pathparts);
while( @pathparts && not ($zipfile =~ m/\.zip/ && -e $zipfile) ) {
unshift @zipparts, pop @pathparts;
$zipfile = join("/", @pathparts);
}
if($zipfile) {
my $zip = Archive::Zip->new($zipfile);
if($zip) {
my $membername = join("/",@zipparts);
if(@zipparts) {
my $member = $zip->memberNamed($membername);
if($member) {
return [200,
[
'Content-type'=>Plack::MIME->mime_type($member->fileName),
'Content-length'=>$member->uncompressedSize(),
],[scalar($member->contents)]];
} else {
print STDERR "WARN: $membername not found in $zipfile\n";
return not_found();
}
} else {
return [200,
[
'Content-type'=>'text/html',
],
[ '<html><body><ul>',
(map { sprintf('<li><a href="%s">%s (%i bytes)</a>', $_->fileName, $_->fileName, $_->uncompressedSize()) } $zip->members),
'</ul></body></html>'
]
];
}
} else {
print STDERR "ERROR: $env->{SCRIPT_FILENAME} exists but is broken?\n";
return [500,['Content-type'=>'text/plain'],['Error opening archive']];
}
} else {
print STDERR "WARN: $env->{SCRIPT_FILENAME} does not exist\n";
return not_found();
}
};
builder {
$app;
};