ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.49 by root, Wed Nov 28 03:39:46 2001 UTC vs.
Revision 1.84 by root, Sat Dec 8 21:01:16 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines