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