1 | #!/usr/bin/perl |
|
|
2 | |
|
|
3 | use Coro; |
1 | use Coro; |
4 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
5 | use Coro::Event; |
3 | use Coro::Event; |
6 | use Coro::Socket; |
4 | use Coro::Socket; |
7 | |
5 | |
… | |
… | |
9 | use bytes; |
7 | use bytes; |
10 | |
8 | |
11 | # at least on my machine, this thingy serves files |
9 | # at least on my machine, this thingy serves files |
12 | # quite a bit faster than apache, ;) |
10 | # quite a bit faster than apache, ;) |
13 | # and quite a bit slower than thttpd :( |
11 | # and quite a bit slower than thttpd :( |
14 | |
|
|
15 | my $port = new Coro::Socket |
|
|
16 | LocalAddr => $SERVER_HOST, |
|
|
17 | LocalPort => $SERVER_PORT, |
|
|
18 | ReuseAddr => 1, |
|
|
19 | Listen => 1, |
|
|
20 | or die "unable to start server"; |
|
|
21 | |
12 | |
22 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
23 | |
14 | |
24 | sub slog { |
15 | sub slog { |
25 | my $level = shift; |
16 | my $level = shift; |
… | |
… | |
27 | printf "---: $format\n", @_; |
18 | printf "---: $format\n", @_; |
28 | } |
19 | } |
29 | |
20 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
31 | |
22 | |
32 | my @fh; |
23 | my @newcons; |
33 | my @pool; |
24 | my @pool; |
34 | |
25 | |
|
|
26 | # one "execution thread" |
35 | sub handler { |
27 | sub handler { |
36 | while () { |
28 | while () { |
37 | my $fh = pop @fh; |
29 | my $new = pop @newcons; |
38 | if ($fh) { |
30 | if ($new) { |
39 | eval { |
31 | eval { |
40 | conn->new($fh)->handle; |
32 | conn->new(@$new)->handle; |
41 | }; |
33 | }; |
42 | close $fh; |
|
|
43 | slog 1, "$@" if $@ && !ref $@; |
34 | slog 1, "$@" if $@ && !ref $@; |
44 | $connections->up; |
35 | $connections->up; |
45 | } else { |
36 | } else { |
46 | last if @pool >= $MAX_POOL; |
37 | last if @pool >= $MAX_POOL; |
47 | push @pool, $Coro::current; |
38 | push @pool, $Coro::current; |
48 | schedule; |
39 | schedule; |
49 | } |
40 | } |
50 | } |
41 | } |
51 | } |
42 | } |
52 | |
43 | |
|
|
44 | my $http_port = new Coro::Socket |
|
|
45 | LocalAddr => $SERVER_HOST, |
|
|
46 | LocalPort => $SERVER_PORT, |
|
|
47 | ReuseAddr => 1, |
|
|
48 | Listen => 1, |
|
|
49 | or die "unable to start server"; |
|
|
50 | |
|
|
51 | push @listen_sockets, $http_port; |
|
|
52 | |
|
|
53 | # the "main thread" |
53 | async { |
54 | async { |
54 | slog 1, "accepting connections"; |
55 | slog 1, "accepting connections"; |
55 | while () { |
56 | while () { |
56 | $connections->down; |
57 | $connections->down; |
57 | push @fh, $port->accept; |
58 | push @newcons, [$http_port->accept]; |
58 | #slog 3, "accepted @$connections ".scalar(@pool); |
59 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
60 | $::NOW = time; |
59 | if (@pool) { |
61 | if (@pool) { |
60 | (pop @pool)->ready; |
62 | (pop @pool)->ready; |
61 | } else { |
63 | } else { |
62 | async \&handler; |
64 | async \&handler; |
63 | } |
65 | } |
64 | |
66 | |
65 | } |
67 | } |
66 | }; |
68 | }; |
67 | |
69 | |
68 | loop; |
|
|
69 | print "ende\n";#d# |
|
|
70 | |
|
|
71 | package conn; |
70 | package conn; |
72 | |
71 | |
73 | use Socket; |
72 | use Socket; |
74 | use HTTP::Date; |
73 | use HTTP::Date; |
|
|
74 | use Convert::Scalar 'weaken'; |
|
|
75 | |
|
|
76 | our %conn; # $conn{ip}{fh} => connobj |
|
|
77 | our %blocked; |
75 | |
78 | |
76 | sub new { |
79 | sub new { |
77 | my $class = shift; |
80 | my $class = shift; |
|
|
81 | my $peername = shift; |
78 | my $fh = shift; |
82 | my $fh = shift; |
|
|
83 | my $self = bless { fh => $fh }, $class; |
79 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
84 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
80 | or $self->err(500, "unable to get peername"); |
85 | or $self->err(500, "unable to decode peername"); |
81 | $self->{remote_address} = inet_ntoa $iaddr; |
86 | $self->{remote_addr} = inet_ntoa $iaddr; |
82 | bless { fh => $fh }, $class; |
87 | |
|
|
88 | # enter ourselves into various lists |
|
|
89 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
90 | |
|
|
91 | $self; |
|
|
92 | } |
|
|
93 | |
|
|
94 | sub DESTROY { |
|
|
95 | my $self = shift; |
|
|
96 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
97 | delete $uri{$self->{uri}}{$self*1}; |
83 | } |
98 | } |
84 | |
99 | |
85 | sub slog { |
100 | sub slog { |
86 | main::slog(@_); |
101 | my $self = shift; |
|
|
102 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
87 | } |
103 | } |
88 | |
104 | |
89 | sub print_response { |
105 | sub response { |
90 | my ($self, $code, $msg, $hdr, $content) = @_; |
106 | my ($self, $code, $msg, $hdr, $content) = @_; |
91 | my $res = "HTTP/1.0 $code $msg\015\012"; |
107 | my $res = "HTTP/1.0 $code $msg\015\012"; |
92 | |
108 | |
93 | $hdr->{Date} = time2str time; # slow? nah. |
109 | $res .= "Connection: close\015\012"; |
|
|
110 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
94 | |
111 | |
95 | while (my ($h, $v) = each %$hdr) { |
112 | while (my ($h, $v) = each %$hdr) { |
96 | $res .= "$h: $v\015\012" |
113 | $res .= "$h: $v\015\012" |
97 | } |
114 | } |
|
|
115 | |
98 | $res .= "\015\012$content" if defined $content; |
116 | $res .= "\015\012$content" if defined $content; |
|
|
117 | |
|
|
118 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
99 | |
119 | |
100 | print {$self->{fh}} $res; |
120 | print {$self->{fh}} $res; |
101 | } |
121 | } |
102 | |
122 | |
103 | sub err { |
123 | sub err { |
… | |
… | |
108 | $content = "$code $msg"; |
128 | $content = "$code $msg"; |
109 | $hdr->{"Content-Type"} = "text/plain"; |
129 | $hdr->{"Content-Type"} = "text/plain"; |
110 | $hdr->{"Content-Length"} = length $content; |
130 | $hdr->{"Content-Length"} = length $content; |
111 | } |
131 | } |
112 | |
132 | |
113 | $self->slog($msg) if $code; |
|
|
114 | |
|
|
115 | $self->print_response($code, $msg, $hdr, $content); |
133 | $self->response($code, $msg, $hdr, $content); |
116 | |
134 | |
117 | die bless {}, err::; |
135 | die bless {}, err::; |
|
|
136 | } |
|
|
137 | |
|
|
138 | sub err_blocked { |
|
|
139 | my $self = shift; |
|
|
140 | my $ip = $self->{remote_addr}; |
|
|
141 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
142 | $self->err(403, "too many connections", |
|
|
143 | { |
|
|
144 | "Content-Type" => "text/html", |
|
|
145 | "Retry-After" => $::BLOCKTIME |
|
|
146 | }, |
|
|
147 | <<EOF); |
|
|
148 | <html><p> |
|
|
149 | You have been blocked because you opened too many connections. You |
|
|
150 | may retry at</p> |
|
|
151 | |
|
|
152 | <p><blockquote>$time.</blockquote></p> |
|
|
153 | |
|
|
154 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
155 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
|
|
156 | </html> |
|
|
157 | EOF |
118 | } |
158 | } |
119 | |
159 | |
120 | sub handle { |
160 | sub handle { |
121 | my $self = shift; |
161 | my $self = shift; |
122 | my $fh = $self->{fh}; |
162 | my $fh = $self->{fh}; |
… | |
… | |
129 | my $req = $fh->readline("\015\012\015\012"); |
169 | my $req = $fh->readline("\015\012\015\012"); |
130 | $fh->timeout($::RES_TIMEOUT); |
170 | $fh->timeout($::RES_TIMEOUT); |
131 | |
171 | |
132 | defined $req or |
172 | defined $req or |
133 | $self->err(408, "request timeout"); |
173 | $self->err(408, "request timeout"); |
|
|
174 | |
|
|
175 | my $ip = $self->{remote_addr}; |
|
|
176 | |
|
|
177 | if ($blocked{$ip}) { |
|
|
178 | $self->err_blocked($blocked{$ip}) |
|
|
179 | if $blocked{$ip} > $::NOW; |
|
|
180 | |
|
|
181 | delete $blocked{$ip}; |
|
|
182 | } |
|
|
183 | |
|
|
184 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
185 | $self->slog("blocked ip $ip"); |
|
|
186 | $self->err_blocked; |
|
|
187 | } |
134 | |
188 | |
135 | $req =~ /^(?:\015\012)? |
189 | $req =~ /^(?:\015\012)? |
136 | (GET|HEAD) \040+ |
190 | (GET|HEAD) \040+ |
137 | ([^\040]+) \040+ |
191 | ([^\040]+) \040+ |
138 | HTTP\/([0-9]+\.[0-9]+) |
192 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
163 | $self->{h}{$h} = substr $v, 1 |
217 | $self->{h}{$h} = substr $v, 1 |
164 | while ($h, $v) = each %hdr; |
218 | while ($h, $v) = each %hdr; |
165 | } |
219 | } |
166 | |
220 | |
167 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
221 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
|
|
222 | |
|
|
223 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
168 | |
224 | |
169 | $self->map_uri; |
225 | $self->map_uri; |
170 | $self->respond; |
226 | $self->respond; |
171 | #} |
227 | #} |
172 | } |
228 | } |
… | |
… | |
307 | } |
363 | } |
308 | $hdr->{"Content-Range"} = "bytes */$length"; |
364 | $hdr->{"Content-Range"} = "bytes */$length"; |
309 | $self->err(416, "not satisfiable", $hdr); |
365 | $self->err(416, "not satisfiable", $hdr); |
310 | |
366 | |
311 | satisfiable: |
367 | satisfiable: |
|
|
368 | # check for segmented downloads |
|
|
369 | if ($l && $NO_SEGMENTED) { |
|
|
370 | if (%{$uri{$self->{uri}}} > 1) { |
|
|
371 | $self->slog("segmented download refused\n"); |
|
|
372 | $self->err(400, "segmented downloads are not allowed"); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
312 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
376 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
313 | @code = (206, "partial content"); |
377 | @code = (206, "partial content"); |
314 | $length = $h - $l + 1; |
378 | $length = $h - $l + 1; |
315 | |
379 | |
316 | ignore: |
380 | ignore: |
… | |
… | |
324 | $hdr->{"Content-Type"} = "application/octet-stream"; |
388 | $hdr->{"Content-Type"} = "application/octet-stream"; |
325 | } |
389 | } |
326 | |
390 | |
327 | $hdr->{"Content-Length"} = $length; |
391 | $hdr->{"Content-Length"} = $length; |
328 | |
392 | |
329 | $self->print_response(@code, $hdr, ""); |
393 | $self->response(@code, $hdr, ""); |
330 | |
394 | |
331 | if ($self->{method} eq "GET") { |
395 | if ($self->{method} eq "GET") { |
332 | my ($fh, $buf); |
396 | my ($fh, $buf); |
333 | open $fh, "<", $self->{path} |
397 | open $fh, "<", $self->{path} |
334 | or die "$self->{path}: late open failure ($!)"; |
398 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
339 | } |
403 | } |
340 | |
404 | |
341 | $h -= $l - 1; |
405 | $h -= $l - 1; |
342 | |
406 | |
343 | while ($h > 0) { |
407 | while ($h > 0) { |
344 | $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
408 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
345 | print {$self->{fh}} $buf |
409 | $self->{fh}->syswrite($buf) |
346 | or last; |
410 | or last; |
347 | } |
411 | } |
348 | } |
412 | } |
349 | |
413 | |
350 | close $fh; |
414 | close $fh; |
351 | } |
415 | } |
|
|
416 | |
|
|
417 | 1; |