… | |
… | |
75 | $queue_file->force_wake_next; |
75 | $queue_file->force_wake_next; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
77 | } |
77 | } |
78 | } |
78 | } |
79 | |
79 | |
80 | my @newcons; |
|
|
81 | my @pool; |
|
|
82 | |
|
|
83 | # one "execution thread" |
|
|
84 | sub handler { |
|
|
85 | while () { |
|
|
86 | if (@newcons) { |
|
|
87 | eval { |
|
|
88 | conn->new (@{pop @newcons})->handle; |
|
|
89 | }; |
|
|
90 | slog 1, "$@" if $@ && !ref $@; |
|
|
91 | |
|
|
92 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
93 | |
|
|
94 | $connections->up; |
|
|
95 | } else { |
|
|
96 | last if @pool >= $MAX_POOL; |
|
|
97 | push @pool, $Coro::current; |
|
|
98 | schedule; |
|
|
99 | } |
|
|
100 | } |
|
|
101 | } |
|
|
102 | |
|
|
103 | sub listen_on { |
80 | sub listen_on { |
104 | my $listen = $_[0]; |
81 | my $listen = $_[0]; |
105 | |
82 | |
106 | push @listen_sockets, $listen; |
83 | push @listen_sockets, $listen; |
107 | |
84 | |
108 | # the "main thread" |
85 | # the "main thread" |
109 | async { |
86 | async { |
110 | slog 1, "accepting connections"; |
87 | slog 1, "accepting connections"; |
111 | while () { |
88 | while () { |
112 | $connections->down; |
89 | $connections->down; |
113 | push @newcons, [$listen->accept]; |
90 | my @conn = $listen->accept; |
114 | #slog 3, "accepted @$connections ".scalar(@pool); |
91 | #slog 3, "accepted @$connections ".scalar(@pool); |
115 | if (@pool) { |
92 | |
116 | (pop @pool)->ready; |
93 | async_pool { |
117 | } else { |
94 | eval { |
118 | 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; |
119 | } |
102 | } |
120 | } |
103 | } |
121 | }; |
104 | }; |
122 | } |
105 | } |
123 | |
106 | |
… | |
… | |
483 | } else { |
466 | } else { |
484 | $self->err (404, "not found"); |
467 | $self->err (404, "not found"); |
485 | } |
468 | } |
486 | } else { |
469 | } else { |
487 | |
470 | |
488 | stat $path |
471 | Coro::AIO::aio_stat $path |
489 | or $self->err (404, "not found"); |
472 | and $self->err (404, "not found"); |
490 | |
473 | |
491 | $self->{stat} = [stat _]; |
474 | $self->{stat} = [stat _]; |
492 | |
475 | |
493 | # idiotic netscape sends idiotic headers AGAIN |
476 | # idiotic netscape sends idiotic headers AGAIN |
494 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
477 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
555 | }; |
538 | }; |
556 | |
539 | |
557 | my @code = (200, "ok"); |
540 | my @code = (200, "ok"); |
558 | my ($l, $h); |
541 | my ($l, $h); |
559 | |
542 | |
560 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
543 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
561 | for (split /,/, $1) { |
544 | for (split /,/, $1) { |
562 | if (/^-(\d+)$/) { |
545 | if (/^-(\d+)$/) { |
563 | ($l, $h) = ($length - $1, $length - 1); |
546 | ($l, $h) = ($length - $1, $length - 1); |
564 | } elsif (/^(\d+)-(\d*)$/) { |
547 | } elsif (/^(\d+)-(\d*)$/) { |
565 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
548 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
631 | |
614 | |
632 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
615 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
633 | or last; |
616 | or last; |
634 | |
617 | |
635 | $tbf->request (length $buf); |
618 | $tbf->request (length $buf); |
636 | my $w = syswrite $self->{fh}, $buf |
619 | my $w = $self->{fh}->syswrite ($buf) |
637 | or last; |
620 | or last; |
638 | $::written += $w; |
621 | $::written += $w; |
639 | $self->{written} += $w; |
622 | $self->{written} += $w; |
640 | $l += $w; |
623 | $l += $w; |
641 | } |
624 | } |