… | |
… | |
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 | |
168 | Coro::Event::do_timer(after => 15); |
181 | Coro::Event::do_timer(after => 20*rand); |
169 | |
182 | |
170 | $self->err(401, "too many connections", |
183 | $self->err(401, "too many connections", |
171 | { |
184 | { |
172 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
173 | "Retry-After" => $::BLOCKTIME |
186 | "Retry-After" => $::BLOCKTIME, |
|
|
187 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
188 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
174 | }, |
189 | }, |
175 | <<EOF); |
190 | <<EOF); |
176 | <html><p> |
191 | <html><p> |
177 | You have been blocked because you opened too many connections. You |
192 | You have been blocked because you opened too many connections. You |
178 | may retry at</p> |
193 | may retry at</p> |
… | |
… | |
187 | |
202 | |
188 | sub handle { |
203 | sub handle { |
189 | my $self = shift; |
204 | my $self = shift; |
190 | my $fh = $self->{fh}; |
205 | my $fh = $self->{fh}; |
191 | |
206 | |
|
|
207 | $fh->timeout($::REQ_TIMEOUT); |
192 | #while() { |
208 | while() { |
|
|
209 | $self->{reqs}++; |
|
|
210 | |
|
|
211 | # read request and parse first line |
|
|
212 | my $req = $fh->readline("\015\012\015\012"); |
|
|
213 | |
|
|
214 | unless (defined $req) { |
|
|
215 | if (exists $self->{version}) { |
|
|
216 | last; |
|
|
217 | } else { |
|
|
218 | $self->err(408, "request timeout"); |
|
|
219 | } |
|
|
220 | } |
|
|
221 | |
193 | $self->{h} = {}; |
222 | $self->{h} = {}; |
194 | |
223 | |
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); |
224 | $fh->timeout($::RES_TIMEOUT); |
199 | |
|
|
200 | defined $req or |
|
|
201 | $self->err(408, "request timeout"); |
|
|
202 | |
|
|
203 | my $ip = $self->{remote_addr}; |
225 | my $ip = $self->{remote_addr}; |
204 | |
226 | |
205 | if ($blocked{$ip}) { |
227 | if ($blocked{$ip}) { |
206 | $self->err_blocked($blocked{$ip}) |
228 | $self->err_blocked($blocked{$ip}) |
207 | if $blocked{$ip} > $::NOW; |
229 | if $blocked{$ip} > $::NOW; |
… | |
… | |
219 | ([^\040]+) \040+ |
241 | ([^\040]+) \040+ |
220 | HTTP\/([0-9]+\.[0-9]+) |
242 | HTTP\/([0-9]+\.[0-9]+) |
221 | \015\012/gx |
243 | \015\012/gx |
222 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
244 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
223 | |
245 | |
224 | $2 ne "1.0" |
|
|
225 | or $self->err(506, "http protocol version not supported"); |
|
|
226 | |
|
|
227 | $self->{method} = $1; |
246 | $self->{method} = $1; |
228 | $self->{uri} = $2; |
247 | $self->{uri} = $2; |
|
|
248 | $self->{version} = $3; |
|
|
249 | |
|
|
250 | $3 =~ /^1\./ |
|
|
251 | or $self->err(506, "http protocol version $3 not supported"); |
229 | |
252 | |
230 | # parse headers |
253 | # parse headers |
231 | { |
254 | { |
232 | my (%hdr, $h, $v); |
255 | my (%hdr, $h, $v); |
233 | |
256 | |
… | |
… | |
248 | |
271 | |
249 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
272 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
250 | |
273 | |
251 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
274 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
252 | |
275 | |
|
|
276 | eval { |
253 | $self->map_uri; |
277 | $self->map_uri; |
254 | $self->respond; |
278 | $self->respond; |
|
|
279 | }; |
|
|
280 | |
|
|
281 | die if $@ && !ref $@; |
|
|
282 | |
|
|
283 | $self->eoconn; |
|
|
284 | |
|
|
285 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
286 | |
|
|
287 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
288 | $fh->timeout($::PER_TIMEOUT); |
255 | #} |
289 | } |
256 | } |
290 | } |
257 | |
291 | |
258 | # uri => path mapping |
292 | # uri => path mapping |
259 | sub map_uri { |
293 | sub map_uri { |
260 | my $self = shift; |
294 | my $self = shift; |
… | |
… | |
399 | goto ignore; |
433 | goto ignore; |
400 | } |
434 | } |
401 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
435 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
402 | } |
436 | } |
403 | $hdr->{"Content-Range"} = "bytes */$length"; |
437 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
438 | $hdr->{"Content-Length"} = $length; |
|
|
439 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
404 | $self->err(416, "not satisfiable", $hdr); |
440 | $self->err(416, "not satisfiable", $hdr, ""); |
405 | |
441 | |
406 | satisfiable: |
442 | satisfiable: |
407 | # check for segmented downloads |
443 | # check for segmented downloads |
408 | if ($l && $::NO_SEGMENTED) { |
444 | if ($l && $::NO_SEGMENTED) { |
409 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
445 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
… | |
… | |
427 | $hdr->{"Content-Length"} = $length; |
463 | $hdr->{"Content-Length"} = $length; |
428 | |
464 | |
429 | $self->response(@code, $hdr, ""); |
465 | $self->response(@code, $hdr, ""); |
430 | |
466 | |
431 | if ($self->{method} eq "GET") { |
467 | if ($self->{method} eq "GET") { |
432 | my ($fh, $buf); |
468 | my ($fh, $buf, $r); |
|
|
469 | my $current = $Coro::current; |
433 | open $fh, "<", $self->{path} |
470 | open $fh, "<", $self->{path} |
434 | or die "$self->{path}: late open failure ($!)"; |
471 | or die "$self->{path}: late open failure ($!)"; |
435 | |
472 | |
436 | if ($l) { |
|
|
437 | sysseek $fh, $l, 0 |
|
|
438 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
439 | } |
|
|
440 | |
|
|
441 | $h -= $l - 1; |
473 | $h -= $l - 1; |
442 | |
474 | |
|
|
475 | if (0) { |
|
|
476 | if ($l) { |
|
|
477 | sysseek $fh, $l, 0; |
|
|
478 | } |
|
|
479 | } |
|
|
480 | |
443 | while ($h > 0) { |
481 | while ($h > 0) { |
|
|
482 | if (0) { |
444 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
483 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
484 | or last; |
|
|
485 | } else { |
|
|
486 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
487 | $buf, 0, sub { |
|
|
488 | $r = $_[0]; |
|
|
489 | $current->ready; |
|
|
490 | }); |
|
|
491 | &Coro::schedule; |
|
|
492 | last unless $r; |
|
|
493 | } |
445 | my $w = $self->{fh}->syswrite($buf) |
494 | my $w = $self->{fh}->syswrite($buf) |
446 | or last; |
495 | or last; |
447 | $::written += $w; |
496 | $::written += $w; |
448 | $self->{written} += $w; |
497 | $self->{written} += $w; |
|
|
498 | $l += $r; |
449 | } |
499 | } |
450 | } |
500 | } |
451 | |
501 | |
452 | close $fh; |
502 | close $fh; |
453 | } |
503 | } |