1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::EV; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
|
|
6 | use Coro::AIO (); |
6 | |
7 | |
7 | use HTTP::Date; |
8 | use HTTP::Date; |
|
|
9 | use POSIX (); |
|
|
10 | |
|
|
11 | use Compress::Zlib (); |
8 | |
12 | |
9 | no utf8; |
13 | no utf8; |
10 | use bytes; |
14 | use bytes; |
11 | |
15 | |
12 | # at least on my machine, this thingy serves files |
16 | # at least on my machine, this thingy serves files |
… | |
… | |
14 | # and quite a bit slower than thttpd :( |
18 | # and quite a bit slower than thttpd :( |
15 | |
19 | |
16 | $SIG{PIPE} = 'IGNORE'; |
20 | $SIG{PIPE} = 'IGNORE'; |
17 | |
21 | |
18 | our $accesslog; |
22 | our $accesslog; |
|
|
23 | our $errorlog; |
|
|
24 | |
|
|
25 | our $NOW; |
|
|
26 | our $HTTP_NOW; |
|
|
27 | |
|
|
28 | our $ERROR_LOG; |
|
|
29 | our $ACCESS_LOG; |
|
|
30 | |
|
|
31 | our $update_time = EV::periodic 0, 1, undef, sub { |
|
|
32 | $NOW = time; |
|
|
33 | $HTTP_NOW = time2str $NOW; |
|
|
34 | }; |
|
|
35 | $update_time->invoke; |
|
|
36 | |
|
|
37 | if ($ERROR_LOG) { |
|
|
38 | use IO::Handle; |
|
|
39 | open $errorlog, ">>$ERROR_LOG" |
|
|
40 | or die "$ERROR_LOG: $!"; |
|
|
41 | $errorlog->autoflush(1); |
|
|
42 | } |
19 | |
43 | |
20 | if ($ACCESS_LOG) { |
44 | if ($ACCESS_LOG) { |
21 | use IO::Handle; |
45 | use IO::Handle; |
22 | open $accesslog, ">>$ACCESS_LOG" |
46 | open $accesslog, ">>$ACCESS_LOG" |
23 | or die "$ACCESS_LOG: $!"; |
47 | or die "$ACCESS_LOG: $!"; |
… | |
… | |
25 | } |
49 | } |
26 | |
50 | |
27 | sub slog { |
51 | sub slog { |
28 | my $level = shift; |
52 | my $level = shift; |
29 | my $format = shift; |
53 | my $format = shift; |
|
|
54 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
30 | printf "---: $format\n", @_; |
55 | printf "$NOW: $format\n", @_; |
|
|
56 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
31 | } |
57 | } |
32 | |
58 | |
33 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
59 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
34 | our $httpevent = new Coro::Signal; |
60 | our $httpevent = new Coro::Signal; |
35 | |
61 | |
36 | our $wait_factor = 0.95; |
62 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
|
|
63 | our $queue_index = new transferqueue 10; |
37 | |
64 | |
38 | our @transfers = ( |
65 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
39 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
|
|
40 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
|
|
41 | ); |
|
|
42 | |
66 | |
43 | my @newcons; |
67 | my $unused_bytes = 0; |
44 | my @pool; |
68 | my $unused_last = time; |
45 | |
69 | |
46 | # one "execution thread" |
70 | sub unused_bandwidth { |
47 | sub handler { |
71 | $unused_bytes += $_[0]; |
48 | while () { |
72 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
49 | if (@newcons) { |
73 | $unused_last = $NOW; |
50 | eval { |
74 | $unused_bytes = 0; |
51 | conn->new(@{pop @newcons})->handle; |
75 | $queue_file->force_wake_next; |
52 | }; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
53 | slog 1, "$@" if $@ && !ref $@; |
|
|
54 | $connections->up; |
|
|
55 | } else { |
|
|
56 | last if @pool >= $MAX_POOL; |
|
|
57 | push @pool, $Coro::current; |
|
|
58 | schedule; |
|
|
59 | } |
|
|
60 | } |
77 | } |
61 | } |
78 | } |
62 | |
79 | |
63 | sub listen_on { |
80 | sub listen_on { |
64 | my $listen = $_[0]; |
81 | my $listen = $_[0]; |
… | |
… | |
68 | # the "main thread" |
85 | # the "main thread" |
69 | async { |
86 | async { |
70 | slog 1, "accepting connections"; |
87 | slog 1, "accepting connections"; |
71 | while () { |
88 | while () { |
72 | $connections->down; |
89 | $connections->down; |
73 | push @newcons, [$listen->accept]; |
90 | my @conn = $listen->accept; |
74 | #slog 3, "accepted @$connections ".scalar(@pool); |
91 | #slog 3, "accepted @$connections ".scalar(@pool); |
75 | if (@pool) { |
92 | |
76 | (pop @pool)->ready; |
93 | async_pool { |
77 | } else { |
94 | eval { |
78 | async \&handler; |
95 | conn->new (@conn)->handle; |
79 | } |
96 | }; |
|
|
97 | slog 1, "$@" if $@ && !ref $@; |
80 | |
98 | |
|
|
99 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
100 | |
|
|
101 | $connections->up; |
|
|
102 | } |
81 | } |
103 | } |
82 | }; |
104 | }; |
83 | } |
105 | } |
84 | |
106 | |
85 | my $http_port = new Coro::Socket |
107 | my $http_port = new Coro::Socket |
86 | LocalAddr => $SERVER_HOST, |
108 | LocalAddr => $SERVER_HOST, |
87 | LocalPort => $SERVER_PORT, |
109 | LocalPort => $SERVER_PORT, |
88 | ReuseAddr => 1, |
110 | ReuseAddr => 1, |
89 | Listen => 50, |
111 | Listen => 50, |
90 | or die "unable to start server"; |
112 | or die "unable to start server"; |
91 | |
113 | |
92 | listen_on $http_port; |
114 | listen_on $http_port; |
93 | |
115 | |
94 | if ($SERVER_PORT2) { |
116 | if ($SERVER_PORT2) { |
95 | my $http_port = new Coro::Socket |
117 | my $http_port = new Coro::Socket |
96 | LocalAddr => $SERVER_HOST, |
118 | LocalAddr => $SERVER_HOST, |
97 | LocalPort => $SERVER_PORT2, |
119 | LocalPort => $SERVER_PORT2, |
98 | ReuseAddr => 1, |
120 | ReuseAddr => 1, |
99 | Listen => 50, |
121 | Listen => 50, |
100 | or die "unable to start server"; |
122 | or die "unable to start server"; |
101 | |
123 | |
102 | listen_on $http_port; |
124 | listen_on $http_port; |
103 | } |
125 | } |
104 | |
126 | |
105 | our $NOW; |
|
|
106 | our $HTTP_NOW; |
|
|
107 | |
|
|
108 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
109 | $NOW = time; |
|
|
110 | $HTTP_NOW = time2str $NOW; |
|
|
111 | })->now; |
|
|
112 | |
|
|
113 | package conn; |
127 | package conn; |
|
|
128 | |
|
|
129 | use strict; |
|
|
130 | use bytes; |
114 | |
131 | |
115 | use Socket; |
132 | use Socket; |
116 | use HTTP::Date; |
133 | use HTTP::Date; |
117 | use Convert::Scalar 'weaken'; |
134 | use Convert::Scalar 'weaken'; |
118 | use Linux::AIO; |
135 | use IO::AIO; |
119 | |
136 | |
120 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
137 | IO::AIO::min_parallel $::AIO_PARALLEL; |
121 | |
138 | |
122 | Event->io(fd => Linux::AIO::poll_fileno, |
139 | our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
123 | poll => 'r', async => 1, |
|
|
124 | cb => \&Linux::AIO::poll_cb); |
|
|
125 | |
140 | |
126 | our %conn; # $conn{ip}{self} => connobj |
141 | our %conn; # $conn{ip}{self} => connobj |
127 | our %uri; # $uri{ip}{uri}{self} |
142 | our %uri; # $uri{ip}{uri}{self} |
128 | our %blocked; |
143 | our %blocked; |
129 | our %mimetype; |
144 | our %mimetype; |
130 | |
145 | |
131 | sub read_mimetypes { |
146 | sub read_mimetypes { |
132 | local *M; |
|
|
133 | if (open M, "<mime_types") { |
147 | if (open my $fh, "<mime_types") { |
134 | while (<M>) { |
148 | while (<$fh>) { |
135 | if (/^([^#]\S+)\t+(\S+)$/) { |
149 | if (/^([^#]\S+)\t+(\S+)$/) { |
136 | $mimetype{lc $1} = $2; |
150 | $mimetype{lc $1} = $2; |
137 | } |
151 | } |
138 | } |
152 | } |
139 | } else { |
153 | } else { |
… | |
… | |
147 | my $class = shift; |
161 | my $class = shift; |
148 | my $fh = shift; |
162 | my $fh = shift; |
149 | my $peername = shift; |
163 | my $peername = shift; |
150 | my $self = bless { fh => $fh }, $class; |
164 | my $self = bless { fh => $fh }, $class; |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
165 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
152 | or $self->err(500, "unable to decode peername"); |
166 | or $self->err (500, "unable to decode peername"); |
153 | |
167 | |
|
|
168 | $self->{remote_addr} = |
154 | $self->{remote_addr} = inet_ntoa $iaddr; |
169 | $self->{remote_id} = inet_ntoa $iaddr; |
|
|
170 | |
155 | $self->{time} = $::NOW; |
171 | $self->{time} = $::NOW; |
156 | |
172 | |
|
|
173 | weaken ($Coro::current->{conn} = $self); |
|
|
174 | |
157 | $::conns++; |
175 | ++$::conns; |
|
|
176 | $::maxconns = $::conns if $::conns > $::maxconns; |
158 | |
177 | |
159 | $self; |
178 | $self |
160 | } |
179 | } |
161 | |
180 | |
162 | sub DESTROY { |
181 | sub DESTROY { |
163 | my $self = shift; |
182 | my $self = shift; |
|
|
183 | |
164 | $::conns--; |
184 | --$::conns; |
165 | $self->eoconn; |
|
|
166 | } |
185 | } |
167 | |
186 | |
168 | # end of connection |
187 | sub prune_cache { |
169 | sub eoconn { |
188 | my $hash = $_[0]; |
170 | my $self = shift; |
|
|
171 | |
189 | |
172 | # clean up hints |
190 | for (keys %$hash) { |
173 | delete $conn{$self->{remote_id}}{$self*1}; |
191 | if (ref $hash->{$_} eq HASH::) { |
174 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
192 | prune_cache($hash->{$_}); |
175 | |
193 | unless (scalar keys %{$hash->{$_}}) { |
176 | $httpevent->broadcast; |
194 | delete $hash->{$_}; |
|
|
195 | } |
|
|
196 | } |
|
|
197 | } |
177 | } |
198 | } |
|
|
199 | |
|
|
200 | sub prune_caches { |
|
|
201 | prune_cache \%conn; |
|
|
202 | prune_cache \%uri; |
|
|
203 | |
|
|
204 | for (keys %blocked) { |
|
|
205 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
206 | } |
|
|
207 | } |
|
|
208 | |
|
|
209 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
178 | |
210 | |
179 | sub slog { |
211 | sub slog { |
180 | my $self = shift; |
212 | my $self = shift; |
181 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
213 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
182 | } |
214 | } |
183 | |
215 | |
184 | sub response { |
216 | sub response { |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
217 | my ($self, $code, $msg, $hdr, $content) = @_; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
218 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
219 | my $GZ = ""; |
187 | |
220 | |
|
|
221 | if (exists $hdr->{Connection}) { |
|
|
222 | if ($hdr->{Connection} =~ /close/) { |
188 | $self->{h}{connection} = "close" |
223 | $self->{h}{connection} = "close" |
189 | if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification |
224 | } |
190 | and $hdr->{Connection} =~ /close/; |
225 | } else { |
|
|
226 | if ($self->{version} < 1.1) { |
|
|
227 | if ($self->{h}{connection} =~ /keep-alive/i) { |
|
|
228 | $hdr->{Connection} = "Keep-Alive"; |
|
|
229 | } else { |
|
|
230 | $self->{h}{connection} = "close" |
|
|
231 | } |
|
|
232 | } |
|
|
233 | } |
|
|
234 | |
|
|
235 | if ($self->{method} ne "HEAD" |
|
|
236 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
237 | && 400 < length $content |
|
|
238 | && $hdr->{"Content-Length"} == length $content |
|
|
239 | && !exists $hdr->{"Content-Encoding"} |
|
|
240 | ) { |
|
|
241 | my $orig = length $content; |
|
|
242 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
243 | $content = Compress::Zlib::memGzip(\$content); |
|
|
244 | $hdr->{"Content-Length"} = length $content; |
|
|
245 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
246 | } |
191 | |
247 | |
192 | $res .= "Date: $HTTP_NOW\015\012"; |
248 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
249 | $res .= "Server: $::NAME\015\012"; |
193 | |
250 | |
194 | while (my ($h, $v) = each %$hdr) { |
251 | while (my ($h, $v) = each %$hdr) { |
195 | $res .= "$h: $v\015\012" |
252 | $res .= "$h: $v\015\012" |
196 | } |
253 | } |
197 | $res .= "\015\012"; |
254 | $res .= "\015\012"; |
198 | |
255 | |
199 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
256 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
200 | |
257 | |
201 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
258 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
|
|
259 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
|
|
260 | " \"$self->{h}{referer}\"\n"; |
202 | |
261 | |
203 | print $accesslog $log if $accesslog; |
262 | print $::accesslog $log if $::accesslog; |
204 | print STDERR $log; |
263 | print STDERR $log; |
205 | |
264 | |
206 | $self->{written} += |
265 | $tbf_top->request(length $res, 1e6); |
207 | print {$self->{fh}} $res; |
266 | $self->{written} += print {$self->{fh}} $res; |
208 | } |
267 | } |
209 | |
268 | |
210 | sub err { |
269 | sub err { |
211 | my $self = shift; |
270 | my $self = shift; |
212 | my ($code, $msg, $hdr, $content) = @_; |
271 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
216 | $hdr->{"Content-Type"} = "text/plain"; |
275 | $hdr->{"Content-Type"} = "text/plain"; |
217 | $hdr->{"Content-Length"} = length $content; |
276 | $hdr->{"Content-Length"} = length $content; |
218 | } |
277 | } |
219 | $hdr->{"Connection"} = "close"; |
278 | $hdr->{"Connection"} = "close"; |
220 | |
279 | |
221 | $self->response($code, $msg, $hdr, $content); |
280 | $self->response ($code, $msg, $hdr, $content); |
222 | |
281 | |
223 | die bless {}, err::; |
282 | die bless {}, err:: |
224 | } |
283 | } |
225 | |
284 | |
226 | sub handle { |
285 | sub handle { |
227 | my $self = shift; |
286 | my $self = shift; |
228 | my $fh = $self->{fh}; |
287 | my $fh = $self->{fh}; |
229 | |
288 | |
230 | my $host; |
289 | my $host; |
231 | |
290 | |
232 | $fh->timeout($::REQ_TIMEOUT); |
291 | $fh->timeout($::REQ_TIMEOUT); |
233 | while() { |
292 | while () { |
234 | $self->{reqs}++; |
293 | $self->{reqs}++; |
235 | |
294 | |
236 | # read request and parse first line |
295 | # read request and parse first line |
237 | my $req = $fh->readline("\015\012\015\012"); |
296 | my $req = $fh->readline("\015\012\015\012"); |
238 | |
297 | |
… | |
… | |
267 | my (%hdr, $h, $v); |
326 | my (%hdr, $h, $v); |
268 | |
327 | |
269 | $hdr{lc $1} .= ",$2" |
328 | $hdr{lc $1} .= ",$2" |
270 | while $req =~ /\G |
329 | while $req =~ /\G |
271 | ([^:\000-\040]+): |
330 | ([^:\000-\040]+): |
272 | [\008\040]* |
331 | [\011\040]* |
273 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
332 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
274 | \015\012 |
333 | \015\012 |
275 | /gxc; |
334 | /gxc; |
276 | |
335 | |
277 | $req =~ /\G\015\012$/ |
336 | $req =~ /\G\015\012$/ |
278 | or $self->err(400, "bad request"); |
337 | or $self->err(400, "bad request"); |
… | |
… | |
290 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
349 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
291 | } |
350 | } |
292 | |
351 | |
293 | $self->{remote_id} = $id; |
352 | $self->{remote_id} = $id; |
294 | |
353 | |
|
|
354 | weaken (local $conn{$id}{$self*1} = $self); |
|
|
355 | |
295 | if ($blocked{$id}) { |
356 | if ($blocked{$id}) { |
296 | $self->err_blocked($blocked{$id}) |
357 | $self->err_blocked |
297 | if $blocked{$id} > $::NOW; |
358 | if $blocked{$id}[0] > $::NOW; |
298 | |
359 | |
299 | delete $blocked{$id}; |
360 | delete $blocked{$id}; |
300 | } |
|
|
301 | |
|
|
302 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
303 | my $delay = $::PER_TIMEOUT + $::NOW + 15; |
|
|
304 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
305 | if ($delay < $::NOW) { |
|
|
306 | $self->slog(2, "blocked ip $id"); |
|
|
307 | $self->err_blocked; |
|
|
308 | } else { |
|
|
309 | $httpevent->wait; |
|
|
310 | } |
|
|
311 | } |
|
|
312 | } |
361 | } |
313 | |
362 | |
314 | # find out server name and port |
363 | # find out server name and port |
315 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
364 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
316 | $host = $1; |
365 | $host = $1; |
… | |
… | |
327 | $host = inet_ntoa $host; |
376 | $host = inet_ntoa $host; |
328 | } |
377 | } |
329 | |
378 | |
330 | $self->{server_name} = $host; |
379 | $self->{server_name} = $host; |
331 | |
380 | |
332 | # enter ourselves into various lists |
|
|
333 | weaken ($conn{$id}{$self*1} = $self); |
|
|
334 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
381 | weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self); |
335 | |
382 | |
336 | eval { |
383 | eval { |
337 | $self->map_uri; |
384 | $self->map_uri; |
338 | $self->respond; |
385 | $self->respond; |
339 | }; |
386 | }; |
340 | |
387 | |
341 | $self->eoconn; |
|
|
342 | |
|
|
343 | die if $@ && !ref $@; |
388 | die if $@ && !ref $@; |
344 | |
389 | |
345 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
390 | last if $self->{h}{connection} =~ /close/i; |
346 | |
391 | |
347 | $httpevent->broadcast; |
392 | $httpevent->broadcast; |
348 | |
393 | |
349 | $fh->timeout($::PER_TIMEOUT); |
394 | $fh->timeout($::PER_TIMEOUT); |
350 | } |
395 | } |
|
|
396 | } |
|
|
397 | |
|
|
398 | sub block { |
|
|
399 | my $self = shift; |
|
|
400 | |
|
|
401 | $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]]; |
|
|
402 | $self->slog(2, "blocked ip $self->{remote_id}"); |
|
|
403 | $self->err_blocked; |
351 | } |
404 | } |
352 | |
405 | |
353 | # uri => path mapping |
406 | # uri => path mapping |
354 | sub map_uri { |
407 | sub map_uri { |
355 | my $self = shift; |
408 | my $self = shift; |
356 | my $host = $self->{server_name}; |
409 | my $host = $self->{server_name}; |
357 | my $uri = $self->{uri}; |
410 | my $uri = $self->{uri}; |
|
|
411 | |
|
|
412 | $host =~ /[\/\\]/ |
|
|
413 | and $self->err(400, "bad request"); |
358 | |
414 | |
359 | # some massaging, also makes it more secure |
415 | # some massaging, also makes it more secure |
360 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
416 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
361 | $uri =~ s%//+%/%g; |
417 | $uri =~ s%//+%/%g; |
362 | $uri =~ s%/\.(?=/|$)%%g; |
418 | $uri =~ s%/\.(?=/|$)%%g; |
… | |
… | |
402 | |
458 | |
403 | sub respond { |
459 | sub respond { |
404 | my $self = shift; |
460 | my $self = shift; |
405 | my $path = $self->{path}; |
461 | my $path = $self->{path}; |
406 | |
462 | |
407 | stat $path |
463 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
408 | or $self->err(404, "not found"); |
464 | if ($::internal{$1}) { |
409 | |
465 | $::internal{$1}->($self); |
410 | $self->{stat} = [stat _]; |
|
|
411 | |
|
|
412 | # idiotic netscape sends idiotic headers AGAIN |
|
|
413 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
414 | ? str2time $1 : 0; |
|
|
415 | |
|
|
416 | if (-d _ && -r _) { |
|
|
417 | # directory |
|
|
418 | if ($path !~ /\/$/) { |
|
|
419 | # create a redirect to get the trailing "/" |
|
|
420 | # we don't try to avoid the :80 |
|
|
421 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
|
|
422 | } else { |
466 | } else { |
423 | $ims < $self->{stat}[9] |
|
|
424 | or $self->err(304, "not modified"); |
467 | $self->err (404, "not found"); |
|
|
468 | } |
|
|
469 | } else { |
425 | |
470 | |
426 | if (-r "$path/index.html") { |
471 | Coro::AIO::aio_stat $path |
427 | # replace directory "size" by index.html filesize |
472 | and $self->err (404, "not found"); |
428 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
473 | |
429 | $self->handle_file; |
474 | $self->{stat} = [stat _]; |
|
|
475 | |
|
|
476 | # idiotic netscape sends idiotic headers AGAIN |
|
|
477 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
478 | ? str2time $1 : 0; |
|
|
479 | |
|
|
480 | if (-d _ && -r _) { |
|
|
481 | # directory |
|
|
482 | if ($path !~ /\/$/) { |
|
|
483 | # create a redirect to get the trailing "/" |
|
|
484 | # we don't try to avoid the :80 |
|
|
485 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
430 | } else { |
486 | } else { |
|
|
487 | $ims < $self->{stat}[9] |
|
|
488 | or $self->err (304, "not modified"); |
|
|
489 | |
|
|
490 | if (-r "$path/index.html") { |
|
|
491 | # replace directory "size" by index.html filesize |
|
|
492 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
|
|
493 | $self->handle_file ($queue_index, $tbf_top); |
|
|
494 | } else { |
431 | $self->handle_dir; |
495 | $self->handle_dir; |
432 | } |
496 | } |
433 | } |
497 | } |
434 | } elsif (-f _ && -r _) { |
498 | } elsif (-f _ && -r _) { |
435 | -x _ and $self->err(403, "forbidden"); |
499 | -x _ and $self->err (403, "forbidden"); |
436 | $self->handle_file; |
500 | |
|
|
501 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
502 | my $timeout = $::NOW + 10; |
|
|
503 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
504 | if ($timeout < $::NOW) { |
|
|
505 | $self->block($::BLOCKTIME, "too many connections"); |
|
|
506 | } else { |
|
|
507 | $httpevent->wait; |
|
|
508 | } |
|
|
509 | } |
|
|
510 | } |
|
|
511 | |
|
|
512 | $self->handle_file ($queue_file, $tbf_top); |
437 | } else { |
513 | } else { |
438 | $self->err(404, "not found"); |
514 | $self->err (404, "not found"); |
|
|
515 | } |
439 | } |
516 | } |
440 | } |
517 | } |
441 | |
518 | |
442 | sub handle_dir { |
519 | sub handle_dir { |
443 | my $self = shift; |
520 | my $self = shift; |
444 | my $idx = $self->diridx; |
521 | my $idx = $self->diridx; |
445 | |
522 | |
446 | $self->response(200, "ok", |
523 | $self->response (200, "ok", |
447 | { |
524 | { |
448 | "Content-Type" => "text/html", |
525 | "Content-Type" => "text/html; charset=utf-8", |
449 | "Content-Length" => length $idx, |
526 | "Content-Length" => length $idx, |
|
|
527 | "Last-Modified" => time2str ($self->{stat}[9]), |
450 | }, |
528 | }, |
451 | $idx); |
529 | $idx); |
452 | } |
530 | } |
453 | |
531 | |
454 | sub handle_file { |
532 | sub handle_file { |
455 | my $self = shift; |
533 | my ($self, $queue, $tbf) = @_; |
456 | my $length = $self->{stat}[7]; |
534 | my $length = $self->{stat}[7]; |
457 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
|
|
458 | my $hdr = { |
535 | my $hdr = { |
459 | "Last-Modified" => time2str ((stat _)[9]), |
536 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
537 | "Accept-Ranges" => "bytes", |
460 | }; |
538 | }; |
461 | |
539 | |
462 | my @code = (200, "ok"); |
540 | my @code = (200, "ok"); |
463 | my ($l, $h); |
541 | my ($l, $h); |
464 | |
542 | |
465 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
543 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
466 | for (split /,/, $1) { |
544 | for (split /,/, $1) { |
467 | if (/^-(\d+)$/) { |
545 | if (/^-(\d+)$/) { |
468 | ($l, $h) = ($length - $1, $length - 1); |
546 | ($l, $h) = ($length - $1, $length - 1); |
469 | } elsif (/^(\d+)-(\d*)$/) { |
547 | } elsif (/^(\d+)-(\d*)$/) { |
470 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
548 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
474 | } |
552 | } |
475 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
553 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
476 | } |
554 | } |
477 | $hdr->{"Content-Range"} = "bytes */$length"; |
555 | $hdr->{"Content-Range"} = "bytes */$length"; |
478 | $hdr->{"Content-Length"} = $length; |
556 | $hdr->{"Content-Length"} = $length; |
479 | $self->err(416, "not satisfiable", $hdr, ""); |
557 | $self->err (416, "not satisfiable", $hdr, ""); |
480 | |
558 | |
481 | satisfiable: |
559 | satisfiable: |
482 | # check for segmented downloads |
560 | # check for segmented downloads |
483 | if ($l && $::NO_SEGMENTED) { |
561 | if ($l && $::NO_SEGMENTED) { |
484 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
562 | my $timeout = $::NOW + 15; |
485 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
563 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
486 | if ($delay <= $::NOW) { |
564 | if ($timeout <= $::NOW) { |
|
|
565 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
487 | $self->err_segmented_download; |
566 | #$self->err_segmented_download; |
488 | } else { |
567 | } else { |
489 | $httpevent->broadcast; |
568 | $httpevent->wait; |
490 | } |
569 | } |
491 | } |
570 | } |
492 | } |
571 | } |
493 | |
572 | |
494 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
573 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
502 | |
581 | |
503 | $self->{path} =~ /\.([^.]+)$/; |
582 | $self->{path} =~ /\.([^.]+)$/; |
504 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
583 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
505 | $hdr->{"Content-Length"} = $length; |
584 | $hdr->{"Content-Length"} = $length; |
506 | |
585 | |
507 | $self->response(@code, $hdr, ""); |
586 | $self->response (@code, $hdr, ""); |
508 | |
587 | |
509 | if ($self->{method} eq "GET") { |
588 | if ($self->{method} eq "GET") { |
510 | $self->{time} = $::NOW; |
589 | $self->{time} = $::NOW; |
511 | |
|
|
512 | my $fudge = $queue->[0]->waiters; |
|
|
513 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
514 | |
|
|
515 | $queue->[1] *= $fudge; |
|
|
516 | my $transfer = $queue->[0]->guard; |
|
|
517 | |
|
|
518 | if ($fudge != 1) { |
|
|
519 | $queue->[1] /= $fudge; |
|
|
520 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
521 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
522 | } |
|
|
523 | $self->{time} = $::NOW; |
590 | $self->{written} = 0; |
524 | |
591 | |
525 | $self->{fh}->writable or return; |
|
|
526 | |
|
|
527 | my ($fh, $buf, $r); |
|
|
528 | my $current = $Coro::current; |
|
|
529 | open $fh, "<", $self->{path} |
592 | open my $fh, "<", $self->{path} |
530 | or die "$self->{path}: late open failure ($!)"; |
593 | or die "$self->{path}: late open failure ($!)"; |
531 | |
594 | |
532 | $h -= $l - 1; |
595 | $h -= $l - 1; |
533 | |
596 | |
534 | if (0) { |
597 | my $transfer = $queue->start_transfer ($h); |
535 | if ($l) { |
598 | my $locked; |
536 | sysseek $fh, $l, 0; |
599 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
537 | } |
|
|
538 | } |
|
|
539 | |
600 | |
540 | while ($h > 0) { |
601 | while ($h > 0) { |
541 | if (0) { |
602 | unless ($locked) { |
542 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
603 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
|
|
604 | $bufsize = $::BUFSIZE; |
|
|
605 | $self->{time} = $::NOW; |
|
|
606 | $self->{written} = 0; |
|
|
607 | } |
|
|
608 | } |
|
|
609 | |
|
|
610 | if ($blocked{$self->{remote_id}}) { |
|
|
611 | $self->{h}{connection} = "close"; |
|
|
612 | die bless {}, err::; |
|
|
613 | } |
|
|
614 | |
|
|
615 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
543 | or last; |
616 | or last; |
544 | } else { |
617 | |
545 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
618 | $tbf->request (length $buf); |
546 | $buf, 0, sub { |
619 | my $w = $self->{fh}->syswrite ($buf) |
547 | $r = $_[0]; |
|
|
548 | Coro::ready($current); |
|
|
549 | }); |
|
|
550 | &Coro::schedule; |
|
|
551 | last unless $r; |
|
|
552 | } |
|
|
553 | my $w = syswrite $self->{fh}, $buf |
|
|
554 | or last; |
620 | or last; |
555 | $::written += $w; |
621 | $::written += $w; |
556 | $self->{written} += $w; |
622 | $self->{written} += $w; |
557 | $l += $r; |
623 | $l += $w; |
558 | } |
624 | } |
559 | |
625 | |
560 | close $fh; |
626 | close $fh; |
561 | } |
627 | } |
562 | } |
628 | } |
563 | |
629 | |
564 | 1; |
630 | 1 |
|
|
631 | |