line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyrights 2013-2020 by [Mark Overmeer]. |
2
|
|
|
|
|
|
|
# For other contributors see ChangeLog. |
3
|
|
|
|
|
|
|
# See the manual pages for details on the licensing terms. |
4
|
|
|
|
|
|
|
# Pod stripped from pm file by OODoc 2.02. |
5
|
|
|
|
|
|
|
# This code is part of distribution Any-Daemon-HTTP. Meta-POD processed |
6
|
|
|
|
|
|
|
# with OODoc into POD and HTML manual-pages. See README.md |
7
|
|
|
|
|
|
|
# Copyright Mark Overmeer. Licensed under the same terms as Perl itself. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
package Any::Daemon::HTTP::Source; |
10
|
2
|
|
|
2
|
|
732
|
use vars '$VERSION'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
79
|
|
11
|
|
|
|
|
|
|
$VERSION = '0.30'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
56
|
|
15
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
33
|
|
16
|
|
|
|
|
|
|
|
17
|
2
|
|
|
2
|
|
9
|
use Log::Report 'any-daemon-http'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
10
|
|
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
1332
|
use Net::CIDR qw/cidrlookup/; |
|
2
|
|
|
|
|
9514
|
|
|
2
|
|
|
|
|
116
|
|
20
|
2
|
|
|
2
|
|
15
|
use List::Util qw/first/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
113
|
|
21
|
2
|
|
|
2
|
|
807
|
use HTTP::Status qw/HTTP_FORBIDDEN/; |
|
2
|
|
|
|
|
7654
|
|
|
2
|
|
|
|
|
1106
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub _allow_cleanup($); |
24
|
|
|
|
|
|
|
sub _allow_match($$$$); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub new(@) |
28
|
0
|
|
|
0
|
1
|
|
{ my $class = shift; |
29
|
0
|
0
|
|
|
|
|
my $args = @_==1 ? shift : +{@_}; |
30
|
0
|
|
|
|
|
|
(bless {}, $class)->init($args); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub init($) |
34
|
0
|
|
|
0
|
0
|
|
{ my ($self, $args) = @_; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
0
|
|
|
|
my $path = $self->{ADHS_path} = $args->{path} || '/'; |
37
|
0
|
|
|
|
|
|
$self->{ADHS_allow} = _allow_cleanup $args->{allow}; |
38
|
0
|
|
|
|
|
|
$self->{ADHS_deny} = _allow_cleanup $args->{deny}; |
39
|
0
|
|
0
|
|
|
|
$self->{ADHS_name} = $args->{name} || $path; |
40
|
0
|
|
|
|
|
|
$self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
#----------------- |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
0
|
1
|
|
sub path() {shift->{ADHS_path}} |
46
|
0
|
|
|
0
|
1
|
|
sub name() {shift->{ADHS_name}} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
#----------------- |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub allow($$$$) |
51
|
0
|
|
|
0
|
1
|
|
{ my ($self, $session, $req, $uri) = @_; |
52
|
0
|
0
|
|
|
|
|
if(my $allow = $self->{ADHS_allow}) |
53
|
0
|
0
|
|
|
|
|
{ $self->_allow_match($session, $uri, $allow) or return 0; |
54
|
|
|
|
|
|
|
} |
55
|
0
|
0
|
|
|
|
|
if(my $deny = $self->{ADHS_deny}) |
56
|
0
|
0
|
|
|
|
|
{ $self->_allow_match($session, $uri, $deny) and return 0; |
57
|
|
|
|
|
|
|
} |
58
|
0
|
|
|
|
|
|
1; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub _allow_match($$$$) |
62
|
0
|
|
|
0
|
|
|
{ my ($self, $session, $uri, $rules) = @_; |
63
|
0
|
|
|
|
|
|
my $peer = $session->get('peer'); |
64
|
0
|
|
|
0
|
|
|
first { $_->($peer->{ip}, $peer->{host}, $session, $uri) } @$rules; |
|
0
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _allow_cleanup($) |
68
|
0
|
0
|
|
0
|
|
|
{ my $p = shift or return; |
69
|
0
|
|
|
|
|
|
my @p; |
70
|
0
|
0
|
|
|
|
|
foreach my $r (ref $p eq 'ARRAY' ? @$p : $p) |
71
|
|
|
|
|
|
|
{ push @p |
72
|
|
|
|
|
|
|
, ref $r eq 'CODE' ? $r |
73
|
0
|
|
|
0
|
|
|
: index($r, ':') >= 0 ? sub {cidrlookup $_[0], $r} # IPv6 |
74
|
0
|
|
|
0
|
|
|
: $r !~ m/[a-zA-Z]/ ? sub {cidrlookup $_[0], $r} # IPv4 |
75
|
0
|
|
|
0
|
|
|
: substr($r,0,1) eq '.' ? sub {$_[1] =~ qr/(^|\.)\Q$r\E$/i} # Domain |
76
|
0
|
|
|
0
|
|
|
: sub {lc($_[1]) eq lc($r)} # hostname |
77
|
0
|
0
|
|
|
|
|
} |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
78
|
0
|
0
|
|
|
|
|
@p ? \@p : undef; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub collect($$$$) |
83
|
0
|
|
|
0
|
1
|
|
{ my ($self, $vhost, $session, $req, $uri) = @_; |
84
|
|
|
|
|
|
|
|
85
|
0
|
0
|
|
|
|
|
$self->allow($session, $req, $uri) |
86
|
|
|
|
|
|
|
or return HTTP::Response->new(HTTP_FORBIDDEN); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
$self->_collect($vhost, $session, $req, $uri); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
0
|
|
|
sub _collect($$$) { panic "must be extended" } |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
#----------------------- |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
#----------------------- |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
1; |