… | |
… | |
7 | use bytes; |
7 | use bytes; |
8 | |
8 | |
9 | # at least on my machine, this thingy serves files |
9 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
10 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
11 | # and quite a bit slower than thttpd :( |
12 | |
|
|
13 | my $port = new Coro::Socket |
|
|
14 | LocalAddr => $SERVER_HOST, |
|
|
15 | LocalPort => $SERVER_PORT, |
|
|
16 | ReuseAddr => 1, |
|
|
17 | Listen => 1, |
|
|
18 | or die "unable to start server"; |
|
|
19 | |
12 | |
20 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
21 | |
14 | |
22 | sub slog { |
15 | sub slog { |
23 | my $level = shift; |
16 | my $level = shift; |
… | |
… | |
25 | printf "---: $format\n", @_; |
18 | printf "---: $format\n", @_; |
26 | } |
19 | } |
27 | |
20 | |
28 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
29 | |
22 | |
30 | my @fh; |
23 | my @newcons; |
31 | my @pool; |
24 | my @pool; |
32 | |
25 | |
33 | # one "execution thread" |
26 | # one "execution thread" |
34 | sub handler { |
27 | sub handler { |
35 | while () { |
28 | while () { |
36 | my $fh = pop @fh; |
29 | my $new = pop @newcons; |
37 | if ($fh) { |
30 | if ($new) { |
38 | eval { |
31 | eval { |
39 | conn->new($fh)->handle; |
32 | conn->new(@$new)->handle; |
40 | }; |
33 | }; |
41 | close $fh; |
|
|
42 | slog 1, "$@" if $@ && !ref $@; |
34 | slog 1, "$@" if $@ && !ref $@; |
43 | $connections->up; |
35 | $connections->up; |
44 | } else { |
36 | } else { |
45 | last if @pool >= $MAX_POOL; |
37 | last if @pool >= $MAX_POOL; |
46 | push @pool, $Coro::current; |
38 | push @pool, $Coro::current; |
47 | schedule; |
39 | schedule; |
48 | } |
40 | } |
49 | } |
41 | } |
50 | } |
42 | } |
51 | |
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 | |
52 | # the "main thread" |
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; |
75 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
76 | |
75 | |
77 | my %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
|
|
77 | our %blocked; |
78 | |
78 | |
79 | sub new { |
79 | sub new { |
80 | my $class = shift; |
80 | my $class = shift; |
|
|
81 | my $peername = shift; |
81 | my $fh = shift; |
82 | my $fh = shift; |
82 | my $self = bless { fh => $fh }, $class; |
83 | my $self = bless { fh => $fh }, $class; |
83 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
84 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
84 | or $self->err(500, "unable to get peername"); |
85 | or $self->err(500, "unable to decode peername"); |
|
|
86 | |
85 | $self->{remote_address} = inet_ntoa $iaddr; |
87 | $self->{remote_addr} = inet_ntoa $iaddr; |
86 | |
88 | |
87 | # enter ourselves into various lists |
89 | # enter ourselves into various lists |
88 | weaken ($conn{$self->{remote_address}}{$self*1} = $self); |
90 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
89 | print $self->{remote_address}.": ".($self*1)." > ".%{$conn{$self->{remote_address}}},"\n"; |
91 | |
90 | $self; |
92 | $self; |
91 | } |
93 | } |
92 | |
94 | |
93 | sub DESTROY { |
95 | sub DESTROY { |
94 | my $self = shift; |
96 | my $self = shift; |
95 | delete $conn{$self->{remote_address}}{$self*1}; |
97 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
98 | delete $uri{$self->{uri}}{$self*1}; |
96 | } |
99 | } |
97 | |
100 | |
98 | sub slog { |
101 | sub slog { |
99 | main::slog(@_); |
102 | my $self = shift; |
|
|
103 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
100 | } |
104 | } |
101 | |
105 | |
102 | sub print_response { |
106 | sub response { |
103 | my ($self, $code, $msg, $hdr, $content) = @_; |
107 | my ($self, $code, $msg, $hdr, $content) = @_; |
104 | my $res = "HTTP/1.0 $code $msg\015\012"; |
108 | my $res = "HTTP/1.0 $code $msg\015\012"; |
105 | |
109 | |
106 | $hdr->{Date} = time2str time; # slow? nah. |
110 | $res .= "Connection: close\015\012"; |
|
|
111 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
107 | |
112 | |
108 | while (my ($h, $v) = each %$hdr) { |
113 | while (my ($h, $v) = each %$hdr) { |
109 | $res .= "$h: $v\015\012" |
114 | $res .= "$h: $v\015\012" |
110 | } |
115 | } |
|
|
116 | |
111 | $res .= "\015\012$content" if defined $content; |
117 | $res .= "\015\012$content" if defined $content; |
112 | |
118 | |
113 | print STDERR "$self->{remote_address} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
119 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
114 | |
120 | |
115 | print {$self->{fh}} $res; |
121 | print {$self->{fh}} $res; |
116 | } |
122 | } |
117 | |
123 | |
118 | sub err { |
124 | sub err { |
… | |
… | |
123 | $content = "$code $msg"; |
129 | $content = "$code $msg"; |
124 | $hdr->{"Content-Type"} = "text/plain"; |
130 | $hdr->{"Content-Type"} = "text/plain"; |
125 | $hdr->{"Content-Length"} = length $content; |
131 | $hdr->{"Content-Length"} = length $content; |
126 | } |
132 | } |
127 | |
133 | |
128 | $self->slog($msg) if $code; |
|
|
129 | |
|
|
130 | $self->print_response($code, $msg, $hdr, $content); |
134 | $self->response($code, $msg, $hdr, $content); |
131 | |
135 | |
132 | die bless {}, err::; |
136 | die bless {}, err::; |
|
|
137 | } |
|
|
138 | |
|
|
139 | sub err_blocked { |
|
|
140 | my $self = shift; |
|
|
141 | my $ip = $self->{remote_addr}; |
|
|
142 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
143 | $self->err(403, "too many connections", |
|
|
144 | { |
|
|
145 | "Content-Type" => "text/html", |
|
|
146 | "Retry-After" => $::BLOCKTIME |
|
|
147 | }, |
|
|
148 | <<EOF); |
|
|
149 | <html><p> |
|
|
150 | You have been blocked because you opened too many connections. You |
|
|
151 | may retry at</p> |
|
|
152 | |
|
|
153 | <p><blockquote>$time.</blockquote></p> |
|
|
154 | |
|
|
155 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
156 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
|
|
157 | </html> |
|
|
158 | EOF |
133 | } |
159 | } |
134 | |
160 | |
135 | sub handle { |
161 | sub handle { |
136 | my $self = shift; |
162 | my $self = shift; |
137 | my $fh = $self->{fh}; |
163 | my $fh = $self->{fh}; |
… | |
… | |
144 | my $req = $fh->readline("\015\012\015\012"); |
170 | my $req = $fh->readline("\015\012\015\012"); |
145 | $fh->timeout($::RES_TIMEOUT); |
171 | $fh->timeout($::RES_TIMEOUT); |
146 | |
172 | |
147 | defined $req or |
173 | defined $req or |
148 | $self->err(408, "request timeout"); |
174 | $self->err(408, "request timeout"); |
|
|
175 | |
|
|
176 | my $ip = $self->{remote_addr}; |
|
|
177 | |
|
|
178 | if ($blocked{$ip}) { |
|
|
179 | $self->err_blocked($blocked{$ip}) |
|
|
180 | if $blocked{$ip} > $::NOW; |
|
|
181 | |
|
|
182 | delete $blocked{$ip}; |
|
|
183 | } |
|
|
184 | |
|
|
185 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
186 | $self->slog("blocked ip $ip"); |
|
|
187 | $self->err_blocked; |
|
|
188 | } |
149 | |
189 | |
150 | $req =~ /^(?:\015\012)? |
190 | $req =~ /^(?:\015\012)? |
151 | (GET|HEAD) \040+ |
191 | (GET|HEAD) \040+ |
152 | ([^\040]+) \040+ |
192 | ([^\040]+) \040+ |
153 | HTTP\/([0-9]+\.[0-9]+) |
193 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
179 | while ($h, $v) = each %hdr; |
219 | while ($h, $v) = each %hdr; |
180 | } |
220 | } |
181 | |
221 | |
182 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
222 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
183 | |
223 | |
|
|
224 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
|
|
225 | |
184 | $self->map_uri; |
226 | $self->map_uri; |
185 | |
|
|
186 | Coro::Event::do_timer(after => 5); |
|
|
187 | |
|
|
188 | $self->respond; |
227 | $self->respond; |
189 | #} |
228 | #} |
190 | } |
229 | } |
191 | |
230 | |
192 | # uri => path mapping |
231 | # uri => path mapping |
… | |
… | |
206 | |
245 | |
207 | $self->{name} = $uri; |
246 | $self->{name} = $uri; |
208 | |
247 | |
209 | # now do the path mapping |
248 | # now do the path mapping |
210 | $self->{path} = "$::DOCROOT/$host$uri"; |
249 | $self->{path} = "$::DOCROOT/$host$uri"; |
|
|
250 | |
|
|
251 | $self->access_check; |
211 | } |
252 | } |
212 | |
253 | |
213 | sub server_address { |
254 | sub server_address { |
214 | my $self = shift; |
255 | my $self = shift; |
215 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
256 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
… | |
… | |
325 | } |
366 | } |
326 | $hdr->{"Content-Range"} = "bytes */$length"; |
367 | $hdr->{"Content-Range"} = "bytes */$length"; |
327 | $self->err(416, "not satisfiable", $hdr); |
368 | $self->err(416, "not satisfiable", $hdr); |
328 | |
369 | |
329 | satisfiable: |
370 | satisfiable: |
|
|
371 | # check for segmented downloads |
|
|
372 | if ($l && $NO_SEGMENTED) { |
|
|
373 | if (%{$uri{$self->{uri}}} > 1) { |
|
|
374 | $self->slog("segmented download refused\n"); |
|
|
375 | $self->err(400, "segmented downloads are not allowed"); |
|
|
376 | } |
|
|
377 | } |
|
|
378 | |
330 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
379 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
331 | @code = (206, "partial content"); |
380 | @code = (206, "partial content"); |
332 | $length = $h - $l + 1; |
381 | $length = $h - $l + 1; |
333 | |
382 | |
334 | ignore: |
383 | ignore: |
… | |
… | |
342 | $hdr->{"Content-Type"} = "application/octet-stream"; |
391 | $hdr->{"Content-Type"} = "application/octet-stream"; |
343 | } |
392 | } |
344 | |
393 | |
345 | $hdr->{"Content-Length"} = $length; |
394 | $hdr->{"Content-Length"} = $length; |
346 | |
395 | |
347 | $self->print_response(@code, $hdr, ""); |
396 | $self->response(@code, $hdr, ""); |
348 | |
397 | |
349 | if ($self->{method} eq "GET") { |
398 | if ($self->{method} eq "GET") { |
350 | my ($fh, $buf); |
399 | my ($fh, $buf); |
351 | open $fh, "<", $self->{path} |
400 | open $fh, "<", $self->{path} |
352 | or die "$self->{path}: late open failure ($!)"; |
401 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
357 | } |
406 | } |
358 | |
407 | |
359 | $h -= $l - 1; |
408 | $h -= $l - 1; |
360 | |
409 | |
361 | while ($h > 0) { |
410 | while ($h > 0) { |
362 | $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
411 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
363 | print {$self->{fh}} $buf |
412 | $self->{fh}->syswrite($buf) |
364 | or last; |
413 | or last; |
365 | } |
414 | } |
366 | } |
415 | } |
367 | |
416 | |
368 | close $fh; |
417 | close $fh; |
369 | } |
418 | } |
370 | |
419 | |
|
|
420 | sub access_check { |
|
|
421 | my $self = shift; |
|
|
422 | my $whois = ::ip_request($self->{remote_addr}); |
|
|
423 | |
|
|
424 | if ($whois =~ /^\*cy: (\S+)/m) { |
|
|
425 | $self->slog(9, "COUNTRY($1)"); |
|
|
426 | } else { |
|
|
427 | $self->slog(9, "no country($whois)"); |
|
|
428 | } |
|
|
429 | } |
|
|
430 | |
371 | 1; |
431 | 1; |