… | |
… | |
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 | |
… | |
… | |
104 | $self->{time} = $::NOW; |
111 | $self->{time} = $::NOW; |
105 | |
112 | |
106 | # enter ourselves into various lists |
113 | # enter ourselves into various lists |
107 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
114 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
108 | |
115 | |
|
|
116 | $::conns++; |
|
|
117 | |
109 | $self; |
118 | $self; |
110 | } |
119 | } |
111 | |
120 | |
112 | sub DESTROY { |
121 | sub DESTROY { |
113 | my $self = shift; |
122 | my $self = shift; |
|
|
123 | |
|
|
124 | $::conns--; |
|
|
125 | |
114 | delete $conn{$self->{remote_addr}}{$self*1}; |
126 | delete $conn{$self->{remote_addr}}{$self*1}; |
115 | delete $uri{$self->{uri}}{$self*1}; |
127 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
116 | } |
128 | } |
117 | |
129 | |
118 | sub slog { |
130 | sub slog { |
119 | my $self = shift; |
131 | my $self = shift; |
120 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
132 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
121 | } |
133 | } |
122 | |
134 | |
123 | sub response { |
135 | sub response { |
124 | my ($self, $code, $msg, $hdr, $content) = @_; |
136 | my ($self, $code, $msg, $hdr, $content) = @_; |
125 | my $res = "HTTP/1.0 $code $msg\015\012"; |
137 | my $res = "HTTP/1.1 $code $msg\015\012"; |
126 | |
138 | |
127 | $res .= "Connection: close\015\012"; |
139 | #$res .= "Connection: close\015\012"; |
128 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
140 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
129 | |
141 | |
130 | while (my ($h, $v) = each %$hdr) { |
142 | while (my ($h, $v) = each %$hdr) { |
131 | $res .= "$h: $v\015\012" |
143 | $res .= "$h: $v\015\012" |
132 | } |
144 | } |
133 | $res .= "\015\012"; |
145 | $res .= "\015\012"; |
134 | |
146 | |
135 | $res .= $content if defined $content and $self->{method} eq "GET"; |
147 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
136 | |
148 | |
137 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
149 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
138 | |
150 | |
139 | $self->{written} += |
151 | $self->{written} += |
140 | print {$self->{fh}} $res; |
152 | print {$self->{fh}} $res; |
… | |
… | |
147 | unless (defined $content) { |
159 | unless (defined $content) { |
148 | $content = "$code $msg"; |
160 | $content = "$code $msg"; |
149 | $hdr->{"Content-Type"} = "text/plain"; |
161 | $hdr->{"Content-Type"} = "text/plain"; |
150 | $hdr->{"Content-Length"} = length $content; |
162 | $hdr->{"Content-Length"} = length $content; |
151 | } |
163 | } |
|
|
164 | $hdr->{"Connection"} = "close"; |
152 | |
165 | |
153 | $self->response($code, $msg, $hdr, $content); |
166 | $self->response($code, $msg, $hdr, $content); |
154 | |
167 | |
155 | die bless {}, err::; |
168 | die bless {}, err::; |
156 | } |
169 | } |
… | |
… | |
158 | sub err_blocked { |
171 | sub err_blocked { |
159 | my $self = shift; |
172 | my $self = shift; |
160 | my $ip = $self->{remote_addr}; |
173 | my $ip = $self->{remote_addr}; |
161 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
174 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
162 | |
175 | |
163 | Coro::Event::do_timer(after => 5); |
176 | Coro::Event::do_timer(after => 15); |
164 | |
177 | |
165 | $self->err(403, "too many connections", |
178 | $self->err(401, "too many connections", |
166 | { |
179 | { |
167 | "Content-Type" => "text/html", |
180 | "Content-Type" => "text/html", |
168 | "Retry-After" => $::BLOCKTIME |
181 | "Retry-After" => $::BLOCKTIME |
169 | }, |
182 | }, |
170 | <<EOF); |
183 | <<EOF); |
… | |
… | |
182 | |
195 | |
183 | sub handle { |
196 | sub handle { |
184 | my $self = shift; |
197 | my $self = shift; |
185 | my $fh = $self->{fh}; |
198 | my $fh = $self->{fh}; |
186 | |
199 | |
|
|
200 | $fh->timeout($::REQ_TIMEOUT); |
187 | #while() { |
201 | while() { |
|
|
202 | $self->{reqs}++; |
|
|
203 | |
|
|
204 | # read request and parse first line |
|
|
205 | my $req = $fh->readline("\015\012\015\012"); |
|
|
206 | |
|
|
207 | unless (defined $req) { |
|
|
208 | if (exists $self->{version}) { |
|
|
209 | last; |
|
|
210 | } else { |
|
|
211 | $self->err(408, "request timeout"); |
|
|
212 | } |
|
|
213 | } |
|
|
214 | |
188 | $self->{h} = {}; |
215 | $self->{h} = {}; |
189 | |
216 | |
190 | # read request and parse first line |
|
|
191 | $fh->timeout($::REQ_TIMEOUT); |
|
|
192 | my $req = $fh->readline("\015\012\015\012"); |
|
|
193 | $fh->timeout($::RES_TIMEOUT); |
217 | $fh->timeout($::RES_TIMEOUT); |
194 | |
|
|
195 | defined $req or |
|
|
196 | $self->err(408, "request timeout"); |
|
|
197 | |
|
|
198 | my $ip = $self->{remote_addr}; |
218 | my $ip = $self->{remote_addr}; |
199 | |
219 | |
200 | if ($blocked{$ip}) { |
220 | if ($blocked{$ip}) { |
201 | $self->err_blocked($blocked{$ip}) |
221 | $self->err_blocked($blocked{$ip}) |
202 | if $blocked{$ip} > $::NOW; |
222 | if $blocked{$ip} > $::NOW; |
203 | |
223 | |
204 | delete $blocked{$ip}; |
224 | delete $blocked{$ip}; |
205 | } |
225 | } |
206 | |
226 | |
207 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
227 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
208 | $self->slog("blocked ip $ip"); |
228 | $self->slog(2, "blocked ip $ip"); |
209 | $self->err_blocked; |
229 | $self->err_blocked; |
210 | } |
230 | } |
211 | |
231 | |
212 | $req =~ /^(?:\015\012)? |
232 | $req =~ /^(?:\015\012)? |
213 | (GET|HEAD) \040+ |
233 | (GET|HEAD) \040+ |
214 | ([^\040]+) \040+ |
234 | ([^\040]+) \040+ |
215 | HTTP\/([0-9]+\.[0-9]+) |
235 | HTTP\/([0-9]+\.[0-9]+) |
216 | \015\012/gx |
236 | \015\012/gx |
217 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
237 | or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
218 | |
|
|
219 | $2 ne "1.0" |
|
|
220 | or $self->err(506, "http protocol version not supported"); |
|
|
221 | |
238 | |
222 | $self->{method} = $1; |
239 | $self->{method} = $1; |
223 | $self->{uri} = $2; |
240 | $self->{uri} = $2; |
|
|
241 | $self->{version} = $3; |
|
|
242 | |
|
|
243 | $3 eq "1.0" or $3 eq "1.1" |
|
|
244 | or $self->err(506, "http protocol version $3 not supported"); |
224 | |
245 | |
225 | # parse headers |
246 | # parse headers |
226 | { |
247 | { |
227 | my (%hdr, $h, $v); |
248 | my (%hdr, $h, $v); |
228 | |
249 | |
… | |
… | |
241 | while ($h, $v) = each %hdr; |
262 | while ($h, $v) = each %hdr; |
242 | } |
263 | } |
243 | |
264 | |
244 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
265 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
245 | |
266 | |
246 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
267 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
247 | |
268 | |
248 | $self->map_uri; |
269 | $self->map_uri; |
249 | $self->respond; |
270 | $self->respond; |
|
|
271 | |
|
|
272 | last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1"; |
|
|
273 | |
|
|
274 | $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]"); |
|
|
275 | $fh->timeout($::PER_TIMEOUT); |
250 | #} |
276 | } |
251 | } |
277 | } |
252 | |
278 | |
253 | # uri => path mapping |
279 | # uri => path mapping |
254 | sub map_uri { |
280 | sub map_uri { |
255 | my $self = shift; |
281 | my $self = shift; |
… | |
… | |
399 | $self->err(416, "not satisfiable", $hdr); |
425 | $self->err(416, "not satisfiable", $hdr); |
400 | |
426 | |
401 | satisfiable: |
427 | satisfiable: |
402 | # check for segmented downloads |
428 | # check for segmented downloads |
403 | if ($l && $::NO_SEGMENTED) { |
429 | if ($l && $::NO_SEGMENTED) { |
404 | if (%{$uri{$self->{uri}}} > 1) { |
430 | if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
405 | $self->slog("segmented download refused\n"); |
431 | Coro::Event::do_timer(after => 15); |
|
|
432 | |
406 | $self->err(400, "segmented downloads are not allowed"); |
433 | $self->err(400, "segmented downloads are not allowed"); |
407 | } |
434 | } |
408 | } |
435 | } |
409 | |
436 | |
410 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
437 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
421 | $hdr->{"Content-Length"} = $length; |
448 | $hdr->{"Content-Length"} = $length; |
422 | |
449 | |
423 | $self->response(@code, $hdr, ""); |
450 | $self->response(@code, $hdr, ""); |
424 | |
451 | |
425 | if ($self->{method} eq "GET") { |
452 | if ($self->{method} eq "GET") { |
426 | my ($fh, $buf); |
453 | my ($fh, $buf, $r); |
|
|
454 | my $current = $Coro::current; |
427 | open $fh, "<", $self->{path} |
455 | open $fh, "<", $self->{path} |
428 | or die "$self->{path}: late open failure ($!)"; |
456 | or die "$self->{path}: late open failure ($!)"; |
429 | |
457 | |
430 | if ($l) { |
|
|
431 | sysseek $fh, $l, 0 |
|
|
432 | or die "$self->{path}: cannot seek to $l ($!)"; |
|
|
433 | } |
|
|
434 | |
|
|
435 | $h -= $l - 1; |
458 | $h -= $l - 1; |
436 | |
459 | |
437 | while ($h > 0) { |
460 | while ($h > 0) { |
438 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
461 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
|
|
462 | $buf, 0, sub { |
|
|
463 | $r = $_[0]; |
|
|
464 | $current->ready; |
|
|
465 | }); |
|
|
466 | &Coro::schedule; |
|
|
467 | last unless $r; |
439 | my $w = $self->{fh}->syswrite($buf) |
468 | my $w = $self->{fh}->syswrite($buf) |
440 | or last; |
469 | or last; |
441 | $::written += $w; |
470 | $::written += $w; |
442 | $self->{written} += $w; |
471 | $self->{written} += $w; |
|
|
472 | $l += $r; |
443 | } |
473 | } |
444 | } |
474 | } |
445 | |
475 | |
446 | close $fh; |
476 | close $fh; |
447 | } |
477 | } |