… | |
… | |
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); |
59 | $::NOW = time; |
60 | $::NOW = time; |
60 | if (@pool) { |
61 | if (@pool) { |
61 | (pop @pool)->ready; |
62 | (pop @pool)->ready; |
62 | } else { |
63 | } else { |
… | |
… | |
64 | } |
65 | } |
65 | |
66 | |
66 | } |
67 | } |
67 | }; |
68 | }; |
68 | |
69 | |
69 | loop; |
|
|
70 | print "ende\n";#d# |
|
|
71 | |
|
|
72 | package conn; |
70 | package conn; |
73 | |
71 | |
74 | use Socket; |
72 | use Socket; |
75 | use HTTP::Date; |
73 | use HTTP::Date; |
76 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
77 | |
75 | |
78 | our %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
79 | our %blocked; |
77 | our %blocked; |
|
|
78 | our %mimetype; |
|
|
79 | |
|
|
80 | sub read_mimetypes { |
|
|
81 | local *M; |
|
|
82 | if (open M, "<mime_types") { |
|
|
83 | while (<M>) { |
|
|
84 | if (/^([^#]\S+)\t+(\S+)$/) { |
|
|
85 | $mimetype{lc $1} = $2; |
|
|
86 | } |
|
|
87 | } |
|
|
88 | } else { |
|
|
89 | print "cannot open mime_types\n"; |
|
|
90 | } |
|
|
91 | } |
|
|
92 | |
|
|
93 | read_mimetypes; |
80 | |
94 | |
81 | sub new { |
95 | sub new { |
82 | my $class = shift; |
96 | my $class = shift; |
|
|
97 | my $peername = shift; |
83 | my $fh = shift; |
98 | my $fh = shift; |
84 | my $self = bless { fh => $fh }, $class; |
99 | my $self = bless { fh => $fh }, $class; |
85 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
86 | or $self->err(500, "unable to get peername"); |
101 | or $self->err(500, "unable to decode peername"); |
|
|
102 | |
87 | $self->{remote_addr} = inet_ntoa $iaddr; |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
104 | $self->{time} = $::NOW; |
88 | |
105 | |
89 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
90 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
91 | |
108 | |
92 | print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n"; |
|
|
93 | $self; |
109 | $self; |
94 | } |
110 | } |
95 | |
111 | |
96 | sub DESTROY { |
112 | sub DESTROY { |
97 | my $self = shift; |
113 | my $self = shift; |
98 | delete $conn{$self->{remote_addr}}{$self*1}; |
114 | delete $conn{$self->{remote_addr}}{$self*1}; |
99 | delete $uri{$self->{uri}}{$self*1}; |
115 | delete $uri{$self->{uri}}{$self*1}; |
100 | } |
116 | } |
101 | |
117 | |
102 | sub slog { |
118 | sub slog { |
103 | main::slog(@_); |
119 | my $self = shift; |
|
|
120 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
104 | } |
121 | } |
105 | |
122 | |
106 | sub print_response { |
123 | sub response { |
107 | my ($self, $code, $msg, $hdr, $content) = @_; |
124 | my ($self, $code, $msg, $hdr, $content) = @_; |
108 | my $res = "HTTP/1.0 $code $msg\015\012"; |
125 | my $res = "HTTP/1.0 $code $msg\015\012"; |
109 | |
126 | |
110 | $hdr->{Date} = time2str $::NOW; # slow? nah. |
127 | $res .= "Connection: close\015\012"; |
|
|
128 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
111 | |
129 | |
112 | while (my ($h, $v) = each %$hdr) { |
130 | while (my ($h, $v) = each %$hdr) { |
113 | $res .= "$h: $v\015\012" |
131 | $res .= "$h: $v\015\012" |
114 | } |
132 | } |
115 | $res .= "\015\012$content" if defined $content; |
133 | $res .= "\015\012"; |
|
|
134 | |
|
|
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
116 | |
136 | |
117 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
137 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
118 | |
138 | |
|
|
139 | $self->{written} += |
119 | print {$self->{fh}} $res; |
140 | print {$self->{fh}} $res; |
120 | } |
141 | } |
121 | |
142 | |
122 | sub err { |
143 | sub err { |
123 | my $self = shift; |
144 | my $self = shift; |
124 | my ($code, $msg, $hdr, $content) = @_; |
145 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
127 | $content = "$code $msg"; |
148 | $content = "$code $msg"; |
128 | $hdr->{"Content-Type"} = "text/plain"; |
149 | $hdr->{"Content-Type"} = "text/plain"; |
129 | $hdr->{"Content-Length"} = length $content; |
150 | $hdr->{"Content-Length"} = length $content; |
130 | } |
151 | } |
131 | |
152 | |
132 | $self->slog($msg) if $code; |
|
|
133 | |
|
|
134 | $self->print_response($code, $msg, $hdr, $content); |
153 | $self->response($code, $msg, $hdr, $content); |
135 | |
154 | |
136 | die bless {}, err::; |
155 | die bless {}, err::; |
137 | } |
156 | } |
138 | |
157 | |
139 | sub err_blocked { |
158 | sub err_blocked { |
140 | my $self = shift; |
159 | my $self = shift; |
141 | my $ip = $self->{remote_addr}; |
160 | my $ip = $self->{remote_addr}; |
142 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
162 | |
|
|
163 | Coro::Event::do_timer(after => 5); |
|
|
164 | |
143 | $self->err(403, "too many connections", |
165 | $self->err(403, "too many connections", |
|
|
166 | { |
|
|
167 | "Content-Type" => "text/html", |
144 | { "Retry-After" => $::BLOCKTIME }, |
168 | "Retry-After" => $::BLOCKTIME |
|
|
169 | }, |
145 | <<EOF); |
170 | <<EOF); |
|
|
171 | <html><p> |
146 | You have been blocked because you opened too many connections. You |
172 | You have been blocked because you opened too many connections. You |
147 | may retry at $time. Until then, |
173 | may retry at</p> |
148 | every new access will renew the block. |
174 | |
|
|
175 | <p><blockquote>$time.</blockquote></p> |
|
|
176 | |
|
|
177 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
178 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
|
|
179 | </html> |
149 | EOF |
180 | EOF |
150 | } |
181 | } |
151 | |
182 | |
152 | sub handle { |
183 | sub handle { |
153 | my $self = shift; |
184 | my $self = shift; |
… | |
… | |
172 | |
203 | |
173 | delete $blocked{$ip}; |
204 | delete $blocked{$ip}; |
174 | } |
205 | } |
175 | |
206 | |
176 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
207 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
177 | $self->slog("blocked ip $ip"); |
208 | $self->slog(2, "blocked ip $ip"); |
178 | $self->err_blocked; |
209 | $self->err_blocked; |
179 | } |
210 | } |
180 | |
211 | |
181 | $req =~ /^(?:\015\012)? |
212 | $req =~ /^(?:\015\012)? |
182 | (GET|HEAD) \040+ |
213 | (GET|HEAD) \040+ |
… | |
… | |
213 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
214 | |
245 | |
215 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
216 | |
247 | |
217 | $self->map_uri; |
248 | $self->map_uri; |
218 | |
|
|
219 | Coro::Event::do_timer(after => 5); |
|
|
220 | |
|
|
221 | $self->respond; |
249 | $self->respond; |
222 | #} |
250 | #} |
223 | } |
251 | } |
224 | |
252 | |
225 | # uri => path mapping |
253 | # uri => path mapping |
… | |
… | |
239 | |
267 | |
240 | $self->{name} = $uri; |
268 | $self->{name} = $uri; |
241 | |
269 | |
242 | # now do the path mapping |
270 | # now do the path mapping |
243 | $self->{path} = "$::DOCROOT/$host$uri"; |
271 | $self->{path} = "$::DOCROOT/$host$uri"; |
|
|
272 | |
|
|
273 | $self->access_check; |
244 | } |
274 | } |
245 | |
275 | |
246 | sub server_address { |
276 | sub server_address { |
247 | my $self = shift; |
277 | my $self = shift; |
248 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
278 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
… | |
… | |
282 | if (chdir $::DOCROOT) { |
312 | if (chdir $::DOCROOT) { |
283 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
313 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
284 | $ENV{HTTP_HOST} = $self->server_host; |
314 | $ENV{HTTP_HOST} = $self->server_host; |
285 | $ENV{HTTP_PORT} = $self->{server_host}; |
315 | $ENV{HTTP_PORT} = $self->{server_host}; |
286 | $ENV{SCRIPT_NAME} = $self->{name}; |
316 | $ENV{SCRIPT_NAME} = $self->{name}; |
287 | exec $::INDEXPROG; |
317 | exec $path; |
288 | } |
318 | } |
289 | Coro::State::_exit(0); |
319 | Coro::State::_exit(0); |
290 | } else { |
320 | } else { |
291 | } |
321 | } |
292 | } |
322 | } |
… | |
… | |
295 | my $self = shift; |
325 | my $self = shift; |
296 | my $path = $self->{path}; |
326 | my $path = $self->{path}; |
297 | |
327 | |
298 | stat $path |
328 | stat $path |
299 | or $self->err(404, "not found"); |
329 | or $self->err(404, "not found"); |
|
|
330 | |
|
|
331 | $self->{stat} = [stat _]; |
300 | |
332 | |
301 | # idiotic netscape sends idiotic headers AGAIN |
333 | # idiotic netscape sends idiotic headers AGAIN |
302 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
334 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
303 | ? str2time $1 : 0; |
335 | ? str2time $1 : 0; |
304 | |
336 | |
… | |
… | |
307 | if ($path !~ /\/$/) { |
339 | if ($path !~ /\/$/) { |
308 | # create a redirect to get the trailing "/" |
340 | # create a redirect to get the trailing "/" |
309 | my $host = $self->server_hostport; |
341 | my $host = $self->server_hostport; |
310 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
342 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
311 | } else { |
343 | } else { |
312 | $ims < (stat _)[9] |
344 | $ims < $self->{stat}[9] |
313 | or $self->err(304, "not modified"); |
345 | or $self->err(304, "not modified"); |
314 | |
346 | |
315 | if ($self->{method} eq "GET") { |
347 | if ($self->{method} eq "GET") { |
316 | if (-r "$path/index.html") { |
348 | if (-r "$path/index.html") { |
317 | $self->{path} .= "/index.html"; |
349 | $self->{path} .= "/index.html"; |
… | |
… | |
329 | } |
361 | } |
330 | } |
362 | } |
331 | |
363 | |
332 | sub handle_dir { |
364 | sub handle_dir { |
333 | my $self = shift; |
365 | my $self = shift; |
334 | $self->_cgi($::INDEXPROG); |
366 | my $idx = $self->diridx; |
|
|
367 | |
|
|
368 | $self->response(200, "ok", |
|
|
369 | { |
|
|
370 | "Content-Type" => "text/html", |
|
|
371 | "Content-Length" => length $idx, |
|
|
372 | }, |
|
|
373 | $idx); |
335 | } |
374 | } |
336 | |
375 | |
337 | sub handle_file { |
376 | sub handle_file { |
338 | my $self = shift; |
377 | my $self = shift; |
339 | my $length = -s _; |
378 | my $length = -s _; |
… | |
… | |
358 | } |
397 | } |
359 | $hdr->{"Content-Range"} = "bytes */$length"; |
398 | $hdr->{"Content-Range"} = "bytes */$length"; |
360 | $self->err(416, "not satisfiable", $hdr); |
399 | $self->err(416, "not satisfiable", $hdr); |
361 | |
400 | |
362 | satisfiable: |
401 | satisfiable: |
|
|
402 | # check for segmented downloads |
|
|
403 | if ($l && $::NO_SEGMENTED) { |
|
|
404 | if (%{$uri{$self->{uri}}} > 1) { |
|
|
405 | $self->err(400, "segmented downloads are not allowed"); |
|
|
406 | } |
|
|
407 | } |
|
|
408 | |
363 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
364 | @code = (206, "partial content"); |
410 | @code = (206, "partial content"); |
365 | $length = $h - $l + 1; |
411 | $length = $h - $l + 1; |
366 | |
412 | |
367 | ignore: |
413 | ignore: |
368 | } else { |
414 | } else { |
369 | ($l, $h) = (0, $length - 1); |
415 | ($l, $h) = (0, $length - 1); |
370 | } |
416 | } |
371 | |
417 | |
372 | if ($self->{path} =~ /\.html$/) { |
418 | $self->{path} =~ /\.([^.]+)$/; |
373 | $hdr->{"Content-Type"} = "text/html"; |
|
|
374 | } else { |
|
|
375 | $hdr->{"Content-Type"} = "application/octet-stream"; |
419 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
376 | } |
|
|
377 | |
|
|
378 | $hdr->{"Content-Length"} = $length; |
420 | $hdr->{"Content-Length"} = $length; |
379 | |
421 | |
380 | $self->print_response(@code, $hdr, ""); |
422 | $self->response(@code, $hdr, ""); |
381 | |
423 | |
382 | if ($self->{method} eq "GET") { |
424 | if ($self->{method} eq "GET") { |
383 | my ($fh, $buf); |
425 | my ($fh, $buf); |
384 | open $fh, "<", $self->{path} |
426 | open $fh, "<", $self->{path} |
385 | or die "$self->{path}: late open failure ($!)"; |
427 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
390 | } |
432 | } |
391 | |
433 | |
392 | $h -= $l - 1; |
434 | $h -= $l - 1; |
393 | |
435 | |
394 | while ($h > 0) { |
436 | while ($h > 0) { |
395 | $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
437 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
396 | print {$self->{fh}} $buf |
438 | my $w = $self->{fh}->syswrite($buf) |
397 | or last; |
439 | or last; |
|
|
440 | $::written += $w; |
|
|
441 | $self->{written} += $w; |
398 | } |
442 | } |
399 | } |
443 | } |
400 | |
444 | |
401 | close $fh; |
445 | close $fh; |
402 | } |
446 | } |