… | |
… | |
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 | |
84 | Event->add_hooks(prepare => sub { |
83 | Event->add_hooks(prepare => sub { |
85 | &Coro::cede while &Coro::nready; |
84 | &Coro::cede; |
86 | 1e6; |
|
|
87 | }); |
85 | }); |
88 | |
86 | |
89 | our %conn; # $conn{ip}{fh} => connobj |
87 | our %conn; # $conn{ip}{fh} => connobj |
90 | our %blocked; |
88 | our %blocked; |
91 | our %mimetype; |
89 | our %mimetype; |
… | |
… | |
138 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
136 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
139 | } |
137 | } |
140 | |
138 | |
141 | sub response { |
139 | sub response { |
142 | my ($self, $code, $msg, $hdr, $content) = @_; |
140 | my ($self, $code, $msg, $hdr, $content) = @_; |
143 | my $res = "HTTP/1.0 $code $msg\015\012"; |
141 | my $res = "HTTP/1.1 $code $msg\015\012"; |
144 | |
142 | |
145 | $res .= "Connection: close\015\012"; |
143 | #$res .= "Connection: close\015\012"; |
146 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
144 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
147 | |
145 | |
148 | while (my ($h, $v) = each %$hdr) { |
146 | while (my ($h, $v) = each %$hdr) { |
149 | $res .= "$h: $v\015\012" |
147 | $res .= "$h: $v\015\012" |
150 | } |
148 | } |
… | |
… | |
165 | unless (defined $content) { |
163 | unless (defined $content) { |
166 | $content = "$code $msg"; |
164 | $content = "$code $msg"; |
167 | $hdr->{"Content-Type"} = "text/plain"; |
165 | $hdr->{"Content-Type"} = "text/plain"; |
168 | $hdr->{"Content-Length"} = length $content; |
166 | $hdr->{"Content-Length"} = length $content; |
169 | } |
167 | } |
|
|
168 | $hdr->{"Connection"} = "close"; |
170 | |
169 | |
171 | $self->response($code, $msg, $hdr, $content); |
170 | $self->response($code, $msg, $hdr, $content); |
172 | |
171 | |
173 | die bless {}, err::; |
172 | die bless {}, err::; |
174 | } |
173 | } |
… | |
… | |
200 | |
199 | |
201 | sub handle { |
200 | sub handle { |
202 | my $self = shift; |
201 | my $self = shift; |
203 | my $fh = $self->{fh}; |
202 | my $fh = $self->{fh}; |
204 | |
203 | |
|
|
204 | $fh->timeout($::REQ_TIMEOUT); |
205 | #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 | |
206 | $self->{h} = {}; |
219 | $self->{h} = {}; |
207 | |
220 | |
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); |
221 | $fh->timeout($::RES_TIMEOUT); |
212 | |
|
|
213 | defined $req or |
|
|
214 | $self->err(408, "request timeout"); |
|
|
215 | |
|
|
216 | my $ip = $self->{remote_addr}; |
222 | my $ip = $self->{remote_addr}; |
217 | |
223 | |
218 | if ($blocked{$ip}) { |
224 | if ($blocked{$ip}) { |
219 | $self->err_blocked($blocked{$ip}) |
225 | $self->err_blocked($blocked{$ip}) |
220 | if $blocked{$ip} > $::NOW; |
226 | if $blocked{$ip} > $::NOW; |
… | |
… | |
232 | ([^\040]+) \040+ |
238 | ([^\040]+) \040+ |
233 | HTTP\/([0-9]+\.[0-9]+) |
239 | HTTP\/([0-9]+\.[0-9]+) |
234 | \015\012/gx |
240 | \015\012/gx |
235 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
241 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
236 | |
242 | |
237 | $2 ne "1.0" |
|
|
238 | or $self->err(506, "http protocol version not supported"); |
|
|
239 | |
|
|
240 | $self->{method} = $1; |
243 | $self->{method} = $1; |
241 | $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"); |
242 | |
249 | |
243 | # parse headers |
250 | # parse headers |
244 | { |
251 | { |
245 | my (%hdr, $h, $v); |
252 | my (%hdr, $h, $v); |
246 | |
253 | |
… | |
… | |
263 | |
270 | |
264 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
271 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
265 | |
272 | |
266 | $self->map_uri; |
273 | $self->map_uri; |
267 | $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); |
268 | #} |
280 | } |
269 | } |
281 | } |
270 | |
282 | |
271 | # uri => path mapping |
283 | # uri => path mapping |
272 | sub map_uri { |
284 | sub map_uri { |
273 | my $self = shift; |
285 | my $self = shift; |