… | |
… | |
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; |
… | |
… | |
125 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
127 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
126 | |
128 | |
127 | while (my ($h, $v) = each %$hdr) { |
129 | while (my ($h, $v) = each %$hdr) { |
128 | $res .= "$h: $v\015\012" |
130 | $res .= "$h: $v\015\012" |
129 | } |
131 | } |
|
|
132 | $res .= "\015\012"; |
130 | |
133 | |
131 | $res .= "\015\012$content" if defined $content; |
134 | $res .= $content if defined $content and $self->{method} eq "GET"; |
132 | |
135 | |
133 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
136 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
134 | |
137 | |
135 | print {$self->{fh}} $res; |
138 | print {$self->{fh}} $res; |
136 | } |
139 | } |
… | |
… | |
152 | |
155 | |
153 | sub err_blocked { |
156 | sub err_blocked { |
154 | my $self = shift; |
157 | my $self = shift; |
155 | my $ip = $self->{remote_addr}; |
158 | my $ip = $self->{remote_addr}; |
156 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
159 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
160 | |
|
|
161 | Coro::Event::do_timer(after => 5); |
|
|
162 | |
157 | $self->err(403, "too many connections", |
163 | $self->err(403, "too many connections", |
158 | { |
164 | { |
159 | "Content-Type" => "text/html", |
165 | "Content-Type" => "text/html", |
160 | "Retry-After" => $::BLOCKTIME |
166 | "Retry-After" => $::BLOCKTIME |
161 | }, |
167 | }, |
… | |
… | |
304 | if (chdir $::DOCROOT) { |
310 | if (chdir $::DOCROOT) { |
305 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
311 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
306 | $ENV{HTTP_HOST} = $self->server_host; |
312 | $ENV{HTTP_HOST} = $self->server_host; |
307 | $ENV{HTTP_PORT} = $self->{server_host}; |
313 | $ENV{HTTP_PORT} = $self->{server_host}; |
308 | $ENV{SCRIPT_NAME} = $self->{name}; |
314 | $ENV{SCRIPT_NAME} = $self->{name}; |
309 | exec $::INDEXPROG; |
315 | exec $path; |
310 | } |
316 | } |
311 | Coro::State::_exit(0); |
317 | Coro::State::_exit(0); |
312 | } else { |
318 | } else { |
313 | } |
319 | } |
314 | } |
320 | } |
… | |
… | |
317 | my $self = shift; |
323 | my $self = shift; |
318 | my $path = $self->{path}; |
324 | my $path = $self->{path}; |
319 | |
325 | |
320 | stat $path |
326 | stat $path |
321 | or $self->err(404, "not found"); |
327 | or $self->err(404, "not found"); |
|
|
328 | |
|
|
329 | $self->{stat} = [stat _]; |
322 | |
330 | |
323 | # idiotic netscape sends idiotic headers AGAIN |
331 | # idiotic netscape sends idiotic headers AGAIN |
324 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
332 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
325 | ? str2time $1 : 0; |
333 | ? str2time $1 : 0; |
326 | |
334 | |
… | |
… | |
329 | if ($path !~ /\/$/) { |
337 | if ($path !~ /\/$/) { |
330 | # create a redirect to get the trailing "/" |
338 | # create a redirect to get the trailing "/" |
331 | my $host = $self->server_hostport; |
339 | my $host = $self->server_hostport; |
332 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
340 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
333 | } else { |
341 | } else { |
334 | $ims < (stat _)[9] |
342 | $ims < $self->{stat}[9] |
335 | or $self->err(304, "not modified"); |
343 | or $self->err(304, "not modified"); |
336 | |
344 | |
337 | if ($self->{method} eq "GET") { |
345 | if ($self->{method} eq "GET") { |
338 | if (-r "$path/index.html") { |
346 | if (-r "$path/index.html") { |
339 | $self->{path} .= "/index.html"; |
347 | $self->{path} .= "/index.html"; |
… | |
… | |
351 | } |
359 | } |
352 | } |
360 | } |
353 | |
361 | |
354 | sub handle_dir { |
362 | sub handle_dir { |
355 | my $self = shift; |
363 | my $self = shift; |
356 | $self->_cgi($::INDEXPROG); |
364 | my $idx = $self->diridx; |
|
|
365 | |
|
|
366 | $self->response(200, "ok", |
|
|
367 | { |
|
|
368 | "Content-Type" => "text/html", |
|
|
369 | "Content-Length" => length $idx, |
|
|
370 | }, |
|
|
371 | $idx); |
357 | } |
372 | } |
358 | |
373 | |
359 | sub handle_file { |
374 | sub handle_file { |
360 | my $self = shift; |
375 | my $self = shift; |
361 | my $length = -s _; |
376 | my $length = -s _; |
… | |
… | |
381 | $hdr->{"Content-Range"} = "bytes */$length"; |
396 | $hdr->{"Content-Range"} = "bytes */$length"; |
382 | $self->err(416, "not satisfiable", $hdr); |
397 | $self->err(416, "not satisfiable", $hdr); |
383 | |
398 | |
384 | satisfiable: |
399 | satisfiable: |
385 | # check for segmented downloads |
400 | # check for segmented downloads |
386 | if ($l && $NO_SEGMENTED) { |
401 | if ($l && $::NO_SEGMENTED) { |
387 | if (%{$uri{$self->{uri}}} > 1) { |
402 | if (%{$uri{$self->{uri}}} > 1) { |
388 | $self->slog("segmented download refused\n"); |
403 | $self->slog("segmented download refused\n"); |
389 | $self->err(400, "segmented downloads are not allowed"); |
404 | $self->err(400, "segmented downloads are not allowed"); |
390 | } |
405 | } |
391 | } |
406 | } |