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