… | |
… | |
15 | # and quite a bit slower than thttpd :( |
15 | # and quite a bit slower than thttpd :( |
16 | |
16 | |
17 | $SIG{PIPE} = 'IGNORE'; |
17 | $SIG{PIPE} = 'IGNORE'; |
18 | |
18 | |
19 | our $accesslog; |
19 | our $accesslog; |
|
|
20 | our $errorlog; |
|
|
21 | |
|
|
22 | our $NOW; |
|
|
23 | our $HTTP_NOW; |
|
|
24 | |
|
|
25 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
26 | $NOW = time; |
|
|
27 | $HTTP_NOW = time2str $NOW; |
|
|
28 | })->now; |
|
|
29 | |
|
|
30 | if ($ERROR_LOG) { |
|
|
31 | use IO::Handle; |
|
|
32 | open $errorlog, ">>$ERROR_LOG" |
|
|
33 | or die "$ERROR_LOG: $!"; |
|
|
34 | $errorlog->autoflush(1); |
|
|
35 | } |
20 | |
36 | |
21 | if ($ACCESS_LOG) { |
37 | if ($ACCESS_LOG) { |
22 | use IO::Handle; |
38 | use IO::Handle; |
23 | open $accesslog, ">>$ACCESS_LOG" |
39 | open $accesslog, ">>$ACCESS_LOG" |
24 | or die "$ACCESS_LOG: $!"; |
40 | or die "$ACCESS_LOG: $!"; |
… | |
… | |
26 | } |
42 | } |
27 | |
43 | |
28 | sub slog { |
44 | sub slog { |
29 | my $level = shift; |
45 | my $level = shift; |
30 | my $format = shift; |
46 | my $format = shift; |
|
|
47 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
31 | printf "---: $format\n", @_; |
48 | printf "$NOW: $format\n", @_; |
|
|
49 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
32 | } |
50 | } |
33 | |
51 | |
34 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
35 | our $httpevent = new Coro::Signal; |
53 | our $httpevent = new Coro::Signal; |
36 | |
54 | |
37 | our $wait_factor = 0.95; |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 250_000_000; |
38 | |
|
|
39 | our $queue_small = new transferqueue $MAX_TRANSFERS_SMALL; |
|
|
40 | our $queue_large = new transferqueue $MAX_TRANSFERS_LARGE; |
|
|
41 | our $queue_index = new transferqueue 5; |
56 | our $queue_index = new transferqueue slots => 10; |
|
|
57 | |
|
|
58 | our $requests; |
42 | |
59 | |
43 | my @newcons; |
60 | my @newcons; |
44 | my @pool; |
61 | my @pool; |
45 | |
62 | |
46 | # one "execution thread" |
63 | # one "execution thread" |
… | |
… | |
49 | if (@newcons) { |
66 | if (@newcons) { |
50 | eval { |
67 | eval { |
51 | conn->new(@{pop @newcons})->handle; |
68 | conn->new(@{pop @newcons})->handle; |
52 | }; |
69 | }; |
53 | slog 1, "$@" if $@ && !ref $@; |
70 | slog 1, "$@" if $@ && !ref $@; |
|
|
71 | |
|
|
72 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
73 | |
54 | $connections->up; |
74 | $connections->up; |
55 | } else { |
75 | } else { |
56 | last if @pool >= $MAX_POOL; |
76 | last if @pool >= $MAX_POOL; |
57 | push @pool, $Coro::current; |
77 | push @pool, $Coro::current; |
58 | schedule; |
78 | schedule; |
… | |
… | |
100 | or die "unable to start server"; |
120 | or die "unable to start server"; |
101 | |
121 | |
102 | listen_on $http_port; |
122 | listen_on $http_port; |
103 | } |
123 | } |
104 | |
124 | |
105 | our $NOW; |
|
|
106 | our $HTTP_NOW; |
|
|
107 | |
|
|
108 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
109 | $NOW = time; |
|
|
110 | $HTTP_NOW = time2str $NOW; |
|
|
111 | })->now; |
|
|
112 | |
|
|
113 | package conn; |
125 | package conn; |
114 | |
126 | |
115 | use Socket; |
127 | use Socket; |
116 | use HTTP::Date; |
128 | use HTTP::Date; |
117 | use Convert::Scalar 'weaken'; |
129 | use Convert::Scalar 'weaken'; |
… | |
… | |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
163 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
152 | or $self->err(500, "unable to decode peername"); |
164 | or $self->err(500, "unable to decode peername"); |
153 | |
165 | |
154 | $self->{remote_addr} = |
166 | $self->{remote_addr} = |
155 | $self->{remote_id} = inet_ntoa $iaddr; |
167 | $self->{remote_id} = inet_ntoa $iaddr; |
|
|
168 | |
156 | $self->{time} = $::NOW; |
169 | $self->{time} = $::NOW; |
157 | |
170 | |
158 | weaken ($Coro::current->{conn} = $self); |
171 | weaken ($Coro::current->{conn} = $self); |
159 | |
172 | |
160 | $::conns++; |
173 | $::conns++; |
|
|
174 | $::maxconns = $::conns if $::conns > $::maxconns; |
161 | |
175 | |
162 | $self; |
176 | $self; |
163 | } |
177 | } |
164 | |
178 | |
165 | sub DESTROY { |
179 | sub DESTROY { |
166 | my $self = shift; |
180 | #my $self = shift; |
167 | $::conns--; |
181 | $::conns--; |
168 | $self->eoconn; |
|
|
169 | } |
|
|
170 | |
|
|
171 | # end of connection |
|
|
172 | sub eoconn { |
|
|
173 | my $self = shift; |
|
|
174 | |
|
|
175 | # clean up hints |
|
|
176 | delete $conn{$self->{remote_id}}{$self*1}; |
|
|
177 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
|
|
178 | |
|
|
179 | $httpevent->broadcast; |
|
|
180 | } |
182 | } |
181 | |
183 | |
182 | sub slog { |
184 | sub slog { |
183 | my $self = shift; |
185 | my $self = shift; |
184 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
186 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
… | |
… | |
294 | |
296 | |
295 | $self->{h}{$h} = substr $v, 1 |
297 | $self->{h}{$h} = substr $v, 1 |
296 | while ($h, $v) = each %hdr; |
298 | while ($h, $v) = each %hdr; |
297 | } |
299 | } |
298 | |
300 | |
|
|
301 | $requests++; |
|
|
302 | |
299 | # remote id should be unique per user |
303 | # remote id should be unique per user |
300 | my $id = $self->{remote_addr}; |
304 | my $id = $self->{remote_addr}; |
301 | |
305 | |
302 | if (exists $self->{h}{"client-ip"}) { |
306 | if (exists $self->{h}{"client-ip"}) { |
303 | $id .= "[".$self->{h}{"client-ip"}."]"; |
307 | $id .= "[".$self->{h}{"client-ip"}."]"; |
… | |
… | |
305 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
309 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
306 | } |
310 | } |
307 | |
311 | |
308 | $self->{remote_id} = $id; |
312 | $self->{remote_id} = $id; |
309 | |
313 | |
|
|
314 | weaken (local $conn{$id}{$self*1} = $self); |
|
|
315 | |
310 | if ($blocked{$id}) { |
316 | if ($blocked{$id}) { |
311 | $self->err_blocked($blocked{$id}) |
317 | $self->err_blocked |
312 | if $blocked{$id} > $::NOW; |
318 | if $blocked{$id}[0] > $::NOW; |
313 | |
319 | |
314 | delete $blocked{$id}; |
320 | delete $blocked{$id}; |
315 | } |
|
|
316 | |
|
|
317 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
318 | my $delay = $::PER_TIMEOUT + $::NOW + 15; |
|
|
319 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
320 | if ($delay < $::NOW) { |
|
|
321 | $self->slog(2, "blocked ip $id"); |
|
|
322 | $self->err_blocked; |
|
|
323 | } else { |
|
|
324 | $httpevent->wait; |
|
|
325 | } |
|
|
326 | } |
|
|
327 | } |
321 | } |
328 | |
322 | |
329 | # find out server name and port |
323 | # find out server name and port |
330 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
324 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
331 | $host = $1; |
325 | $host = $1; |
… | |
… | |
342 | $host = inet_ntoa $host; |
336 | $host = inet_ntoa $host; |
343 | } |
337 | } |
344 | |
338 | |
345 | $self->{server_name} = $host; |
339 | $self->{server_name} = $host; |
346 | |
340 | |
347 | # enter ourselves into various lists |
|
|
348 | weaken ($conn{$id}{$self*1} = $self); |
|
|
349 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
341 | weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self); |
350 | |
342 | |
351 | eval { |
343 | eval { |
352 | $self->map_uri; |
344 | $self->map_uri; |
353 | $self->respond; |
345 | $self->respond; |
354 | }; |
346 | }; |
355 | |
347 | |
356 | $self->eoconn; |
|
|
357 | |
|
|
358 | die if $@ && !ref $@; |
348 | die if $@ && !ref $@; |
359 | |
349 | |
360 | last if $self->{h}{connection} =~ /close/; |
350 | last if $self->{h}{connection} =~ /close/i; |
361 | |
351 | |
362 | $httpevent->broadcast; |
352 | $httpevent->broadcast; |
363 | |
353 | |
364 | $fh->timeout($::PER_TIMEOUT); |
354 | $fh->timeout($::PER_TIMEOUT); |
365 | } |
355 | } |
|
|
356 | } |
|
|
357 | |
|
|
358 | sub block { |
|
|
359 | my $self = shift; |
|
|
360 | |
|
|
361 | $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]]; |
|
|
362 | $self->slog(2, "blocked ip $self->{remote_id}"); |
|
|
363 | $self->err_blocked; |
366 | } |
364 | } |
367 | |
365 | |
368 | # uri => path mapping |
366 | # uri => path mapping |
369 | sub map_uri { |
367 | sub map_uri { |
370 | my $self = shift; |
368 | my $self = shift; |
… | |
… | |
446 | $ims < $self->{stat}[9] |
444 | $ims < $self->{stat}[9] |
447 | or $self->err(304, "not modified"); |
445 | or $self->err(304, "not modified"); |
448 | |
446 | |
449 | if (-r "$path/index.html") { |
447 | if (-r "$path/index.html") { |
450 | # replace directory "size" by index.html filesize |
448 | # replace directory "size" by index.html filesize |
451 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
449 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
452 | $self->handle_file($queue_index); |
450 | $self->handle_file($queue_index); |
453 | } else { |
451 | } else { |
454 | $self->handle_dir; |
452 | $self->handle_dir; |
455 | } |
453 | } |
456 | } |
454 | } |
457 | } elsif (-f _ && -r _) { |
455 | } elsif (-f _ && -r _) { |
458 | -x _ and $self->err(403, "forbidden"); |
456 | -x _ and $self->err(403, "forbidden"); |
459 | $self->handle_file(-s _ >= $::TRANSFER_SMALL ? $queue_large : $queue_small); |
457 | |
|
|
458 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
459 | my $timeout = $::NOW + 10; |
|
|
460 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
461 | if ($timeout < $::NOW) { |
|
|
462 | $self->block($::BLOCKTIME, "too many connections"); |
|
|
463 | } else { |
|
|
464 | $httpevent->wait; |
|
|
465 | } |
|
|
466 | } |
|
|
467 | } |
|
|
468 | |
|
|
469 | $self->handle_file($queue_file); |
460 | } else { |
470 | } else { |
461 | $self->err(404, "not found"); |
471 | $self->err(404, "not found"); |
462 | } |
472 | } |
463 | } |
473 | } |
464 | } |
474 | } |
… | |
… | |
469 | |
479 | |
470 | $self->response(200, "ok", |
480 | $self->response(200, "ok", |
471 | { |
481 | { |
472 | "Content-Type" => "text/html", |
482 | "Content-Type" => "text/html", |
473 | "Content-Length" => length $idx, |
483 | "Content-Length" => length $idx, |
474 | #d# directories change all the time, so X- |
|
|
475 | "X-Last-Modified" => time2str ((stat _)[9]), |
484 | "Last-Modified" => time2str ($self->{stat}[9]), |
476 | }, |
485 | }, |
477 | $idx); |
486 | $idx); |
478 | } |
487 | } |
479 | |
488 | |
480 | sub handle_file { |
489 | sub handle_file { |
… | |
… | |
504 | $self->err(416, "not satisfiable", $hdr, ""); |
513 | $self->err(416, "not satisfiable", $hdr, ""); |
505 | |
514 | |
506 | satisfiable: |
515 | satisfiable: |
507 | # check for segmented downloads |
516 | # check for segmented downloads |
508 | if ($l && $::NO_SEGMENTED) { |
517 | if ($l && $::NO_SEGMENTED) { |
509 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
518 | my $timeout = $::NOW + 15; |
510 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
519 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
511 | if ($delay <= $::NOW) { |
520 | if ($timeout <= $::NOW) { |
|
|
521 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
512 | $self->err_segmented_download; |
522 | #$self->err_segmented_download; |
513 | } else { |
523 | } else { |
514 | $httpevent->wait; |
524 | $httpevent->wait; |
515 | } |
525 | } |
516 | } |
526 | } |
517 | } |
527 | } |
… | |
… | |
547 | if ($l) { |
557 | if ($l) { |
548 | sysseek $fh, $l, 0; |
558 | sysseek $fh, $l, 0; |
549 | } |
559 | } |
550 | } |
560 | } |
551 | |
561 | |
552 | my $transfer = $queue->start_transfer; |
562 | my $transfer = $queue->start_transfer($h); |
553 | my $locked; |
563 | my $locked; |
554 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
564 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
555 | |
565 | |
556 | while ($h > 0) { |
566 | while ($h > 0) { |
557 | unless ($locked) { |
567 | unless ($locked) { |
558 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
568 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
559 | $bufsize = $::BUFSIZE; |
569 | $bufsize = $::BUFSIZE; |
560 | $self->{time} = $::NOW; |
570 | $self->{time} = $::NOW; |
561 | } |
571 | } |
|
|
572 | } |
|
|
573 | |
|
|
574 | if ($blocked{$self->{remote_id}}) { |
|
|
575 | $self->{h}{connection} = "close"; |
|
|
576 | die bless {}, err::; |
562 | } |
577 | } |
563 | |
578 | |
564 | if (0) { # !AIO |
579 | if (0) { # !AIO |
565 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
580 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
566 | or last; |
581 | or last; |