… | |
… | |
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 | |
|
|
84 | Event->add_hooks(prepare => sub { |
|
|
85 | &Coro::cede while &Coro::nready; |
|
|
86 | 1e6; |
|
|
87 | }); |
|
|
88 | |
82 | |
89 | our %conn; # $conn{ip}{fh} => connobj |
83 | our %conn; # $conn{ip}{fh} => connobj |
90 | our %blocked; |
84 | our %blocked; |
91 | our %mimetype; |
85 | our %mimetype; |
92 | |
86 | |
… | |
… | |
127 | sub DESTROY { |
121 | sub DESTROY { |
128 | my $self = shift; |
122 | my $self = shift; |
129 | |
123 | |
130 | $::conns--; |
124 | $::conns--; |
131 | |
125 | |
|
|
126 | $self->eoconn; |
132 | delete $conn{$self->{remote_addr}}{$self*1}; |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
128 | } |
|
|
129 | |
|
|
130 | # end of connection |
|
|
131 | sub eoconn { |
133 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
134 | } |
133 | } |
135 | |
134 | |
136 | sub slog { |
135 | sub slog { |
137 | my $self = shift; |
136 | my $self = shift; |
138 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
137 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
139 | } |
138 | } |
140 | |
139 | |
141 | sub response { |
140 | sub response { |
142 | my ($self, $code, $msg, $hdr, $content) = @_; |
141 | my ($self, $code, $msg, $hdr, $content) = @_; |
143 | my $res = "HTTP/1.0 $code $msg\015\012"; |
142 | my $res = "HTTP/1.1 $code $msg\015\012"; |
144 | |
143 | |
145 | $res .= "Connection: close\015\012"; |
144 | #$res .= "Connection: close\015\012"; |
146 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
145 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
147 | |
146 | |
148 | while (my ($h, $v) = each %$hdr) { |
147 | while (my ($h, $v) = each %$hdr) { |
149 | $res .= "$h: $v\015\012" |
148 | $res .= "$h: $v\015\012" |
150 | } |
149 | } |
… | |
… | |
165 | unless (defined $content) { |
164 | unless (defined $content) { |
166 | $content = "$code $msg"; |
165 | $content = "$code $msg"; |
167 | $hdr->{"Content-Type"} = "text/plain"; |
166 | $hdr->{"Content-Type"} = "text/plain"; |
168 | $hdr->{"Content-Length"} = length $content; |
167 | $hdr->{"Content-Length"} = length $content; |
169 | } |
168 | } |
|
|
169 | $hdr->{"Connection"} = "close"; |
170 | |
170 | |
171 | $self->response($code, $msg, $hdr, $content); |
171 | $self->response($code, $msg, $hdr, $content); |
172 | |
172 | |
173 | die bless {}, err::; |
173 | die bless {}, err::; |
174 | } |
174 | } |
… | |
… | |
200 | |
200 | |
201 | sub handle { |
201 | sub handle { |
202 | my $self = shift; |
202 | my $self = shift; |
203 | my $fh = $self->{fh}; |
203 | my $fh = $self->{fh}; |
204 | |
204 | |
|
|
205 | $fh->timeout($::REQ_TIMEOUT); |
205 | #while() { |
206 | while() { |
|
|
207 | $self->{reqs}++; |
|
|
208 | |
|
|
209 | # read request and parse first line |
|
|
210 | my $req = $fh->readline("\015\012\015\012"); |
|
|
211 | |
|
|
212 | unless (defined $req) { |
|
|
213 | if (exists $self->{version}) { |
|
|
214 | last; |
|
|
215 | } else { |
|
|
216 | $self->err(408, "request timeout"); |
|
|
217 | } |
|
|
218 | } |
|
|
219 | |
206 | $self->{h} = {}; |
220 | $self->{h} = {}; |
207 | |
221 | |
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); |
222 | $fh->timeout($::RES_TIMEOUT); |
212 | |
|
|
213 | defined $req or |
|
|
214 | $self->err(408, "request timeout"); |
|
|
215 | |
|
|
216 | my $ip = $self->{remote_addr}; |
223 | my $ip = $self->{remote_addr}; |
217 | |
224 | |
218 | if ($blocked{$ip}) { |
225 | if ($blocked{$ip}) { |
219 | $self->err_blocked($blocked{$ip}) |
226 | $self->err_blocked($blocked{$ip}) |
220 | if $blocked{$ip} > $::NOW; |
227 | if $blocked{$ip} > $::NOW; |
… | |
… | |
232 | ([^\040]+) \040+ |
239 | ([^\040]+) \040+ |
233 | HTTP\/([0-9]+\.[0-9]+) |
240 | HTTP\/([0-9]+\.[0-9]+) |
234 | \015\012/gx |
241 | \015\012/gx |
235 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
242 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
236 | |
243 | |
237 | $2 ne "1.0" |
|
|
238 | or $self->err(506, "http protocol version not supported"); |
|
|
239 | |
|
|
240 | $self->{method} = $1; |
244 | $self->{method} = $1; |
241 | $self->{uri} = $2; |
245 | $self->{uri} = $2; |
|
|
246 | $self->{version} = $3; |
|
|
247 | |
|
|
248 | $3 eq "1.0" or $3 eq "1.1" |
|
|
249 | or $self->err(506, "http protocol version $3 not supported"); |
242 | |
250 | |
243 | # parse headers |
251 | # parse headers |
244 | { |
252 | { |
245 | my (%hdr, $h, $v); |
253 | my (%hdr, $h, $v); |
246 | |
254 | |
… | |
… | |
263 | |
271 | |
264 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
272 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
265 | |
273 | |
266 | $self->map_uri; |
274 | $self->map_uri; |
267 | $self->respond; |
275 | $self->respond; |
|
|
276 | |
|
|
277 | $self->eoconn; |
|
|
278 | |
|
|
279 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
280 | |
|
|
281 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
282 | $fh->timeout($::PER_TIMEOUT); |
268 | #} |
283 | } |
269 | } |
284 | } |
270 | |
285 | |
271 | # uri => path mapping |
286 | # uri => path mapping |
272 | sub map_uri { |
287 | sub map_uri { |
273 | my $self = shift; |
288 | my $self = shift; |
… | |
… | |
447 | open $fh, "<", $self->{path} |
462 | open $fh, "<", $self->{path} |
448 | or die "$self->{path}: late open failure ($!)"; |
463 | or die "$self->{path}: late open failure ($!)"; |
449 | |
464 | |
450 | $h -= $l - 1; |
465 | $h -= $l - 1; |
451 | |
466 | |
|
|
467 | if (0) { |
|
|
468 | if ($l) { |
|
|
469 | sysseek $fh, $l, 0; |
|
|
470 | } |
|
|
471 | } |
|
|
472 | |
452 | while ($h > 0) { |
473 | while ($h > 0) { |
|
|
474 | if (0) { |
|
|
475 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
476 | or last; |
|
|
477 | } else { |
453 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
478 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
454 | $buf, 0, sub { |
479 | $buf, 0, sub { |
455 | $r = $_[0]; |
480 | $r = $_[0]; |
456 | $current->ready; |
481 | $current->ready; |
457 | }); |
482 | }); |
458 | &Coro::schedule; |
483 | &Coro::schedule; |
459 | last unless $r; |
484 | last unless $r; |
|
|
485 | } |
460 | my $w = $self->{fh}->syswrite($buf) |
486 | my $w = $self->{fh}->syswrite($buf) |
461 | or last; |
487 | or last; |
462 | $::written += $w; |
488 | $::written += $w; |
463 | $self->{written} += $w; |
489 | $self->{written} += $w; |
464 | $l += $r; |
490 | $l += $r; |