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