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