… | |
… | |
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; |
78 | our %mimetype; |
85 | our %mimetype; |
79 | |
86 | |
… | |
… | |
104 | $self->{time} = $::NOW; |
111 | $self->{time} = $::NOW; |
105 | |
112 | |
106 | # enter ourselves into various lists |
113 | # enter ourselves into various lists |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
114 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
108 | |
115 | |
|
|
116 | $::conns++; |
|
|
117 | |
109 | $self; |
118 | $self; |
110 | } |
119 | } |
111 | |
120 | |
112 | sub DESTROY { |
121 | sub DESTROY { |
113 | my $self = shift; |
122 | my $self = shift; |
|
|
123 | |
|
|
124 | $::conns--; |
|
|
125 | |
|
|
126 | $self->eoconn; |
114 | delete $conn{$self->{remote_addr}}{$self*1}; |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
128 | } |
|
|
129 | |
|
|
130 | # end of connection |
|
|
131 | sub eoconn { |
115 | delete $uri{$self->{uri}}{$self*1}; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
116 | } |
133 | } |
117 | |
134 | |
118 | sub slog { |
135 | sub slog { |
119 | my $self = shift; |
136 | my $self = shift; |
120 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
137 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
121 | } |
138 | } |
122 | |
139 | |
123 | sub response { |
140 | sub response { |
124 | my ($self, $code, $msg, $hdr, $content) = @_; |
141 | my ($self, $code, $msg, $hdr, $content) = @_; |
125 | my $res = "HTTP/1.0 $code $msg\015\012"; |
142 | my $res = "HTTP/1.1 $code $msg\015\012"; |
126 | |
143 | |
127 | $res .= "Connection: close\015\012"; |
144 | #$res .= "Connection: close\015\012"; |
128 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
145 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
129 | |
146 | |
130 | while (my ($h, $v) = each %$hdr) { |
147 | while (my ($h, $v) = each %$hdr) { |
131 | $res .= "$h: $v\015\012" |
148 | $res .= "$h: $v\015\012" |
132 | } |
149 | } |
133 | $res .= "\015\012"; |
150 | $res .= "\015\012"; |
134 | |
151 | |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
136 | |
153 | |
137 | 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"; |
138 | |
155 | |
139 | $self->{written} += |
156 | $self->{written} += |
140 | print {$self->{fh}} $res; |
157 | print {$self->{fh}} $res; |
141 | } |
158 | } |
142 | |
159 | |
… | |
… | |
147 | unless (defined $content) { |
164 | unless (defined $content) { |
148 | $content = "$code $msg"; |
165 | $content = "$code $msg"; |
149 | $hdr->{"Content-Type"} = "text/plain"; |
166 | $hdr->{"Content-Type"} = "text/plain"; |
150 | $hdr->{"Content-Length"} = length $content; |
167 | $hdr->{"Content-Length"} = length $content; |
151 | } |
168 | } |
|
|
169 | $hdr->{"Connection"} = "close"; |
152 | |
170 | |
153 | $self->response($code, $msg, $hdr, $content); |
171 | $self->response($code, $msg, $hdr, $content); |
154 | |
172 | |
155 | die bless {}, err::; |
173 | die bless {}, err::; |
156 | } |
174 | } |
… | |
… | |
158 | sub err_blocked { |
176 | sub err_blocked { |
159 | my $self = shift; |
177 | my $self = shift; |
160 | my $ip = $self->{remote_addr}; |
178 | my $ip = $self->{remote_addr}; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
162 | |
180 | |
163 | Coro::Event::do_timer(after => 5); |
181 | Coro::Event::do_timer(after => 20*rand); |
164 | |
182 | |
165 | $self->err(403, "too many connections", |
183 | $self->err(401, "too many connections", |
166 | { |
184 | { |
167 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
168 | "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\"", |
169 | }, |
189 | }, |
170 | <<EOF); |
190 | <<EOF); |
171 | <html><p> |
191 | <html><p> |
172 | You have been blocked because you opened too many connections. You |
192 | You have been blocked because you opened too many connections. You |
173 | may retry at</p> |
193 | may retry at</p> |
… | |
… | |
182 | |
202 | |
183 | sub handle { |
203 | sub handle { |
184 | my $self = shift; |
204 | my $self = shift; |
185 | my $fh = $self->{fh}; |
205 | my $fh = $self->{fh}; |
186 | |
206 | |
|
|
207 | $fh->timeout($::REQ_TIMEOUT); |
187 | #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 | |
188 | $self->{h} = {}; |
222 | $self->{h} = {}; |
189 | |
223 | |
190 | # read request and parse first line |
|
|
191 | $fh->timeout($::REQ_TIMEOUT); |
|
|
192 | my $req = $fh->readline("\015\012\015\012"); |
|
|
193 | $fh->timeout($::RES_TIMEOUT); |
224 | $fh->timeout($::RES_TIMEOUT); |
194 | |
|
|
195 | defined $req or |
|
|
196 | $self->err(408, "request timeout"); |
|
|
197 | |
|
|
198 | my $ip = $self->{remote_addr}; |
225 | my $ip = $self->{remote_addr}; |
199 | |
226 | |
200 | if ($blocked{$ip}) { |
227 | if ($blocked{$ip}) { |
201 | $self->err_blocked($blocked{$ip}) |
228 | $self->err_blocked($blocked{$ip}) |
202 | if $blocked{$ip} > $::NOW; |
229 | if $blocked{$ip} > $::NOW; |
… | |
… | |
212 | $req =~ /^(?:\015\012)? |
239 | $req =~ /^(?:\015\012)? |
213 | (GET|HEAD) \040+ |
240 | (GET|HEAD) \040+ |
214 | ([^\040]+) \040+ |
241 | ([^\040]+) \040+ |
215 | HTTP\/([0-9]+\.[0-9]+) |
242 | HTTP\/([0-9]+\.[0-9]+) |
216 | \015\012/gx |
243 | \015\012/gx |
217 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
244 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
218 | |
|
|
219 | $2 ne "1.0" |
|
|
220 | or $self->err(506, "http protocol version not supported"); |
|
|
221 | |
245 | |
222 | $self->{method} = $1; |
246 | $self->{method} = $1; |
223 | $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"); |
224 | |
252 | |
225 | # parse headers |
253 | # parse headers |
226 | { |
254 | { |
227 | my (%hdr, $h, $v); |
255 | my (%hdr, $h, $v); |
228 | |
256 | |
… | |
… | |
241 | while ($h, $v) = each %hdr; |
269 | while ($h, $v) = each %hdr; |
242 | } |
270 | } |
243 | |
271 | |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
245 | |
273 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
275 | |
|
|
276 | eval { |
248 | $self->map_uri; |
277 | $self->map_uri; |
249 | $self->respond; |
278 | $self->respond; |
|
|
279 | }; |
|
|
280 | |
|
|
281 | die if $@ && !ref $@; |
|
|
282 | |
|
|
283 | $self->eoconn; |
|
|
284 | |
|
|
285 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
286 | |
|
|
287 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
288 | $fh->timeout($::PER_TIMEOUT); |
250 | #} |
289 | } |
251 | } |
290 | } |
252 | |
291 | |
253 | # uri => path mapping |
292 | # uri => path mapping |
254 | sub map_uri { |
293 | sub map_uri { |
255 | my $self = shift; |
294 | my $self = shift; |
… | |
… | |
342 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
381 | $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
343 | } else { |
382 | } else { |
344 | $ims < $self->{stat}[9] |
383 | $ims < $self->{stat}[9] |
345 | or $self->err(304, "not modified"); |
384 | or $self->err(304, "not modified"); |
346 | |
385 | |
347 | if ($self->{method} eq "GET") { |
|
|
348 | if (-r "$path/index.html") { |
386 | if (-r "$path/index.html") { |
349 | $self->{path} .= "/index.html"; |
387 | $self->{path} .= "/index.html"; |
350 | $self->handle_file; |
388 | $self->handle_file; |
351 | } else { |
389 | } else { |
352 | $self->handle_dir; |
390 | $self->handle_dir; |
353 | } |
|
|
354 | } |
391 | } |
355 | } |
392 | } |
356 | } elsif (-f _ && -r _) { |
393 | } elsif (-f _ && -r _) { |
357 | -x _ and $self->err(403, "forbidden"); |
394 | -x _ and $self->err(403, "forbidden"); |
358 | $self->handle_file; |
395 | $self->handle_file; |
… | |
… | |
394 | goto ignore; |
431 | goto ignore; |
395 | } |
432 | } |
396 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
433 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
397 | } |
434 | } |
398 | $hdr->{"Content-Range"} = "bytes */$length"; |
435 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
436 | $hdr->{"Content-Length"} = $length; |
|
|
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
399 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr, ""); |
400 | |
439 | |
401 | satisfiable: |
440 | satisfiable: |
402 | # check for segmented downloads |
441 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
443 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
444 | Coro::Event::do_timer(after => 15); |
|
|
445 | |
405 | $self->err(400, "segmented downloads are not allowed"); |
446 | $self->err(400, "segmented downloads are not allowed"); |
406 | } |
447 | } |
407 | } |
448 | } |
408 | |
449 | |
409 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
420 | $hdr->{"Content-Length"} = $length; |
461 | $hdr->{"Content-Length"} = $length; |
421 | |
462 | |
422 | $self->response(@code, $hdr, ""); |
463 | $self->response(@code, $hdr, ""); |
423 | |
464 | |
424 | if ($self->{method} eq "GET") { |
465 | if ($self->{method} eq "GET") { |
425 | my ($fh, $buf); |
466 | my ($fh, $buf, $r); |
|
|
467 | my $current = $Coro::current; |
426 | open $fh, "<", $self->{path} |
468 | open $fh, "<", $self->{path} |
427 | or die "$self->{path}: late open failure ($!)"; |
469 | or die "$self->{path}: late open failure ($!)"; |
428 | |
470 | |
429 | if ($l) { |
|
|
430 | sysseek $fh, $l, 0 |
|
|
431 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
432 | } |
|
|
433 | |
|
|
434 | $h -= $l - 1; |
471 | $h -= $l - 1; |
435 | |
472 | |
|
|
473 | if (0) { |
|
|
474 | if ($l) { |
|
|
475 | sysseek $fh, $l, 0; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
436 | while ($h > 0) { |
479 | while ($h > 0) { |
|
|
480 | if (0) { |
437 | $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 | } |
438 | my $w = $self->{fh}->syswrite($buf) |
492 | my $w = $self->{fh}->syswrite($buf) |
439 | or last; |
493 | or last; |
440 | $::written += $w; |
494 | $::written += $w; |
441 | $self->{written} += $w; |
495 | $self->{written} += $w; |
|
|
496 | $l += $r; |
442 | } |
497 | } |
443 | } |
498 | } |
444 | |
499 | |
445 | close $fh; |
500 | close $fh; |
446 | } |
501 | } |