… | |
… | |
75 | use Linux::AIO; |
75 | use Linux::AIO; |
76 | |
76 | |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
77 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
78 | |
78 | |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
79 | Event->io(fd => Linux::AIO::poll_fileno, |
80 | poll => 'r', |
80 | poll => 'r', async => 1, |
81 | async => 1, |
|
|
82 | cb => \&Linux::AIO::poll_cb ); |
81 | cb => \&Linux::AIO::poll_cb); |
83 | |
82 | my $scheduler = Event->idle( |
84 | Event->add_hooks(prepare => sub { |
83 | max => 0, min => 0, prio => 6, parked => 1, |
85 | &Coro::cede while &Coro::nready; |
84 | cb => \&Coro::schedule); |
86 | 1e6; |
|
|
87 | }); |
|
|
88 | |
85 | |
89 | our %conn; # $conn{ip}{fh} => connobj |
86 | our %conn; # $conn{ip}{fh} => connobj |
90 | our %blocked; |
87 | our %blocked; |
91 | our %mimetype; |
88 | our %mimetype; |
92 | |
89 | |
… | |
… | |
127 | sub DESTROY { |
124 | sub DESTROY { |
128 | my $self = shift; |
125 | my $self = shift; |
129 | |
126 | |
130 | $::conns--; |
127 | $::conns--; |
131 | |
128 | |
|
|
129 | $self->eoconn; |
132 | delete $conn{$self->{remote_addr}}{$self*1}; |
130 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
131 | } |
|
|
132 | |
|
|
133 | # end of connection |
|
|
134 | sub eoconn { |
133 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
135 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
134 | } |
136 | } |
135 | |
137 | |
136 | sub slog { |
138 | sub slog { |
137 | my $self = shift; |
139 | my $self = shift; |
138 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
140 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
139 | } |
141 | } |
140 | |
142 | |
141 | sub response { |
143 | sub response { |
142 | my ($self, $code, $msg, $hdr, $content) = @_; |
144 | my ($self, $code, $msg, $hdr, $content) = @_; |
143 | my $res = "HTTP/1.0 $code $msg\015\012"; |
145 | my $res = "HTTP/1.1 $code $msg\015\012"; |
144 | |
146 | |
145 | $res .= "Connection: close\015\012"; |
147 | #$res .= "Connection: close\015\012"; |
146 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
148 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
147 | |
149 | |
148 | while (my ($h, $v) = each %$hdr) { |
150 | while (my ($h, $v) = each %$hdr) { |
149 | $res .= "$h: $v\015\012" |
151 | $res .= "$h: $v\015\012" |
150 | } |
152 | } |
… | |
… | |
165 | unless (defined $content) { |
167 | unless (defined $content) { |
166 | $content = "$code $msg"; |
168 | $content = "$code $msg"; |
167 | $hdr->{"Content-Type"} = "text/plain"; |
169 | $hdr->{"Content-Type"} = "text/plain"; |
168 | $hdr->{"Content-Length"} = length $content; |
170 | $hdr->{"Content-Length"} = length $content; |
169 | } |
171 | } |
|
|
172 | $hdr->{"Connection"} = "close"; |
170 | |
173 | |
171 | $self->response($code, $msg, $hdr, $content); |
174 | $self->response($code, $msg, $hdr, $content); |
172 | |
175 | |
173 | die bless {}, err::; |
176 | die bless {}, err::; |
174 | } |
177 | } |
… | |
… | |
176 | sub err_blocked { |
179 | sub err_blocked { |
177 | my $self = shift; |
180 | my $self = shift; |
178 | my $ip = $self->{remote_addr}; |
181 | my $ip = $self->{remote_addr}; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
182 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
180 | |
183 | |
181 | Coro::Event::do_timer(after => 15); |
184 | Coro::Event::do_timer(after => 20*rand); |
182 | |
185 | |
183 | $self->err(401, "too many connections", |
186 | $self->err(401, "too many connections", |
184 | { |
187 | { |
185 | "Content-Type" => "text/html", |
188 | "Content-Type" => "text/html", |
186 | "Retry-After" => $::BLOCKTIME |
189 | "Retry-After" => $::BLOCKTIME, |
|
|
190 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
191 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
187 | }, |
192 | }, |
188 | <<EOF); |
193 | <<EOF); |
189 | <html><p> |
194 | <html><p> |
190 | You have been blocked because you opened too many connections. You |
195 | You have been blocked because you opened too many connections. You |
191 | may retry at</p> |
196 | may retry at</p> |
… | |
… | |
200 | |
205 | |
201 | sub handle { |
206 | sub handle { |
202 | my $self = shift; |
207 | my $self = shift; |
203 | my $fh = $self->{fh}; |
208 | my $fh = $self->{fh}; |
204 | |
209 | |
|
|
210 | $fh->timeout($::REQ_TIMEOUT); |
205 | #while() { |
211 | while() { |
|
|
212 | $self->{reqs}++; |
|
|
213 | |
|
|
214 | # read request and parse first line |
|
|
215 | my $req = $fh->readline("\015\012\015\012"); |
|
|
216 | |
|
|
217 | unless (defined $req) { |
|
|
218 | if (exists $self->{version}) { |
|
|
219 | last; |
|
|
220 | } else { |
|
|
221 | $self->err(408, "request timeout"); |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
206 | $self->{h} = {}; |
225 | $self->{h} = {}; |
207 | |
226 | |
208 | # read request and parse first line |
|
|
209 | $fh->timeout($::REQ_TIMEOUT); |
|
|
210 | my $req = $fh->readline("\015\012\015\012"); |
|
|
211 | $fh->timeout($::RES_TIMEOUT); |
227 | $fh->timeout($::RES_TIMEOUT); |
212 | |
|
|
213 | defined $req or |
|
|
214 | $self->err(408, "request timeout"); |
|
|
215 | |
|
|
216 | my $ip = $self->{remote_addr}; |
228 | my $ip = $self->{remote_addr}; |
217 | |
229 | |
218 | if ($blocked{$ip}) { |
230 | if ($blocked{$ip}) { |
219 | $self->err_blocked($blocked{$ip}) |
231 | $self->err_blocked($blocked{$ip}) |
220 | if $blocked{$ip} > $::NOW; |
232 | if $blocked{$ip} > $::NOW; |
… | |
… | |
232 | ([^\040]+) \040+ |
244 | ([^\040]+) \040+ |
233 | HTTP\/([0-9]+\.[0-9]+) |
245 | HTTP\/([0-9]+\.[0-9]+) |
234 | \015\012/gx |
246 | \015\012/gx |
235 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
247 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
236 | |
248 | |
237 | $2 ne "1.0" |
|
|
238 | or $self->err(506, "http protocol version not supported"); |
|
|
239 | |
|
|
240 | $self->{method} = $1; |
249 | $self->{method} = $1; |
241 | $self->{uri} = $2; |
250 | $self->{uri} = $2; |
|
|
251 | $self->{version} = $3; |
|
|
252 | |
|
|
253 | $3 =~ /^1\./ |
|
|
254 | or $self->err(506, "http protocol version $3 not supported"); |
242 | |
255 | |
243 | # parse headers |
256 | # parse headers |
244 | { |
257 | { |
245 | my (%hdr, $h, $v); |
258 | my (%hdr, $h, $v); |
246 | |
259 | |
… | |
… | |
263 | |
276 | |
264 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
277 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
265 | |
278 | |
266 | $self->map_uri; |
279 | $self->map_uri; |
267 | $self->respond; |
280 | $self->respond; |
|
|
281 | |
|
|
282 | $self->eoconn; |
|
|
283 | |
|
|
284 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
285 | |
|
|
286 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
287 | $fh->timeout($::PER_TIMEOUT); |
268 | #} |
288 | } |
269 | } |
289 | } |
270 | |
290 | |
271 | # uri => path mapping |
291 | # uri => path mapping |
272 | sub map_uri { |
292 | sub map_uri { |
273 | my $self = shift; |
293 | my $self = shift; |
… | |
… | |
412 | goto ignore; |
432 | goto ignore; |
413 | } |
433 | } |
414 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
434 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
415 | } |
435 | } |
416 | $hdr->{"Content-Range"} = "bytes */$length"; |
436 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
417 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr); |
418 | |
439 | |
419 | satisfiable: |
440 | satisfiable: |
420 | # check for segmented downloads |
441 | # check for segmented downloads |
421 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
… | |
… | |
447 | open $fh, "<", $self->{path} |
468 | open $fh, "<", $self->{path} |
448 | or die "$self->{path}: late open failure ($!)"; |
469 | or die "$self->{path}: late open failure ($!)"; |
449 | |
470 | |
450 | $h -= $l - 1; |
471 | $h -= $l - 1; |
451 | |
472 | |
|
|
473 | if (0) { |
|
|
474 | if ($l) { |
|
|
475 | sysseek $fh, $l, 0; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
452 | while ($h > 0) { |
479 | while ($h > 0) { |
|
|
480 | if (0) { |
|
|
481 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
482 | or last; |
|
|
483 | } else { |
453 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
454 | $buf, 0, sub { |
485 | $buf, 0, sub { |
455 | $r = $_[0]; |
486 | $r = $_[0]; |
456 | $current->ready; |
487 | $current->ready; |
|
|
488 | $scheduler->now; |
457 | }); |
489 | }); |
458 | &Coro::schedule; |
490 | &Coro::schedule; |
459 | last unless $r; |
491 | last unless $r; |
|
|
492 | } |
460 | my $w = $self->{fh}->syswrite($buf) |
493 | my $w = $self->{fh}->syswrite($buf) |
461 | or last; |
494 | or last; |
462 | $::written += $w; |
495 | $::written += $w; |
463 | $self->{written} += $w; |
496 | $self->{written} += $w; |
464 | $l += $r; |
497 | $l += $r; |