… | |
… | |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
6 | |
6 | |
7 | use HTTP::Date; |
7 | use HTTP::Date; |
|
|
8 | use POSIX (); |
8 | |
9 | |
9 | no utf8; |
10 | no utf8; |
10 | use bytes; |
11 | use bytes; |
11 | |
12 | |
12 | # at least on my machine, this thingy serves files |
13 | # at least on my machine, this thingy serves files |
… | |
… | |
34 | our $httpevent = new Coro::Signal; |
35 | our $httpevent = new Coro::Signal; |
35 | |
36 | |
36 | our $wait_factor = 0.95; |
37 | our $wait_factor = 0.95; |
37 | |
38 | |
38 | our @transfers = ( |
39 | our @transfers = ( |
39 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
40 | (new Coro::Semaphore $MAX_TRANSFERS_SMALL), |
40 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
41 | (new Coro::Semaphore $MAX_TRANSFERS_LARGE), |
41 | ); |
42 | ); |
42 | |
43 | |
43 | my @newcons; |
44 | my @newcons; |
44 | my @pool; |
45 | my @pool; |
45 | |
46 | |
… | |
… | |
149 | my $peername = shift; |
150 | my $peername = shift; |
150 | my $self = bless { fh => $fh }, $class; |
151 | my $self = bless { fh => $fh }, $class; |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
152 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
152 | or $self->err(500, "unable to decode peername"); |
153 | or $self->err(500, "unable to decode peername"); |
153 | |
154 | |
|
|
155 | $self->{remote_addr} = |
154 | $self->{remote_addr} = inet_ntoa $iaddr; |
156 | $self->{remote_id} = inet_ntoa $iaddr; |
155 | $self->{time} = $::NOW; |
157 | $self->{time} = $::NOW; |
|
|
158 | |
|
|
159 | weaken ($Coro::current->{conn} = $self); |
156 | |
160 | |
157 | $::conns++; |
161 | $::conns++; |
158 | |
162 | |
159 | $self; |
163 | $self; |
160 | } |
164 | } |
… | |
… | |
176 | $httpevent->broadcast; |
180 | $httpevent->broadcast; |
177 | } |
181 | } |
178 | |
182 | |
179 | sub slog { |
183 | sub slog { |
180 | my $self = shift; |
184 | my $self = shift; |
181 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
185 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
182 | } |
186 | } |
183 | |
187 | |
184 | sub response { |
188 | sub response { |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my ($self, $code, $msg, $hdr, $content) = @_; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
190 | my $res = "HTTP/1.1 $code $msg\015\012"; |
187 | |
191 | |
188 | $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; |
192 | if (exists $hdr->{Connection}) { |
|
|
193 | if ($hdr->{Connection} =~ /close/) { |
|
|
194 | $self->{h}{connection} = "close" |
|
|
195 | } |
|
|
196 | } else { |
|
|
197 | if ($self->{version} < 1.1) { |
|
|
198 | if ($self->{h}{connection} =~ /keep-alive/i) { |
|
|
199 | $hdr->{Connection} = "Keep-Alive"; |
|
|
200 | } else { |
|
|
201 | $self->{h}{connection} = "close" |
|
|
202 | } |
|
|
203 | } |
|
|
204 | } |
189 | |
205 | |
190 | $res .= "Date: $HTTP_NOW\015\012"; |
206 | $res .= "Date: $HTTP_NOW\015\012"; |
191 | |
207 | |
192 | while (my ($h, $v) = each %$hdr) { |
208 | while (my ($h, $v) = each %$hdr) { |
193 | $res .= "$h: $v\015\012" |
209 | $res .= "$h: $v\015\012" |
194 | } |
210 | } |
195 | $res .= "\015\012"; |
211 | $res .= "\015\012"; |
196 | |
212 | |
197 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
198 | |
214 | |
199 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
215 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
|
|
216 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
|
|
217 | " \"$self->{h}{referer}\"\n"; |
200 | |
218 | |
201 | print $accesslog $log if $accesslog; |
219 | print $accesslog $log if $accesslog; |
202 | print STDERR $log; |
220 | print STDERR $log; |
203 | |
221 | |
204 | $self->{written} += |
222 | $self->{written} += |
… | |
… | |
338 | |
356 | |
339 | $self->eoconn; |
357 | $self->eoconn; |
340 | |
358 | |
341 | die if $@ && !ref $@; |
359 | die if $@ && !ref $@; |
342 | |
360 | |
343 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
361 | last if $self->{h}{connection} =~ /close/; |
344 | |
362 | |
345 | $httpevent->broadcast; |
363 | $httpevent->broadcast; |
346 | |
364 | |
347 | $fh->timeout($::PER_TIMEOUT); |
365 | $fh->timeout($::PER_TIMEOUT); |
348 | } |
366 | } |
… | |
… | |
400 | |
418 | |
401 | sub respond { |
419 | sub respond { |
402 | my $self = shift; |
420 | my $self = shift; |
403 | my $path = $self->{path}; |
421 | my $path = $self->{path}; |
404 | |
422 | |
405 | stat $path |
423 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
406 | or $self->err(404, "not found"); |
424 | if ($::internal{$1}) { |
407 | |
425 | $::internal{$1}->($self); |
408 | $self->{stat} = [stat _]; |
|
|
409 | |
|
|
410 | # idiotic netscape sends idiotic headers AGAIN |
|
|
411 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
412 | ? str2time $1 : 0; |
|
|
413 | |
|
|
414 | if (-d _ && -r _) { |
|
|
415 | # directory |
|
|
416 | if ($path !~ /\/$/) { |
|
|
417 | # create a redirect to get the trailing "/" |
|
|
418 | # we don't try to avoid the :80 |
|
|
419 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
|
|
420 | } else { |
426 | } else { |
421 | $ims < $self->{stat}[9] |
427 | $self->err(404, "not found"); |
|
|
428 | } |
|
|
429 | } else { |
|
|
430 | |
|
|
431 | stat $path |
422 | or $self->err(304, "not modified"); |
432 | or $self->err(404, "not found"); |
423 | |
433 | |
424 | if (-r "$path/index.html") { |
434 | $self->{stat} = [stat _]; |
425 | $self->{path} .= "/index.html"; |
435 | |
426 | $self->handle_file; |
436 | # idiotic netscape sends idiotic headers AGAIN |
|
|
437 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
438 | ? str2time $1 : 0; |
|
|
439 | |
|
|
440 | if (-d _ && -r _) { |
|
|
441 | # directory |
|
|
442 | if ($path !~ /\/$/) { |
|
|
443 | # create a redirect to get the trailing "/" |
|
|
444 | # we don't try to avoid the :80 |
|
|
445 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
427 | } else { |
446 | } else { |
|
|
447 | $ims < $self->{stat}[9] |
|
|
448 | or $self->err(304, "not modified"); |
|
|
449 | |
|
|
450 | if (-r "$path/index.html") { |
|
|
451 | # replace directory "size" by index.html filesize |
|
|
452 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
|
|
453 | $self->handle_file; |
|
|
454 | } else { |
428 | $self->handle_dir; |
455 | $self->handle_dir; |
429 | } |
456 | } |
430 | } |
457 | } |
431 | } elsif (-f _ && -r _) { |
458 | } elsif (-f _ && -r _) { |
432 | -x _ and $self->err(403, "forbidden"); |
459 | -x _ and $self->err(403, "forbidden"); |
433 | $self->handle_file; |
460 | $self->handle_file; |
434 | } else { |
461 | } else { |
435 | $self->err(404, "not found"); |
462 | $self->err(404, "not found"); |
|
|
463 | } |
436 | } |
464 | } |
437 | } |
465 | } |
438 | |
466 | |
439 | sub handle_dir { |
467 | sub handle_dir { |
440 | my $self = shift; |
468 | my $self = shift; |
… | |
… | |
442 | |
470 | |
443 | $self->response(200, "ok", |
471 | $self->response(200, "ok", |
444 | { |
472 | { |
445 | "Content-Type" => "text/html", |
473 | "Content-Type" => "text/html", |
446 | "Content-Length" => length $idx, |
474 | "Content-Length" => length $idx, |
|
|
475 | #d# directories change all the time, so X- |
|
|
476 | "X-Last-Modified" => time2str ((stat _)[9]), |
447 | }, |
477 | }, |
448 | $idx); |
478 | $idx); |
449 | } |
479 | } |
450 | |
480 | |
451 | sub handle_file { |
481 | sub handle_file { |
… | |
… | |
481 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
511 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
482 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
512 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
483 | if ($delay <= $::NOW) { |
513 | if ($delay <= $::NOW) { |
484 | $self->err_segmented_download; |
514 | $self->err_segmented_download; |
485 | } else { |
515 | } else { |
486 | $httpevent->broadcast; |
516 | $httpevent->wait; |
487 | } |
517 | } |
488 | } |
518 | } |
489 | } |
519 | } |
490 | |
520 | |
491 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
521 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
504 | $self->response(@code, $hdr, ""); |
534 | $self->response(@code, $hdr, ""); |
505 | |
535 | |
506 | if ($self->{method} eq "GET") { |
536 | if ($self->{method} eq "GET") { |
507 | $self->{time} = $::NOW; |
537 | $self->{time} = $::NOW; |
508 | |
538 | |
509 | my $fudge = $queue->[0]->waiters; |
539 | my $current = $Coro::current; |
510 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
511 | |
|
|
512 | $queue->[1] *= $fudge; |
|
|
513 | my $transfer = $queue->[0]->guard; |
|
|
514 | |
|
|
515 | if ($fudge != 1) { |
|
|
516 | $queue->[1] /= $fudge; |
|
|
517 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
518 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
519 | } |
|
|
520 | $self->{time} = $::NOW; |
|
|
521 | |
|
|
522 | $self->{fh}->writable or return; |
|
|
523 | |
540 | |
524 | my ($fh, $buf, $r); |
541 | my ($fh, $buf, $r); |
525 | my $current = $Coro::current; |
542 | |
526 | open $fh, "<", $self->{path} |
543 | open $fh, "<", $self->{path} |
527 | or die "$self->{path}: late open failure ($!)"; |
544 | or die "$self->{path}: late open failure ($!)"; |
528 | |
545 | |
529 | $h -= $l - 1; |
546 | $h -= $l - 1; |
530 | |
547 | |
531 | if (0) { |
548 | if (0) { # !AIO |
532 | if ($l) { |
549 | if ($l) { |
533 | sysseek $fh, $l, 0; |
550 | sysseek $fh, $l, 0; |
534 | } |
551 | } |
535 | } |
552 | } |
|
|
553 | |
|
|
554 | my $transfer; # transfer guard |
|
|
555 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
|
|
556 | |
|
|
557 | $self->{time} = $::NOW; |
536 | |
558 | |
537 | while ($h > 0) { |
559 | while ($h > 0) { |
|
|
560 | unless ($transfer) { |
|
|
561 | if ($transfer ||= $queue->timed_guard($::WAIT_INTERVAL)) { |
|
|
562 | $bufsize = $::BUFSIZE; |
|
|
563 | $self->{time} = $::NOW; |
|
|
564 | } |
|
|
565 | } |
|
|
566 | |
538 | if (0) { |
567 | if (0) { # !AIO |
539 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
568 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
540 | or last; |
569 | or last; |
541 | } else { |
570 | } else { |
542 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
571 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
543 | $buf, 0, sub { |
572 | $buf, 0, sub { |
544 | $r = $_[0]; |
573 | $r = $_[0]; |
545 | Coro::ready($current); |
574 | Coro::ready($current); |
546 | }); |
575 | }); |
547 | &Coro::schedule; |
576 | &Coro::schedule; |