ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/access.pl
Revision: 1.20
Committed: Mon Dec 3 05:40:09 2001 UTC (22 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.19: +5 -2 lines
Log Message:
*** empty log message ***

File Contents

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