… | |
… | |
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 | |
… | |
… | |
424 | sub map_uri { |
407 | sub map_uri { |
425 | my $self = shift; |
408 | my $self = shift; |
426 | my $host = $self->{server_name}; |
409 | my $host = $self->{server_name}; |
427 | my $uri = $self->{uri}; |
410 | my $uri = $self->{uri}; |
428 | |
411 | |
|
|
412 | $host =~ /[\/\\]/ |
|
|
413 | and $self->err(400, "bad request"); |
|
|
414 | |
429 | # some massaging, also makes it more secure |
415 | # some massaging, also makes it more secure |
430 | $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; |
431 | $uri =~ s%//+%/%g; |
417 | $uri =~ s%//+%/%g; |
432 | $uri =~ s%/\.(?=/|$)%%g; |
418 | $uri =~ s%/\.(?=/|$)%%g; |
433 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
419 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
… | |
… | |
552 | }; |
538 | }; |
553 | |
539 | |
554 | my @code = (200, "ok"); |
540 | my @code = (200, "ok"); |
555 | my ($l, $h); |
541 | my ($l, $h); |
556 | |
542 | |
557 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
543 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
558 | for (split /,/, $1) { |
544 | for (split /,/, $1) { |
559 | if (/^-(\d+)$/) { |
545 | if (/^-(\d+)$/) { |
560 | ($l, $h) = ($length - $1, $length - 1); |
546 | ($l, $h) = ($length - $1, $length - 1); |
561 | } elsif (/^(\d+)-(\d*)$/) { |
547 | } elsif (/^(\d+)-(\d*)$/) { |
562 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
548 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
628 | |
614 | |
629 | 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 |
630 | or last; |
616 | or last; |
631 | |
617 | |
632 | $tbf->request (length $buf); |
618 | $tbf->request (length $buf); |
633 | my $w = syswrite $self->{fh}, $buf |
619 | my $w = $self->{fh}->syswrite ($buf) |
634 | or last; |
620 | or last; |
635 | $::written += $w; |
621 | $::written += $w; |
636 | $self->{written} += $w; |
622 | $self->{written} += $w; |
637 | $l += $w; |
623 | $l += $w; |
638 | } |
624 | } |