-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend-uncompressed.cgi
executable file
·82 lines (73 loc) · 1.5 KB
/
send-uncompressed.cgi
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
#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use HTTP::Date;
use lib '/home/protected/lib';
use Gzip::Faster;
my %mimetypes = (
html => 'text/html',
js => 'application/javascript',
css => 'text/css',
);
my $query = $ENV{REDIRECT_QUERY_STRING};
if (! defined $query) {
print <<EOF;
Content-Type: text/plain
Status: 200
No request received.
EOF
}
else {
my $file = "$query.gz";
if (-f $file) {
send_file ($file);
}
else {
print <<EOF;
Status: 404
Requested file '$file' was not found on this server.
EOF
}
}
exit;
sub mtime
{
my ($file) = @_;
my @stat = stat ($file);
return $stat[9];
}
sub send_file
{
my ($file) = @_;
my $mtime = mtime ($file);
my $if_modified_since = $ENV{HTTP_IF_MODIFIED_SINCE};
if ($if_modified_since) {
my $if_modified_since_epoch = str2time ($if_modified_since);
if ($if_modified_since_epoch >= $mtime) {
# Print not modified header. This must contain a blank
# line after the header, regardless of not having any body
# content.
print <<EOF;
Status: 304
EOF
return;
}
}
my $mimetype = 'text/plain';
if ($file =~ /.*?\.(\w+)\.gz$/) {
my $ext = lc $1;
if ($mimetypes{$ext}) {
$mimetype = $mimetypes{$ext};
}
}
# Send the ungzipped file
my $file_contents = gunzip_file ($file);
my $http_date = time2str ($mtime);
print <<EOF;
Content-type: $mimetype; charset=UTF-8
Last-Modified: $http_date
X-Requester-No-Gzip: yes
$file_contents
EOF
return;
}