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