… | |
… | |
42 | my @pool; |
42 | my @pool; |
43 | |
43 | |
44 | # one "execution thread" |
44 | # one "execution thread" |
45 | sub handler { |
45 | sub handler { |
46 | while () { |
46 | while () { |
47 | my $new = pop @newcons; |
|
|
48 | if ($new) { |
47 | if (@newcons) { |
49 | eval { |
48 | eval { |
50 | conn->new(@$new)->handle; |
49 | conn->new(@{pop @newcons})->handle; |
51 | }; |
50 | }; |
52 | slog 1, "$@" if $@ && !ref $@; |
51 | slog 1, "$@" if $@ && !ref $@; |
53 | $connections->up; |
52 | $connections->up; |
54 | } else { |
53 | } else { |
55 | last if @pool >= $MAX_POOL; |
54 | last if @pool >= $MAX_POOL; |
… | |
… | |
57 | schedule; |
56 | schedule; |
58 | } |
57 | } |
59 | } |
58 | } |
60 | } |
59 | } |
61 | |
60 | |
|
|
61 | sub listen_on { |
|
|
62 | my $listen = $_[0]; |
|
|
63 | |
|
|
64 | push @listen_sockets, $listen; |
|
|
65 | |
|
|
66 | # the "main thread" |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @newcons, [$listen->accept]; |
|
|
72 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
73 | if (@pool) { |
|
|
74 | (pop @pool)->ready; |
|
|
75 | } else { |
|
|
76 | async \&handler; |
|
|
77 | } |
|
|
78 | |
|
|
79 | } |
|
|
80 | }; |
|
|
81 | } |
|
|
82 | |
62 | my $http_port = new Coro::Socket |
83 | my $http_port = new Coro::Socket |
63 | LocalAddr => $SERVER_HOST, |
84 | LocalAddr => $SERVER_HOST, |
64 | LocalPort => $SERVER_PORT, |
85 | LocalPort => $SERVER_PORT, |
65 | ReuseAddr => 1, |
86 | ReuseAddr => 1, |
66 | Listen => 50, |
87 | Listen => 50, |
67 | or die "unable to start server"; |
88 | or die "unable to start server"; |
68 | |
89 | |
69 | push @listen_sockets, $http_port; |
90 | listen_on $http_port; |
|
|
91 | |
|
|
92 | if ($SERVER_PORT2) { |
|
|
93 | my $http_port = new Coro::Socket |
|
|
94 | LocalAddr => $SERVER_HOST, |
|
|
95 | LocalPort => $SERVER_PORT2, |
|
|
96 | ReuseAddr => 1, |
|
|
97 | Listen => 50, |
|
|
98 | or die "unable to start server"; |
|
|
99 | |
|
|
100 | listen_on $http_port; |
|
|
101 | } |
70 | |
102 | |
71 | our $NOW; |
103 | our $NOW; |
72 | our $HTTP_NOW; |
104 | our $HTTP_NOW; |
73 | |
105 | |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
106 | Event->timer(interval => 1, hard => 1, cb => sub { |
75 | $NOW = time; |
107 | $NOW = time; |
76 | $HTTP_NOW = time2str $NOW; |
108 | $HTTP_NOW = time2str $NOW; |
77 | })->now; |
109 | })->now; |
78 | |
110 | |
79 | # the "main thread" |
|
|
80 | async { |
|
|
81 | slog 1, "accepting connections"; |
|
|
82 | while () { |
|
|
83 | $connections->down; |
|
|
84 | push @newcons, [$http_port->accept]; |
|
|
85 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
86 | if (@pool) { |
|
|
87 | (pop @pool)->ready; |
|
|
88 | } else { |
|
|
89 | async \&handler; |
|
|
90 | } |
|
|
91 | |
|
|
92 | } |
|
|
93 | }; |
|
|
94 | |
|
|
95 | package conn; |
111 | package conn; |
96 | |
112 | |
97 | use Socket; |
113 | use Socket; |
98 | use HTTP::Date; |
114 | use HTTP::Date; |
99 | use Convert::Scalar 'weaken'; |
115 | use Convert::Scalar 'weaken'; |
100 | use Linux::AIO; |
116 | use Linux::AIO; |
101 | |
117 | |
102 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
118 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
103 | |
|
|
104 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
105 | |
119 | |
106 | Event->io(fd => Linux::AIO::poll_fileno, |
120 | Event->io(fd => Linux::AIO::poll_fileno, |
107 | poll => 'r', async => 1, |
121 | poll => 'r', async => 1, |
108 | cb => \&Linux::AIO::poll_cb); |
122 | cb => \&Linux::AIO::poll_cb); |
109 | |
123 | |
… | |
… | |
127 | |
141 | |
128 | read_mimetypes; |
142 | read_mimetypes; |
129 | |
143 | |
130 | sub new { |
144 | sub new { |
131 | my $class = shift; |
145 | my $class = shift; |
|
|
146 | my $fh = shift; |
132 | my $peername = shift; |
147 | my $peername = shift; |
133 | my $fh = shift; |
|
|
134 | my $self = bless { fh => $fh }, $class; |
148 | my $self = bless { fh => $fh }, $class; |
135 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
149 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
136 | or $self->err(500, "unable to decode peername"); |
150 | or $self->err(500, "unable to decode peername"); |
137 | |
151 | |
138 | $self->{remote_addr} = inet_ntoa $iaddr; |
152 | $self->{remote_addr} = inet_ntoa $iaddr; |
139 | $self->{time} = $::NOW; |
153 | $self->{time} = $::NOW; |
140 | |
154 | |
141 | # enter ourselves into various lists |
|
|
142 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
143 | |
|
|
144 | $::conns++; |
155 | $::conns++; |
145 | |
156 | |
146 | $self; |
157 | $self; |
147 | } |
158 | } |
148 | |
159 | |
149 | sub DESTROY { |
160 | sub DESTROY { |
150 | my $self = shift; |
161 | my $self = shift; |
151 | |
|
|
152 | $::conns--; |
162 | $::conns--; |
153 | |
|
|
154 | $self->eoconn; |
163 | $self->eoconn; |
155 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
156 | } |
164 | } |
157 | |
165 | |
158 | # end of connection |
166 | # end of connection |
159 | sub eoconn { |
167 | sub eoconn { |
160 | my $self = shift; |
168 | my $self = shift; |
|
|
169 | |
|
|
170 | # clean up hints |
|
|
171 | delete $conn{$self->{remote_id}}{$self*1}; |
161 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
172 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
162 | } |
173 | } |
163 | |
174 | |
164 | sub slog { |
175 | sub slog { |
165 | my $self = shift; |
176 | my $self = shift; |
166 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
177 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
168 | |
179 | |
169 | sub response { |
180 | sub response { |
170 | my ($self, $code, $msg, $hdr, $content) = @_; |
181 | my ($self, $code, $msg, $hdr, $content) = @_; |
171 | my $res = "HTTP/1.1 $code $msg\015\012"; |
182 | my $res = "HTTP/1.1 $code $msg\015\012"; |
172 | |
183 | |
173 | $self->{h}{connection} ||= $hdr->{Connection}; |
184 | $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; |
174 | |
185 | |
175 | $res .= "Date: $HTTP_NOW\015\012"; |
186 | $res .= "Date: $HTTP_NOW\015\012"; |
176 | |
187 | |
177 | while (my ($h, $v) = each %$hdr) { |
188 | while (my ($h, $v) = each %$hdr) { |
178 | $res .= "$h: $v\015\012" |
189 | $res .= "$h: $v\015\012" |
… | |
… | |
228 | } |
239 | } |
229 | |
240 | |
230 | $self->{h} = {}; |
241 | $self->{h} = {}; |
231 | |
242 | |
232 | $fh->timeout($::RES_TIMEOUT); |
243 | $fh->timeout($::RES_TIMEOUT); |
233 | my $ip = $self->{remote_addr}; |
|
|
234 | |
|
|
235 | if ($blocked{$ip}) { |
|
|
236 | $self->err_blocked($blocked{$ip}) |
|
|
237 | if $blocked{$ip} > $::NOW; |
|
|
238 | |
|
|
239 | delete $blocked{$ip}; |
|
|
240 | } |
|
|
241 | |
|
|
242 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
243 | my $delay = 120; |
|
|
244 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
245 | if ($delay <= 0) { |
|
|
246 | $self->slog(2, "blocked ip $ip"); |
|
|
247 | $self->err_blocked; |
|
|
248 | } else { |
|
|
249 | Coro::Event::do_timer(after => 3); |
|
|
250 | $delay -= 3; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | } |
|
|
254 | |
244 | |
255 | $req =~ /^(?:\015\012)? |
245 | $req =~ /^(?:\015\012)? |
256 | (GET|HEAD) \040+ |
246 | (GET|HEAD) \040+ |
257 | ([^\040]+) \040+ |
247 | ([^\040]+) \040+ |
258 | HTTP\/([0-9]+\.[0-9]+) |
248 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
283 | |
273 | |
284 | $self->{h}{$h} = substr $v, 1 |
274 | $self->{h}{$h} = substr $v, 1 |
285 | while ($h, $v) = each %hdr; |
275 | while ($h, $v) = each %hdr; |
286 | } |
276 | } |
287 | |
277 | |
|
|
278 | # remote id should be unique per user |
|
|
279 | my $id = $self->{remote_addr}; |
|
|
280 | |
|
|
281 | if (exists $self->{h}{"client-ip"}) { |
|
|
282 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
283 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
284 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
285 | } |
|
|
286 | |
|
|
287 | $self->{remote_id} = $id; |
|
|
288 | |
|
|
289 | if ($blocked{$id}) { |
|
|
290 | $self->err_blocked($blocked{$id}) |
|
|
291 | if $blocked{$id} > $::NOW; |
|
|
292 | |
|
|
293 | delete $blocked{$id}; |
|
|
294 | } |
|
|
295 | |
|
|
296 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
297 | my $delay = $::PER_TIMEOUT + 15; |
|
|
298 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
299 | if ($delay <= 0) { |
|
|
300 | $self->slog(2, "blocked ip $id"); |
|
|
301 | $self->err_blocked; |
|
|
302 | } else { |
|
|
303 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
304 | } |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
288 | # find out server name and port |
308 | # find out server name and port |
289 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
309 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
290 | $host = $1; |
310 | $host = $1; |
291 | } else { |
311 | } else { |
292 | $host = $self->{h}{host}; |
312 | $host = $self->{h}{host}; |
… | |
… | |
294 | |
314 | |
295 | if (defined $host) { |
315 | if (defined $host) { |
296 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
316 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
297 | } else { |
317 | } else { |
298 | ($self->{server_port}, $host) |
318 | ($self->{server_port}, $host) |
299 | = unpack_sockaddr_in $self->{fh}->getsockname |
319 | = unpack_sockaddr_in $self->{fh}->sockname |
300 | or $self->err(500, "unable to get socket name"); |
320 | or $self->err(500, "unable to get socket name"); |
301 | $host = inet_ntoa $host; |
321 | $host = inet_ntoa $host; |
302 | } |
322 | } |
303 | |
323 | |
304 | $self->{server_name} = $host; |
324 | $self->{server_name} = $host; |
305 | |
325 | |
306 | # remote id should be unique per user |
326 | # enter ourselves into various lists |
307 | $self->{remote_id} = $self->{remote_addr}; |
327 | weaken ($conn{$id}{$self*1} = $self); |
308 | |
|
|
309 | if (exists $self->{h}{"client-ip"}) { |
|
|
310 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
311 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
312 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
313 | } |
|
|
314 | |
|
|
315 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
328 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
316 | |
329 | |
317 | eval { |
330 | eval { |
318 | $self->map_uri; |
331 | $self->map_uri; |
319 | $self->respond; |
332 | $self->respond; |
320 | }; |
333 | }; |
… | |
… | |
457 | $self->err(416, "not satisfiable", $hdr, ""); |
470 | $self->err(416, "not satisfiable", $hdr, ""); |
458 | |
471 | |
459 | satisfiable: |
472 | satisfiable: |
460 | # check for segmented downloads |
473 | # check for segmented downloads |
461 | if ($l && $::NO_SEGMENTED) { |
474 | if ($l && $::NO_SEGMENTED) { |
462 | my $delay = 180; |
475 | my $delay = $::PER_TIMEOUT + 15; |
463 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
476 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
464 | if ($delay <= 0) { |
477 | if ($delay <= 0) { |
465 | $self->err_segmented_download; |
478 | $self->err_segmented_download; |
466 | } else { |
479 | } else { |
467 | Coro::Event::do_timer(after => 3); $delay -= 3; |
480 | Coro::Event::do_timer(after => 4); $delay -= 4; |
468 | } |
481 | } |
469 | } |
482 | } |
470 | } |
483 | } |
471 | |
484 | |
472 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
485 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
518 | while ($h > 0) { |
531 | while ($h > 0) { |
519 | if (0) { |
532 | if (0) { |
520 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
533 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
521 | or last; |
534 | or last; |
522 | } else { |
535 | } else { |
523 | undef $buf; |
|
|
524 | $aio_requests->down; |
|
|
525 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
536 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
526 | $buf, 0, sub { |
537 | $buf, 0, sub { |
527 | $r = $_[0]; |
538 | $r = $_[0]; |
528 | $current->ready; |
539 | Coro::ready($current); |
529 | }); |
540 | }); |
530 | &Coro::schedule; |
541 | &Coro::schedule; |
531 | $aio_requests->up; |
|
|
532 | last unless $r; |
542 | last unless $r; |
533 | } |
543 | } |
534 | my $w = $self->{fh}->syswrite($buf) |
544 | my $w = syswrite $self->{fh}, $buf |
535 | or last; |
545 | or last; |
536 | $::written += $w; |
546 | $::written += $w; |
537 | $self->{written} += $w; |
547 | $self->{written} += $w; |
538 | $l += $r; |
548 | $l += $r; |
539 | } |
549 | } |