… | |
… | |
18 | printf "---: $format\n", @_; |
18 | printf "---: $format\n", @_; |
19 | } |
19 | } |
20 | |
20 | |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
22 | |
22 | |
23 | my @fh; |
23 | my @newcons; |
24 | my @pool; |
24 | my @pool; |
25 | |
25 | |
26 | # one "execution thread" |
26 | # one "execution thread" |
27 | sub handler { |
27 | sub handler { |
28 | while () { |
28 | while () { |
29 | my $fh = pop @fh; |
29 | my $new = pop @newcons; |
30 | if ($fh) { |
30 | if ($new) { |
31 | eval { |
31 | eval { |
32 | conn->new($fh)->handle; |
32 | conn->new(@$new)->handle; |
33 | }; |
33 | }; |
34 | close $fh; |
|
|
35 | slog 1, "$@" if $@ && !ref $@; |
34 | slog 1, "$@" if $@ && !ref $@; |
36 | $connections->up; |
35 | $connections->up; |
37 | } else { |
36 | } else { |
38 | last if @pool >= $MAX_POOL; |
37 | last if @pool >= $MAX_POOL; |
39 | push @pool, $Coro::current; |
38 | push @pool, $Coro::current; |
… | |
… | |
44 | |
43 | |
45 | my $http_port = new Coro::Socket |
44 | my $http_port = new Coro::Socket |
46 | LocalAddr => $SERVER_HOST, |
45 | LocalAddr => $SERVER_HOST, |
47 | LocalPort => $SERVER_PORT, |
46 | LocalPort => $SERVER_PORT, |
48 | ReuseAddr => 1, |
47 | ReuseAddr => 1, |
49 | Listen => 1, |
48 | Listen => 50, |
50 | or die "unable to start server"; |
49 | or die "unable to start server"; |
51 | |
50 | |
52 | push @listen_sockets, $http_port; |
51 | push @listen_sockets, $http_port; |
53 | |
52 | |
54 | # the "main thread" |
53 | # the "main thread" |
55 | async { |
54 | async { |
56 | slog 1, "accepting connections"; |
55 | slog 1, "accepting connections"; |
57 | while () { |
56 | while () { |
58 | $connections->down; |
57 | $connections->down; |
59 | push @fh, $http_port->accept; |
58 | push @newcons, [$http_port->accept]; |
60 | #slog 3, "accepted @$connections ".scalar(@pool); |
59 | #slog 3, "accepted @$connections ".scalar(@pool); |
61 | $::NOW = time; |
60 | $::NOW = time; |
62 | if (@pool) { |
61 | if (@pool) { |
63 | (pop @pool)->ready; |
62 | (pop @pool)->ready; |
64 | } else { |
63 | } else { |
… | |
… | |
74 | use HTTP::Date; |
73 | use HTTP::Date; |
75 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
76 | |
75 | |
77 | our %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
78 | 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; |
79 | |
94 | |
80 | sub new { |
95 | sub new { |
81 | my $class = shift; |
96 | my $class = shift; |
|
|
97 | my $peername = shift; |
82 | my $fh = shift; |
98 | my $fh = shift; |
83 | my $self = bless { fh => $fh }, $class; |
99 | my $self = bless { fh => $fh }, $class; |
84 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
85 | or $self->err(500, "unable to get peername"); |
101 | or $self->err(500, "unable to decode peername"); |
|
|
102 | |
86 | $self->{remote_addr} = inet_ntoa $iaddr; |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
104 | $self->{time} = $::NOW; |
87 | |
105 | |
88 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
89 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
90 | |
108 | |
|
|
109 | $::conns++; |
|
|
110 | |
91 | $self; |
111 | $self; |
92 | } |
112 | } |
93 | |
113 | |
94 | sub DESTROY { |
114 | sub DESTROY { |
95 | my $self = shift; |
115 | my $self = shift; |
|
|
116 | |
|
|
117 | $::conns--; |
|
|
118 | |
96 | delete $conn{$self->{remote_addr}}{$self*1}; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
97 | delete $uri{$self->{uri}}{$self*1}; |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
98 | } |
121 | } |
99 | |
122 | |
100 | sub slog { |
123 | sub slog { |
101 | my $self = shift; |
124 | my $self = shift; |
102 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
… | |
… | |
110 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
133 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
111 | |
134 | |
112 | while (my ($h, $v) = each %$hdr) { |
135 | while (my ($h, $v) = each %$hdr) { |
113 | $res .= "$h: $v\015\012" |
136 | $res .= "$h: $v\015\012" |
114 | } |
137 | } |
|
|
138 | $res .= "\015\012"; |
115 | |
139 | |
116 | $res .= "\015\012$content" if defined $content; |
140 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
117 | |
141 | |
118 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
142 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
119 | |
143 | |
|
|
144 | $self->{written} += |
120 | print {$self->{fh}} $res; |
145 | print {$self->{fh}} $res; |
121 | } |
146 | } |
122 | |
147 | |
123 | sub err { |
148 | sub err { |
124 | my $self = shift; |
149 | my $self = shift; |
125 | my ($code, $msg, $hdr, $content) = @_; |
150 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
137 | |
162 | |
138 | sub err_blocked { |
163 | sub err_blocked { |
139 | my $self = shift; |
164 | my $self = shift; |
140 | my $ip = $self->{remote_addr}; |
165 | my $ip = $self->{remote_addr}; |
141 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
167 | |
142 | $self->err(403, "too many connections", |
168 | $self->err(403, "too many connections", |
143 | { |
169 | { |
144 | "Content-Type" => "text/html", |
170 | "Content-Type" => "text/html", |
145 | "Retry-After" => $::BLOCKTIME |
171 | "Retry-After" => $::BLOCKTIME |
146 | }, |
172 | }, |
… | |
… | |
180 | |
206 | |
181 | delete $blocked{$ip}; |
207 | delete $blocked{$ip}; |
182 | } |
208 | } |
183 | |
209 | |
184 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
210 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
185 | $self->slog("blocked ip $ip"); |
211 | $self->slog(2, "blocked ip $ip"); |
186 | $self->err_blocked; |
212 | $self->err_blocked; |
187 | } |
213 | } |
188 | |
214 | |
189 | $req =~ /^(?:\015\012)? |
215 | $req =~ /^(?:\015\012)? |
190 | (GET|HEAD) \040+ |
216 | (GET|HEAD) \040+ |
… | |
… | |
218 | while ($h, $v) = each %hdr; |
244 | while ($h, $v) = each %hdr; |
219 | } |
245 | } |
220 | |
246 | |
221 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
247 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
222 | |
248 | |
223 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
249 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
224 | |
250 | |
225 | $self->map_uri; |
251 | $self->map_uri; |
226 | $self->respond; |
252 | $self->respond; |
227 | #} |
253 | #} |
228 | } |
254 | } |
… | |
… | |
244 | |
270 | |
245 | $self->{name} = $uri; |
271 | $self->{name} = $uri; |
246 | |
272 | |
247 | # now do the path mapping |
273 | # now do the path mapping |
248 | $self->{path} = "$::DOCROOT/$host$uri"; |
274 | $self->{path} = "$::DOCROOT/$host$uri"; |
|
|
275 | |
|
|
276 | $self->access_check; |
249 | } |
277 | } |
250 | |
278 | |
251 | sub server_address { |
279 | sub server_address { |
252 | my $self = shift; |
280 | my $self = shift; |
253 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
281 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
… | |
… | |
287 | if (chdir $::DOCROOT) { |
315 | if (chdir $::DOCROOT) { |
288 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
316 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
289 | $ENV{HTTP_HOST} = $self->server_host; |
317 | $ENV{HTTP_HOST} = $self->server_host; |
290 | $ENV{HTTP_PORT} = $self->{server_host}; |
318 | $ENV{HTTP_PORT} = $self->{server_host}; |
291 | $ENV{SCRIPT_NAME} = $self->{name}; |
319 | $ENV{SCRIPT_NAME} = $self->{name}; |
292 | exec $::INDEXPROG; |
320 | exec $path; |
293 | } |
321 | } |
294 | Coro::State::_exit(0); |
322 | Coro::State::_exit(0); |
295 | } else { |
323 | } else { |
296 | } |
324 | } |
297 | } |
325 | } |
… | |
… | |
300 | my $self = shift; |
328 | my $self = shift; |
301 | my $path = $self->{path}; |
329 | my $path = $self->{path}; |
302 | |
330 | |
303 | stat $path |
331 | stat $path |
304 | or $self->err(404, "not found"); |
332 | or $self->err(404, "not found"); |
|
|
333 | |
|
|
334 | $self->{stat} = [stat _]; |
305 | |
335 | |
306 | # idiotic netscape sends idiotic headers AGAIN |
336 | # idiotic netscape sends idiotic headers AGAIN |
307 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
337 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
308 | ? str2time $1 : 0; |
338 | ? str2time $1 : 0; |
309 | |
339 | |
… | |
… | |
312 | if ($path !~ /\/$/) { |
342 | if ($path !~ /\/$/) { |
313 | # create a redirect to get the trailing "/" |
343 | # create a redirect to get the trailing "/" |
314 | my $host = $self->server_hostport; |
344 | my $host = $self->server_hostport; |
315 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
345 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
316 | } else { |
346 | } else { |
317 | $ims < (stat _)[9] |
347 | $ims < $self->{stat}[9] |
318 | or $self->err(304, "not modified"); |
348 | or $self->err(304, "not modified"); |
319 | |
349 | |
320 | if ($self->{method} eq "GET") { |
350 | if ($self->{method} eq "GET") { |
321 | if (-r "$path/index.html") { |
351 | if (-r "$path/index.html") { |
322 | $self->{path} .= "/index.html"; |
352 | $self->{path} .= "/index.html"; |
… | |
… | |
334 | } |
364 | } |
335 | } |
365 | } |
336 | |
366 | |
337 | sub handle_dir { |
367 | sub handle_dir { |
338 | my $self = shift; |
368 | my $self = shift; |
339 | $self->_cgi($::INDEXPROG); |
369 | my $idx = $self->diridx; |
|
|
370 | |
|
|
371 | $self->response(200, "ok", |
|
|
372 | { |
|
|
373 | "Content-Type" => "text/html", |
|
|
374 | "Content-Length" => length $idx, |
|
|
375 | }, |
|
|
376 | $idx); |
340 | } |
377 | } |
341 | |
378 | |
342 | sub handle_file { |
379 | sub handle_file { |
343 | my $self = shift; |
380 | my $self = shift; |
344 | my $length = -s _; |
381 | my $length = -s _; |
… | |
… | |
364 | $hdr->{"Content-Range"} = "bytes */$length"; |
401 | $hdr->{"Content-Range"} = "bytes */$length"; |
365 | $self->err(416, "not satisfiable", $hdr); |
402 | $self->err(416, "not satisfiable", $hdr); |
366 | |
403 | |
367 | satisfiable: |
404 | satisfiable: |
368 | # check for segmented downloads |
405 | # check for segmented downloads |
369 | if ($l && $NO_SEGMENTED) { |
406 | if ($l && $::NO_SEGMENTED) { |
370 | if (%{$uri{$self->{uri}}} > 1) { |
407 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
371 | $self->slog("segmented download refused\n"); |
|
|
372 | $self->err(400, "segmented downloads are not allowed"); |
408 | $self->err(400, "segmented downloads are not allowed"); |
373 | } |
409 | } |
374 | } |
410 | } |
375 | |
411 | |
376 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
412 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
380 | ignore: |
416 | ignore: |
381 | } else { |
417 | } else { |
382 | ($l, $h) = (0, $length - 1); |
418 | ($l, $h) = (0, $length - 1); |
383 | } |
419 | } |
384 | |
420 | |
385 | if ($self->{path} =~ /\.html$/) { |
421 | $self->{path} =~ /\.([^.]+)$/; |
386 | $hdr->{"Content-Type"} = "text/html"; |
|
|
387 | } else { |
|
|
388 | $hdr->{"Content-Type"} = "application/octet-stream"; |
422 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
389 | } |
|
|
390 | |
|
|
391 | $hdr->{"Content-Length"} = $length; |
423 | $hdr->{"Content-Length"} = $length; |
392 | |
424 | |
393 | $self->response(@code, $hdr, ""); |
425 | $self->response(@code, $hdr, ""); |
394 | |
426 | |
395 | if ($self->{method} eq "GET") { |
427 | if ($self->{method} eq "GET") { |
… | |
… | |
404 | |
436 | |
405 | $h -= $l - 1; |
437 | $h -= $l - 1; |
406 | |
438 | |
407 | while ($h > 0) { |
439 | while ($h > 0) { |
408 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
440 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
409 | $self->{fh}->syswrite($buf) |
441 | my $w = $self->{fh}->syswrite($buf) |
410 | or last; |
442 | or last; |
|
|
443 | $::written += $w; |
|
|
444 | $self->{written} += $w; |
411 | } |
445 | } |
412 | } |
446 | } |
413 | |
447 | |
414 | close $fh; |
448 | close $fh; |
415 | } |
449 | } |