1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
|
|
5 | use Coro::Signal; |
|
|
6 | |
|
|
7 | use HTTP::Date; |
5 | |
8 | |
6 | no utf8; |
9 | no utf8; |
7 | use bytes; |
10 | use bytes; |
8 | |
11 | |
9 | # at least on my machine, this thingy serves files |
12 | # at least on my machine, this thingy serves files |
… | |
… | |
25 | my $level = shift; |
28 | my $level = shift; |
26 | my $format = shift; |
29 | my $format = shift; |
27 | printf "---: $format\n", @_; |
30 | printf "---: $format\n", @_; |
28 | } |
31 | } |
29 | |
32 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
33 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
34 | our $httpevent = new Coro::Signal; |
|
|
35 | |
|
|
36 | our $wait_factor = 0.95; |
|
|
37 | |
|
|
38 | our @transfers = ( |
|
|
39 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
|
|
40 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
|
|
41 | ); |
31 | |
42 | |
32 | my @newcons; |
43 | my @newcons; |
33 | my @pool; |
44 | my @pool; |
34 | |
45 | |
35 | # one "execution thread" |
46 | # one "execution thread" |
36 | sub handler { |
47 | sub handler { |
37 | while () { |
48 | while () { |
38 | my $new = pop @newcons; |
|
|
39 | if ($new) { |
49 | if (@newcons) { |
40 | eval { |
50 | eval { |
41 | conn->new(@$new)->handle; |
51 | conn->new(@{pop @newcons})->handle; |
42 | }; |
52 | }; |
43 | slog 1, "$@" if $@ && !ref $@; |
53 | slog 1, "$@" if $@ && !ref $@; |
44 | $connections->up; |
54 | $connections->up; |
45 | } else { |
55 | } else { |
46 | last if @pool >= $MAX_POOL; |
56 | last if @pool >= $MAX_POOL; |
… | |
… | |
48 | schedule; |
58 | schedule; |
49 | } |
59 | } |
50 | } |
60 | } |
51 | } |
61 | } |
52 | |
62 | |
|
|
63 | sub listen_on { |
|
|
64 | my $listen = $_[0]; |
|
|
65 | |
|
|
66 | push @listen_sockets, $listen; |
|
|
67 | |
|
|
68 | # the "main thread" |
|
|
69 | async { |
|
|
70 | slog 1, "accepting connections"; |
|
|
71 | while () { |
|
|
72 | $connections->down; |
|
|
73 | push @newcons, [$listen->accept]; |
|
|
74 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
75 | if (@pool) { |
|
|
76 | (pop @pool)->ready; |
|
|
77 | } else { |
|
|
78 | async \&handler; |
|
|
79 | } |
|
|
80 | |
|
|
81 | } |
|
|
82 | }; |
|
|
83 | } |
|
|
84 | |
53 | my $http_port = new Coro::Socket |
85 | my $http_port = new Coro::Socket |
54 | LocalAddr => $SERVER_HOST, |
86 | LocalAddr => $SERVER_HOST, |
55 | LocalPort => $SERVER_PORT, |
87 | LocalPort => $SERVER_PORT, |
56 | ReuseAddr => 1, |
88 | ReuseAddr => 1, |
57 | Listen => 50, |
89 | Listen => 50, |
58 | or die "unable to start server"; |
90 | or die "unable to start server"; |
59 | |
91 | |
60 | push @listen_sockets, $http_port; |
92 | listen_on $http_port; |
61 | |
93 | |
62 | # the "main thread" |
94 | if ($SERVER_PORT2) { |
63 | async { |
95 | my $http_port = new Coro::Socket |
64 | slog 1, "accepting connections"; |
96 | LocalAddr => $SERVER_HOST, |
65 | while () { |
97 | LocalPort => $SERVER_PORT2, |
66 | $connections->down; |
98 | ReuseAddr => 1, |
67 | push @newcons, [$http_port->accept]; |
99 | Listen => 50, |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
100 | or die "unable to start server"; |
|
|
101 | |
|
|
102 | listen_on $http_port; |
|
|
103 | } |
|
|
104 | |
|
|
105 | our $NOW; |
|
|
106 | our $HTTP_NOW; |
|
|
107 | |
|
|
108 | Event->timer(interval => 1, hard => 1, cb => sub { |
69 | $::NOW = time; |
109 | $NOW = time; |
70 | if (@pool) { |
110 | $HTTP_NOW = time2str $NOW; |
71 | (pop @pool)->ready; |
111 | })->now; |
72 | } else { |
|
|
73 | async \&handler; |
|
|
74 | } |
|
|
75 | |
|
|
76 | } |
|
|
77 | }; |
|
|
78 | |
112 | |
79 | package conn; |
113 | package conn; |
80 | |
114 | |
81 | use Socket; |
115 | use Socket; |
82 | use HTTP::Date; |
116 | use HTTP::Date; |
83 | use Convert::Scalar 'weaken'; |
117 | use Convert::Scalar 'weaken'; |
84 | use Linux::AIO; |
118 | use Linux::AIO; |
85 | |
119 | |
86 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
120 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
87 | |
|
|
88 | my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4; |
|
|
89 | |
121 | |
90 | Event->io(fd => Linux::AIO::poll_fileno, |
122 | Event->io(fd => Linux::AIO::poll_fileno, |
91 | poll => 'r', async => 1, |
123 | poll => 'r', async => 1, |
92 | cb => \&Linux::AIO::poll_cb); |
124 | cb => \&Linux::AIO::poll_cb); |
93 | |
125 | |
… | |
… | |
111 | |
143 | |
112 | read_mimetypes; |
144 | read_mimetypes; |
113 | |
145 | |
114 | sub new { |
146 | sub new { |
115 | my $class = shift; |
147 | my $class = shift; |
|
|
148 | my $fh = shift; |
116 | my $peername = shift; |
149 | my $peername = shift; |
117 | my $fh = shift; |
|
|
118 | my $self = bless { fh => $fh }, $class; |
150 | my $self = bless { fh => $fh }, $class; |
119 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
120 | or $self->err(500, "unable to decode peername"); |
152 | or $self->err(500, "unable to decode peername"); |
121 | |
153 | |
122 | $self->{remote_addr} = inet_ntoa $iaddr; |
154 | $self->{remote_addr} = inet_ntoa $iaddr; |
123 | $self->{time} = $::NOW; |
155 | $self->{time} = $::NOW; |
124 | |
156 | |
125 | # enter ourselves into various lists |
|
|
126 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
127 | |
|
|
128 | $::conns++; |
157 | $::conns++; |
129 | |
158 | |
130 | $self; |
159 | $self; |
131 | } |
160 | } |
132 | |
161 | |
133 | sub DESTROY { |
162 | sub DESTROY { |
134 | my $self = shift; |
163 | my $self = shift; |
135 | |
|
|
136 | $::conns--; |
164 | $::conns--; |
137 | |
|
|
138 | $self->eoconn; |
165 | $self->eoconn; |
139 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
140 | } |
166 | } |
141 | |
167 | |
142 | # end of connection |
168 | # end of connection |
143 | sub eoconn { |
169 | sub eoconn { |
144 | my $self = shift; |
170 | my $self = shift; |
|
|
171 | |
|
|
172 | # clean up hints |
|
|
173 | delete $conn{$self->{remote_id}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
174 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
|
|
175 | |
|
|
176 | $httpevent->broadcast; |
146 | } |
177 | } |
147 | |
178 | |
148 | sub slog { |
179 | sub slog { |
149 | my $self = shift; |
180 | my $self = shift; |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
181 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
152 | |
183 | |
153 | sub response { |
184 | sub response { |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
156 | |
187 | |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
188 | if (exists $hdr->{Connection}) { |
|
|
189 | if ($hdr->{Connection} =~ /close/) { |
|
|
190 | $self->{h}{connection} = "close" |
|
|
191 | } |
|
|
192 | } else { |
|
|
193 | if ($self->{version} < 1.1) { |
|
|
194 | if ($self->{h}{connection} =~ /keep-alive/i) { |
|
|
195 | $hdr->{Connection} = "Keep-Alive"; |
|
|
196 | } else { |
|
|
197 | $self->{h}{connection} = "close" |
|
|
198 | } |
|
|
199 | } |
|
|
200 | } |
158 | |
201 | |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
202 | $res .= "Date: $HTTP_NOW\015\012"; |
160 | |
203 | |
161 | while (my ($h, $v) = each %$hdr) { |
204 | while (my ($h, $v) = each %$hdr) { |
162 | $res .= "$h: $v\015\012" |
205 | $res .= "$h: $v\015\012" |
163 | } |
206 | } |
164 | $res .= "\015\012"; |
207 | $res .= "\015\012"; |
… | |
… | |
177 | sub err { |
220 | sub err { |
178 | my $self = shift; |
221 | my $self = shift; |
179 | my ($code, $msg, $hdr, $content) = @_; |
222 | my ($code, $msg, $hdr, $content) = @_; |
180 | |
223 | |
181 | unless (defined $content) { |
224 | unless (defined $content) { |
182 | $content = "$code $msg"; |
225 | $content = "$code $msg\n"; |
183 | $hdr->{"Content-Type"} = "text/plain"; |
226 | $hdr->{"Content-Type"} = "text/plain"; |
184 | $hdr->{"Content-Length"} = length $content; |
227 | $hdr->{"Content-Length"} = length $content; |
185 | } |
228 | } |
186 | $hdr->{"Connection"} = "close"; |
229 | $hdr->{"Connection"} = "close"; |
187 | |
230 | |
… | |
… | |
212 | } |
255 | } |
213 | |
256 | |
214 | $self->{h} = {}; |
257 | $self->{h} = {}; |
215 | |
258 | |
216 | $fh->timeout($::RES_TIMEOUT); |
259 | $fh->timeout($::RES_TIMEOUT); |
217 | my $ip = $self->{remote_addr}; |
|
|
218 | |
|
|
219 | if ($blocked{$ip}) { |
|
|
220 | $self->err_blocked($blocked{$ip}) |
|
|
221 | if $blocked{$ip} > $::NOW; |
|
|
222 | |
|
|
223 | delete $blocked{$ip}; |
|
|
224 | } |
|
|
225 | |
|
|
226 | if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
227 | my $delay = 120; |
|
|
228 | while (%{$conn{$ip}} > $::MAX_CONN_IP) { |
|
|
229 | if ($delay <= 0) { |
|
|
230 | $self->slog(2, "blocked ip $ip"); |
|
|
231 | $self->err_blocked; |
|
|
232 | } else { |
|
|
233 | Coro::Event::do_timer(after => 3); |
|
|
234 | $delay -= 3; |
|
|
235 | } |
|
|
236 | } |
|
|
237 | } |
|
|
238 | |
260 | |
239 | $req =~ /^(?:\015\012)? |
261 | $req =~ /^(?:\015\012)? |
240 | (GET|HEAD) \040+ |
262 | (GET|HEAD) \040+ |
241 | ([^\040]+) \040+ |
263 | ([^\040]+) \040+ |
242 | HTTP\/([0-9]+\.[0-9]+) |
264 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
267 | |
289 | |
268 | $self->{h}{$h} = substr $v, 1 |
290 | $self->{h}{$h} = substr $v, 1 |
269 | while ($h, $v) = each %hdr; |
291 | while ($h, $v) = each %hdr; |
270 | } |
292 | } |
271 | |
293 | |
|
|
294 | # remote id should be unique per user |
|
|
295 | my $id = $self->{remote_addr}; |
|
|
296 | |
|
|
297 | if (exists $self->{h}{"client-ip"}) { |
|
|
298 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
299 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
300 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
301 | } |
|
|
302 | |
|
|
303 | $self->{remote_id} = $id; |
|
|
304 | |
|
|
305 | if ($blocked{$id}) { |
|
|
306 | $self->err_blocked($blocked{$id}) |
|
|
307 | if $blocked{$id} > $::NOW; |
|
|
308 | |
|
|
309 | delete $blocked{$id}; |
|
|
310 | } |
|
|
311 | |
|
|
312 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
313 | my $delay = $::PER_TIMEOUT + $::NOW + 15; |
|
|
314 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
315 | if ($delay < $::NOW) { |
|
|
316 | $self->slog(2, "blocked ip $id"); |
|
|
317 | $self->err_blocked; |
|
|
318 | } else { |
|
|
319 | $httpevent->wait; |
|
|
320 | } |
|
|
321 | } |
|
|
322 | } |
|
|
323 | |
272 | # find out server name and port |
324 | # find out server name and port |
273 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
325 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
274 | $host = $1; |
326 | $host = $1; |
275 | } else { |
327 | } else { |
276 | $host = $self->{h}{host}; |
328 | $host = $self->{h}{host}; |
… | |
… | |
278 | |
330 | |
279 | if (defined $host) { |
331 | if (defined $host) { |
280 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
332 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
281 | } else { |
333 | } else { |
282 | ($self->{server_port}, $host) |
334 | ($self->{server_port}, $host) |
283 | = unpack_sockaddr_in $self->{fh}->getsockname |
335 | = unpack_sockaddr_in $self->{fh}->sockname |
284 | or $self->err(500, "unable to get socket name"); |
336 | or $self->err(500, "unable to get socket name"); |
285 | $host = inet_ntoa $host; |
337 | $host = inet_ntoa $host; |
286 | } |
338 | } |
287 | |
339 | |
288 | $self->{server_name} = $host; |
340 | $self->{server_name} = $host; |
289 | |
341 | |
290 | # remote id should be unique per user |
342 | # enter ourselves into various lists |
291 | $self->{remote_id} = $self->{remote_addr}; |
343 | weaken ($conn{$id}{$self*1} = $self); |
292 | |
|
|
293 | if (exists $self->{h}{"client-ip"}) { |
|
|
294 | $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]"; |
|
|
295 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
296 | $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
297 | } |
|
|
298 | |
|
|
299 | weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); |
344 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
300 | |
345 | |
301 | eval { |
346 | eval { |
302 | $self->map_uri; |
347 | $self->map_uri; |
303 | $self->respond; |
348 | $self->respond; |
304 | }; |
349 | }; |
305 | |
350 | |
306 | $self->eoconn; |
351 | $self->eoconn; |
307 | |
352 | |
308 | die if $@ && !ref $@; |
353 | die if $@ && !ref $@; |
309 | |
354 | |
310 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
355 | last if $self->{h}{connection} =~ /close/; |
|
|
356 | |
|
|
357 | $httpevent->broadcast; |
311 | |
358 | |
312 | $fh->timeout($::PER_TIMEOUT); |
359 | $fh->timeout($::PER_TIMEOUT); |
313 | } |
360 | } |
314 | } |
361 | } |
315 | |
362 | |
… | |
… | |
385 | } else { |
432 | } else { |
386 | $ims < $self->{stat}[9] |
433 | $ims < $self->{stat}[9] |
387 | or $self->err(304, "not modified"); |
434 | or $self->err(304, "not modified"); |
388 | |
435 | |
389 | if (-r "$path/index.html") { |
436 | if (-r "$path/index.html") { |
390 | $self->{path} .= "/index.html"; |
437 | # replace directory "size" by index.html filesize |
|
|
438 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
391 | $self->handle_file; |
439 | $self->handle_file; |
392 | } else { |
440 | } else { |
393 | $self->handle_dir; |
441 | $self->handle_dir; |
394 | } |
442 | } |
395 | } |
443 | } |
… | |
… | |
413 | $idx); |
461 | $idx); |
414 | } |
462 | } |
415 | |
463 | |
416 | sub handle_file { |
464 | sub handle_file { |
417 | my $self = shift; |
465 | my $self = shift; |
418 | my $length = -s _; |
466 | my $length = $self->{stat}[7]; |
|
|
467 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
419 | my $hdr = { |
468 | my $hdr = { |
420 | "Last-Modified" => time2str ((stat _)[9]), |
469 | "Last-Modified" => time2str ((stat _)[9]), |
421 | }; |
470 | }; |
422 | |
471 | |
423 | my @code = (200, "ok"); |
472 | my @code = (200, "ok"); |
… | |
… | |
435 | } |
484 | } |
436 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
485 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
437 | } |
486 | } |
438 | $hdr->{"Content-Range"} = "bytes */$length"; |
487 | $hdr->{"Content-Range"} = "bytes */$length"; |
439 | $hdr->{"Content-Length"} = $length; |
488 | $hdr->{"Content-Length"} = $length; |
440 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
441 | $self->err(416, "not satisfiable", $hdr, ""); |
489 | $self->err(416, "not satisfiable", $hdr, ""); |
442 | |
490 | |
443 | satisfiable: |
491 | satisfiable: |
444 | # check for segmented downloads |
492 | # check for segmented downloads |
445 | if ($l && $::NO_SEGMENTED) { |
493 | if ($l && $::NO_SEGMENTED) { |
446 | my $delay = 180; |
494 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
447 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
495 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
448 | if ($delay <= 0) { |
496 | if ($delay <= $::NOW) { |
449 | $self->err_segmented_download; |
497 | $self->err_segmented_download; |
450 | } else { |
498 | } else { |
451 | Coro::Event::do_timer(after => 3); $delay -= 3; |
499 | $httpevent->wait; |
452 | } |
500 | } |
453 | } |
501 | } |
454 | } |
502 | } |
455 | |
503 | |
456 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
504 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
467 | $hdr->{"Content-Length"} = $length; |
515 | $hdr->{"Content-Length"} = $length; |
468 | |
516 | |
469 | $self->response(@code, $hdr, ""); |
517 | $self->response(@code, $hdr, ""); |
470 | |
518 | |
471 | if ($self->{method} eq "GET") { |
519 | if ($self->{method} eq "GET") { |
|
|
520 | $self->{time} = $::NOW; |
|
|
521 | |
|
|
522 | my $fudge = $queue->[0]->waiters; |
|
|
523 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
524 | |
|
|
525 | $queue->[1] *= $fudge; |
|
|
526 | my $transfer = $queue->[0]->guard; |
|
|
527 | |
|
|
528 | if ($fudge != 1) { |
|
|
529 | $queue->[1] /= $fudge; |
|
|
530 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
531 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
532 | } |
|
|
533 | $self->{time} = $::NOW; |
|
|
534 | |
|
|
535 | $self->{fh}->writable or return; |
|
|
536 | |
472 | my ($fh, $buf, $r); |
537 | my ($fh, $buf, $r); |
473 | my $current = $Coro::current; |
538 | my $current = $Coro::current; |
474 | open $fh, "<", $self->{path} |
539 | open $fh, "<", $self->{path} |
475 | or die "$self->{path}: late open failure ($!)"; |
540 | or die "$self->{path}: late open failure ($!)"; |
476 | |
541 | |
… | |
… | |
485 | while ($h > 0) { |
550 | while ($h > 0) { |
486 | if (0) { |
551 | if (0) { |
487 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
552 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
488 | or last; |
553 | or last; |
489 | } else { |
554 | } else { |
490 | undef $buf; |
|
|
491 | $aio_requests->down; |
|
|
492 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
555 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
493 | $buf, 0, sub { |
556 | $buf, 0, sub { |
494 | $r = $_[0]; |
557 | $r = $_[0]; |
495 | $current->ready; |
558 | Coro::ready($current); |
496 | }); |
559 | }); |
497 | &Coro::schedule; |
560 | &Coro::schedule; |
498 | $aio_requests->up; |
|
|
499 | last unless $r; |
561 | last unless $r; |
500 | } |
562 | } |
501 | my $w = $self->{fh}->syswrite($buf) |
563 | my $w = syswrite $self->{fh}, $buf |
502 | or last; |
564 | or last; |
503 | $::written += $w; |
565 | $::written += $w; |
504 | $self->{written} += $w; |
566 | $self->{written} += $w; |
505 | $l += $r; |
567 | $l += $r; |
506 | } |
568 | } |
507 | } |
|
|
508 | |
569 | |
509 | close $fh; |
570 | close $fh; |
|
|
571 | } |
510 | } |
572 | } |
511 | |
573 | |
512 | 1; |
574 | 1; |