… | |
… | |
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" |
… | |
… | |
70 | package conn; |
70 | package conn; |
71 | |
71 | |
72 | use Socket; |
72 | use Socket; |
73 | use HTTP::Date; |
73 | use HTTP::Date; |
74 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
|
|
75 | use Linux::AIO; |
75 | |
76 | |
|
|
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
|
|
78 | |
|
|
79 | Event->io(fd => Linux::AIO::poll_fileno, |
|
|
80 | poll => 'r', async => 1, |
|
|
81 | cb => \&Linux::AIO::poll_cb); |
|
|
82 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{self} => connobj |
|
|
84 | our %uri; # $uri{ip}{uri}{self} |
77 | our %blocked; |
85 | our %blocked; |
|
|
86 | our %mimetype; |
|
|
87 | |
|
|
88 | sub read_mimetypes { |
|
|
89 | local *M; |
|
|
90 | if (open M, "<mime_types") { |
|
|
91 | while (<M>) { |
|
|
92 | if (/^([^#]\S+)\t+(\S+)$/) { |
|
|
93 | $mimetype{lc $1} = $2; |
|
|
94 | } |
|
|
95 | } |
|
|
96 | } else { |
|
|
97 | print "cannot open mime_types\n"; |
|
|
98 | } |
|
|
99 | } |
|
|
100 | |
|
|
101 | read_mimetypes; |
78 | |
102 | |
79 | sub new { |
103 | sub new { |
80 | my $class = shift; |
104 | my $class = shift; |
81 | my $peername = shift; |
105 | my $peername = shift; |
82 | my $fh = shift; |
106 | my $fh = shift; |
83 | my $self = bless { fh => $fh }, $class; |
107 | my $self = bless { fh => $fh }, $class; |
84 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
108 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
85 | or $self->err(500, "unable to decode peername"); |
109 | or $self->err(500, "unable to decode peername"); |
86 | |
110 | |
87 | $self->{remote_addr} = inet_ntoa $iaddr; |
111 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
112 | $self->{time} = $::NOW; |
88 | |
113 | |
89 | # enter ourselves into various lists |
114 | # enter ourselves into various lists |
90 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
115 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
91 | |
116 | |
|
|
117 | $::conns++; |
|
|
118 | |
92 | $self; |
119 | $self; |
93 | } |
120 | } |
94 | |
121 | |
95 | sub DESTROY { |
122 | sub DESTROY { |
96 | my $self = shift; |
123 | my $self = shift; |
|
|
124 | |
|
|
125 | $::conns--; |
|
|
126 | |
|
|
127 | $self->eoconn; |
97 | delete $conn{$self->{remote_addr}}{$self*1}; |
128 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
129 | } |
|
|
130 | |
|
|
131 | # end of connection |
|
|
132 | sub eoconn { |
|
|
133 | my $self = shift; |
98 | delete $uri{$self->{uri}}{$self*1}; |
134 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
99 | } |
135 | } |
100 | |
136 | |
101 | sub slog { |
137 | sub slog { |
102 | my $self = shift; |
138 | my $self = shift; |
103 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
139 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
104 | } |
140 | } |
105 | |
141 | |
106 | sub response { |
142 | sub response { |
107 | my ($self, $code, $msg, $hdr, $content) = @_; |
143 | my ($self, $code, $msg, $hdr, $content) = @_; |
108 | my $res = "HTTP/1.0 $code $msg\015\012"; |
144 | my $res = "HTTP/1.1 $code $msg\015\012"; |
109 | |
145 | |
110 | $res .= "Connection: close\015\012"; |
146 | #$res .= "Connection: close\015\012"; |
111 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
147 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
112 | |
148 | |
113 | while (my ($h, $v) = each %$hdr) { |
149 | while (my ($h, $v) = each %$hdr) { |
114 | $res .= "$h: $v\015\012" |
150 | $res .= "$h: $v\015\012" |
115 | } |
151 | } |
|
|
152 | $res .= "\015\012"; |
116 | |
153 | |
117 | $res .= "\015\012$content" if defined $content; |
154 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
118 | |
155 | |
119 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
156 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
120 | |
157 | |
|
|
158 | $self->{written} += |
121 | print {$self->{fh}} $res; |
159 | print {$self->{fh}} $res; |
122 | } |
160 | } |
123 | |
161 | |
124 | sub err { |
162 | sub err { |
125 | my $self = shift; |
163 | my $self = shift; |
126 | my ($code, $msg, $hdr, $content) = @_; |
164 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
128 | unless (defined $content) { |
166 | unless (defined $content) { |
129 | $content = "$code $msg"; |
167 | $content = "$code $msg"; |
130 | $hdr->{"Content-Type"} = "text/plain"; |
168 | $hdr->{"Content-Type"} = "text/plain"; |
131 | $hdr->{"Content-Length"} = length $content; |
169 | $hdr->{"Content-Length"} = length $content; |
132 | } |
170 | } |
|
|
171 | $hdr->{"Connection"} = "close"; |
133 | |
172 | |
134 | $self->response($code, $msg, $hdr, $content); |
173 | $self->response($code, $msg, $hdr, $content); |
135 | |
174 | |
136 | die bless {}, err::; |
175 | die bless {}, err::; |
137 | } |
176 | } |
138 | |
177 | |
139 | sub err_blocked { |
178 | sub err_blocked { |
140 | my $self = shift; |
179 | my $self = shift; |
141 | my $ip = $self->{remote_addr}; |
180 | my $ip = $self->{remote_addr}; |
142 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
181 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
|
|
182 | |
|
|
183 | Coro::Event::do_timer(after => 20*rand); |
|
|
184 | |
143 | $self->err(403, "too many connections", |
185 | $self->err(401, "too many connections", |
144 | { |
186 | { |
145 | "Content-Type" => "text/html", |
187 | "Content-Type" => "text/html", |
146 | "Retry-After" => $::BLOCKTIME |
188 | "Retry-After" => $::BLOCKTIME, |
|
|
189 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
190 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
147 | }, |
191 | }, |
148 | <<EOF); |
192 | <<EOF); |
149 | <html><p> |
193 | <html><p> |
150 | You have been blocked because you opened too many connections. You |
194 | You have been blocked because you opened too many connections. You |
151 | may retry at</p> |
195 | may retry at</p> |
… | |
… | |
160 | |
204 | |
161 | sub handle { |
205 | sub handle { |
162 | my $self = shift; |
206 | my $self = shift; |
163 | my $fh = $self->{fh}; |
207 | my $fh = $self->{fh}; |
164 | |
208 | |
|
|
209 | $fh->timeout($::REQ_TIMEOUT); |
165 | #while() { |
210 | while() { |
|
|
211 | $self->{reqs}++; |
|
|
212 | |
|
|
213 | # read request and parse first line |
|
|
214 | my $req = $fh->readline("\015\012\015\012"); |
|
|
215 | |
|
|
216 | unless (defined $req) { |
|
|
217 | if (exists $self->{version}) { |
|
|
218 | last; |
|
|
219 | } else { |
|
|
220 | $self->err(408, "request timeout"); |
|
|
221 | } |
|
|
222 | } |
|
|
223 | |
166 | $self->{h} = {}; |
224 | $self->{h} = {}; |
167 | |
225 | |
168 | # read request and parse first line |
|
|
169 | $fh->timeout($::REQ_TIMEOUT); |
|
|
170 | my $req = $fh->readline("\015\012\015\012"); |
|
|
171 | $fh->timeout($::RES_TIMEOUT); |
226 | $fh->timeout($::RES_TIMEOUT); |
172 | |
|
|
173 | defined $req or |
|
|
174 | $self->err(408, "request timeout"); |
|
|
175 | |
|
|
176 | my $ip = $self->{remote_addr}; |
227 | my $ip = $self->{remote_addr}; |
177 | |
228 | |
178 | if ($blocked{$ip}) { |
229 | if ($blocked{$ip}) { |
179 | $self->err_blocked($blocked{$ip}) |
230 | $self->err_blocked($blocked{$ip}) |
180 | if $blocked{$ip} > $::NOW; |
231 | if $blocked{$ip} > $::NOW; |
181 | |
232 | |
182 | delete $blocked{$ip}; |
233 | delete $blocked{$ip}; |
183 | } |
234 | } |
184 | |
235 | |
185 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
236 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
186 | $self->slog("blocked ip $ip"); |
237 | $self->slog(2, "blocked ip $ip"); |
187 | $self->err_blocked; |
238 | $self->err_blocked; |
188 | } |
239 | } |
189 | |
240 | |
190 | $req =~ /^(?:\015\012)? |
241 | $req =~ /^(?:\015\012)? |
191 | (GET|HEAD) \040+ |
242 | (GET|HEAD) \040+ |
192 | ([^\040]+) \040+ |
243 | ([^\040]+) \040+ |
193 | HTTP\/([0-9]+\.[0-9]+) |
244 | HTTP\/([0-9]+\.[0-9]+) |
194 | \015\012/gx |
245 | \015\012/gx |
195 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
246 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
196 | |
|
|
197 | $2 ne "1.0" |
|
|
198 | or $self->err(506, "http protocol version not supported"); |
|
|
199 | |
247 | |
200 | $self->{method} = $1; |
248 | $self->{method} = $1; |
201 | $self->{uri} = $2; |
249 | $self->{uri} = $2; |
|
|
250 | $self->{version} = $3; |
|
|
251 | |
|
|
252 | $3 =~ /^1\./ |
|
|
253 | or $self->err(506, "http protocol version $3 not supported"); |
202 | |
254 | |
203 | # parse headers |
255 | # parse headers |
204 | { |
256 | { |
205 | my (%hdr, $h, $v); |
257 | my (%hdr, $h, $v); |
206 | |
258 | |
… | |
… | |
219 | while ($h, $v) = each %hdr; |
271 | while ($h, $v) = each %hdr; |
220 | } |
272 | } |
221 | |
273 | |
222 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
274 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
223 | |
275 | |
224 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
276 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
225 | |
277 | |
|
|
278 | eval { |
226 | $self->map_uri; |
279 | $self->map_uri; |
227 | $self->respond; |
280 | $self->respond; |
|
|
281 | }; |
|
|
282 | |
|
|
283 | $self->eoconn; |
|
|
284 | |
|
|
285 | die if $@ && !ref $@; |
|
|
286 | |
|
|
287 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
288 | |
|
|
289 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
290 | $fh->timeout($::PER_TIMEOUT); |
228 | #} |
291 | } |
229 | } |
292 | } |
230 | |
293 | |
231 | # uri => path mapping |
294 | # uri => path mapping |
232 | sub map_uri { |
295 | sub map_uri { |
233 | my $self = shift; |
296 | my $self = shift; |
… | |
… | |
290 | if (chdir $::DOCROOT) { |
353 | if (chdir $::DOCROOT) { |
291 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
354 | $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
292 | $ENV{HTTP_HOST} = $self->server_host; |
355 | $ENV{HTTP_HOST} = $self->server_host; |
293 | $ENV{HTTP_PORT} = $self->{server_host}; |
356 | $ENV{HTTP_PORT} = $self->{server_host}; |
294 | $ENV{SCRIPT_NAME} = $self->{name}; |
357 | $ENV{SCRIPT_NAME} = $self->{name}; |
295 | exec $::INDEXPROG; |
358 | exec $path; |
296 | } |
359 | } |
297 | Coro::State::_exit(0); |
360 | Coro::State::_exit(0); |
298 | } else { |
361 | } else { |
299 | } |
362 | } |
300 | } |
363 | } |
… | |
… | |
303 | my $self = shift; |
366 | my $self = shift; |
304 | my $path = $self->{path}; |
367 | my $path = $self->{path}; |
305 | |
368 | |
306 | stat $path |
369 | stat $path |
307 | or $self->err(404, "not found"); |
370 | or $self->err(404, "not found"); |
|
|
371 | |
|
|
372 | $self->{stat} = [stat _]; |
308 | |
373 | |
309 | # idiotic netscape sends idiotic headers AGAIN |
374 | # idiotic netscape sends idiotic headers AGAIN |
310 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
375 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
311 | ? str2time $1 : 0; |
376 | ? str2time $1 : 0; |
312 | |
377 | |
… | |
… | |
315 | if ($path !~ /\/$/) { |
380 | if ($path !~ /\/$/) { |
316 | # create a redirect to get the trailing "/" |
381 | # create a redirect to get the trailing "/" |
317 | my $host = $self->server_hostport; |
382 | my $host = $self->server_hostport; |
318 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
383 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
319 | } else { |
384 | } else { |
320 | $ims < (stat _)[9] |
385 | $ims < $self->{stat}[9] |
321 | or $self->err(304, "not modified"); |
386 | or $self->err(304, "not modified"); |
322 | |
387 | |
323 | if ($self->{method} eq "GET") { |
|
|
324 | if (-r "$path/index.html") { |
388 | if (-r "$path/index.html") { |
325 | $self->{path} .= "/index.html"; |
389 | $self->{path} .= "/index.html"; |
326 | $self->handle_file; |
390 | $self->handle_file; |
327 | } else { |
391 | } else { |
328 | $self->handle_dir; |
392 | $self->handle_dir; |
329 | } |
|
|
330 | } |
393 | } |
331 | } |
394 | } |
332 | } elsif (-f _ && -r _) { |
395 | } elsif (-f _ && -r _) { |
333 | -x _ and $self->err(403, "forbidden"); |
396 | -x _ and $self->err(403, "forbidden"); |
334 | $self->handle_file; |
397 | $self->handle_file; |
… | |
… | |
337 | } |
400 | } |
338 | } |
401 | } |
339 | |
402 | |
340 | sub handle_dir { |
403 | sub handle_dir { |
341 | my $self = shift; |
404 | my $self = shift; |
342 | $self->_cgi($::INDEXPROG); |
405 | my $idx = $self->diridx; |
|
|
406 | |
|
|
407 | $self->response(200, "ok", |
|
|
408 | { |
|
|
409 | "Content-Type" => "text/html", |
|
|
410 | "Content-Length" => length $idx, |
|
|
411 | }, |
|
|
412 | $idx); |
343 | } |
413 | } |
344 | |
414 | |
345 | sub handle_file { |
415 | sub handle_file { |
346 | my $self = shift; |
416 | my $self = shift; |
347 | my $length = -s _; |
417 | my $length = -s _; |
… | |
… | |
360 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
430 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
361 | } else { |
431 | } else { |
362 | ($l, $h) = (0, $length - 1); |
432 | ($l, $h) = (0, $length - 1); |
363 | goto ignore; |
433 | goto ignore; |
364 | } |
434 | } |
365 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
366 | } |
436 | } |
367 | $hdr->{"Content-Range"} = "bytes */$length"; |
437 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
438 | $hdr->{"Content-Length"} = $length; |
|
|
439 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
368 | $self->err(416, "not satisfiable", $hdr); |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
369 | |
441 | |
370 | satisfiable: |
442 | satisfiable: |
371 | # check for segmented downloads |
443 | # check for segmented downloads |
372 | if ($l && $NO_SEGMENTED) { |
444 | if ($l && $::NO_SEGMENTED) { |
373 | if (%{$uri{$self->{uri}}} > 1) { |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
374 | $self->slog("segmented download refused\n"); |
|
|
375 | $self->err(400, "segmented downloads are not allowed"); |
446 | $self->err(400, "segmented downloads are not allowed"); |
376 | } |
447 | } |
377 | } |
448 | } |
378 | |
449 | |
379 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
383 | ignore: |
454 | ignore: |
384 | } else { |
455 | } else { |
385 | ($l, $h) = (0, $length - 1); |
456 | ($l, $h) = (0, $length - 1); |
386 | } |
457 | } |
387 | |
458 | |
388 | if ($self->{path} =~ /\.html$/) { |
459 | $self->{path} =~ /\.([^.]+)$/; |
389 | $hdr->{"Content-Type"} = "text/html"; |
|
|
390 | } else { |
|
|
391 | $hdr->{"Content-Type"} = "application/octet-stream"; |
460 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
392 | } |
|
|
393 | |
|
|
394 | $hdr->{"Content-Length"} = $length; |
461 | $hdr->{"Content-Length"} = $length; |
395 | |
462 | |
396 | $self->response(@code, $hdr, ""); |
463 | $self->response(@code, $hdr, ""); |
397 | |
464 | |
398 | if ($self->{method} eq "GET") { |
465 | if ($self->{method} eq "GET") { |
399 | my ($fh, $buf); |
466 | my ($fh, $buf, $r); |
|
|
467 | my $current = $Coro::current; |
400 | open $fh, "<", $self->{path} |
468 | open $fh, "<", $self->{path} |
401 | or die "$self->{path}: late open failure ($!)"; |
469 | or die "$self->{path}: late open failure ($!)"; |
402 | |
470 | |
403 | if ($l) { |
|
|
404 | sysseek $fh, $l, 0 |
|
|
405 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
406 | } |
|
|
407 | |
|
|
408 | $h -= $l - 1; |
471 | $h -= $l - 1; |
409 | |
472 | |
|
|
473 | if (0) { |
|
|
474 | if ($l) { |
|
|
475 | sysseek $fh, $l, 0; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
410 | while ($h > 0) { |
479 | while ($h > 0) { |
|
|
480 | if (0) { |
411 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
481 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
482 | or last; |
|
|
483 | } else { |
|
|
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
485 | $buf, 0, sub { |
|
|
486 | $r = $_[0]; |
|
|
487 | $current->ready; |
|
|
488 | }); |
|
|
489 | &Coro::schedule; |
|
|
490 | last unless $r; |
|
|
491 | } |
412 | $self->{fh}->syswrite($buf) |
492 | my $w = $self->{fh}->syswrite($buf) |
413 | or last; |
493 | or last; |
|
|
494 | $::written += $w; |
|
|
495 | $self->{written} += $w; |
|
|
496 | $l += $r; |
414 | } |
497 | } |
415 | } |
498 | } |
416 | |
499 | |
417 | close $fh; |
500 | close $fh; |
418 | } |
501 | } |
419 | |
502 | |
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; |
503 | 1; |