… | |
… | |
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 | |
… | |
… | |
114 | sub DESTROY { |
121 | sub DESTROY { |
115 | my $self = shift; |
122 | my $self = shift; |
116 | |
123 | |
117 | $::conns--; |
124 | $::conns--; |
118 | |
125 | |
|
|
126 | $self->eoconn; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
128 | } |
|
|
129 | |
|
|
130 | # end of connection |
|
|
131 | sub eoconn { |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
121 | } |
133 | } |
122 | |
134 | |
123 | sub slog { |
135 | sub slog { |
124 | my $self = shift; |
136 | my $self = shift; |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
137 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
126 | } |
138 | } |
127 | |
139 | |
128 | sub response { |
140 | sub response { |
129 | my ($self, $code, $msg, $hdr, $content) = @_; |
141 | my ($self, $code, $msg, $hdr, $content) = @_; |
130 | my $res = "HTTP/1.0 $code $msg\015\012"; |
142 | my $res = "HTTP/1.1 $code $msg\015\012"; |
131 | |
143 | |
132 | $res .= "Connection: close\015\012"; |
144 | #$res .= "Connection: close\015\012"; |
133 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
145 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
134 | |
146 | |
135 | while (my ($h, $v) = each %$hdr) { |
147 | while (my ($h, $v) = each %$hdr) { |
136 | $res .= "$h: $v\015\012" |
148 | $res .= "$h: $v\015\012" |
137 | } |
149 | } |
… | |
… | |
152 | unless (defined $content) { |
164 | unless (defined $content) { |
153 | $content = "$code $msg"; |
165 | $content = "$code $msg"; |
154 | $hdr->{"Content-Type"} = "text/plain"; |
166 | $hdr->{"Content-Type"} = "text/plain"; |
155 | $hdr->{"Content-Length"} = length $content; |
167 | $hdr->{"Content-Length"} = length $content; |
156 | } |
168 | } |
|
|
169 | $hdr->{"Connection"} = "close"; |
157 | |
170 | |
158 | $self->response($code, $msg, $hdr, $content); |
171 | $self->response($code, $msg, $hdr, $content); |
159 | |
172 | |
160 | die bless {}, err::; |
173 | die bless {}, err::; |
161 | } |
174 | } |
… | |
… | |
163 | sub err_blocked { |
176 | sub err_blocked { |
164 | my $self = shift; |
177 | my $self = shift; |
165 | my $ip = $self->{remote_addr}; |
178 | my $ip = $self->{remote_addr}; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
167 | |
180 | |
|
|
181 | Coro::Event::do_timer(after => 15); |
|
|
182 | |
168 | $self->err(403, "too many connections", |
183 | $self->err(401, "too many connections", |
169 | { |
184 | { |
170 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
171 | "Retry-After" => $::BLOCKTIME |
186 | "Retry-After" => $::BLOCKTIME |
172 | }, |
187 | }, |
173 | <<EOF); |
188 | <<EOF); |
… | |
… | |
185 | |
200 | |
186 | sub handle { |
201 | sub handle { |
187 | my $self = shift; |
202 | my $self = shift; |
188 | my $fh = $self->{fh}; |
203 | my $fh = $self->{fh}; |
189 | |
204 | |
|
|
205 | $fh->timeout($::REQ_TIMEOUT); |
190 | #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 | |
191 | $self->{h} = {}; |
220 | $self->{h} = {}; |
192 | |
221 | |
193 | # read request and parse first line |
|
|
194 | $fh->timeout($::REQ_TIMEOUT); |
|
|
195 | my $req = $fh->readline("\015\012\015\012"); |
|
|
196 | $fh->timeout($::RES_TIMEOUT); |
222 | $fh->timeout($::RES_TIMEOUT); |
197 | |
|
|
198 | defined $req or |
|
|
199 | $self->err(408, "request timeout"); |
|
|
200 | |
|
|
201 | my $ip = $self->{remote_addr}; |
223 | my $ip = $self->{remote_addr}; |
202 | |
224 | |
203 | if ($blocked{$ip}) { |
225 | if ($blocked{$ip}) { |
204 | $self->err_blocked($blocked{$ip}) |
226 | $self->err_blocked($blocked{$ip}) |
205 | if $blocked{$ip} > $::NOW; |
227 | if $blocked{$ip} > $::NOW; |
… | |
… | |
215 | $req =~ /^(?:\015\012)? |
237 | $req =~ /^(?:\015\012)? |
216 | (GET|HEAD) \040+ |
238 | (GET|HEAD) \040+ |
217 | ([^\040]+) \040+ |
239 | ([^\040]+) \040+ |
218 | HTTP\/([0-9]+\.[0-9]+) |
240 | HTTP\/([0-9]+\.[0-9]+) |
219 | \015\012/gx |
241 | \015\012/gx |
220 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
242 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
221 | |
|
|
222 | $2 ne "1.0" |
|
|
223 | or $self->err(506, "http protocol version not supported"); |
|
|
224 | |
243 | |
225 | $self->{method} = $1; |
244 | $self->{method} = $1; |
226 | $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"); |
227 | |
250 | |
228 | # parse headers |
251 | # parse headers |
229 | { |
252 | { |
230 | my (%hdr, $h, $v); |
253 | my (%hdr, $h, $v); |
231 | |
254 | |
… | |
… | |
248 | |
271 | |
249 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
272 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
250 | |
273 | |
251 | $self->map_uri; |
274 | $self->map_uri; |
252 | $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); |
253 | #} |
283 | } |
254 | } |
284 | } |
255 | |
285 | |
256 | # uri => path mapping |
286 | # uri => path mapping |
257 | sub map_uri { |
287 | sub map_uri { |
258 | my $self = shift; |
288 | my $self = shift; |
… | |
… | |
403 | |
433 | |
404 | satisfiable: |
434 | satisfiable: |
405 | # check for segmented downloads |
435 | # check for segmented downloads |
406 | if ($l && $::NO_SEGMENTED) { |
436 | if ($l && $::NO_SEGMENTED) { |
407 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
437 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
438 | Coro::Event::do_timer(after => 15); |
|
|
439 | |
408 | $self->err(400, "segmented downloads are not allowed"); |
440 | $self->err(400, "segmented downloads are not allowed"); |
409 | } |
441 | } |
410 | } |
442 | } |
411 | |
443 | |
412 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
444 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
423 | $hdr->{"Content-Length"} = $length; |
455 | $hdr->{"Content-Length"} = $length; |
424 | |
456 | |
425 | $self->response(@code, $hdr, ""); |
457 | $self->response(@code, $hdr, ""); |
426 | |
458 | |
427 | if ($self->{method} eq "GET") { |
459 | if ($self->{method} eq "GET") { |
428 | my ($fh, $buf); |
460 | my ($fh, $buf, $r); |
|
|
461 | my $current = $Coro::current; |
429 | open $fh, "<", $self->{path} |
462 | open $fh, "<", $self->{path} |
430 | or die "$self->{path}: late open failure ($!)"; |
463 | or die "$self->{path}: late open failure ($!)"; |
431 | |
464 | |
432 | if ($l) { |
|
|
433 | sysseek $fh, $l, 0 |
|
|
434 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
435 | } |
|
|
436 | |
|
|
437 | $h -= $l - 1; |
465 | $h -= $l - 1; |
438 | |
466 | |
|
|
467 | if (0) { |
|
|
468 | if ($l) { |
|
|
469 | sysseek $fh, $l, 0; |
|
|
470 | } |
|
|
471 | } |
|
|
472 | |
439 | while ($h > 0) { |
473 | while ($h > 0) { |
|
|
474 | if (0) { |
440 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
475 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
476 | or last; |
|
|
477 | } else { |
|
|
478 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
479 | $buf, 0, sub { |
|
|
480 | $r = $_[0]; |
|
|
481 | $current->ready; |
|
|
482 | }); |
|
|
483 | &Coro::schedule; |
|
|
484 | last unless $r; |
|
|
485 | } |
441 | my $w = $self->{fh}->syswrite($buf) |
486 | my $w = $self->{fh}->syswrite($buf) |
442 | or last; |
487 | or last; |
443 | $::written += $w; |
488 | $::written += $w; |
444 | $self->{written} += $w; |
489 | $self->{written} += $w; |
|
|
490 | $l += $r; |
445 | } |
491 | } |
446 | } |
492 | } |
447 | |
493 | |
448 | close $fh; |
494 | close $fh; |
449 | } |
495 | } |