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