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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines