… | |
… | |
43 | |
43 | |
44 | my $http_port = new Coro::Socket |
44 | my $http_port = new Coro::Socket |
45 | LocalAddr => $SERVER_HOST, |
45 | LocalAddr => $SERVER_HOST, |
46 | LocalPort => $SERVER_PORT, |
46 | LocalPort => $SERVER_PORT, |
47 | ReuseAddr => 1, |
47 | ReuseAddr => 1, |
48 | Listen => 1, |
48 | Listen => 50, |
49 | or die "unable to start server"; |
49 | or die "unable to start server"; |
50 | |
50 | |
51 | push @listen_sockets, $http_port; |
51 | push @listen_sockets, $http_port; |
52 | |
52 | |
53 | # the "main thread" |
53 | # the "main thread" |
… | |
… | |
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 | |
… | |
… | |
99 | my $self = bless { fh => $fh }, $class; |
106 | my $self = bless { fh => $fh }, $class; |
100 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
107 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
101 | or $self->err(500, "unable to decode peername"); |
108 | or $self->err(500, "unable to decode peername"); |
102 | |
109 | |
103 | $self->{remote_addr} = inet_ntoa $iaddr; |
110 | $self->{remote_addr} = inet_ntoa $iaddr; |
|
|
111 | $self->{time} = $::NOW; |
104 | |
112 | |
105 | # enter ourselves into various lists |
113 | # enter ourselves into various lists |
106 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
114 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
107 | |
115 | |
|
|
116 | $::conns++; |
|
|
117 | |
108 | $self; |
118 | $self; |
109 | } |
119 | } |
110 | |
120 | |
111 | sub DESTROY { |
121 | sub DESTROY { |
112 | my $self = shift; |
122 | my $self = shift; |
|
|
123 | |
|
|
124 | $::conns--; |
|
|
125 | |
|
|
126 | $self->eoconn; |
113 | delete $conn{$self->{remote_addr}}{$self*1}; |
127 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
128 | } |
|
|
129 | |
|
|
130 | # end of connection |
|
|
131 | sub eoconn { |
114 | delete $uri{$self->{uri}}{$self*1}; |
132 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
115 | } |
133 | } |
116 | |
134 | |
117 | sub slog { |
135 | sub slog { |
118 | my $self = shift; |
136 | my $self = shift; |
119 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
137 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
120 | } |
138 | } |
121 | |
139 | |
122 | sub response { |
140 | sub response { |
123 | my ($self, $code, $msg, $hdr, $content) = @_; |
141 | my ($self, $code, $msg, $hdr, $content) = @_; |
124 | my $res = "HTTP/1.0 $code $msg\015\012"; |
142 | my $res = "HTTP/1.1 $code $msg\015\012"; |
125 | |
143 | |
126 | $res .= "Connection: close\015\012"; |
144 | #$res .= "Connection: close\015\012"; |
127 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
145 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
128 | |
146 | |
129 | while (my ($h, $v) = each %$hdr) { |
147 | while (my ($h, $v) = each %$hdr) { |
130 | $res .= "$h: $v\015\012" |
148 | $res .= "$h: $v\015\012" |
131 | } |
149 | } |
132 | $res .= "\015\012"; |
150 | $res .= "\015\012"; |
133 | |
151 | |
134 | $res .= $content if defined $content and $self->{method} eq "GET"; |
152 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
135 | |
153 | |
136 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
154 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
137 | |
155 | |
|
|
156 | $self->{written} += |
138 | print {$self->{fh}} $res; |
157 | print {$self->{fh}} $res; |
139 | } |
158 | } |
140 | |
159 | |
141 | sub err { |
160 | sub err { |
142 | my $self = shift; |
161 | my $self = shift; |
143 | my ($code, $msg, $hdr, $content) = @_; |
162 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
145 | unless (defined $content) { |
164 | unless (defined $content) { |
146 | $content = "$code $msg"; |
165 | $content = "$code $msg"; |
147 | $hdr->{"Content-Type"} = "text/plain"; |
166 | $hdr->{"Content-Type"} = "text/plain"; |
148 | $hdr->{"Content-Length"} = length $content; |
167 | $hdr->{"Content-Length"} = length $content; |
149 | } |
168 | } |
|
|
169 | $hdr->{"Connection"} = "close"; |
150 | |
170 | |
151 | $self->response($code, $msg, $hdr, $content); |
171 | $self->response($code, $msg, $hdr, $content); |
152 | |
172 | |
153 | die bless {}, err::; |
173 | die bless {}, err::; |
154 | } |
174 | } |
… | |
… | |
156 | sub err_blocked { |
176 | sub err_blocked { |
157 | my $self = shift; |
177 | my $self = shift; |
158 | my $ip = $self->{remote_addr}; |
178 | my $ip = $self->{remote_addr}; |
159 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
179 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
160 | |
180 | |
161 | Coro::Event::do_timer(after => 5); |
181 | Coro::Event::do_timer(after => 15); |
162 | |
182 | |
163 | $self->err(403, "too many connections", |
183 | $self->err(401, "too many connections", |
164 | { |
184 | { |
165 | "Content-Type" => "text/html", |
185 | "Content-Type" => "text/html", |
166 | "Retry-After" => $::BLOCKTIME |
186 | "Retry-After" => $::BLOCKTIME |
167 | }, |
187 | }, |
168 | <<EOF); |
188 | <<EOF); |
… | |
… | |
180 | |
200 | |
181 | sub handle { |
201 | sub handle { |
182 | my $self = shift; |
202 | my $self = shift; |
183 | my $fh = $self->{fh}; |
203 | my $fh = $self->{fh}; |
184 | |
204 | |
|
|
205 | $fh->timeout($::REQ_TIMEOUT); |
185 | #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 | |
186 | $self->{h} = {}; |
220 | $self->{h} = {}; |
187 | |
221 | |
188 | # read request and parse first line |
|
|
189 | $fh->timeout($::REQ_TIMEOUT); |
|
|
190 | my $req = $fh->readline("\015\012\015\012"); |
|
|
191 | $fh->timeout($::RES_TIMEOUT); |
222 | $fh->timeout($::RES_TIMEOUT); |
192 | |
|
|
193 | defined $req or |
|
|
194 | $self->err(408, "request timeout"); |
|
|
195 | |
|
|
196 | my $ip = $self->{remote_addr}; |
223 | my $ip = $self->{remote_addr}; |
197 | |
224 | |
198 | if ($blocked{$ip}) { |
225 | if ($blocked{$ip}) { |
199 | $self->err_blocked($blocked{$ip}) |
226 | $self->err_blocked($blocked{$ip}) |
200 | if $blocked{$ip} > $::NOW; |
227 | if $blocked{$ip} > $::NOW; |
201 | |
228 | |
202 | delete $blocked{$ip}; |
229 | delete $blocked{$ip}; |
203 | } |
230 | } |
204 | |
231 | |
205 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
232 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
206 | $self->slog("blocked ip $ip"); |
233 | $self->slog(2, "blocked ip $ip"); |
207 | $self->err_blocked; |
234 | $self->err_blocked; |
208 | } |
235 | } |
209 | |
236 | |
210 | $req =~ /^(?:\015\012)? |
237 | $req =~ /^(?:\015\012)? |
211 | (GET|HEAD) \040+ |
238 | (GET|HEAD) \040+ |
212 | ([^\040]+) \040+ |
239 | ([^\040]+) \040+ |
213 | HTTP\/([0-9]+\.[0-9]+) |
240 | HTTP\/([0-9]+\.[0-9]+) |
214 | \015\012/gx |
241 | \015\012/gx |
215 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
242 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
216 | |
|
|
217 | $2 ne "1.0" |
|
|
218 | or $self->err(506, "http protocol version not supported"); |
|
|
219 | |
243 | |
220 | $self->{method} = $1; |
244 | $self->{method} = $1; |
221 | $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"); |
222 | |
250 | |
223 | # parse headers |
251 | # parse headers |
224 | { |
252 | { |
225 | my (%hdr, $h, $v); |
253 | my (%hdr, $h, $v); |
226 | |
254 | |
… | |
… | |
239 | while ($h, $v) = each %hdr; |
267 | while ($h, $v) = each %hdr; |
240 | } |
268 | } |
241 | |
269 | |
242 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
270 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
243 | |
271 | |
244 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
272 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
245 | |
273 | |
246 | $self->map_uri; |
274 | $self->map_uri; |
247 | $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); |
248 | #} |
283 | } |
249 | } |
284 | } |
250 | |
285 | |
251 | # uri => path mapping |
286 | # uri => path mapping |
252 | sub map_uri { |
287 | sub map_uri { |
253 | my $self = shift; |
288 | my $self = shift; |
… | |
… | |
397 | $self->err(416, "not satisfiable", $hdr); |
432 | $self->err(416, "not satisfiable", $hdr); |
398 | |
433 | |
399 | satisfiable: |
434 | satisfiable: |
400 | # check for segmented downloads |
435 | # check for segmented downloads |
401 | if ($l && $::NO_SEGMENTED) { |
436 | if ($l && $::NO_SEGMENTED) { |
402 | if (%{$uri{$self->{uri}}} > 1) { |
437 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
403 | $self->slog("segmented download refused\n"); |
438 | Coro::Event::do_timer(after => 15); |
|
|
439 | |
404 | $self->err(400, "segmented downloads are not allowed"); |
440 | $self->err(400, "segmented downloads are not allowed"); |
405 | } |
441 | } |
406 | } |
442 | } |
407 | |
443 | |
408 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
444 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
419 | $hdr->{"Content-Length"} = $length; |
455 | $hdr->{"Content-Length"} = $length; |
420 | |
456 | |
421 | $self->response(@code, $hdr, ""); |
457 | $self->response(@code, $hdr, ""); |
422 | |
458 | |
423 | if ($self->{method} eq "GET") { |
459 | if ($self->{method} eq "GET") { |
424 | my ($fh, $buf); |
460 | my ($fh, $buf, $r); |
|
|
461 | my $current = $Coro::current; |
425 | open $fh, "<", $self->{path} |
462 | open $fh, "<", $self->{path} |
426 | or die "$self->{path}: late open failure ($!)"; |
463 | or die "$self->{path}: late open failure ($!)"; |
427 | |
464 | |
428 | if ($l) { |
|
|
429 | sysseek $fh, $l, 0 |
|
|
430 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
431 | } |
|
|
432 | |
|
|
433 | $h -= $l - 1; |
465 | $h -= $l - 1; |
434 | |
466 | |
|
|
467 | if (0) { |
|
|
468 | if ($l) { |
|
|
469 | sysseek $fh, $l, 0; |
|
|
470 | } |
|
|
471 | } |
|
|
472 | |
435 | while ($h > 0) { |
473 | while ($h > 0) { |
|
|
474 | if (0) { |
436 | $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 | } |
437 | $self->{fh}->syswrite($buf) |
486 | my $w = $self->{fh}->syswrite($buf) |
438 | or last; |
487 | or last; |
|
|
488 | $::written += $w; |
|
|
489 | $self->{written} += $w; |
|
|
490 | $l += $r; |
439 | } |
491 | } |
440 | } |
492 | } |
441 | |
493 | |
442 | close $fh; |
494 | close $fh; |
443 | } |
495 | } |