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; |
5 | |
6 | |
6 | use HTTP::Date; |
7 | use HTTP::Date; |
7 | |
8 | |
8 | no utf8; |
9 | no utf8; |
9 | use bytes; |
10 | use bytes; |
… | |
… | |
28 | my $format = shift; |
29 | my $format = shift; |
29 | printf "---: $format\n", @_; |
30 | printf "---: $format\n", @_; |
30 | } |
31 | } |
31 | |
32 | |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
33 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
34 | our $httpevent = new Coro::Signal; |
33 | |
35 | |
34 | our $wait_factor = 0.95; |
36 | our $wait_factor = 0.95; |
35 | |
37 | |
36 | our @transfers = ( |
38 | our @transfers = ( |
37 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
39 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
… | |
… | |
56 | schedule; |
58 | schedule; |
57 | } |
59 | } |
58 | } |
60 | } |
59 | } |
61 | } |
60 | |
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 | |
61 | my $http_port = new Coro::Socket |
85 | my $http_port = new Coro::Socket |
62 | LocalAddr => $SERVER_HOST, |
86 | LocalAddr => $SERVER_HOST, |
63 | LocalPort => $SERVER_PORT, |
87 | LocalPort => $SERVER_PORT, |
64 | ReuseAddr => 1, |
88 | ReuseAddr => 1, |
65 | Listen => 50, |
89 | Listen => 50, |
66 | or die "unable to start server"; |
90 | or die "unable to start server"; |
67 | |
91 | |
68 | push @listen_sockets, $http_port; |
92 | listen_on $http_port; |
|
|
93 | |
|
|
94 | if ($SERVER_PORT2) { |
|
|
95 | my $http_port = new Coro::Socket |
|
|
96 | LocalAddr => $SERVER_HOST, |
|
|
97 | LocalPort => $SERVER_PORT2, |
|
|
98 | ReuseAddr => 1, |
|
|
99 | Listen => 50, |
|
|
100 | or die "unable to start server"; |
|
|
101 | |
|
|
102 | listen_on $http_port; |
|
|
103 | } |
69 | |
104 | |
70 | our $NOW; |
105 | our $NOW; |
71 | our $HTTP_NOW; |
106 | our $HTTP_NOW; |
72 | |
107 | |
73 | Event->timer(interval => 1, hard => 1, cb => sub { |
108 | Event->timer(interval => 1, hard => 1, cb => sub { |
74 | $NOW = time; |
109 | $NOW = time; |
75 | $HTTP_NOW = time2str $NOW; |
110 | $HTTP_NOW = time2str $NOW; |
76 | })->now; |
111 | })->now; |
77 | |
|
|
78 | # the "main thread" |
|
|
79 | async { |
|
|
80 | slog 1, "accepting connections"; |
|
|
81 | while () { |
|
|
82 | $connections->down; |
|
|
83 | push @newcons, [$http_port->accept]; |
|
|
84 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
85 | if (@pool) { |
|
|
86 | (pop @pool)->ready; |
|
|
87 | } else { |
|
|
88 | async \&handler; |
|
|
89 | } |
|
|
90 | |
|
|
91 | } |
|
|
92 | }; |
|
|
93 | |
112 | |
94 | package conn; |
113 | package conn; |
95 | |
114 | |
96 | use Socket; |
115 | use Socket; |
97 | use HTTP::Date; |
116 | use HTTP::Date; |
… | |
… | |
124 | |
143 | |
125 | read_mimetypes; |
144 | read_mimetypes; |
126 | |
145 | |
127 | sub new { |
146 | sub new { |
128 | my $class = shift; |
147 | my $class = shift; |
|
|
148 | my $fh = shift; |
129 | my $peername = shift; |
149 | my $peername = shift; |
130 | my $fh = shift; |
|
|
131 | my $self = bless { fh => $fh }, $class; |
150 | my $self = bless { fh => $fh }, $class; |
132 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
133 | or $self->err(500, "unable to decode peername"); |
152 | or $self->err(500, "unable to decode peername"); |
134 | |
153 | |
135 | $self->{remote_addr} = inet_ntoa $iaddr; |
154 | $self->{remote_addr} = inet_ntoa $iaddr; |
… | |
… | |
151 | my $self = shift; |
170 | my $self = shift; |
152 | |
171 | |
153 | # clean up hints |
172 | # clean up hints |
154 | delete $conn{$self->{remote_id}}{$self*1}; |
173 | delete $conn{$self->{remote_id}}{$self*1}; |
155 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
174 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
|
|
175 | |
|
|
176 | $httpevent->broadcast; |
156 | } |
177 | } |
157 | |
178 | |
158 | sub slog { |
179 | sub slog { |
159 | my $self = shift; |
180 | my $self = shift; |
160 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
181 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
… | |
… | |
162 | |
183 | |
163 | sub response { |
184 | sub response { |
164 | my ($self, $code, $msg, $hdr, $content) = @_; |
185 | my ($self, $code, $msg, $hdr, $content) = @_; |
165 | my $res = "HTTP/1.1 $code $msg\015\012"; |
186 | my $res = "HTTP/1.1 $code $msg\015\012"; |
166 | |
187 | |
167 | $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 | } |
168 | |
201 | |
169 | $res .= "Date: $HTTP_NOW\015\012"; |
202 | $res .= "Date: $HTTP_NOW\015\012"; |
170 | |
203 | |
171 | while (my ($h, $v) = each %$hdr) { |
204 | while (my ($h, $v) = each %$hdr) { |
172 | $res .= "$h: $v\015\012" |
205 | $res .= "$h: $v\015\012" |
… | |
… | |
275 | |
308 | |
276 | delete $blocked{$id}; |
309 | delete $blocked{$id}; |
277 | } |
310 | } |
278 | |
311 | |
279 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
312 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
280 | my $delay = $::PER_TIMEOUT + 15; |
313 | my $delay = $::PER_TIMEOUT + $::NOW + 15; |
281 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
314 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
282 | if ($delay <= 0) { |
315 | if ($delay < $::NOW) { |
283 | $self->slog(2, "blocked ip $id"); |
316 | $self->slog(2, "blocked ip $id"); |
284 | $self->err_blocked; |
317 | $self->err_blocked; |
285 | } else { |
318 | } else { |
286 | Coro::Event::do_timer(after => 4); $delay -= 4; |
319 | $httpevent->wait; |
287 | } |
320 | } |
288 | } |
321 | } |
289 | } |
322 | } |
290 | |
323 | |
291 | # find out server name and port |
324 | # find out server name and port |
… | |
… | |
297 | |
330 | |
298 | if (defined $host) { |
331 | if (defined $host) { |
299 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
332 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
300 | } else { |
333 | } else { |
301 | ($self->{server_port}, $host) |
334 | ($self->{server_port}, $host) |
302 | = unpack_sockaddr_in $self->{fh}->getsockname |
335 | = unpack_sockaddr_in $self->{fh}->sockname |
303 | or $self->err(500, "unable to get socket name"); |
336 | or $self->err(500, "unable to get socket name"); |
304 | $host = inet_ntoa $host; |
337 | $host = inet_ntoa $host; |
305 | } |
338 | } |
306 | |
339 | |
307 | $self->{server_name} = $host; |
340 | $self->{server_name} = $host; |
… | |
… | |
317 | |
350 | |
318 | $self->eoconn; |
351 | $self->eoconn; |
319 | |
352 | |
320 | die if $@ && !ref $@; |
353 | die if $@ && !ref $@; |
321 | |
354 | |
322 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
355 | last if $self->{h}{connection} =~ /close/; |
|
|
356 | |
|
|
357 | $httpevent->broadcast; |
323 | |
358 | |
324 | $fh->timeout($::PER_TIMEOUT); |
359 | $fh->timeout($::PER_TIMEOUT); |
325 | } |
360 | } |
326 | } |
361 | } |
327 | |
362 | |
… | |
… | |
397 | } else { |
432 | } else { |
398 | $ims < $self->{stat}[9] |
433 | $ims < $self->{stat}[9] |
399 | or $self->err(304, "not modified"); |
434 | or $self->err(304, "not modified"); |
400 | |
435 | |
401 | if (-r "$path/index.html") { |
436 | if (-r "$path/index.html") { |
402 | $self->{path} .= "/index.html"; |
437 | # replace directory "size" by index.html filesize |
|
|
438 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
403 | $self->handle_file; |
439 | $self->handle_file; |
404 | } else { |
440 | } else { |
405 | $self->handle_dir; |
441 | $self->handle_dir; |
406 | } |
442 | } |
407 | } |
443 | } |
… | |
… | |
453 | $self->err(416, "not satisfiable", $hdr, ""); |
489 | $self->err(416, "not satisfiable", $hdr, ""); |
454 | |
490 | |
455 | satisfiable: |
491 | satisfiable: |
456 | # check for segmented downloads |
492 | # check for segmented downloads |
457 | if ($l && $::NO_SEGMENTED) { |
493 | if ($l && $::NO_SEGMENTED) { |
458 | my $delay = $::PER_TIMEOUT + 15; |
494 | my $delay = $::NOW + $::PER_TIMEOUT + 15; |
459 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
495 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
460 | if ($delay <= 0) { |
496 | if ($delay <= $::NOW) { |
461 | $self->err_segmented_download; |
497 | $self->err_segmented_download; |
462 | } else { |
498 | } else { |
463 | Coro::Event::do_timer(after => 4); $delay -= 4; |
499 | $httpevent->wait; |
464 | } |
500 | } |
465 | } |
501 | } |
466 | } |
502 | } |
467 | |
503 | |
468 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
504 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |