ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/access.pl
Revision: 1.16
Committed: Fri Nov 30 05:11:23 2001 UTC (22 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.15: +13 -11 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.11 package transferqueue;
2    
3     sub new {
4     my $class = shift;
5     bless {
6     conns => $_[0],
7     }, $class;
8     }
9    
10     sub start_transfer {
11     my $self = shift;
12    
13 root 1.15 my $trans = bless [ $self ], transfer::;
14 root 1.11 Scalar::Util::weaken($trans->[0]);
15    
16     push @{$self->{wait}}, $trans;
17 root 1.13 Scalar::Util::weaken($self->{wait}[-1]);
18 root 1.11
19 root 1.16 --$self->{conns};
20     $self->wake_next;
21 root 1.11
22     $trans;
23     }
24    
25     sub wake_next {
26     my $self = shift;
27    
28 root 1.14 if ($self->{conns} >= 0) {
29     while(@{$self->{wait}}) {
30 root 1.13 my $transfer = shift @{$self->{wait}};
31     if ($transfer) {
32     $transfer->wake;
33     last;
34     }
35     }
36     }
37 root 1.11 }
38    
39     sub waiters {
40     map $_->[1], @{$_[0]{wait}};
41     }
42    
43     package transfer;
44    
45     use Coro::Timer ();
46    
47 root 1.16 sub wake {
48     my $self = shift;
49     $self->[2] = 1;
50     ref $self->[1] and $self->[1]->ready;
51     }
52    
53 root 1.11 sub try {
54     my $self = shift;
55    
56 root 1.15 unless ($self->[2]) {
57 root 1.16 my $timeout = Coro::Timer::timeout $_[0];
58     $self->[1] = $Coro::current;
59    
60 root 1.15 Coro::schedule;
61 root 1.16
62     undef $self->[1];
63 root 1.15 }
64 root 1.11
65     return $self->[2];
66     }
67    
68     sub DESTROY {
69     my $self = shift;
70     $self->[0]{conns}++;
71     $self->[0]->wake_next;
72     }
73    
74 root 1.8 package conn;
75 root 1.1
76 root 1.8 our %blockuri;
77     our $blockref;
78 root 1.1
79 root 1.6 sub read_blockuri {
80 root 1.1 local *B;
81     my %group;
82 root 1.8 %blockuri = ();
83 root 1.6 if (open B, "<blockuri") {
84 root 1.1 while (<B>) {
85     chomp;
86     if (/^group\s+(\S+)\s+(.*)/i) {
87     $group{$1} = [split /\s+/, $2];
88     } elsif (/^!([^\t]*)\t\s*(.*)/) {
89     my $g = $1;
90     my @r;
91     for (split /\s+/, $2) {
92     push @r, $group{$_} ? @{$group{$_}} : $_;
93     }
94     print "not($g) => (@r)\n";
95 root 1.8 push @{$blockuri{$_}}, $g for @r;
96 root 1.6 push @blockuri, [qr/$g/i, \@r];
97 root 1.1 } elsif (/\S/) {
98 root 1.6 print "blockuri: unparsable line: $_\n";
99 root 1.1 }
100     }
101 root 1.8 for (keys %blockuri) {
102     my $qr = join ")|(?:", @{$blockuri{$_}};
103     $blockuri{$_} = qr{(?:$qr)}i;
104     }
105 root 1.1 } else {
106 root 1.6 print "no blockuri\n";
107 root 1.1 }
108     }
109    
110 root 1.6 sub read_blockref {
111     local *B;
112 root 1.8 my @blockref;
113 root 1.6 if (open B, "<blockreferer") {
114     while (<B>) {
115     chomp;
116     if (/^([^\t]*)\t\s*(.*)/) {
117 root 1.8 push @blockref, $1;
118 root 1.6 } elsif (/\S/) {
119     print "blockref: unparsable line: $_\n";
120     }
121     }
122 root 1.8 $blockref = join ")|(?:", @blockref;
123     $blockref = qr{^(?:$blockref)}i;
124 root 1.6 } else {
125     print "no blockref\n";
126 root 1.8 $blockref = qr{^x^};
127 root 1.6 }
128     }
129    
130     read_blockuri;
131     read_blockref;
132 root 1.1
133 root 1.5 use Tie::Cache;
134 root 1.10 tie %whois_cache, Tie::Cache::, 32;
135 root 1.1
136 root 1.8 sub access_check {
137 root 1.6 my $self = shift;
138    
139     my $ref = $self->{h}{referer};
140     my $uri = $self->{path};
141     my %disallow;
142    
143 root 1.8 $self->err_block_referer
144     if $self->{h}{referer} =~ $blockref;
145 root 1.6
146     my $whois = $whois_cache{$self->{remote_addr}}
147 root 1.9 ||= netgeo::ip_request($self->{remote_addr});
148 root 1.6
149     my $country = "XX";
150    
151     if ($whois =~ /^\*cy: (\S+)/m) {
152     $country = uc $1;
153     } else {
154     $self->slog(9, "no country($whois)");
155     }
156    
157     $self->{country} = $country;
158    
159 root 1.8 $self->err_block_country($whois)
160     if $self->{path} =~ $blockuri{$country};
161 root 1.1 }
162    
163     1;