… | |
… | |
77 | our %blocked; |
77 | our %blocked; |
78 | our %mimetype; |
78 | our %mimetype; |
79 | |
79 | |
80 | sub read_mimetypes { |
80 | sub read_mimetypes { |
81 | local *M; |
81 | local *M; |
82 | if (open M, "<mimetypes") { |
82 | if (open M, "<mime_types") { |
83 | while (<M>) { |
83 | while (<M>) { |
84 | if (/^([^#]\S+)\t+(\S+)$/) { |
84 | if (/^([^#]\S+)\t+(\S+)$/) { |
85 | $mimetype{lc $1} = $2; |
85 | $mimetype{lc $1} = $2; |
86 | } |
86 | } |
87 | } |
87 | } |
88 | } else { |
88 | } else { |
89 | $self->slog(1, "cannot open mimetypes\n"); |
89 | print "cannot open mime_types\n"; |
90 | } |
90 | } |
91 | } |
91 | } |
|
|
92 | |
|
|
93 | read_mimetypes; |
92 | |
94 | |
93 | sub new { |
95 | sub new { |
94 | my $class = shift; |
96 | my $class = shift; |
95 | my $peername = shift; |
97 | my $peername = shift; |
96 | my $fh = shift; |
98 | my $fh = shift; |
97 | my $self = bless { fh => $fh }, $class; |
99 | my $self = bless { fh => $fh }, $class; |
98 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
99 | or $self->err(500, "unable to decode peername"); |
101 | or $self->err(500, "unable to decode peername"); |
100 | |
102 | |
101 | $self->{remote_addr} = inet_ntoa $iaddr; |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
104 | $self->{time} = $::NOW; |
102 | |
105 | |
103 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
104 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
105 | |
108 | |
106 | $self; |
109 | $self; |
… | |
… | |
125 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
128 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
126 | |
129 | |
127 | while (my ($h, $v) = each %$hdr) { |
130 | while (my ($h, $v) = each %$hdr) { |
128 | $res .= "$h: $v\015\012" |
131 | $res .= "$h: $v\015\012" |
129 | } |
132 | } |
|
|
133 | $res .= "\015\012"; |
130 | |
134 | |
131 | $res .= "\015\012$content" if defined $content; |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
132 | |
136 | |
133 | 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# |
134 | |
138 | |
|
|
139 | $self->{written} += |
135 | print {$self->{fh}} $res; |
140 | print {$self->{fh}} $res; |
136 | } |
141 | } |
137 | |
142 | |
138 | sub err { |
143 | sub err { |
139 | my $self = shift; |
144 | my $self = shift; |
140 | my ($code, $msg, $hdr, $content) = @_; |
145 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
152 | |
157 | |
153 | sub err_blocked { |
158 | sub err_blocked { |
154 | my $self = shift; |
159 | my $self = shift; |
155 | my $ip = $self->{remote_addr}; |
160 | my $ip = $self->{remote_addr}; |
156 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
162 | |
|
|
163 | Coro::Event::do_timer(after => 5); |
|
|
164 | |
157 | $self->err(403, "too many connections", |
165 | $self->err(403, "too many connections", |
158 | { |
166 | { |
159 | "Content-Type" => "text/html", |
167 | "Content-Type" => "text/html", |
160 | "Retry-After" => $::BLOCKTIME |
168 | "Retry-After" => $::BLOCKTIME |
161 | }, |
169 | }, |
… | |
… | |
195 | |
203 | |
196 | delete $blocked{$ip}; |
204 | delete $blocked{$ip}; |
197 | } |
205 | } |
198 | |
206 | |
199 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
207 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
200 | $self->slog("blocked ip $ip"); |
208 | $self->slog(2, "blocked ip $ip"); |
201 | $self->err_blocked; |
209 | $self->err_blocked; |
202 | } |
210 | } |
203 | |
211 | |
204 | $req =~ /^(?:\015\012)? |
212 | $req =~ /^(?:\015\012)? |
205 | (GET|HEAD) \040+ |
213 | (GET|HEAD) \040+ |
… | |
… | |
304 | if (chdir $::DOCROOT) { |
312 | if (chdir $::DOCROOT) { |
305 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
313 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
306 | $ENV{HTTP_HOST} = $self->server_host; |
314 | $ENV{HTTP_HOST} = $self->server_host; |
307 | $ENV{HTTP_PORT} = $self->{server_host}; |
315 | $ENV{HTTP_PORT} = $self->{server_host}; |
308 | $ENV{SCRIPT_NAME} = $self->{name}; |
316 | $ENV{SCRIPT_NAME} = $self->{name}; |
309 | exec $::INDEXPROG; |
317 | exec $path; |
310 | } |
318 | } |
311 | Coro::State::_exit(0); |
319 | Coro::State::_exit(0); |
312 | } else { |
320 | } else { |
313 | } |
321 | } |
314 | } |
322 | } |
… | |
… | |
317 | my $self = shift; |
325 | my $self = shift; |
318 | my $path = $self->{path}; |
326 | my $path = $self->{path}; |
319 | |
327 | |
320 | stat $path |
328 | stat $path |
321 | or $self->err(404, "not found"); |
329 | or $self->err(404, "not found"); |
|
|
330 | |
|
|
331 | $self->{stat} = [stat _]; |
322 | |
332 | |
323 | # idiotic netscape sends idiotic headers AGAIN |
333 | # idiotic netscape sends idiotic headers AGAIN |
324 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
334 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
325 | ? str2time $1 : 0; |
335 | ? str2time $1 : 0; |
326 | |
336 | |
… | |
… | |
329 | if ($path !~ /\/$/) { |
339 | if ($path !~ /\/$/) { |
330 | # create a redirect to get the trailing "/" |
340 | # create a redirect to get the trailing "/" |
331 | my $host = $self->server_hostport; |
341 | my $host = $self->server_hostport; |
332 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
342 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
333 | } else { |
343 | } else { |
334 | $ims < (stat _)[9] |
344 | $ims < $self->{stat}[9] |
335 | or $self->err(304, "not modified"); |
345 | or $self->err(304, "not modified"); |
336 | |
346 | |
337 | if ($self->{method} eq "GET") { |
347 | if ($self->{method} eq "GET") { |
338 | if (-r "$path/index.html") { |
348 | if (-r "$path/index.html") { |
339 | $self->{path} .= "/index.html"; |
349 | $self->{path} .= "/index.html"; |
… | |
… | |
351 | } |
361 | } |
352 | } |
362 | } |
353 | |
363 | |
354 | sub handle_dir { |
364 | sub handle_dir { |
355 | my $self = shift; |
365 | my $self = shift; |
356 | $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); |
357 | } |
374 | } |
358 | |
375 | |
359 | sub handle_file { |
376 | sub handle_file { |
360 | my $self = shift; |
377 | my $self = shift; |
361 | my $length = -s _; |
378 | my $length = -s _; |
… | |
… | |
381 | $hdr->{"Content-Range"} = "bytes */$length"; |
398 | $hdr->{"Content-Range"} = "bytes */$length"; |
382 | $self->err(416, "not satisfiable", $hdr); |
399 | $self->err(416, "not satisfiable", $hdr); |
383 | |
400 | |
384 | satisfiable: |
401 | satisfiable: |
385 | # check for segmented downloads |
402 | # check for segmented downloads |
386 | if ($l && $NO_SEGMENTED) { |
403 | if ($l && $::NO_SEGMENTED) { |
387 | if (%{$uri{$self->{uri}}} > 1) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
388 | $self->slog("segmented download refused\n"); |
|
|
389 | $self->err(400, "segmented downloads are not allowed"); |
405 | $self->err(400, "segmented downloads are not allowed"); |
390 | } |
406 | } |
391 | } |
407 | } |
392 | |
408 | |
393 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
417 | |
433 | |
418 | $h -= $l - 1; |
434 | $h -= $l - 1; |
419 | |
435 | |
420 | while ($h > 0) { |
436 | while ($h > 0) { |
421 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
437 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
422 | $self->{fh}->syswrite($buf) |
438 | my $w = $self->{fh}->syswrite($buf) |
423 | or last; |
439 | or last; |
|
|
440 | $::written += $w; |
|
|
441 | $self->{written} += $w; |
424 | } |
442 | } |
425 | } |
443 | } |
426 | |
444 | |
427 | close $fh; |
445 | close $fh; |
428 | } |
446 | } |