… | |
… | |
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 | |
… | |
… | |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
132 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
126 | } |
133 | } |
127 | |
134 | |
128 | sub response { |
135 | sub response { |
129 | my ($self, $code, $msg, $hdr, $content) = @_; |
136 | my ($self, $code, $msg, $hdr, $content) = @_; |
130 | my $res = "HTTP/1.0 $code $msg\015\012"; |
137 | my $res = "HTTP/1.1 $code $msg\015\012"; |
131 | |
138 | |
132 | $res .= "Connection: close\015\012"; |
139 | #$res .= "Connection: close\015\012"; |
133 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
140 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
134 | |
141 | |
135 | while (my ($h, $v) = each %$hdr) { |
142 | while (my ($h, $v) = each %$hdr) { |
136 | $res .= "$h: $v\015\012" |
143 | $res .= "$h: $v\015\012" |
137 | } |
144 | } |
… | |
… | |
152 | unless (defined $content) { |
159 | unless (defined $content) { |
153 | $content = "$code $msg"; |
160 | $content = "$code $msg"; |
154 | $hdr->{"Content-Type"} = "text/plain"; |
161 | $hdr->{"Content-Type"} = "text/plain"; |
155 | $hdr->{"Content-Length"} = length $content; |
162 | $hdr->{"Content-Length"} = length $content; |
156 | } |
163 | } |
|
|
164 | $hdr->{"Connection"} = "close"; |
157 | |
165 | |
158 | $self->response($code, $msg, $hdr, $content); |
166 | $self->response($code, $msg, $hdr, $content); |
159 | |
167 | |
160 | die bless {}, err::; |
168 | die bless {}, err::; |
161 | } |
169 | } |
… | |
… | |
187 | |
195 | |
188 | sub handle { |
196 | sub handle { |
189 | my $self = shift; |
197 | my $self = shift; |
190 | my $fh = $self->{fh}; |
198 | my $fh = $self->{fh}; |
191 | |
199 | |
|
|
200 | $fh->timeout($::REQ_TIMEOUT); |
192 | #while() { |
201 | while() { |
|
|
202 | $self->{reqs}++; |
|
|
203 | |
|
|
204 | # read request and parse first line |
|
|
205 | my $req = $fh->readline("\015\012\015\012"); |
|
|
206 | |
|
|
207 | unless (defined $req) { |
|
|
208 | if (exists $self->{version}) { |
|
|
209 | last; |
|
|
210 | } else { |
|
|
211 | $self->err(408, "request timeout"); |
|
|
212 | } |
|
|
213 | } |
|
|
214 | |
193 | $self->{h} = {}; |
215 | $self->{h} = {}; |
194 | |
216 | |
195 | # read request and parse first line |
|
|
196 | $fh->timeout($::REQ_TIMEOUT); |
|
|
197 | my $req = $fh->readline("\015\012\015\012"); |
|
|
198 | $fh->timeout($::RES_TIMEOUT); |
217 | $fh->timeout($::RES_TIMEOUT); |
199 | |
|
|
200 | defined $req or |
|
|
201 | $self->err(408, "request timeout"); |
|
|
202 | |
|
|
203 | my $ip = $self->{remote_addr}; |
218 | my $ip = $self->{remote_addr}; |
204 | |
219 | |
205 | $self->err_blocked($blocked{$ip}); |
|
|
206 | if ($blocked{$ip}) { |
220 | if ($blocked{$ip}) { |
207 | $self->err_blocked($blocked{$ip}) |
221 | $self->err_blocked($blocked{$ip}) |
208 | if $blocked{$ip} > $::NOW; |
222 | if $blocked{$ip} > $::NOW; |
209 | |
223 | |
210 | delete $blocked{$ip}; |
224 | delete $blocked{$ip}; |
… | |
… | |
220 | ([^\040]+) \040+ |
234 | ([^\040]+) \040+ |
221 | HTTP\/([0-9]+\.[0-9]+) |
235 | HTTP\/([0-9]+\.[0-9]+) |
222 | \015\012/gx |
236 | \015\012/gx |
223 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
237 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
224 | |
238 | |
225 | $2 ne "1.0" |
|
|
226 | or $self->err(506, "http protocol version not supported"); |
|
|
227 | |
|
|
228 | $self->{method} = $1; |
239 | $self->{method} = $1; |
229 | $self->{uri} = $2; |
240 | $self->{uri} = $2; |
|
|
241 | $self->{version} = $3; |
|
|
242 | |
|
|
243 | $3 eq "1.0" or $3 eq "1.1" |
|
|
244 | or $self->err(506, "http protocol version $3 not supported"); |
230 | |
245 | |
231 | # parse headers |
246 | # parse headers |
232 | { |
247 | { |
233 | my (%hdr, $h, $v); |
248 | my (%hdr, $h, $v); |
234 | |
249 | |
… | |
… | |
251 | |
266 | |
252 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
253 | |
268 | |
254 | $self->map_uri; |
269 | $self->map_uri; |
255 | $self->respond; |
270 | $self->respond; |
|
|
271 | |
|
|
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
273 | |
|
|
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
275 | $fh->timeout($::PER_TIMEOUT); |
256 | #} |
276 | } |
257 | } |
277 | } |
258 | |
278 | |
259 | # uri => path mapping |
279 | # uri => path mapping |
260 | sub map_uri { |
280 | sub map_uri { |
261 | my $self = shift; |
281 | my $self = shift; |
… | |
… | |
428 | $hdr->{"Content-Length"} = $length; |
448 | $hdr->{"Content-Length"} = $length; |
429 | |
449 | |
430 | $self->response(@code, $hdr, ""); |
450 | $self->response(@code, $hdr, ""); |
431 | |
451 | |
432 | if ($self->{method} eq "GET") { |
452 | if ($self->{method} eq "GET") { |
433 | my ($fh, $buf); |
453 | my ($fh, $buf, $r); |
|
|
454 | my $current = $Coro::current; |
434 | open $fh, "<", $self->{path} |
455 | open $fh, "<", $self->{path} |
435 | or die "$self->{path}: late open failure ($!)"; |
456 | or die "$self->{path}: late open failure ($!)"; |
436 | |
457 | |
437 | if ($l) { |
|
|
438 | sysseek $fh, $l, 0 |
|
|
439 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
440 | } |
|
|
441 | |
|
|
442 | $h -= $l - 1; |
458 | $h -= $l - 1; |
443 | |
459 | |
444 | while ($h > 0) { |
460 | while ($h > 0) { |
445 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
462 | $buf, 0, sub { |
|
|
463 | $r = $_[0]; |
|
|
464 | $current->ready; |
|
|
465 | }); |
|
|
466 | &Coro::schedule; |
|
|
467 | last unless $r; |
446 | my $w = $self->{fh}->syswrite($buf) |
468 | my $w = $self->{fh}->syswrite($buf) |
447 | or last; |
469 | or last; |
448 | $::written += $w; |
470 | $::written += $w; |
449 | $self->{written} += $w; |
471 | $self->{written} += $w; |
|
|
472 | $l += $r; |
450 | } |
473 | } |
451 | } |
474 | } |
452 | |
475 | |
453 | close $fh; |
476 | close $fh; |
454 | } |
477 | } |