… | |
… | |
43 | |
43 | |
44 | my $http_port = new Coro::Socket |
44 | my $http_port = new Coro::Socket |
45 | LocalAddr => $SERVER_HOST, |
45 | LocalAddr => $SERVER_HOST, |
46 | LocalPort => $SERVER_PORT, |
46 | LocalPort => $SERVER_PORT, |
47 | ReuseAddr => 1, |
47 | ReuseAddr => 1, |
48 | Listen => 1, |
48 | Listen => 50, |
49 | or die "unable to start server"; |
49 | or die "unable to start server"; |
50 | |
50 | |
51 | push @listen_sockets, $http_port; |
51 | push @listen_sockets, $http_port; |
52 | |
52 | |
53 | # the "main thread" |
53 | # the "main thread" |
… | |
… | |
73 | use HTTP::Date; |
73 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
75 | |
75 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
77 | 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; |
78 | |
94 | |
79 | sub new { |
95 | sub new { |
80 | my $class = shift; |
96 | my $class = shift; |
81 | my $peername = 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 $peername |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
85 | or $self->err(500, "unable to decode peername"); |
101 | or $self->err(500, "unable to decode peername"); |
86 | |
102 | |
87 | $self->{remote_addr} = inet_ntoa $iaddr; |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
104 | $self->{time} = $::NOW; |
88 | |
105 | |
89 | # enter ourselves into various lists |
106 | # enter ourselves into various lists |
90 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
91 | |
108 | |
|
|
109 | $::conns++; |
|
|
110 | |
92 | $self; |
111 | $self; |
93 | } |
112 | } |
94 | |
113 | |
95 | sub DESTROY { |
114 | sub DESTROY { |
96 | my $self = shift; |
115 | my $self = shift; |
|
|
116 | |
|
|
117 | $::conns--; |
|
|
118 | |
97 | delete $conn{$self->{remote_addr}}{$self*1}; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
98 | delete $uri{$self->{uri}}{$self*1}; |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
99 | } |
121 | } |
100 | |
122 | |
101 | sub slog { |
123 | sub slog { |
102 | my $self = shift; |
124 | my $self = shift; |
103 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
… | |
… | |
111 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
133 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
112 | |
134 | |
113 | while (my ($h, $v) = each %$hdr) { |
135 | while (my ($h, $v) = each %$hdr) { |
114 | $res .= "$h: $v\015\012" |
136 | $res .= "$h: $v\015\012" |
115 | } |
137 | } |
|
|
138 | $res .= "\015\012"; |
116 | |
139 | |
117 | $res .= "\015\012$content" if defined $content; |
140 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
118 | |
141 | |
119 | 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# |
120 | |
143 | |
|
|
144 | $self->{written} += |
121 | print {$self->{fh}} $res; |
145 | print {$self->{fh}} $res; |
122 | } |
146 | } |
123 | |
147 | |
124 | sub err { |
148 | sub err { |
125 | my $self = shift; |
149 | my $self = shift; |
126 | my ($code, $msg, $hdr, $content) = @_; |
150 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
138 | |
162 | |
139 | sub err_blocked { |
163 | sub err_blocked { |
140 | my $self = shift; |
164 | my $self = shift; |
141 | my $ip = $self->{remote_addr}; |
165 | my $ip = $self->{remote_addr}; |
142 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
167 | |
|
|
168 | Coro::Event::do_timer(after => 15); |
|
|
169 | |
143 | $self->err(403, "too many connections", |
170 | $self->err(401, "too many connections", |
144 | { |
171 | { |
145 | "Content-Type" => "text/html", |
172 | "Content-Type" => "text/html", |
146 | "Retry-After" => $::BLOCKTIME |
173 | "Retry-After" => $::BLOCKTIME |
147 | }, |
174 | }, |
148 | <<EOF); |
175 | <<EOF); |
… | |
… | |
173 | defined $req or |
200 | defined $req or |
174 | $self->err(408, "request timeout"); |
201 | $self->err(408, "request timeout"); |
175 | |
202 | |
176 | my $ip = $self->{remote_addr}; |
203 | my $ip = $self->{remote_addr}; |
177 | |
204 | |
|
|
205 | $self->err_blocked($blocked{$ip}); |
178 | if ($blocked{$ip}) { |
206 | if ($blocked{$ip}) { |
179 | $self->err_blocked($blocked{$ip}) |
207 | $self->err_blocked($blocked{$ip}) |
180 | if $blocked{$ip} > $::NOW; |
208 | if $blocked{$ip} > $::NOW; |
181 | |
209 | |
182 | delete $blocked{$ip}; |
210 | delete $blocked{$ip}; |
183 | } |
211 | } |
184 | |
212 | |
185 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
213 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
186 | $self->slog("blocked ip $ip"); |
214 | $self->slog(2, "blocked ip $ip"); |
187 | $self->err_blocked; |
215 | $self->err_blocked; |
188 | } |
216 | } |
189 | |
217 | |
190 | $req =~ /^(?:\015\012)? |
218 | $req =~ /^(?:\015\012)? |
191 | (GET|HEAD) \040+ |
219 | (GET|HEAD) \040+ |
192 | ([^\040]+) \040+ |
220 | ([^\040]+) \040+ |
193 | HTTP\/([0-9]+\.[0-9]+) |
221 | HTTP\/([0-9]+\.[0-9]+) |
194 | \015\012/gx |
222 | \015\012/gx |
195 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
223 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
196 | |
224 | |
197 | $2 ne "1.0" |
225 | $2 ne "1.0" |
198 | or $self->err(506, "http protocol version not supported"); |
226 | or $self->err(506, "http protocol version not supported"); |
199 | |
227 | |
200 | $self->{method} = $1; |
228 | $self->{method} = $1; |
… | |
… | |
219 | while ($h, $v) = each %hdr; |
247 | while ($h, $v) = each %hdr; |
220 | } |
248 | } |
221 | |
249 | |
222 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
250 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
223 | |
251 | |
224 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
252 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
225 | |
253 | |
226 | $self->map_uri; |
254 | $self->map_uri; |
227 | $self->respond; |
255 | $self->respond; |
228 | #} |
256 | #} |
229 | } |
257 | } |
… | |
… | |
290 | if (chdir $::DOCROOT) { |
318 | if (chdir $::DOCROOT) { |
291 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
319 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
292 | $ENV{HTTP_HOST} = $self->server_host; |
320 | $ENV{HTTP_HOST} = $self->server_host; |
293 | $ENV{HTTP_PORT} = $self->{server_host}; |
321 | $ENV{HTTP_PORT} = $self->{server_host}; |
294 | $ENV{SCRIPT_NAME} = $self->{name}; |
322 | $ENV{SCRIPT_NAME} = $self->{name}; |
295 | exec $::INDEXPROG; |
323 | exec $path; |
296 | } |
324 | } |
297 | Coro::State::_exit(0); |
325 | Coro::State::_exit(0); |
298 | } else { |
326 | } else { |
299 | } |
327 | } |
300 | } |
328 | } |
… | |
… | |
303 | my $self = shift; |
331 | my $self = shift; |
304 | my $path = $self->{path}; |
332 | my $path = $self->{path}; |
305 | |
333 | |
306 | stat $path |
334 | stat $path |
307 | or $self->err(404, "not found"); |
335 | or $self->err(404, "not found"); |
|
|
336 | |
|
|
337 | $self->{stat} = [stat _]; |
308 | |
338 | |
309 | # idiotic netscape sends idiotic headers AGAIN |
339 | # idiotic netscape sends idiotic headers AGAIN |
310 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
340 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
311 | ? str2time $1 : 0; |
341 | ? str2time $1 : 0; |
312 | |
342 | |
… | |
… | |
315 | if ($path !~ /\/$/) { |
345 | if ($path !~ /\/$/) { |
316 | # create a redirect to get the trailing "/" |
346 | # create a redirect to get the trailing "/" |
317 | my $host = $self->server_hostport; |
347 | my $host = $self->server_hostport; |
318 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
348 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
319 | } else { |
349 | } else { |
320 | $ims < (stat _)[9] |
350 | $ims < $self->{stat}[9] |
321 | or $self->err(304, "not modified"); |
351 | or $self->err(304, "not modified"); |
322 | |
352 | |
323 | if ($self->{method} eq "GET") { |
353 | if ($self->{method} eq "GET") { |
324 | if (-r "$path/index.html") { |
354 | if (-r "$path/index.html") { |
325 | $self->{path} .= "/index.html"; |
355 | $self->{path} .= "/index.html"; |
… | |
… | |
337 | } |
367 | } |
338 | } |
368 | } |
339 | |
369 | |
340 | sub handle_dir { |
370 | sub handle_dir { |
341 | my $self = shift; |
371 | my $self = shift; |
342 | $self->_cgi($::INDEXPROG); |
372 | my $idx = $self->diridx; |
|
|
373 | |
|
|
374 | $self->response(200, "ok", |
|
|
375 | { |
|
|
376 | "Content-Type" => "text/html", |
|
|
377 | "Content-Length" => length $idx, |
|
|
378 | }, |
|
|
379 | $idx); |
343 | } |
380 | } |
344 | |
381 | |
345 | sub handle_file { |
382 | sub handle_file { |
346 | my $self = shift; |
383 | my $self = shift; |
347 | my $length = -s _; |
384 | my $length = -s _; |
… | |
… | |
367 | $hdr->{"Content-Range"} = "bytes */$length"; |
404 | $hdr->{"Content-Range"} = "bytes */$length"; |
368 | $self->err(416, "not satisfiable", $hdr); |
405 | $self->err(416, "not satisfiable", $hdr); |
369 | |
406 | |
370 | satisfiable: |
407 | satisfiable: |
371 | # check for segmented downloads |
408 | # check for segmented downloads |
372 | if ($l && $NO_SEGMENTED) { |
409 | if ($l && $::NO_SEGMENTED) { |
373 | if (%{$uri{$self->{uri}}} > 1) { |
410 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
374 | $self->slog("segmented download refused\n"); |
411 | Coro::Event::do_timer(after => 15); |
|
|
412 | |
375 | $self->err(400, "segmented downloads are not allowed"); |
413 | $self->err(400, "segmented downloads are not allowed"); |
376 | } |
414 | } |
377 | } |
415 | } |
378 | |
416 | |
379 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
417 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
383 | ignore: |
421 | ignore: |
384 | } else { |
422 | } else { |
385 | ($l, $h) = (0, $length - 1); |
423 | ($l, $h) = (0, $length - 1); |
386 | } |
424 | } |
387 | |
425 | |
388 | if ($self->{path} =~ /\.html$/) { |
426 | $self->{path} =~ /\.([^.]+)$/; |
389 | $hdr->{"Content-Type"} = "text/html"; |
|
|
390 | } else { |
|
|
391 | $hdr->{"Content-Type"} = "application/octet-stream"; |
427 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
392 | } |
|
|
393 | |
|
|
394 | $hdr->{"Content-Length"} = $length; |
428 | $hdr->{"Content-Length"} = $length; |
395 | |
429 | |
396 | $self->response(@code, $hdr, ""); |
430 | $self->response(@code, $hdr, ""); |
397 | |
431 | |
398 | if ($self->{method} eq "GET") { |
432 | if ($self->{method} eq "GET") { |
… | |
… | |
407 | |
441 | |
408 | $h -= $l - 1; |
442 | $h -= $l - 1; |
409 | |
443 | |
410 | while ($h > 0) { |
444 | while ($h > 0) { |
411 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
445 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
412 | $self->{fh}->syswrite($buf) |
446 | my $w = $self->{fh}->syswrite($buf) |
413 | or last; |
447 | or last; |
|
|
448 | $::written += $w; |
|
|
449 | $self->{written} += $w; |
414 | } |
450 | } |
415 | } |
451 | } |
416 | |
452 | |
417 | close $fh; |
453 | close $fh; |
418 | } |
454 | } |
419 | |
455 | |
420 | sub access_check { |
|
|
421 | my $self = shift; |
|
|
422 | my $whois = ::ip_request($self->{remote_addr}); |
|
|
423 | |
|
|
424 | if ($whois =~ /^\*cy: (\S+)/m) { |
|
|
425 | $self->slog(9, "COUNTRY($1)"); |
|
|
426 | } else { |
|
|
427 | $self->slog(9, "no country($whois)"); |
|
|
428 | } |
|
|
429 | } |
|
|
430 | |
|
|
431 | 1; |
456 | 1; |