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