|
|
1 | use AnyEvent (); |
|
|
2 | |
1 | use Coro; |
3 | use Coro; |
2 | use Coro::Semaphore; |
4 | use Coro::Semaphore; |
|
|
5 | use Coro::SemaphoreSet; |
3 | use Coro::Event; |
6 | use Coro::EV; |
4 | use Coro::Socket; |
7 | use Coro::Socket; |
5 | use Coro::Signal; |
8 | use Coro::Signal; |
|
|
9 | use Coro::AIO (); |
6 | |
10 | |
|
|
11 | use Fcntl; |
7 | use HTTP::Date; |
12 | use HTTP::Date; |
8 | use POSIX (); |
13 | use POSIX (); |
9 | |
14 | |
10 | use Compress::Zlib (); |
15 | use Compress::Zlib (); |
11 | |
16 | |
12 | no utf8; |
17 | use common::sense; |
13 | use bytes; |
|
|
14 | |
18 | |
15 | # at least on my machine, this thingy serves files |
19 | # at least on my machine, this thingy serves files |
16 | # quite a bit faster than apache, ;) |
20 | # quite a bit faster than apache, ;) |
17 | # and quite a bit slower than thttpd :( |
21 | # and quite a bit slower than thttpd :( |
18 | |
22 | |
19 | $SIG{PIPE} = 'IGNORE'; |
23 | $SIG{PIPE} = 'IGNORE'; |
20 | |
24 | |
21 | our $accesslog; |
25 | our $accesslog; |
22 | our $errorlog; |
26 | our $errorlog; |
|
|
27 | our @listen_sockets; |
23 | |
28 | |
24 | our $NOW; |
29 | our $NOW; |
25 | our $HTTP_NOW; |
30 | our $HTTP_NOW; |
26 | |
31 | |
27 | Event->timer(interval => 1, hard => 1, cb => sub { |
32 | our $ERROR_LOG; |
|
|
33 | our $ACCESS_LOG; |
|
|
34 | our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip |
|
|
35 | |
|
|
36 | our $update_time = EV::periodic 0, 1, undef, sub { |
28 | $NOW = time; |
37 | $NOW = time; |
29 | $HTTP_NOW = time2str $NOW; |
38 | $HTTP_NOW = time2str $NOW; |
30 | })->now; |
39 | }; |
|
|
40 | $update_time->invoke; |
31 | |
41 | |
32 | if ($ERROR_LOG) { |
42 | if ($ERROR_LOG) { |
33 | use IO::Handle; |
43 | use IO::Handle; |
34 | open $errorlog, ">>$ERROR_LOG" |
44 | open $errorlog, ">>$ERROR_LOG" |
35 | or die "$ERROR_LOG: $!"; |
45 | or die "$ERROR_LOG: $!"; |
… | |
… | |
44 | } |
54 | } |
45 | |
55 | |
46 | sub slog { |
56 | sub slog { |
47 | my $level = shift; |
57 | my $level = shift; |
48 | my $format = shift; |
58 | my $format = shift; |
|
|
59 | |
|
|
60 | $format = sprintf $format, @_ if @_; |
|
|
61 | |
49 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
62 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
50 | printf "$NOW: $format\n", @_; |
63 | print "$NOW: $format\n"; |
51 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
64 | print $errorlog "$NOW: $format\n", @_ if $errorlog; |
52 | } |
65 | } |
53 | |
66 | |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
67 | our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250; |
55 | our $httpevent = new Coro::Signal; |
68 | our $httpevent = new Coro::Signal; |
56 | |
69 | |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
70 | our $queue_file = new transferqueue $::MAX_TRANSFERS; |
58 | our $queue_index = new transferqueue 10; |
71 | our $queue_index = new transferqueue 10; |
59 | |
72 | |
60 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
73 | our $tbf_top = new tbf rate => $::TBF_RATE || 100000; |
61 | |
74 | |
62 | my $unused_bytes = 0; |
75 | my $unused_bytes = 0; |
63 | my $unused_last = time; |
76 | my $unused_last = time; |
64 | |
77 | |
65 | sub unused_bandwidth { |
78 | sub unused_bandwidth { |
66 | $unused_bytes += $_[0]; |
79 | $unused_bytes += $_[0]; |
67 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
80 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
68 | $unused_last = $NOW; |
81 | $unused_last = $NOW; |
69 | $unused_bytes = 0; |
82 | $unused_bytes = 0; |
70 | $queue_file->force_wake_next; |
83 | $queue_file->force_wake_next |
71 | slog 1, "forced filetransfer due to unused bandwidth"; |
84 | and slog 1, "forced filetransfer due to unused bandwidth"; |
72 | } |
|
|
73 | } |
|
|
74 | |
|
|
75 | my @newcons; |
|
|
76 | my @pool; |
|
|
77 | |
|
|
78 | # one "execution thread" |
|
|
79 | sub handler { |
|
|
80 | while () { |
|
|
81 | if (@newcons) { |
|
|
82 | eval { |
|
|
83 | conn->new(@{pop @newcons})->handle; |
|
|
84 | }; |
|
|
85 | slog 1, "$@" if $@ && !ref $@; |
|
|
86 | |
|
|
87 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
88 | |
|
|
89 | $connections->up; |
|
|
90 | } else { |
|
|
91 | last if @pool >= $MAX_POOL; |
|
|
92 | push @pool, $Coro::current; |
|
|
93 | schedule; |
|
|
94 | } |
|
|
95 | } |
85 | } |
96 | } |
86 | } |
97 | |
87 | |
98 | sub listen_on { |
88 | sub listen_on { |
99 | my $listen = $_[0]; |
89 | my $listen = $_[0]; |
… | |
… | |
103 | # the "main thread" |
93 | # the "main thread" |
104 | async { |
94 | async { |
105 | slog 1, "accepting connections"; |
95 | slog 1, "accepting connections"; |
106 | while () { |
96 | while () { |
107 | $connections->down; |
97 | $connections->down; |
108 | push @newcons, [$listen->accept]; |
98 | my @conn = $listen->accept; |
109 | #slog 3, "accepted @$connections ".scalar(@pool); |
99 | #slog 3, "accepted @$connections ".scalar(@pool); |
110 | if (@pool) { |
100 | |
111 | (pop @pool)->ready; |
101 | async_pool { |
112 | } else { |
102 | eval { |
113 | async \&handler; |
103 | conn->new (@conn)->handle; |
|
|
104 | }; |
|
|
105 | slog 1, "$@" if $@ && !ref $@; |
|
|
106 | |
|
|
107 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
108 | |
|
|
109 | $connections->up; |
114 | } |
110 | } |
115 | } |
111 | } |
116 | }; |
112 | }; |
117 | } |
113 | } |
118 | |
114 | |
119 | my $http_port = new Coro::Socket |
115 | my $http_port = new Coro::Socket |
120 | LocalAddr => $SERVER_HOST, |
116 | LocalAddr => $::SERVER_HOST, |
121 | LocalPort => $SERVER_PORT, |
117 | LocalPort => $::SERVER_PORT, |
122 | ReuseAddr => 1, |
118 | ReuseAddr => 1, |
123 | Listen => 50, |
119 | Listen => 50, |
124 | or die "unable to start server"; |
120 | or die "unable to start server"; |
125 | |
121 | |
126 | listen_on $http_port; |
122 | listen_on $http_port; |
127 | |
123 | |
128 | if ($SERVER_PORT2) { |
124 | if ($::SERVER_PORT2) { |
129 | my $http_port = new Coro::Socket |
125 | my $http_port = new Coro::Socket |
130 | LocalAddr => $SERVER_HOST, |
126 | LocalAddr => $::SERVER_HOST, |
131 | LocalPort => $SERVER_PORT2, |
127 | LocalPort => $::SERVER_PORT2, |
132 | ReuseAddr => 1, |
128 | ReuseAddr => 1, |
133 | Listen => 50, |
129 | Listen => 50, |
134 | or die "unable to start server"; |
130 | or die "unable to start server"; |
135 | |
131 | |
136 | listen_on $http_port; |
132 | listen_on $http_port; |
137 | } |
133 | } |
138 | |
134 | |
139 | package conn; |
135 | package conn; |
|
|
136 | |
|
|
137 | use common::sense; |
140 | |
138 | |
141 | use Socket; |
139 | use Socket; |
142 | use HTTP::Date; |
140 | use HTTP::Date; |
143 | use Convert::Scalar 'weaken'; |
141 | use Convert::Scalar 'weaken'; |
144 | use Linux::AIO; |
142 | use IO::AIO; |
|
|
143 | use AnyEvent::AIO; |
145 | |
144 | |
146 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
145 | IO::AIO::min_parallel $::AIO_PARALLEL; |
147 | |
|
|
148 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
149 | poll => 'r', async => 1, |
|
|
150 | cb => \&Linux::AIO::poll_cb); |
|
|
151 | |
146 | |
152 | our %conn; # $conn{ip}{self} => connobj |
147 | our %conn; # $conn{ip}{self} => connobj |
153 | our %uri; # $uri{ip}{uri}{self} |
148 | our %uri; # $uri{ip}{uri}{self} |
154 | our %blocked; |
149 | our %blocked; |
155 | our %mimetype; |
150 | our %mimetype; |
156 | |
151 | |
157 | sub read_mimetypes { |
152 | sub read_mimetypes { |
158 | local *M; |
|
|
159 | if (open M, "<mime_types") { |
153 | if (open my $fh, "<mime_types") { |
160 | while (<M>) { |
154 | while (<$fh>) { |
161 | if (/^([^#]\S+)\t+(\S+)$/) { |
155 | if (/^([^#]\S+)\t+(\S+)$/) { |
162 | $mimetype{lc $1} = $2; |
156 | $mimetype{lc $1} = $2; |
163 | } |
157 | } |
164 | } |
158 | } |
165 | } else { |
159 | } else { |
… | |
… | |
173 | my $class = shift; |
167 | my $class = shift; |
174 | my $fh = shift; |
168 | my $fh = shift; |
175 | my $peername = shift; |
169 | my $peername = shift; |
176 | my $self = bless { fh => $fh }, $class; |
170 | my $self = bless { fh => $fh }, $class; |
177 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
171 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
178 | or $self->err(500, "unable to decode peername"); |
172 | or $self->err (500, "unable to decode peername"); |
179 | |
173 | |
180 | $self->{remote_addr} = |
174 | $self->{remote_addr} = |
181 | $self->{remote_id} = inet_ntoa $iaddr; |
175 | $self->{remote_id} = inet_ntoa $iaddr; |
182 | |
176 | |
183 | $self->{time} = $::NOW; |
177 | $self->{time} = $::NOW; |
184 | |
178 | |
185 | weaken ($Coro::current->{conn} = $self); |
179 | weaken ($Coro::current->{conn} = $self); |
186 | |
180 | |
187 | $::conns++; |
181 | ++$::conns; |
188 | $::maxconns = $::conns if $::conns > $::maxconns; |
182 | $::maxconns = $::conns if $::conns > $::maxconns; |
189 | |
183 | |
190 | $self; |
184 | $self |
191 | } |
185 | } |
192 | |
186 | |
193 | sub DESTROY { |
187 | sub DESTROY { |
194 | #my $self = shift; |
188 | my $self = shift; |
|
|
189 | |
195 | $::conns--; |
190 | --$::conns; |
196 | } |
191 | } |
197 | |
192 | |
198 | sub prune_cache { |
193 | sub prune_cache { |
199 | my $hash = $_[0]; |
194 | my $hash = $_[0]; |
200 | |
195 | |
201 | for (keys %$hash) { |
196 | for (keys %$hash) { |
202 | if (ref $hash->{$_} eq HASH::) { |
197 | if (ref $hash->{$_} eq HASH::) { |
203 | prune_cache($hash->{$_}); |
198 | prune_cache($hash->{$_}); |
204 | unless (scalar keys %{$hash->{$_}}) { |
199 | unless (scalar keys %{$hash->{$_}}) { |
205 | delete $hash->{$_}; |
200 | delete $hash->{$_}; |
206 | $d2++; |
|
|
207 | } |
201 | } |
208 | } |
202 | } |
209 | } |
203 | } |
210 | } |
204 | } |
211 | |
205 | |
212 | sub prune_caches { |
206 | sub prune_caches { |
213 | prune_cache \%conn; |
207 | prune_cache \%conn; |
214 | prune_cache \%uri; |
208 | prune_cache \%uri; |
215 | |
209 | |
216 | for (keys %blocked) { |
210 | for (keys %blocked) { |
217 | delete $blocked{$_} if $blocked{$_}[0] > $::NOW; |
211 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
218 | } |
212 | } |
219 | } |
213 | } |
220 | |
214 | |
221 | Event->timer(interval => 60, cb => \&prune_caches); |
215 | our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches; |
222 | |
216 | |
223 | sub slog { |
217 | sub slog { |
224 | my $self = shift; |
218 | my $self = shift; |
225 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
219 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
226 | } |
220 | } |
… | |
… | |
287 | $hdr->{"Content-Type"} = "text/plain"; |
281 | $hdr->{"Content-Type"} = "text/plain"; |
288 | $hdr->{"Content-Length"} = length $content; |
282 | $hdr->{"Content-Length"} = length $content; |
289 | } |
283 | } |
290 | $hdr->{"Connection"} = "close"; |
284 | $hdr->{"Connection"} = "close"; |
291 | |
285 | |
292 | $self->response($code, $msg, $hdr, $content); |
286 | $self->response ($code, $msg, $hdr, $content); |
293 | |
287 | |
294 | die bless {}, err::; |
288 | die bless {}, err:: |
295 | } |
289 | } |
296 | |
290 | |
297 | sub handle { |
291 | sub handle { |
298 | my $self = shift; |
292 | my $self = shift; |
299 | my $fh = $self->{fh}; |
293 | my $fh = $self->{fh}; |
300 | |
294 | |
301 | my $host; |
295 | my $host; |
302 | |
296 | |
303 | $fh->timeout($::REQ_TIMEOUT); |
297 | $fh->timeout($::REQ_TIMEOUT); |
304 | while() { |
298 | while () { |
305 | $self->{reqs}++; |
299 | $self->{reqs}++; |
306 | |
300 | |
307 | # read request and parse first line |
301 | # read request and parse first line |
308 | my $req = $fh->readline("\015\012\015\012"); |
302 | my $req = $fh->readline("\015\012\015\012"); |
309 | |
303 | |
… | |
… | |
418 | # uri => path mapping |
412 | # uri => path mapping |
419 | sub map_uri { |
413 | sub map_uri { |
420 | my $self = shift; |
414 | my $self = shift; |
421 | my $host = $self->{server_name}; |
415 | my $host = $self->{server_name}; |
422 | my $uri = $self->{uri}; |
416 | my $uri = $self->{uri}; |
|
|
417 | |
|
|
418 | $host =~ /[\/\\]/ |
|
|
419 | and $self->err(400, "bad request"); |
423 | |
420 | |
424 | # some massaging, also makes it more secure |
421 | # some massaging, also makes it more secure |
425 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
422 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
426 | $uri =~ s%//+%/%g; |
423 | $uri =~ s%//+%/%g; |
427 | $uri =~ s%/\.(?=/|$)%%g; |
424 | $uri =~ s%/\.(?=/|$)%%g; |
… | |
… | |
471 | |
468 | |
472 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
469 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
473 | if ($::internal{$1}) { |
470 | if ($::internal{$1}) { |
474 | $::internal{$1}->($self); |
471 | $::internal{$1}->($self); |
475 | } else { |
472 | } else { |
476 | $self->err(404, "not found"); |
473 | $self->err (404, "not found"); |
477 | } |
474 | } |
478 | } else { |
475 | } else { |
479 | |
476 | |
480 | stat $path |
477 | Coro::AIO::aio_stat $path |
481 | or $self->err(404, "not found"); |
478 | and $self->err (404, "not found"); |
482 | |
479 | |
483 | $self->{stat} = [stat _]; |
480 | $self->{stat} = [stat _]; |
484 | |
481 | |
485 | # idiotic netscape sends idiotic headers AGAIN |
482 | # idiotic netscape sends idiotic headers AGAIN |
486 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
483 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
489 | if (-d _ && -r _) { |
486 | if (-d _ && -r _) { |
490 | # directory |
487 | # directory |
491 | if ($path !~ /\/$/) { |
488 | if ($path !~ /\/$/) { |
492 | # create a redirect to get the trailing "/" |
489 | # create a redirect to get the trailing "/" |
493 | # we don't try to avoid the :80 |
490 | # we don't try to avoid the :80 |
494 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
491 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
495 | } else { |
492 | } else { |
496 | $ims < $self->{stat}[9] |
493 | $ims < $self->{stat}[9] |
497 | or $self->err(304, "not modified"); |
494 | or $self->err (304, "not modified"); |
498 | |
495 | |
499 | if (-r "$path/index.html") { |
496 | if (-r "$path/index.html") { |
500 | # replace directory "size" by index.html filesize |
497 | # replace directory "size" by index.html filesize |
501 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
498 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
502 | $self->handle_file($queue_index, $tbf_top); |
499 | $self->handle_file ($queue_index, $tbf_top); |
503 | } else { |
500 | } else { |
504 | $self->handle_dir; |
501 | $self->handle_dir; |
505 | } |
502 | } |
506 | } |
503 | } |
507 | } elsif (-f _ && -r _) { |
504 | } elsif (-f _ && -r _) { |
508 | -x _ and $self->err(403, "forbidden"); |
505 | -x _ and $self->err (403, "forbidden"); |
509 | |
506 | |
510 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
507 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
511 | my $timeout = $::NOW + 10; |
508 | my $timeout = $::NOW + 10; |
512 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
509 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
513 | if ($timeout < $::NOW) { |
510 | if ($timeout < $::NOW) { |
… | |
… | |
516 | $httpevent->wait; |
513 | $httpevent->wait; |
517 | } |
514 | } |
518 | } |
515 | } |
519 | } |
516 | } |
520 | |
517 | |
521 | $self->handle_file($queue_file, $tbf_top); |
518 | $self->handle_file ($queue_file, $tbf_top); |
522 | } else { |
519 | } else { |
523 | $self->err(404, "not found"); |
520 | $self->err (404, "not found"); |
524 | } |
521 | } |
525 | } |
522 | } |
526 | } |
523 | } |
527 | |
524 | |
528 | sub handle_dir { |
525 | sub handle_dir { |
529 | my $self = shift; |
526 | my $self = shift; |
530 | my $idx = $self->diridx; |
527 | my $idx = $self->diridx; |
531 | |
528 | |
532 | $self->response(200, "ok", |
529 | $self->response (200, "ok", |
533 | { |
530 | { |
534 | "Content-Type" => "text/html", |
531 | "Content-Type" => "text/html; charset=utf-8", |
535 | "Content-Length" => length $idx, |
532 | "Content-Length" => length $idx, |
536 | "Last-Modified" => time2str ($self->{stat}[9]), |
533 | "Last-Modified" => time2str ($self->{stat}[9]), |
537 | }, |
534 | }, |
538 | $idx); |
535 | $idx); |
539 | } |
536 | } |
540 | |
537 | |
541 | sub handle_file { |
538 | sub handle_file { |
542 | my ($self, $queue, $tbf) = @_; |
539 | my ($self, $queue, $tbf) = @_; |
543 | my $length = $self->{stat}[7]; |
540 | my $length = $self->{stat}[7]; |
544 | my $hdr = { |
541 | my $hdr = { |
545 | "Last-Modified" => time2str ((stat _)[9]), |
542 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
543 | "Accept-Ranges" => "bytes", |
546 | }; |
544 | }; |
547 | |
545 | |
548 | my @code = (200, "ok"); |
546 | my @code = (200, "ok"); |
549 | my ($l, $h); |
547 | my ($l, $h); |
550 | |
548 | |
551 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
549 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
552 | for (split /,/, $1) { |
550 | for (split /,/, $1) { |
553 | if (/^-(\d+)$/) { |
551 | if (/^-(\d+)$/) { |
554 | ($l, $h) = ($length - $1, $length - 1); |
552 | ($l, $h) = ($length - $1, $length - 1); |
555 | } elsif (/^(\d+)-(\d*)$/) { |
553 | } elsif (/^(\d+)-(\d*)$/) { |
556 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
554 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
560 | } |
558 | } |
561 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
559 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
562 | } |
560 | } |
563 | $hdr->{"Content-Range"} = "bytes */$length"; |
561 | $hdr->{"Content-Range"} = "bytes */$length"; |
564 | $hdr->{"Content-Length"} = $length; |
562 | $hdr->{"Content-Length"} = $length; |
565 | $self->err(416, "not satisfiable", $hdr, ""); |
563 | $self->err (416, "not satisfiable", $hdr, ""); |
566 | |
564 | |
567 | satisfiable: |
565 | satisfiable: |
568 | # check for segmented downloads |
566 | # check for segmented downloads |
569 | if ($l && $::NO_SEGMENTED) { |
567 | if ($l && $::NO_SEGMENTED) { |
570 | my $timeout = $::NOW + 15; |
568 | my $timeout = $::NOW + 60; |
571 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
569 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
572 | if ($timeout <= $::NOW) { |
570 | if ($timeout <= $::NOW) { |
573 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
|
|
574 | #$self->err_segmented_download; |
571 | $self->err_segmented_download; |
575 | } else { |
572 | } else { |
576 | $httpevent->wait; |
573 | $httpevent->wait; |
577 | } |
574 | } |
578 | } |
575 | } |
579 | } |
576 | } |
… | |
… | |
589 | |
586 | |
590 | $self->{path} =~ /\.([^.]+)$/; |
587 | $self->{path} =~ /\.([^.]+)$/; |
591 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
588 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
592 | $hdr->{"Content-Length"} = $length; |
589 | $hdr->{"Content-Length"} = $length; |
593 | |
590 | |
594 | $self->response(@code, $hdr, ""); |
591 | $self->response (@code, $hdr, ""); |
595 | |
592 | |
596 | if ($self->{method} eq "GET") { |
593 | if ($self->{method} eq "GET") { |
597 | $self->{time} = $::NOW; |
594 | $self->{time} = $::NOW; |
598 | $self->{written} = 0; |
595 | $self->{written} = 0; |
599 | |
596 | |
600 | my $current = $Coro::current; |
597 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
601 | |
|
|
602 | my ($fh, $buf, $r); |
|
|
603 | |
|
|
604 | open $fh, "<", $self->{path} |
|
|
605 | or die "$self->{path}: late open failure ($!)"; |
598 | or die "$self->{path}: late open failure ($!)"; |
606 | |
599 | |
607 | $h -= $l - 1; |
600 | $h -= $l - 1; |
608 | |
601 | |
609 | if (0) { # !AIO |
|
|
610 | if ($l) { |
|
|
611 | sysseek $fh, $l, 0; |
|
|
612 | } |
|
|
613 | } |
|
|
614 | |
|
|
615 | my $transfer = $queue->start_transfer($h); |
602 | my $transfer = $queue->start_transfer ($h); |
616 | my $locked; |
603 | my $locked; |
617 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
604 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
618 | |
605 | |
619 | while ($h > 0) { |
606 | while ($h > 0) { |
|
|
607 | Coro::cede; |
|
|
608 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
609 | |
620 | unless ($locked) { |
610 | unless ($locked) { |
621 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
611 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
622 | $bufsize = $::BUFSIZE; |
612 | $bufsize = $::BUFSIZE; |
623 | $self->{time} = $::NOW; |
613 | $self->{time} = $::NOW; |
624 | $self->{written} = 0; |
614 | $self->{written} = 0; |
625 | } |
615 | } |
626 | } |
616 | } |
… | |
… | |
628 | if ($blocked{$self->{remote_id}}) { |
618 | if ($blocked{$self->{remote_id}}) { |
629 | $self->{h}{connection} = "close"; |
619 | $self->{h}{connection} = "close"; |
630 | die bless {}, err::; |
620 | die bless {}, err::; |
631 | } |
621 | } |
632 | |
622 | |
633 | if (0) { # !AIO |
623 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
634 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
635 | or last; |
624 | or last; |
636 | } else { |
|
|
637 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
|
|
638 | $buf, 0, sub { |
|
|
639 | $r = $_[0]; |
|
|
640 | Coro::ready($current); |
|
|
641 | }); |
|
|
642 | &Coro::schedule; |
|
|
643 | last unless $r; |
|
|
644 | } |
|
|
645 | |
625 | |
|
|
626 | # readahead to work around rijk disk issues |
|
|
627 | IO::AIO::aio_readahead $fh, $l + $bufsize, $bufsize; |
|
|
628 | |
646 | $tbf->request(length $buf); |
629 | $tbf->request (length $buf); |
647 | my $w = syswrite $self->{fh}, $buf |
630 | my $w = $self->{fh}->syswrite ($buf) |
648 | or last; |
631 | or last; |
649 | $::written += $w; |
632 | $::written += $w; |
650 | $self->{written} += $w; |
633 | $self->{written} += $w; |
651 | $l += $r; |
634 | $l += $w; |
652 | } |
635 | } |
653 | |
636 | |
654 | close $fh; |
637 | close $fh; |
655 | } |
638 | } |
656 | } |
639 | } |
657 | |
640 | |
658 | 1; |
641 | 1 |
|
|
642 | |