… | |
… | |
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; |
… | |
… | |
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 | |
91 | $self; |
109 | $self; |
… | |
… | |
110 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
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 | } |
|
|
133 | $res .= "\015\012"; |
115 | |
134 | |
116 | $res .= "\015\012$content" if defined $content; |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
117 | |
136 | |
118 | 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# |
119 | |
138 | |
|
|
139 | $self->{written} += |
120 | print {$self->{fh}} $res; |
140 | print {$self->{fh}} $res; |
121 | } |
141 | } |
122 | |
142 | |
123 | sub err { |
143 | sub err { |
124 | my $self = shift; |
144 | my $self = shift; |
125 | my ($code, $msg, $hdr, $content) = @_; |
145 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
137 | |
157 | |
138 | sub err_blocked { |
158 | sub err_blocked { |
139 | my $self = shift; |
159 | my $self = shift; |
140 | my $ip = $self->{remote_addr}; |
160 | my $ip = $self->{remote_addr}; |
141 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
162 | |
|
|
163 | Coro::Event::do_timer(after => 5); |
|
|
164 | |
142 | $self->err(403, "too many connections", |
165 | $self->err(403, "too many connections", |
143 | { |
166 | { |
144 | "Content-Type" => "text/html", |
167 | "Content-Type" => "text/html", |
145 | "Retry-After" => $::BLOCKTIME |
168 | "Retry-After" => $::BLOCKTIME |
146 | }, |
169 | }, |
… | |
… | |
180 | |
203 | |
181 | delete $blocked{$ip}; |
204 | delete $blocked{$ip}; |
182 | } |
205 | } |
183 | |
206 | |
184 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
207 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
185 | $self->slog("blocked ip $ip"); |
208 | $self->slog(2, "blocked ip $ip"); |
186 | $self->err_blocked; |
209 | $self->err_blocked; |
187 | } |
210 | } |
188 | |
211 | |
189 | $req =~ /^(?:\015\012)? |
212 | $req =~ /^(?:\015\012)? |
190 | (GET|HEAD) \040+ |
213 | (GET|HEAD) \040+ |
… | |
… | |
221 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
222 | |
245 | |
223 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
224 | |
247 | |
225 | $self->map_uri; |
248 | $self->map_uri; |
226 | |
|
|
227 | Coro::Event::do_timer(after => 5); |
|
|
228 | |
|
|
229 | $self->respond; |
249 | $self->respond; |
230 | #} |
250 | #} |
231 | } |
251 | } |
232 | |
252 | |
233 | # uri => path mapping |
253 | # uri => path mapping |
… | |
… | |
247 | |
267 | |
248 | $self->{name} = $uri; |
268 | $self->{name} = $uri; |
249 | |
269 | |
250 | # now do the path mapping |
270 | # now do the path mapping |
251 | $self->{path} = "$::DOCROOT/$host$uri"; |
271 | $self->{path} = "$::DOCROOT/$host$uri"; |
|
|
272 | |
|
|
273 | $self->access_check; |
252 | } |
274 | } |
253 | |
275 | |
254 | sub server_address { |
276 | sub server_address { |
255 | my $self = shift; |
277 | my $self = shift; |
256 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
278 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
… | |
… | |
290 | if (chdir $::DOCROOT) { |
312 | if (chdir $::DOCROOT) { |
291 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
313 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
292 | $ENV{HTTP_HOST} = $self->server_host; |
314 | $ENV{HTTP_HOST} = $self->server_host; |
293 | $ENV{HTTP_PORT} = $self->{server_host}; |
315 | $ENV{HTTP_PORT} = $self->{server_host}; |
294 | $ENV{SCRIPT_NAME} = $self->{name}; |
316 | $ENV{SCRIPT_NAME} = $self->{name}; |
295 | exec $::INDEXPROG; |
317 | exec $path; |
296 | } |
318 | } |
297 | Coro::State::_exit(0); |
319 | Coro::State::_exit(0); |
298 | } else { |
320 | } else { |
299 | } |
321 | } |
300 | } |
322 | } |
… | |
… | |
303 | my $self = shift; |
325 | my $self = shift; |
304 | my $path = $self->{path}; |
326 | my $path = $self->{path}; |
305 | |
327 | |
306 | stat $path |
328 | stat $path |
307 | or $self->err(404, "not found"); |
329 | or $self->err(404, "not found"); |
|
|
330 | |
|
|
331 | $self->{stat} = [stat _]; |
308 | |
332 | |
309 | # idiotic netscape sends idiotic headers AGAIN |
333 | # idiotic netscape sends idiotic headers AGAIN |
310 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
334 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
311 | ? str2time $1 : 0; |
335 | ? str2time $1 : 0; |
312 | |
336 | |
… | |
… | |
315 | if ($path !~ /\/$/) { |
339 | if ($path !~ /\/$/) { |
316 | # create a redirect to get the trailing "/" |
340 | # create a redirect to get the trailing "/" |
317 | my $host = $self->server_hostport; |
341 | my $host = $self->server_hostport; |
318 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
342 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
319 | } else { |
343 | } else { |
320 | $ims < (stat _)[9] |
344 | $ims < $self->{stat}[9] |
321 | or $self->err(304, "not modified"); |
345 | or $self->err(304, "not modified"); |
322 | |
346 | |
323 | if ($self->{method} eq "GET") { |
347 | if ($self->{method} eq "GET") { |
324 | if (-r "$path/index.html") { |
348 | if (-r "$path/index.html") { |
325 | $self->{path} .= "/index.html"; |
349 | $self->{path} .= "/index.html"; |
… | |
… | |
337 | } |
361 | } |
338 | } |
362 | } |
339 | |
363 | |
340 | sub handle_dir { |
364 | sub handle_dir { |
341 | my $self = shift; |
365 | my $self = shift; |
342 | $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); |
343 | } |
374 | } |
344 | |
375 | |
345 | sub handle_file { |
376 | sub handle_file { |
346 | my $self = shift; |
377 | my $self = shift; |
347 | my $length = -s _; |
378 | my $length = -s _; |
… | |
… | |
367 | $hdr->{"Content-Range"} = "bytes */$length"; |
398 | $hdr->{"Content-Range"} = "bytes */$length"; |
368 | $self->err(416, "not satisfiable", $hdr); |
399 | $self->err(416, "not satisfiable", $hdr); |
369 | |
400 | |
370 | satisfiable: |
401 | satisfiable: |
371 | # check for segmented downloads |
402 | # check for segmented downloads |
372 | if ($l && $NO_SEGMENTED) { |
403 | if ($l && $::NO_SEGMENTED) { |
373 | if (%{$uri{$self->{uri}}} > 1) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
374 | $self->slog("segmented download refused\n"); |
|
|
375 | $self->err(400, "segmented downloads are not allowed"); |
405 | $self->err(400, "segmented downloads are not allowed"); |
376 | } |
406 | } |
377 | } |
407 | } |
378 | |
408 | |
379 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
383 | ignore: |
413 | ignore: |
384 | } else { |
414 | } else { |
385 | ($l, $h) = (0, $length - 1); |
415 | ($l, $h) = (0, $length - 1); |
386 | } |
416 | } |
387 | |
417 | |
388 | if ($self->{path} =~ /\.html$/) { |
418 | $self->{path} =~ /\.([^.]+)$/; |
389 | $hdr->{"Content-Type"} = "text/html"; |
|
|
390 | } else { |
|
|
391 | $hdr->{"Content-Type"} = "application/octet-stream"; |
419 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
392 | } |
|
|
393 | |
|
|
394 | $hdr->{"Content-Length"} = $length; |
420 | $hdr->{"Content-Length"} = $length; |
395 | |
421 | |
396 | $self->response(@code, $hdr, ""); |
422 | $self->response(@code, $hdr, ""); |
397 | |
423 | |
398 | if ($self->{method} eq "GET") { |
424 | if ($self->{method} eq "GET") { |
… | |
… | |
406 | } |
432 | } |
407 | |
433 | |
408 | $h -= $l - 1; |
434 | $h -= $l - 1; |
409 | |
435 | |
410 | while ($h > 0) { |
436 | while ($h > 0) { |
411 | $h -= sysread $fh, $buf, $h > 16384 ? 16384 : $h; |
437 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
412 | print {$self->{fh}} $buf |
438 | my $w = $self->{fh}->syswrite($buf) |
413 | or last; |
439 | or last; |
414 | cede; |
440 | $::written += $w; |
|
|
441 | $self->{written} += $w; |
415 | } |
442 | } |
416 | } |
443 | } |
417 | |
444 | |
418 | close $fh; |
445 | close $fh; |
419 | } |
446 | } |