… | |
… | |
7 | use bytes; |
7 | use bytes; |
8 | |
8 | |
9 | # at least on my machine, this thingy serves files |
9 | # at least on my machine, this thingy serves files |
10 | # quite a bit faster than apache, ;) |
10 | # quite a bit faster than apache, ;) |
11 | # and quite a bit slower than thttpd :( |
11 | # and quite a bit slower than thttpd :( |
12 | |
|
|
13 | my $port = new Coro::Socket |
|
|
14 | LocalAddr => $SERVER_HOST, |
|
|
15 | LocalPort => $SERVER_PORT, |
|
|
16 | ReuseAddr => 1, |
|
|
17 | Listen => 1, |
|
|
18 | or die "unable to start server"; |
|
|
19 | |
12 | |
20 | $SIG{PIPE} = 'IGNORE'; |
13 | $SIG{PIPE} = 'IGNORE'; |
21 | |
14 | |
22 | sub slog { |
15 | sub slog { |
23 | my $level = shift; |
16 | my $level = shift; |
… | |
… | |
25 | printf "---: $format\n", @_; |
18 | printf "---: $format\n", @_; |
26 | } |
19 | } |
27 | |
20 | |
28 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
29 | |
22 | |
30 | my @fh; |
23 | my @newcons; |
31 | my @pool; |
24 | my @pool; |
32 | |
25 | |
33 | # one "execution thread" |
26 | # one "execution thread" |
34 | sub handler { |
27 | sub handler { |
35 | while () { |
28 | while () { |
36 | my $fh = pop @fh; |
29 | my $new = pop @newcons; |
37 | if ($fh) { |
30 | if ($new) { |
38 | eval { |
31 | eval { |
39 | conn->new($fh)->handle; |
32 | conn->new(@$new)->handle; |
40 | }; |
33 | }; |
41 | close $fh; |
|
|
42 | slog 1, "$@" if $@ && !ref $@; |
34 | slog 1, "$@" if $@ && !ref $@; |
43 | $connections->up; |
35 | $connections->up; |
44 | } else { |
36 | } else { |
45 | last if @pool >= $MAX_POOL; |
37 | last if @pool >= $MAX_POOL; |
46 | push @pool, $Coro::current; |
38 | push @pool, $Coro::current; |
47 | schedule; |
39 | schedule; |
48 | } |
40 | } |
49 | } |
41 | } |
50 | } |
42 | } |
51 | |
43 | |
|
|
44 | my $http_port = new Coro::Socket |
|
|
45 | LocalAddr => $SERVER_HOST, |
|
|
46 | LocalPort => $SERVER_PORT, |
|
|
47 | ReuseAddr => 1, |
|
|
48 | Listen => 1, |
|
|
49 | or die "unable to start server"; |
|
|
50 | |
|
|
51 | push @listen_sockets, $http_port; |
|
|
52 | |
52 | # the "main thread" |
53 | # the "main thread" |
53 | async { |
54 | async { |
54 | slog 1, "accepting connections"; |
55 | slog 1, "accepting connections"; |
55 | while () { |
56 | while () { |
56 | $connections->down; |
57 | $connections->down; |
57 | push @fh, $port->accept; |
58 | push @newcons, [$http_port->accept]; |
58 | #slog 3, "accepted @$connections ".scalar(@pool); |
59 | #slog 3, "accepted @$connections ".scalar(@pool); |
59 | $::NOW = time; |
60 | $::NOW = time; |
60 | if (@pool) { |
61 | if (@pool) { |
61 | (pop @pool)->ready; |
62 | (pop @pool)->ready; |
62 | } else { |
63 | } else { |
… | |
… | |
64 | } |
65 | } |
65 | |
66 | |
66 | } |
67 | } |
67 | }; |
68 | }; |
68 | |
69 | |
69 | loop; |
|
|
70 | print "ende\n";#d# |
|
|
71 | |
|
|
72 | package conn; |
70 | package conn; |
73 | |
71 | |
74 | use Socket; |
72 | use Socket; |
75 | use HTTP::Date; |
73 | use HTTP::Date; |
76 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
… | |
… | |
78 | our %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
79 | our %blocked; |
77 | our %blocked; |
80 | |
78 | |
81 | sub new { |
79 | sub new { |
82 | my $class = shift; |
80 | my $class = shift; |
|
|
81 | my $peername = shift; |
83 | my $fh = shift; |
82 | my $fh = shift; |
84 | my $self = bless { fh => $fh }, $class; |
83 | my $self = bless { fh => $fh }, $class; |
85 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
84 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
86 | or $self->err(500, "unable to get peername"); |
85 | or $self->err(500, "unable to decode peername"); |
87 | $self->{remote_addr} = inet_ntoa $iaddr; |
86 | $self->{remote_addr} = inet_ntoa $iaddr; |
88 | |
87 | |
89 | # enter ourselves into various lists |
88 | # enter ourselves into various lists |
90 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
89 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
91 | |
90 | |
92 | print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n"; |
|
|
93 | $self; |
91 | $self; |
94 | } |
92 | } |
95 | |
93 | |
96 | sub DESTROY { |
94 | sub DESTROY { |
97 | my $self = shift; |
95 | my $self = shift; |
98 | delete $conn{$self->{remote_addr}}{$self*1}; |
96 | delete $conn{$self->{remote_addr}}{$self*1}; |
99 | delete $uri{$self->{uri}}{$self*1}; |
97 | delete $uri{$self->{uri}}{$self*1}; |
100 | } |
98 | } |
101 | |
99 | |
102 | sub slog { |
100 | sub slog { |
103 | main::slog(@_); |
101 | my $self = shift; |
|
|
102 | main::slog($_[0], "$self->{remote_addr}> $_[1]"); |
104 | } |
103 | } |
105 | |
104 | |
106 | sub print_response { |
105 | sub response { |
107 | my ($self, $code, $msg, $hdr, $content) = @_; |
106 | my ($self, $code, $msg, $hdr, $content) = @_; |
108 | my $res = "HTTP/1.0 $code $msg\015\012"; |
107 | my $res = "HTTP/1.0 $code $msg\015\012"; |
109 | |
108 | |
110 | $hdr->{Date} = time2str $::NOW; # slow? nah. |
109 | $res .= "Connection: close\015\012"; |
|
|
110 | $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( |
111 | |
111 | |
112 | while (my ($h, $v) = each %$hdr) { |
112 | while (my ($h, $v) = each %$hdr) { |
113 | $res .= "$h: $v\015\012" |
113 | $res .= "$h: $v\015\012" |
114 | } |
114 | } |
|
|
115 | |
115 | $res .= "\015\012$content" if defined $content; |
116 | $res .= "\015\012$content" if defined $content; |
116 | |
117 | |
117 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
118 | print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
118 | |
119 | |
119 | print {$self->{fh}} $res; |
120 | print {$self->{fh}} $res; |
… | |
… | |
127 | $content = "$code $msg"; |
128 | $content = "$code $msg"; |
128 | $hdr->{"Content-Type"} = "text/plain"; |
129 | $hdr->{"Content-Type"} = "text/plain"; |
129 | $hdr->{"Content-Length"} = length $content; |
130 | $hdr->{"Content-Length"} = length $content; |
130 | } |
131 | } |
131 | |
132 | |
132 | $self->slog($msg) if $code; |
|
|
133 | |
|
|
134 | $self->print_response($code, $msg, $hdr, $content); |
133 | $self->response($code, $msg, $hdr, $content); |
135 | |
134 | |
136 | die bless {}, err::; |
135 | die bless {}, err::; |
137 | } |
136 | } |
138 | |
137 | |
139 | sub err_blocked { |
138 | sub err_blocked { |
140 | my $self = shift; |
139 | my $self = shift; |
141 | my $ip = $self->{remote_addr}; |
140 | my $ip = $self->{remote_addr}; |
142 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
141 | my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
143 | $self->err(403, "too many connections", |
142 | $self->err(403, "too many connections", |
|
|
143 | { |
|
|
144 | "Content-Type" => "text/html", |
144 | { "Retry-After" => $::BLOCKTIME }, |
145 | "Retry-After" => $::BLOCKTIME |
|
|
146 | }, |
145 | <<EOF); |
147 | <<EOF); |
|
|
148 | <html><p> |
146 | You have been blocked because you opened too many connections. You |
149 | You have been blocked because you opened too many connections. You |
147 | may retry at $time. Until then, |
150 | may retry at</p> |
148 | every new access will renew the block. |
151 | |
|
|
152 | <p><blockquote>$time.</blockquote></p> |
|
|
153 | |
|
|
154 | <p>Until then, each new access will renew the block. You might want to have a |
|
|
155 | look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p> |
|
|
156 | </html> |
149 | EOF |
157 | EOF |
150 | } |
158 | } |
151 | |
159 | |
152 | sub handle { |
160 | sub handle { |
153 | my $self = shift; |
161 | my $self = shift; |
… | |
… | |
213 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
221 | $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
214 | |
222 | |
215 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
223 | weaken ($uri{$self->{uri}}{$self*1} = $self); |
216 | |
224 | |
217 | $self->map_uri; |
225 | $self->map_uri; |
218 | |
|
|
219 | Coro::Event::do_timer(after => 5); |
|
|
220 | |
|
|
221 | $self->respond; |
226 | $self->respond; |
222 | #} |
227 | #} |
223 | } |
228 | } |
224 | |
229 | |
225 | # uri => path mapping |
230 | # uri => path mapping |
… | |
… | |
358 | } |
363 | } |
359 | $hdr->{"Content-Range"} = "bytes */$length"; |
364 | $hdr->{"Content-Range"} = "bytes */$length"; |
360 | $self->err(416, "not satisfiable", $hdr); |
365 | $self->err(416, "not satisfiable", $hdr); |
361 | |
366 | |
362 | satisfiable: |
367 | satisfiable: |
|
|
368 | # check for segmented downloads |
|
|
369 | if ($l && $NO_SEGMENTED) { |
|
|
370 | if (%{$uri{$self->{uri}}} > 1) { |
|
|
371 | $self->slog("segmented download refused\n"); |
|
|
372 | $self->err(400, "segmented downloads are not allowed"); |
|
|
373 | } |
|
|
374 | } |
|
|
375 | |
363 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
376 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
364 | @code = (206, "partial content"); |
377 | @code = (206, "partial content"); |
365 | $length = $h - $l + 1; |
378 | $length = $h - $l + 1; |
366 | |
379 | |
367 | ignore: |
380 | ignore: |
… | |
… | |
375 | $hdr->{"Content-Type"} = "application/octet-stream"; |
388 | $hdr->{"Content-Type"} = "application/octet-stream"; |
376 | } |
389 | } |
377 | |
390 | |
378 | $hdr->{"Content-Length"} = $length; |
391 | $hdr->{"Content-Length"} = $length; |
379 | |
392 | |
380 | $self->print_response(@code, $hdr, ""); |
393 | $self->response(@code, $hdr, ""); |
381 | |
394 | |
382 | if ($self->{method} eq "GET") { |
395 | if ($self->{method} eq "GET") { |
383 | my ($fh, $buf); |
396 | my ($fh, $buf); |
384 | open $fh, "<", $self->{path} |
397 | open $fh, "<", $self->{path} |
385 | or die "$self->{path}: late open failure ($!)"; |
398 | or die "$self->{path}: late open failure ($!)"; |
… | |
… | |
390 | } |
403 | } |
391 | |
404 | |
392 | $h -= $l - 1; |
405 | $h -= $l - 1; |
393 | |
406 | |
394 | while ($h > 0) { |
407 | while ($h > 0) { |
395 | $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
408 | $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; |
396 | print {$self->{fh}} $buf |
409 | $self->{fh}->syswrite($buf) |
397 | or last; |
410 | or last; |
398 | } |
411 | } |
399 | } |
412 | } |
400 | |
413 | |
401 | close $fh; |
414 | close $fh; |