… | |
… | |
55 | our $httpevent = new Coro::Signal; |
55 | our $httpevent = new Coro::Signal; |
56 | |
56 | |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
58 | our $queue_index = new transferqueue 10; |
58 | our $queue_index = new transferqueue 10; |
59 | |
59 | |
60 | our $tbf_top = new tbf rate => 200000; |
60 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
|
|
61 | |
|
|
62 | my $unused_bytes = 0; |
|
|
63 | my $unused_last = time; |
|
|
64 | |
|
|
65 | sub unused_bandwidth { |
|
|
66 | $unused_bytes += $_[0]; |
|
|
67 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
|
|
68 | $unused_last = $NOW; |
|
|
69 | $unused_bytes = 0; |
|
|
70 | $queue_file->force_wake_next; |
|
|
71 | slog 1, "forced filetransfer due to unused bandwidth"; |
|
|
72 | } |
|
|
73 | } |
61 | |
74 | |
62 | my @newcons; |
75 | my @newcons; |
63 | my @pool; |
76 | my @pool; |
64 | |
77 | |
65 | # one "execution thread" |
78 | # one "execution thread" |
… | |
… | |
97 | if (@pool) { |
110 | if (@pool) { |
98 | (pop @pool)->ready; |
111 | (pop @pool)->ready; |
99 | } else { |
112 | } else { |
100 | async \&handler; |
113 | async \&handler; |
101 | } |
114 | } |
102 | |
|
|
103 | } |
115 | } |
104 | }; |
116 | }; |
105 | } |
117 | } |
106 | |
118 | |
107 | my $http_port = new Coro::Socket |
119 | my $http_port = new Coro::Socket |
… | |
… | |
127 | package conn; |
139 | package conn; |
128 | |
140 | |
129 | use Socket; |
141 | use Socket; |
130 | use HTTP::Date; |
142 | use HTTP::Date; |
131 | use Convert::Scalar 'weaken'; |
143 | use Convert::Scalar 'weaken'; |
132 | use Linux::AIO; |
144 | use IO::AIO; |
133 | |
145 | |
134 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
146 | IO::AIO::min_parallel $::AIO_PARALLEL; |
135 | |
147 | |
136 | Event->io(fd => Linux::AIO::poll_fileno, |
148 | Event->io(fd => IO::AIO::poll_fileno, |
137 | poll => 'r', async => 1, |
149 | poll => 'r', async => 1, |
138 | cb => \&Linux::AIO::poll_cb); |
150 | cb => \&IO::AIO::poll_cb); |
139 | |
151 | |
140 | our %conn; # $conn{ip}{self} => connobj |
152 | our %conn; # $conn{ip}{self} => connobj |
141 | our %uri; # $uri{ip}{uri}{self} |
153 | our %uri; # $uri{ip}{uri}{self} |
142 | our %blocked; |
154 | our %blocked; |
143 | our %mimetype; |
155 | our %mimetype; |
… | |
… | |
180 | |
192 | |
181 | sub DESTROY { |
193 | sub DESTROY { |
182 | #my $self = shift; |
194 | #my $self = shift; |
183 | $::conns--; |
195 | $::conns--; |
184 | } |
196 | } |
|
|
197 | |
|
|
198 | sub prune_cache { |
|
|
199 | my $hash = $_[0]; |
|
|
200 | |
|
|
201 | for (keys %$hash) { |
|
|
202 | if (ref $hash->{$_} eq HASH::) { |
|
|
203 | prune_cache($hash->{$_}); |
|
|
204 | unless (scalar keys %{$hash->{$_}}) { |
|
|
205 | delete $hash->{$_}; |
|
|
206 | $d2++; |
|
|
207 | } |
|
|
208 | } |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | sub prune_caches { |
|
|
213 | prune_cache \%conn; |
|
|
214 | prune_cache \%uri; |
|
|
215 | |
|
|
216 | for (keys %blocked) { |
|
|
217 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
218 | } |
|
|
219 | } |
|
|
220 | |
|
|
221 | Event->timer(interval => 60, cb => \&prune_caches); |
185 | |
222 | |
186 | sub slog { |
223 | sub slog { |
187 | my $self = shift; |
224 | my $self = shift; |
188 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
225 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
189 | } |
226 | } |
… | |
… | |
219 | $hdr->{"Content-Length"} = length $content; |
256 | $hdr->{"Content-Length"} = length $content; |
220 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
257 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
221 | } |
258 | } |
222 | |
259 | |
223 | $res .= "Date: $HTTP_NOW\015\012"; |
260 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
261 | $res .= "Server: $::NAME\015\012"; |
224 | |
262 | |
225 | while (my ($h, $v) = each %$hdr) { |
263 | while (my ($h, $v) = each %$hdr) { |
226 | $res .= "$h: $v\015\012" |
264 | $res .= "$h: $v\015\012" |
227 | } |
265 | } |
228 | $res .= "\015\012"; |
266 | $res .= "\015\012"; |
… | |
… | |
491 | my $self = shift; |
529 | my $self = shift; |
492 | my $idx = $self->diridx; |
530 | my $idx = $self->diridx; |
493 | |
531 | |
494 | $self->response(200, "ok", |
532 | $self->response(200, "ok", |
495 | { |
533 | { |
496 | "Content-Type" => "text/html", |
534 | "Content-Type" => "text/html; charset=utf-8", |
497 | "Content-Length" => length $idx, |
535 | "Content-Length" => length $idx, |
498 | "Last-Modified" => time2str ($self->{stat}[9]), |
536 | "Last-Modified" => time2str ($self->{stat}[9]), |
499 | }, |
537 | }, |
500 | $idx); |
538 | $idx); |
501 | } |
539 | } |
502 | |
540 | |
503 | sub handle_file { |
541 | sub handle_file { |
504 | my ($self, $queue, $tbf) = @_; |
542 | my ($self, $queue, $tbf) = @_; |
505 | my $length = $self->{stat}[7]; |
543 | my $length = $self->{stat}[7]; |
506 | my $hdr = { |
544 | my $hdr = { |
507 | "Last-Modified" => time2str ((stat _)[9]), |
545 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
546 | "Accept-Ranges" => "bytes", |
508 | }; |
547 | }; |
509 | |
548 | |
510 | my @code = (200, "ok"); |
549 | my @code = (200, "ok"); |
511 | my ($l, $h); |
550 | my ($l, $h); |
512 | |
551 | |
… | |
… | |
555 | |
594 | |
556 | $self->response(@code, $hdr, ""); |
595 | $self->response(@code, $hdr, ""); |
557 | |
596 | |
558 | if ($self->{method} eq "GET") { |
597 | if ($self->{method} eq "GET") { |
559 | $self->{time} = $::NOW; |
598 | $self->{time} = $::NOW; |
|
|
599 | $self->{written} = 0; |
560 | |
600 | |
561 | my $current = $Coro::current; |
601 | my $current = $Coro::current; |
562 | |
602 | |
563 | my ($fh, $buf, $r); |
603 | my ($fh, $buf, $r); |
564 | |
604 | |
… | |
… | |
580 | while ($h > 0) { |
620 | while ($h > 0) { |
581 | unless ($locked) { |
621 | unless ($locked) { |
582 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
622 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
583 | $bufsize = $::BUFSIZE; |
623 | $bufsize = $::BUFSIZE; |
584 | $self->{time} = $::NOW; |
624 | $self->{time} = $::NOW; |
|
|
625 | $self->{written} = 0; |
585 | } |
626 | } |
586 | } |
627 | } |
587 | |
628 | |
588 | if ($blocked{$self->{remote_id}}) { |
629 | if ($blocked{$self->{remote_id}}) { |
589 | $self->{h}{connection} = "close"; |
630 | $self->{h}{connection} = "close"; |
… | |
… | |
613 | |
654 | |
614 | close $fh; |
655 | close $fh; |
615 | } |
656 | } |
616 | } |
657 | } |
617 | |
658 | |
618 | 1; |
659 | 1 |