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 | |
|
|
6 | use HTTP::Date; |
5 | |
7 | |
6 | no utf8; |
8 | no utf8; |
7 | use bytes; |
9 | use bytes; |
8 | |
10 | |
9 | # at least on my machine, this thingy serves files |
11 | # at least on my machine, this thingy serves files |
… | |
… | |
25 | my $level = shift; |
27 | my $level = shift; |
26 | my $format = shift; |
28 | my $format = shift; |
27 | printf "---: $format\n", @_; |
29 | printf "---: $format\n", @_; |
28 | } |
30 | } |
29 | |
31 | |
30 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
33 | |
|
|
34 | our $wait_factor = 0.95; |
|
|
35 | |
|
|
36 | our @transfers = ( |
|
|
37 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
|
|
38 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
|
|
39 | ); |
31 | |
40 | |
32 | my @newcons; |
41 | my @newcons; |
33 | my @pool; |
42 | my @pool; |
34 | |
43 | |
35 | # one "execution thread" |
44 | # one "execution thread" |
… | |
… | |
57 | Listen => 50, |
66 | Listen => 50, |
58 | or die "unable to start server"; |
67 | or die "unable to start server"; |
59 | |
68 | |
60 | push @listen_sockets, $http_port; |
69 | push @listen_sockets, $http_port; |
61 | |
70 | |
|
|
71 | our $NOW; |
|
|
72 | our $HTTP_NOW; |
|
|
73 | |
|
|
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
75 | $NOW = time; |
|
|
76 | $HTTP_NOW = time2str $NOW; |
|
|
77 | })->now; |
|
|
78 | |
62 | # the "main thread" |
79 | # the "main thread" |
63 | async { |
80 | async { |
64 | slog 1, "accepting connections"; |
81 | slog 1, "accepting connections"; |
65 | while () { |
82 | while () { |
66 | $connections->down; |
83 | $connections->down; |
67 | push @newcons, [$http_port->accept]; |
84 | push @newcons, [$http_port->accept]; |
68 | #slog 3, "accepted @$connections ".scalar(@pool); |
85 | #slog 3, "accepted @$connections ".scalar(@pool); |
69 | $::NOW = time; |
|
|
70 | if (@pool) { |
86 | if (@pool) { |
71 | (pop @pool)->ready; |
87 | (pop @pool)->ready; |
72 | } else { |
88 | } else { |
73 | async \&handler; |
89 | async \&handler; |
74 | } |
90 | } |
… | |
… | |
120 | or $self->err(500, "unable to decode peername"); |
136 | or $self->err(500, "unable to decode peername"); |
121 | |
137 | |
122 | $self->{remote_addr} = inet_ntoa $iaddr; |
138 | $self->{remote_addr} = inet_ntoa $iaddr; |
123 | $self->{time} = $::NOW; |
139 | $self->{time} = $::NOW; |
124 | |
140 | |
125 | # enter ourselves into various lists |
|
|
126 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
|
|
127 | |
|
|
128 | $::conns++; |
141 | $::conns++; |
129 | |
142 | |
130 | $self; |
143 | $self; |
131 | } |
144 | } |
132 | |
145 | |
… | |
… | |
134 | my $self = shift; |
147 | my $self = shift; |
135 | |
148 | |
136 | $::conns--; |
149 | $::conns--; |
137 | |
150 | |
138 | $self->eoconn; |
151 | $self->eoconn; |
139 | delete $conn{$self->{remote_addr}}{$self*1}; |
|
|
140 | } |
152 | } |
141 | |
153 | |
142 | # end of connection |
154 | # end of connection |
143 | sub eoconn { |
155 | sub eoconn { |
144 | my $self = shift; |
156 | my $self = shift; |
|
|
157 | |
|
|
158 | # clean up hints |
|
|
159 | delete $conn{$self->{remote_id}}{$self*1}; |
145 | delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; |
160 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
146 | } |
161 | } |
147 | |
162 | |
148 | sub slog { |
163 | sub slog { |
149 | my $self = shift; |
164 | my $self = shift; |
150 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
165 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
154 | my ($self, $code, $msg, $hdr, $content) = @_; |
169 | my ($self, $code, $msg, $hdr, $content) = @_; |
155 | my $res = "HTTP/1.1 $code $msg\015\012"; |
170 | my $res = "HTTP/1.1 $code $msg\015\012"; |
156 | |
171 | |
157 | $self->{h}{connection} ||= $hdr->{Connection}; |
172 | $self->{h}{connection} ||= $hdr->{Connection}; |
158 | |
173 | |
159 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
174 | $res .= "Date: $HTTP_NOW\015\012"; |
160 | |
175 | |
161 | while (my ($h, $v) = each %$hdr) { |
176 | while (my ($h, $v) = each %$hdr) { |
162 | $res .= "$h: $v\015\012" |
177 | $res .= "$h: $v\015\012" |
163 | } |
178 | } |
164 | $res .= "\015\012"; |
179 | $res .= "\015\012"; |
… | |
… | |
177 | sub err { |
192 | sub err { |
178 | my $self = shift; |
193 | my $self = shift; |
179 | my ($code, $msg, $hdr, $content) = @_; |
194 | my ($code, $msg, $hdr, $content) = @_; |
180 | |
195 | |
181 | unless (defined $content) { |
196 | unless (defined $content) { |
182 | $content = "$code $msg"; |
197 | $content = "$code $msg\n"; |
183 | $hdr->{"Content-Type"} = "text/plain"; |
198 | $hdr->{"Content-Type"} = "text/plain"; |
184 | $hdr->{"Content-Length"} = length $content; |
199 | $hdr->{"Content-Length"} = length $content; |
185 | } |
200 | } |
186 | $hdr->{"Connection"} = "close"; |
201 | $hdr->{"Connection"} = "close"; |
187 | |
202 | |
… | |
… | |
212 | } |
227 | } |
213 | |
228 | |
214 | $self->{h} = {}; |
229 | $self->{h} = {}; |
215 | |
230 | |
216 | $fh->timeout($::RES_TIMEOUT); |
231 | $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 | |
232 | |
239 | $req =~ /^(?:\015\012)? |
233 | $req =~ /^(?:\015\012)? |
240 | (GET|HEAD) \040+ |
234 | (GET|HEAD) \040+ |
241 | ([^\040]+) \040+ |
235 | ([^\040]+) \040+ |
242 | HTTP\/([0-9]+\.[0-9]+) |
236 | HTTP\/([0-9]+\.[0-9]+) |
… | |
… | |
267 | |
261 | |
268 | $self->{h}{$h} = substr $v, 1 |
262 | $self->{h}{$h} = substr $v, 1 |
269 | while ($h, $v) = each %hdr; |
263 | while ($h, $v) = each %hdr; |
270 | } |
264 | } |
271 | |
265 | |
|
|
266 | # remote id should be unique per user |
|
|
267 | my $id = $self->{remote_addr}; |
|
|
268 | |
|
|
269 | if (exists $self->{h}{"client-ip"}) { |
|
|
270 | $id .= "[".$self->{h}{"client-ip"}."]"; |
|
|
271 | } elsif (exists $self->{h}{"x-forwarded-for"}) { |
|
|
272 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
|
|
273 | } |
|
|
274 | |
|
|
275 | $self->{remote_id} = $id; |
|
|
276 | |
|
|
277 | if ($blocked{$id}) { |
|
|
278 | $self->err_blocked($blocked{$id}) |
|
|
279 | if $blocked{$id} > $::NOW; |
|
|
280 | |
|
|
281 | delete $blocked{$id}; |
|
|
282 | } |
|
|
283 | |
|
|
284 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
285 | my $delay = $::PER_TIMEOUT + 15; |
|
|
286 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
287 | if ($delay <= 0) { |
|
|
288 | $self->slog(2, "blocked ip $id"); |
|
|
289 | $self->err_blocked; |
|
|
290 | } else { |
|
|
291 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
292 | } |
|
|
293 | } |
|
|
294 | } |
|
|
295 | |
272 | # find out server name and port |
296 | # find out server name and port |
273 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
297 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
274 | $host = $1; |
298 | $host = $1; |
275 | } else { |
299 | } else { |
276 | $host = $self->{h}{host}; |
300 | $host = $self->{h}{host}; |
… | |
… | |
285 | $host = inet_ntoa $host; |
309 | $host = inet_ntoa $host; |
286 | } |
310 | } |
287 | |
311 | |
288 | $self->{server_name} = $host; |
312 | $self->{server_name} = $host; |
289 | |
313 | |
290 | # remote id should be unique per user |
314 | # enter ourselves into various lists |
291 | $self->{remote_id} = $self->{remote_addr}; |
315 | 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); |
316 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
300 | |
317 | |
301 | eval { |
318 | eval { |
302 | $self->map_uri; |
319 | $self->map_uri; |
303 | $self->respond; |
320 | $self->respond; |
304 | }; |
321 | }; |
… | |
… | |
413 | $idx); |
430 | $idx); |
414 | } |
431 | } |
415 | |
432 | |
416 | sub handle_file { |
433 | sub handle_file { |
417 | my $self = shift; |
434 | my $self = shift; |
418 | my $length = -s _; |
435 | my $length = $self->{stat}[7]; |
|
|
436 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
419 | my $hdr = { |
437 | my $hdr = { |
420 | "Last-Modified" => time2str ((stat _)[9]), |
438 | "Last-Modified" => time2str ((stat _)[9]), |
421 | }; |
439 | }; |
422 | |
440 | |
423 | my @code = (200, "ok"); |
441 | my @code = (200, "ok"); |
… | |
… | |
435 | } |
453 | } |
436 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
454 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
437 | } |
455 | } |
438 | $hdr->{"Content-Range"} = "bytes */$length"; |
456 | $hdr->{"Content-Range"} = "bytes */$length"; |
439 | $hdr->{"Content-Length"} = $length; |
457 | $hdr->{"Content-Length"} = $length; |
440 | $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")"); |
|
|
441 | $self->err(416, "not satisfiable", $hdr, ""); |
458 | $self->err(416, "not satisfiable", $hdr, ""); |
442 | |
459 | |
443 | satisfiable: |
460 | satisfiable: |
444 | # check for segmented downloads |
461 | # check for segmented downloads |
445 | if ($l && $::NO_SEGMENTED) { |
462 | if ($l && $::NO_SEGMENTED) { |
446 | my $delay = 180; |
463 | my $delay = $::PER_TIMEOUT + 15; |
447 | while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { |
464 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
448 | if ($delay <= 0) { |
465 | if ($delay <= 0) { |
449 | $self->err_segmented_download; |
466 | $self->err_segmented_download; |
450 | } else { |
467 | } else { |
451 | Coro::Event::do_timer(after => 3); $delay -= 3; |
468 | Coro::Event::do_timer(after => 4); $delay -= 4; |
452 | } |
469 | } |
453 | } |
470 | } |
454 | } |
471 | } |
455 | |
472 | |
456 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
473 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
467 | $hdr->{"Content-Length"} = $length; |
484 | $hdr->{"Content-Length"} = $length; |
468 | |
485 | |
469 | $self->response(@code, $hdr, ""); |
486 | $self->response(@code, $hdr, ""); |
470 | |
487 | |
471 | if ($self->{method} eq "GET") { |
488 | if ($self->{method} eq "GET") { |
|
|
489 | $self->{time} = $::NOW; |
|
|
490 | |
|
|
491 | my $fudge = $queue->[0]->waiters; |
|
|
492 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
493 | |
|
|
494 | $queue->[1] *= $fudge; |
|
|
495 | my $transfer = $queue->[0]->guard; |
|
|
496 | |
|
|
497 | if ($fudge != 1) { |
|
|
498 | $queue->[1] /= $fudge; |
|
|
499 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
500 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
501 | } |
|
|
502 | $self->{time} = $::NOW; |
|
|
503 | |
|
|
504 | $self->{fh}->writable or return; |
|
|
505 | |
472 | my ($fh, $buf, $r); |
506 | my ($fh, $buf, $r); |
473 | my $current = $Coro::current; |
507 | my $current = $Coro::current; |
474 | open $fh, "<", $self->{path} |
508 | open $fh, "<", $self->{path} |
475 | or die "$self->{path}: late open failure ($!)"; |
509 | or die "$self->{path}: late open failure ($!)"; |
476 | |
510 | |
… | |
… | |
502 | or last; |
536 | or last; |
503 | $::written += $w; |
537 | $::written += $w; |
504 | $self->{written} += $w; |
538 | $self->{written} += $w; |
505 | $l += $r; |
539 | $l += $r; |
506 | } |
540 | } |
507 | } |
|
|
508 | |
541 | |
509 | close $fh; |
542 | close $fh; |
|
|
543 | } |
510 | } |
544 | } |
511 | |
545 | |
512 | 1; |
546 | 1; |