… | |
… | |
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 | my $scheduler = Event->idle( |
|
|
83 | max => 0, min => 0, prio => 6, parked => 1, |
|
|
84 | cb => \&Coro::schedule); |
75 | |
85 | |
76 | our %conn; # $conn{ip}{fh} => connobj |
86 | our %conn; # $conn{ip}{fh} => connobj |
77 | our %blocked; |
87 | our %blocked; |
78 | our %mimetype; |
88 | our %mimetype; |
79 | |
89 | |
… | |
… | |
114 | sub DESTROY { |
124 | sub DESTROY { |
115 | my $self = shift; |
125 | my $self = shift; |
116 | |
126 | |
117 | $::conns--; |
127 | $::conns--; |
118 | |
128 | |
|
|
129 | $self->eoconn; |
119 | delete $conn{$self->{remote_addr}}{$self*1}; |
130 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
131 | } |
|
|
132 | |
|
|
133 | # end of connection |
|
|
134 | sub eoconn { |
120 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
135 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
121 | } |
136 | } |
122 | |
137 | |
123 | sub slog { |
138 | sub slog { |
124 | my $self = shift; |
139 | my $self = shift; |
125 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
140 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
126 | } |
141 | } |
127 | |
142 | |
128 | sub response { |
143 | sub response { |
129 | my ($self, $code, $msg, $hdr, $content) = @_; |
144 | my ($self, $code, $msg, $hdr, $content) = @_; |
130 | my $res = "HTTP/1.0 $code $msg\015\012"; |
145 | my $res = "HTTP/1.1 $code $msg\015\012"; |
131 | |
146 | |
132 | $res .= "Connection: close\015\012"; |
147 | #$res .= "Connection: close\015\012"; |
133 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
148 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
134 | |
149 | |
135 | while (my ($h, $v) = each %$hdr) { |
150 | while (my ($h, $v) = each %$hdr) { |
136 | $res .= "$h: $v\015\012" |
151 | $res .= "$h: $v\015\012" |
137 | } |
152 | } |
… | |
… | |
152 | unless (defined $content) { |
167 | unless (defined $content) { |
153 | $content = "$code $msg"; |
168 | $content = "$code $msg"; |
154 | $hdr->{"Content-Type"} = "text/plain"; |
169 | $hdr->{"Content-Type"} = "text/plain"; |
155 | $hdr->{"Content-Length"} = length $content; |
170 | $hdr->{"Content-Length"} = length $content; |
156 | } |
171 | } |
|
|
172 | $hdr->{"Connection"} = "close"; |
157 | |
173 | |
158 | $self->response($code, $msg, $hdr, $content); |
174 | $self->response($code, $msg, $hdr, $content); |
159 | |
175 | |
160 | die bless {}, err::; |
176 | die bless {}, err::; |
161 | } |
177 | } |
… | |
… | |
163 | sub err_blocked { |
179 | sub err_blocked { |
164 | my $self = shift; |
180 | my $self = shift; |
165 | my $ip = $self->{remote_addr}; |
181 | my $ip = $self->{remote_addr}; |
166 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
182 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
167 | |
183 | |
|
|
184 | Coro::Event::do_timer(after => 20*rand); |
|
|
185 | |
168 | $self->err(403, "too many connections", |
186 | $self->err(401, "too many connections", |
169 | { |
187 | { |
170 | "Content-Type" => "text/html", |
188 | "Content-Type" => "text/html", |
171 | "Retry-After" => $::BLOCKTIME |
189 | "Retry-After" => $::BLOCKTIME, |
|
|
190 | "Warning" => "Please do NOT retry, you have been blocked", |
|
|
191 | "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"", |
172 | }, |
192 | }, |
173 | <<EOF); |
193 | <<EOF); |
174 | <html><p> |
194 | <html><p> |
175 | You have been blocked because you opened too many connections. You |
195 | You have been blocked because you opened too many connections. You |
176 | may retry at</p> |
196 | may retry at</p> |
… | |
… | |
185 | |
205 | |
186 | sub handle { |
206 | sub handle { |
187 | my $self = shift; |
207 | my $self = shift; |
188 | my $fh = $self->{fh}; |
208 | my $fh = $self->{fh}; |
189 | |
209 | |
|
|
210 | $fh->timeout($::REQ_TIMEOUT); |
190 | #while() { |
211 | while() { |
|
|
212 | $self->{reqs}++; |
|
|
213 | |
|
|
214 | # read request and parse first line |
|
|
215 | my $req = $fh->readline("\015\012\015\012"); |
|
|
216 | |
|
|
217 | unless (defined $req) { |
|
|
218 | if (exists $self->{version}) { |
|
|
219 | last; |
|
|
220 | } else { |
|
|
221 | $self->err(408, "request timeout"); |
|
|
222 | } |
|
|
223 | } |
|
|
224 | |
191 | $self->{h} = {}; |
225 | $self->{h} = {}; |
192 | |
226 | |
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); |
227 | $fh->timeout($::RES_TIMEOUT); |
197 | |
|
|
198 | defined $req or |
|
|
199 | $self->err(408, "request timeout"); |
|
|
200 | |
|
|
201 | my $ip = $self->{remote_addr}; |
228 | my $ip = $self->{remote_addr}; |
202 | |
229 | |
203 | if ($blocked{$ip}) { |
230 | if ($blocked{$ip}) { |
204 | $self->err_blocked($blocked{$ip}) |
231 | $self->err_blocked($blocked{$ip}) |
205 | if $blocked{$ip} > $::NOW; |
232 | if $blocked{$ip} > $::NOW; |
… | |
… | |
215 | $req =~ /^(?:\015\012)? |
242 | $req =~ /^(?:\015\012)? |
216 | (GET|HEAD) \040+ |
243 | (GET|HEAD) \040+ |
217 | ([^\040]+) \040+ |
244 | ([^\040]+) \040+ |
218 | HTTP\/([0-9]+\.[0-9]+) |
245 | HTTP\/([0-9]+\.[0-9]+) |
219 | \015\012/gx |
246 | \015\012/gx |
220 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
247 | 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 | |
248 | |
225 | $self->{method} = $1; |
249 | $self->{method} = $1; |
226 | $self->{uri} = $2; |
250 | $self->{uri} = $2; |
|
|
251 | $self->{version} = $3; |
|
|
252 | |
|
|
253 | $3 =~ /^1\./ |
|
|
254 | or $self->err(506, "http protocol version $3 not supported"); |
227 | |
255 | |
228 | # parse headers |
256 | # parse headers |
229 | { |
257 | { |
230 | my (%hdr, $h, $v); |
258 | my (%hdr, $h, $v); |
231 | |
259 | |
… | |
… | |
248 | |
276 | |
249 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
277 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
250 | |
278 | |
251 | $self->map_uri; |
279 | $self->map_uri; |
252 | $self->respond; |
280 | $self->respond; |
|
|
281 | |
|
|
282 | $self->eoconn; |
|
|
283 | |
|
|
284 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
285 | |
|
|
286 | $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
287 | $fh->timeout($::PER_TIMEOUT); |
253 | #} |
288 | } |
254 | } |
289 | } |
255 | |
290 | |
256 | # uri => path mapping |
291 | # uri => path mapping |
257 | sub map_uri { |
292 | sub map_uri { |
258 | my $self = shift; |
293 | my $self = shift; |
… | |
… | |
397 | goto ignore; |
432 | goto ignore; |
398 | } |
433 | } |
399 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
434 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
400 | } |
435 | } |
401 | $hdr->{"Content-Range"} = "bytes */$length"; |
436 | $hdr->{"Content-Range"} = "bytes */$length"; |
|
|
437 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
402 | $self->err(416, "not satisfiable", $hdr); |
438 | $self->err(416, "not satisfiable", $hdr); |
403 | |
439 | |
404 | satisfiable: |
440 | satisfiable: |
405 | # check for segmented downloads |
441 | # check for segmented downloads |
406 | if ($l && $::NO_SEGMENTED) { |
442 | if ($l && $::NO_SEGMENTED) { |
407 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
443 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
|
|
444 | Coro::Event::do_timer(after => 15); |
|
|
445 | |
408 | $self->err(400, "segmented downloads are not allowed"); |
446 | $self->err(400, "segmented downloads are not allowed"); |
409 | } |
447 | } |
410 | } |
448 | } |
411 | |
449 | |
412 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
450 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
423 | $hdr->{"Content-Length"} = $length; |
461 | $hdr->{"Content-Length"} = $length; |
424 | |
462 | |
425 | $self->response(@code, $hdr, ""); |
463 | $self->response(@code, $hdr, ""); |
426 | |
464 | |
427 | if ($self->{method} eq "GET") { |
465 | if ($self->{method} eq "GET") { |
428 | my ($fh, $buf); |
466 | my ($fh, $buf, $r); |
|
|
467 | my $current = $Coro::current; |
429 | open $fh, "<", $self->{path} |
468 | open $fh, "<", $self->{path} |
430 | or die "$self->{path}: late open failure ($!)"; |
469 | or die "$self->{path}: late open failure ($!)"; |
431 | |
470 | |
432 | if ($l) { |
|
|
433 | sysseek $fh, $l, 0 |
|
|
434 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
435 | } |
|
|
436 | |
|
|
437 | $h -= $l - 1; |
471 | $h -= $l - 1; |
438 | |
472 | |
|
|
473 | if (0) { |
|
|
474 | if ($l) { |
|
|
475 | sysseek $fh, $l, 0; |
|
|
476 | } |
|
|
477 | } |
|
|
478 | |
439 | while ($h > 0) { |
479 | while ($h > 0) { |
|
|
480 | if (0) { |
440 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
481 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
|
|
482 | or last; |
|
|
483 | } else { |
|
|
484 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
485 | $buf, 0, sub { |
|
|
486 | $r = $_[0]; |
|
|
487 | $current->ready; |
|
|
488 | $scheduler->now; |
|
|
489 | }); |
|
|
490 | &Coro::schedule; |
|
|
491 | last unless $r; |
|
|
492 | } |
441 | my $w = $self->{fh}->syswrite($buf) |
493 | my $w = $self->{fh}->syswrite($buf) |
442 | or last; |
494 | or last; |
443 | $::written += $w; |
495 | $::written += $w; |
444 | $self->{written} += $w; |
496 | $self->{written} += $w; |
|
|
497 | $l += $r; |
445 | } |
498 | } |
446 | } |
499 | } |
447 | |
500 | |
448 | close $fh; |
501 | close $fh; |
449 | } |
502 | } |