-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatalink.conf
42 lines (39 loc) · 1.45 KB
/
datalink.conf
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
# Alias /datalink /var/www/datalink
PerlSetVar dl_dsn "dbi:Pg:service=pg_datalink"
<perl>
my $dirconfig = {
SetHandler => 'perl-script',
PerlHandler => 'Datalink',
AddDefaultCharset => 'UTF-8',
Options => [qw(-Indexes +FollowSymLinks)],
IndexOptions => [qw(+Charset=UTF-8)],
Require => [qw(all granted)]
};
push @Alias, ["/datalink", "/var/www/datalink"];
$Location{"/datalink"} = $dirconfig;
package Datalink;
use strict; use warnings;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::RequestUtil;
use Apache2::Const -compile => qw(OK FORBIDDEN DECLINED);
use APR::Finfo ();
use APR::Const -compile => qw(FINFO_NORM);
use JSON; use DBI;
sub handler {
my $r = shift; my $filename = $r->filename();
my $dbh = DBI->connect($r->dir_config("dl_dsn"),undef,undef,{RaiseError=>0});
my $sth = $dbh->prepare('select datalink.dl_authorize($1,1)');
$sth->execute($filename); # dl_authorize returns true filename if authorized
my $ref = $sth->fetchrow_arrayref;
$filename = $ref->[0];
if(defined($filename) && length($filename)>0) {
$r->filename($filename); # change filename to one without token
if(-f $filename) { # inform apache of the change
$r->finfo(APR::Finfo::stat($filename, APR::Const::FINFO_NORM, $r->pool)); }
return Apache2::Const::DECLINED; # go on to next stage to serve the file
} else { return Apache2::Const::FORBIDDEN; } # unauthorized
return Apache2::Const::DECLINED;
}
1;
</perl>