… | |
… | |
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 | |
12 | |
13 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
14 | |
14 | |
|
|
15 | our $accesslog; |
|
|
16 | |
|
|
17 | if ($ACCESS_LOG) { |
|
|
18 | use IO::Handle; |
|
|
19 | open $accesslog, ">>$ACCESS_LOG" |
|
|
20 | or die "$ACCESS_LOG: $!"; |
|
|
21 | $accesslog->autoflush(1); |
|
|
22 | } |
|
|
23 | |
15 | sub slog { |
24 | sub slog { |
16 | my $level = shift; |
25 | my $level = shift; |
17 | my $format = shift; |
26 | my $format = shift; |
18 | printf "---: $format\n", @_; |
27 | printf "---: $format\n", @_; |
19 | } |
28 | } |
… | |
… | |
74 | use Convert::Scalar 'weaken'; |
83 | use Convert::Scalar 'weaken'; |
75 | use Linux::AIO; |
84 | use Linux::AIO; |
76 | |
85 | |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
78 | |
87 | |
|
|
88 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
89 | |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
90 | Event->io(fd => Linux::AIO::poll_fileno, |
80 | poll => 'r', async => 1, |
91 | poll => 'r', async => 1, |
81 | cb => \&Linux::AIO::poll_cb); |
92 | cb => \&Linux::AIO::poll_cb); |
82 | |
93 | |
83 | our %conn; # $conn{ip}{self} => connobj |
94 | our %conn; # $conn{ip}{self} => connobj |
… | |
… | |
134 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
135 | } |
146 | } |
136 | |
147 | |
137 | sub slog { |
148 | sub slog { |
138 | my $self = shift; |
149 | my $self = shift; |
139 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
140 | } |
151 | } |
141 | |
152 | |
142 | sub response { |
153 | sub response { |
143 | my ($self, $code, $msg, $hdr, $content) = @_; |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
144 | my $res = "HTTP/1.1 $code $msg\015\012"; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
145 | |
156 | |
146 | #$res .= "Connection: close\015\012"; |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
|
|
158 | |
147 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
148 | |
160 | |
149 | while (my ($h, $v) = each %$hdr) { |
161 | while (my ($h, $v) = each %$hdr) { |
150 | $res .= "$h: $v\015\012" |
162 | $res .= "$h: $v\015\012" |
151 | } |
163 | } |
152 | $res .= "\015\012"; |
164 | $res .= "\015\012"; |
153 | |
165 | |
154 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
166 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
155 | |
167 | |
156 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
168 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
|
|
169 | |
|
|
170 | print $accesslog $log if $accesslog; |
|
|
171 | print STDERR $log; |
157 | |
172 | |
158 | $self->{written} += |
173 | $self->{written} += |
159 | print {$self->{fh}} $res; |
174 | print {$self->{fh}} $res; |
160 | } |
175 | } |
161 | |
176 | |
… | |
… | |
173 | $self->response($code, $msg, $hdr, $content); |
188 | $self->response($code, $msg, $hdr, $content); |
174 | |
189 | |
175 | die bless {}, err::; |
190 | die bless {}, err::; |
176 | } |
191 | } |
177 | |
192 | |
178 | sub err_blocked { |
|
|
179 | my $self = shift; |
|
|
180 | my $ip = $self->{remote_addr}; |
|
|
181 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
182 | |
|
|
183 | Coro::Event::do_timer(after => 20*rand); |
|
|
184 | |
|
|
185 | $self->err(401, "too many connections", |
|
|
186 | { |
|
|
187 | "Content-Type" => "text/html", |
|
|
188 | "Retry-After" => $::BLOCKTIME, |
|
|
189 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
190 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
|
|
191 | }, |
|
|
192 | <<EOF); |
|
|
193 | <html><p> |
|
|
194 | You have been blocked because you opened too many connections. You |
|
|
195 | may retry at</p> |
|
|
196 | |
|
|
197 | <p><blockquote>$time.</blockquote></p> |
|
|
198 | |
|
|
199 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
200 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
|
|
201 | </html> |
|
|
202 | EOF |
|
|
203 | } |
|
|
204 | |
|
|
205 | sub handle { |
193 | sub handle { |
206 | my $self = shift; |
194 | my $self = shift; |
207 | my $fh = $self->{fh}; |
195 | my $fh = $self->{fh}; |
|
|
196 | |
|
|
197 | my $host; |
208 | |
198 | |
209 | $fh->timeout($::REQ_TIMEOUT); |
199 | $fh->timeout($::REQ_TIMEOUT); |
210 | while() { |
200 | while() { |
211 | $self->{reqs}++; |
201 | $self->{reqs}++; |
212 | |
202 | |
… | |
… | |
269 | |
259 | |
270 | $self->{h}{$h} = substr $v, 1 |
260 | $self->{h}{$h} = substr $v, 1 |
271 | while ($h, $v) = each %hdr; |
261 | while ($h, $v) = each %hdr; |
272 | } |
262 | } |
273 | |
263 | |
|
|
264 | # find out server name and port |
|
|
265 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
|
|
266 | $host = $1; |
|
|
267 | } else { |
|
|
268 | $host = $self->{h}{host}; |
|
|
269 | } |
|
|
270 | |
|
|
271 | if (defined $host) { |
274 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
|
|
273 | } else { |
|
|
274 | ($self->{server_port}, $host) |
|
|
275 | = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
276 | or $self->err(500, "unable to get socket name"); |
|
|
277 | $host = inet_ntoa $host; |
|
|
278 | } |
|
|
279 | |
|
|
280 | $self->{server_name} = $host; |
|
|
281 | |
|
|
282 | # remote id should be unique per user |
|
|
283 | $self->{remote_id} = $self->{remote_addr}; |
|
|
284 | |
|
|
285 | if (exists $self->{h}{"client-ip"}) { |
|
|
286 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
287 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
288 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
289 | } |
275 | |
290 | |
276 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
291 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
277 | |
292 | |
278 | eval { |
293 | eval { |
279 | $self->map_uri; |
294 | $self->map_uri; |
… | |
… | |
282 | |
297 | |
283 | $self->eoconn; |
298 | $self->eoconn; |
284 | |
299 | |
285 | die if $@ && !ref $@; |
300 | die if $@ && !ref $@; |
286 | |
301 | |
287 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
302 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
288 | |
303 | |
289 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
290 | $fh->timeout($::PER_TIMEOUT); |
304 | $fh->timeout($::PER_TIMEOUT); |
291 | } |
305 | } |
292 | } |
306 | } |
293 | |
307 | |
294 | # uri => path mapping |
308 | # uri => path mapping |
295 | sub map_uri { |
309 | sub map_uri { |
296 | my $self = shift; |
310 | my $self = shift; |
297 | my $host = $self->{h}{host} || "default"; |
311 | my $host = $self->{server_name}; |
298 | my $uri = $self->{uri}; |
312 | my $uri = $self->{uri}; |
299 | |
313 | |
300 | # some massaging, also makes it more secure |
314 | # some massaging, also makes it more secure |
301 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
315 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
302 | $uri =~ s%//+%/%g; |
316 | $uri =~ s%//+%/%g; |
… | |
… | |
312 | $self->{path} = "$::DOCROOT/$host$uri"; |
326 | $self->{path} = "$::DOCROOT/$host$uri"; |
313 | |
327 | |
314 | $self->access_check; |
328 | $self->access_check; |
315 | } |
329 | } |
316 | |
330 | |
317 | sub server_address { |
|
|
318 | my $self = shift; |
|
|
319 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
|
|
320 | or $self->err(500, "unable to get socket name"); |
|
|
321 | ((inet_ntoa $iaddr), $port); |
|
|
322 | } |
|
|
323 | |
|
|
324 | sub server_host { |
|
|
325 | my $self = shift; |
|
|
326 | if (exists $self->{h}{host}) { |
|
|
327 | return $self->{h}{host}; |
|
|
328 | } else { |
|
|
329 | return (($self->server_address)[0]); |
|
|
330 | } |
|
|
331 | } |
|
|
332 | |
|
|
333 | sub server_hostport { |
|
|
334 | my $self = shift; |
|
|
335 | my ($host, $port); |
|
|
336 | if (exists $self->{h}{host}) { |
|
|
337 | ($host, $port) = ($self->{h}{host}, $self->{server_port}); |
|
|
338 | } else { |
|
|
339 | ($host, $port) = $self->server_address; |
|
|
340 | } |
|
|
341 | $port = $port == 80 ? "" : ":$port"; |
|
|
342 | $host.$port; |
|
|
343 | } |
|
|
344 | |
|
|
345 | sub _cgi { |
331 | sub _cgi { |
346 | my $self = shift; |
332 | my $self = shift; |
347 | my $path = shift; |
333 | my $path = shift; |
348 | my $fh; |
334 | my $fh; |
349 | |
335 | |
350 | # no two-way xxx supported |
336 | # no two-way xxx supported |
351 | if (0 == fork) { |
337 | if (0 == fork) { |
352 | open STDOUT, ">&".fileno($self->{fh}); |
338 | open STDOUT, ">&".fileno($self->{fh}); |
353 | if (chdir $::DOCROOT) { |
339 | if (chdir $::DOCROOT) { |
354 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
340 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
355 | $ENV{HTTP_HOST} = $self->server_host; |
341 | $ENV{HTTP_HOST} = $self->{server_name}; |
356 | $ENV{HTTP_PORT} = $self->{server_host}; |
342 | $ENV{HTTP_PORT} = $self->{server_port}; |
357 | $ENV{SCRIPT_NAME} = $self->{name}; |
343 | $ENV{SCRIPT_NAME} = $self->{name}; |
358 | exec $path; |
344 | exec $path; |
359 | } |
345 | } |
360 | Coro::State::_exit(0); |
346 | Coro::State::_exit(0); |
361 | } else { |
347 | } else { |
|
|
348 | die; |
362 | } |
349 | } |
|
|
350 | } |
|
|
351 | |
|
|
352 | sub server_hostport { |
|
|
353 | $_[0]{server_port} == 80 |
|
|
354 | ? $_[0]{server_name} |
|
|
355 | : "$_[0]{server_name}:$_[0]{server_port}"; |
363 | } |
356 | } |
364 | |
357 | |
365 | sub respond { |
358 | sub respond { |
366 | my $self = shift; |
359 | my $self = shift; |
367 | my $path = $self->{path}; |
360 | my $path = $self->{path}; |
… | |
… | |
377 | |
370 | |
378 | if (-d _ && -r _) { |
371 | if (-d _ && -r _) { |
379 | # directory |
372 | # directory |
380 | if ($path !~ /\/$/) { |
373 | if ($path !~ /\/$/) { |
381 | # create a redirect to get the trailing "/" |
374 | # create a redirect to get the trailing "/" |
382 | my $host = $self->server_hostport; |
375 | # we don't try to avoid the :80 |
383 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
376 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
384 | } else { |
377 | } else { |
385 | $ims < $self->{stat}[9] |
378 | $ims < $self->{stat}[9] |
386 | or $self->err(304, "not modified"); |
379 | or $self->err(304, "not modified"); |
387 | |
380 | |
388 | if (-r "$path/index.html") { |
381 | if (-r "$path/index.html") { |
… | |
… | |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
433 | $self->err(416, "not satisfiable", $hdr, ""); |
441 | |
434 | |
442 | satisfiable: |
435 | satisfiable: |
443 | # check for segmented downloads |
436 | # check for segmented downloads |
444 | if ($l && $::NO_SEGMENTED) { |
437 | if ($l && $::NO_SEGMENTED) { |
|
|
438 | my $delay = 180; |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
439 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
446 | $self->err(400, "segmented downloads are not allowed"); |
440 | if ($delay <= 0) { |
|
|
441 | $self->err_segmented_download; |
|
|
442 | } else { |
|
|
443 | Coro::Event::do_timer(after => 3); $delay -= 3; |
|
|
444 | } |
447 | } |
445 | } |
448 | } |
446 | } |
449 | |
447 | |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
448 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
451 | @code = (206, "partial content"); |
449 | @code = (206, "partial content"); |
… | |
… | |
479 | while ($h > 0) { |
477 | while ($h > 0) { |
480 | if (0) { |
478 | if (0) { |
481 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
479 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
482 | or last; |
480 | or last; |
483 | } else { |
481 | } else { |
|
|
482 | undef $buf; |
|
|
483 | $aio_requests->down; |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
485 | $buf, 0, sub { |
485 | $buf, 0, sub { |
486 | $r = $_[0]; |
486 | $r = $_[0]; |
487 | $current->ready; |
487 | $current->ready; |
488 | }); |
488 | }); |
489 | &Coro::schedule; |
489 | &Coro::schedule; |
|
|
490 | $aio_requests->up; |
490 | last unless $r; |
491 | last unless $r; |
491 | } |
492 | } |
492 | my $w = $self->{fh}->syswrite($buf) |
493 | my $w = $self->{fh}->syswrite($buf) |
493 | or last; |
494 | or last; |
494 | $::written += $w; |
495 | $::written += $w; |