1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | |
5 | |
|
|
6 | use HTTP::Date; |
|
|
7 | |
6 | no utf8; |
8 | no utf8; |
7 | use bytes; |
9 | use bytes; |
|
|
10 | |
|
|
11 | our @wait_time = (); # used to calculcate avg. waiting time |
|
|
12 | our $wait_time_length = 25; |
8 | |
13 | |
9 | # at least on my machine, this thingy serves files |
14 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
15 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
16 | # and quite a bit slower than thttpd :( |
12 | |
17 | |
… | |
… | |
25 | my $level = shift; |
30 | my $level = shift; |
26 | my $format = shift; |
31 | my $format = shift; |
27 | printf "---: $format\n", @_; |
32 | printf "---: $format\n", @_; |
28 | } |
33 | } |
29 | |
34 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
35 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
36 | our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50; |
31 | |
37 | |
32 | my @newcons; |
38 | my @newcons; |
33 | my @pool; |
39 | my @pool; |
34 | |
40 | |
35 | # one "execution thread" |
41 | # one "execution thread" |
… | |
… | |
57 | Listen => 50, |
63 | Listen => 50, |
58 | or die "unable to start server"; |
64 | or die "unable to start server"; |
59 | |
65 | |
60 | push @listen_sockets, $http_port; |
66 | push @listen_sockets, $http_port; |
61 | |
67 | |
|
|
68 | our $NOW; |
|
|
69 | our $HTTP_NOW; |
|
|
70 | |
|
|
71 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
72 | $NOW = time; |
|
|
73 | $HTTP_NOW = time2str $NOW; |
|
|
74 | }); |
|
|
75 | |
62 | # the "main thread" |
76 | # the "main thread" |
63 | async { |
77 | async { |
64 | slog 1, "accepting connections"; |
78 | slog 1, "accepting connections"; |
65 | while () { |
79 | while () { |
66 | $connections->down; |
80 | $connections->down; |
67 | push @newcons, [$http_port->accept]; |
81 | push @newcons, [$http_port->accept]; |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
82 | #slog 3, "accepted @$connections ".scalar(@pool); |
69 | $::NOW = time; |
|
|
70 | if (@pool) { |
83 | if (@pool) { |
71 | (pop @pool)->ready; |
84 | (pop @pool)->ready; |
72 | } else { |
85 | } else { |
73 | async \&handler; |
86 | async \&handler; |
74 | } |
87 | } |
… | |
… | |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
167 | my ($self, $code, $msg, $hdr, $content) = @_; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
168 | my $res = "HTTP/1.1 $code $msg\015\012"; |
156 | |
169 | |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
170 | $self->{h}{connection} ||= $hdr->{Connection}; |
158 | |
171 | |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
172 | $res .= "Date: $HTTP_NOW\015\012"; |
160 | |
173 | |
161 | while (my ($h, $v) = each %$hdr) { |
174 | while (my ($h, $v) = each %$hdr) { |
162 | $res .= "$h: $v\015\012" |
175 | $res .= "$h: $v\015\012" |
163 | } |
176 | } |
164 | $res .= "\015\012"; |
177 | $res .= "\015\012"; |
… | |
… | |
222 | |
235 | |
223 | delete $blocked{$ip}; |
236 | delete $blocked{$ip}; |
224 | } |
237 | } |
225 | |
238 | |
226 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
239 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
240 | my $delay = 120; |
|
|
241 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
242 | if ($delay <= 0) { |
227 | $self->slog(2, "blocked ip $ip"); |
243 | $self->slog(2, "blocked ip $ip"); |
228 | $self->err_blocked; |
244 | $self->err_blocked; |
|
|
245 | } else { |
|
|
246 | Coro::Event::do_timer(after => 3); |
|
|
247 | $delay -= 3; |
|
|
248 | } |
|
|
249 | } |
229 | } |
250 | } |
230 | |
251 | |
231 | $req =~ /^(?:\015\012)? |
252 | $req =~ /^(?:\015\012)? |
232 | (GET|HEAD) \040+ |
253 | (GET|HEAD) \040+ |
233 | ([^\040]+) \040+ |
254 | ([^\040]+) \040+ |
… | |
… | |
459 | $hdr->{"Content-Length"} = $length; |
480 | $hdr->{"Content-Length"} = $length; |
460 | |
481 | |
461 | $self->response(@code, $hdr, ""); |
482 | $self->response(@code, $hdr, ""); |
462 | |
483 | |
463 | if ($self->{method} eq "GET") { |
484 | if ($self->{method} eq "GET") { |
|
|
485 | $self->{time} = $::NOW; |
|
|
486 | |
|
|
487 | my $transfer = $::transfers->guard; |
|
|
488 | $self->{fh}->writable or return; |
|
|
489 | |
|
|
490 | push @::wait_time, $::NOW - $self->{time}; |
|
|
491 | shift @::wait_time if @wait_time > $wait_time_length; |
|
|
492 | $self->{time} = $::NOW; |
|
|
493 | |
464 | my ($fh, $buf, $r); |
494 | my ($fh, $buf, $r); |
465 | my $current = $Coro::current; |
495 | my $current = $Coro::current; |
466 | open $fh, "<", $self->{path} |
496 | open $fh, "<", $self->{path} |
467 | or die "$self->{path}: late open failure ($!)"; |
497 | or die "$self->{path}: late open failure ($!)"; |
468 | |
498 | |
… | |
… | |
494 | or last; |
524 | or last; |
495 | $::written += $w; |
525 | $::written += $w; |
496 | $self->{written} += $w; |
526 | $self->{written} += $w; |
497 | $l += $r; |
527 | $l += $r; |
498 | } |
528 | } |
499 | } |
|
|
500 | |
529 | |
501 | close $fh; |
530 | close $fh; |
|
|
531 | } |
502 | } |
532 | } |
503 | |
533 | |
504 | 1; |
534 | 1; |