1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
|
|
3 | use Coro::SemaphoreSet; |
3 | use Coro::EV; |
4 | use Coro::EV; |
4 | use Coro::Socket; |
5 | use Coro::Socket; |
5 | use Coro::Signal; |
6 | use Coro::Signal; |
6 | use Coro::AIO (); |
7 | use Coro::AIO (); |
7 | |
8 | |
|
|
9 | use Fcntl; |
8 | use HTTP::Date; |
10 | use HTTP::Date; |
9 | use POSIX (); |
11 | use POSIX (); |
10 | |
12 | |
11 | use Compress::Zlib (); |
13 | use Compress::Zlib (); |
12 | |
14 | |
13 | no utf8; |
15 | use common::sense; |
14 | use bytes; |
|
|
15 | |
16 | |
16 | # at least on my machine, this thingy serves files |
17 | # at least on my machine, this thingy serves files |
17 | # quite a bit faster than apache, ;) |
18 | # quite a bit faster than apache, ;) |
18 | # and quite a bit slower than thttpd :( |
19 | # and quite a bit slower than thttpd :( |
19 | |
20 | |
20 | $SIG{PIPE} = 'IGNORE'; |
21 | $SIG{PIPE} = 'IGNORE'; |
21 | |
22 | |
22 | our $accesslog; |
23 | our $accesslog; |
23 | our $errorlog; |
24 | our $errorlog; |
|
|
25 | our @listen_sockets; |
24 | |
26 | |
25 | our $NOW; |
27 | our $NOW; |
26 | our $HTTP_NOW; |
28 | our $HTTP_NOW; |
27 | |
29 | |
28 | our $ERROR_LOG; |
30 | our $ERROR_LOG; |
29 | our $ACCESS_LOG; |
31 | our $ACCESS_LOG; |
|
|
32 | our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip |
30 | |
33 | |
31 | our $update_time = EV::periodic 0, 1, undef, sub { |
34 | our $update_time = EV::periodic 0, 1, undef, sub { |
32 | $NOW = time; |
35 | $NOW = time; |
33 | $HTTP_NOW = time2str $NOW; |
36 | $HTTP_NOW = time2str $NOW; |
34 | }; |
37 | }; |
… | |
… | |
54 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
57 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
55 | printf "$NOW: $format\n", @_; |
58 | printf "$NOW: $format\n", @_; |
56 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
59 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
57 | } |
60 | } |
58 | |
61 | |
59 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
62 | our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250; |
60 | our $httpevent = new Coro::Signal; |
63 | our $httpevent = new Coro::Signal; |
61 | |
64 | |
62 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
65 | our $queue_file = new transferqueue $::MAX_TRANSFERS; |
63 | our $queue_index = new transferqueue 10; |
66 | our $queue_index = new transferqueue 10; |
64 | |
67 | |
65 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
68 | our $tbf_top = new tbf rate => $::TBF_RATE || 100000; |
66 | |
69 | |
67 | my $unused_bytes = 0; |
70 | my $unused_bytes = 0; |
68 | my $unused_last = time; |
71 | my $unused_last = time; |
69 | |
72 | |
70 | sub unused_bandwidth { |
73 | sub unused_bandwidth { |
… | |
… | |
103 | } |
106 | } |
104 | }; |
107 | }; |
105 | } |
108 | } |
106 | |
109 | |
107 | my $http_port = new Coro::Socket |
110 | my $http_port = new Coro::Socket |
108 | LocalAddr => $SERVER_HOST, |
111 | LocalAddr => $::SERVER_HOST, |
109 | LocalPort => $SERVER_PORT, |
112 | LocalPort => $::SERVER_PORT, |
110 | ReuseAddr => 1, |
113 | ReuseAddr => 1, |
111 | Listen => 50, |
114 | Listen => 50, |
112 | or die "unable to start server"; |
115 | or die "unable to start server"; |
113 | |
116 | |
114 | listen_on $http_port; |
117 | listen_on $http_port; |
115 | |
118 | |
116 | if ($SERVER_PORT2) { |
119 | if ($::SERVER_PORT2) { |
117 | my $http_port = new Coro::Socket |
120 | my $http_port = new Coro::Socket |
118 | LocalAddr => $SERVER_HOST, |
121 | LocalAddr => $::SERVER_HOST, |
119 | LocalPort => $SERVER_PORT2, |
122 | LocalPort => $::SERVER_PORT2, |
120 | ReuseAddr => 1, |
123 | ReuseAddr => 1, |
121 | Listen => 50, |
124 | Listen => 50, |
122 | or die "unable to start server"; |
125 | or die "unable to start server"; |
123 | |
126 | |
124 | listen_on $http_port; |
127 | listen_on $http_port; |
125 | } |
128 | } |
126 | |
129 | |
127 | package conn; |
130 | package conn; |
128 | |
131 | |
129 | use strict; |
132 | use common::sense; |
130 | use bytes; |
|
|
131 | |
133 | |
132 | use Socket; |
134 | use Socket; |
133 | use HTTP::Date; |
135 | use HTTP::Date; |
134 | use Convert::Scalar 'weaken'; |
136 | use Convert::Scalar 'weaken'; |
135 | use IO::AIO; |
137 | use IO::AIO; |
… | |
… | |
587 | |
589 | |
588 | if ($self->{method} eq "GET") { |
590 | if ($self->{method} eq "GET") { |
589 | $self->{time} = $::NOW; |
591 | $self->{time} = $::NOW; |
590 | $self->{written} = 0; |
592 | $self->{written} = 0; |
591 | |
593 | |
592 | open my $fh, "<", $self->{path} |
594 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
593 | or die "$self->{path}: late open failure ($!)"; |
595 | or die "$self->{path}: late open failure ($!)"; |
594 | |
596 | |
595 | $h -= $l - 1; |
597 | $h -= $l - 1; |
596 | |
598 | |
597 | my $transfer = $queue->start_transfer ($h); |
599 | my $transfer = $queue->start_transfer ($h); |
598 | my $locked; |
600 | my $locked; |
599 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
601 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
600 | |
602 | |
601 | while ($h > 0) { |
603 | while ($h > 0) { |
|
|
604 | Coro::cede; |
|
|
605 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
606 | |
602 | unless ($locked) { |
607 | unless ($locked) { |
603 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
608 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
604 | $bufsize = $::BUFSIZE; |
609 | $bufsize = $::BUFSIZE; |
605 | $self->{time} = $::NOW; |
610 | $self->{time} = $::NOW; |
606 | $self->{written} = 0; |
611 | $self->{written} = 0; |