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