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