1 |
root |
1.1 |
use Coro; |
2 |
|
|
use Coro::Semaphore; |
3 |
|
|
use Coro::Event; |
4 |
|
|
use Coro::Socket; |
5 |
|
|
|
6 |
|
|
no utf8; |
7 |
|
|
use bytes; |
8 |
|
|
|
9 |
|
|
# at least on my machine, this thingy serves files |
10 |
|
|
# quite a bit faster than apache, ;) |
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 |
|
|
|
20 |
|
|
$SIG{PIPE} = 'IGNORE'; |
21 |
|
|
|
22 |
|
|
sub slog { |
23 |
|
|
my $level = shift; |
24 |
|
|
my $format = shift; |
25 |
|
|
printf "---: $format\n", @_; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
my $connections = new Coro::Semaphore $MAX_CONNECTS; |
29 |
|
|
|
30 |
|
|
my @fh; |
31 |
|
|
my @pool; |
32 |
|
|
|
33 |
root |
1.2 |
# one "execution thread" |
34 |
root |
1.1 |
sub handler { |
35 |
|
|
while () { |
36 |
|
|
my $fh = pop @fh; |
37 |
|
|
if ($fh) { |
38 |
|
|
eval { |
39 |
|
|
conn->new($fh)->handle; |
40 |
|
|
}; |
41 |
|
|
close $fh; |
42 |
|
|
slog 1, "$@" if $@ && !ref $@; |
43 |
|
|
$connections->up; |
44 |
|
|
} else { |
45 |
|
|
last if @pool >= $MAX_POOL; |
46 |
|
|
push @pool, $Coro::current; |
47 |
|
|
schedule; |
48 |
|
|
} |
49 |
|
|
} |
50 |
|
|
} |
51 |
|
|
|
52 |
root |
1.2 |
# the "main thread" |
53 |
root |
1.1 |
async { |
54 |
|
|
slog 1, "accepting connections"; |
55 |
|
|
while () { |
56 |
|
|
$connections->down; |
57 |
|
|
push @fh, $port->accept; |
58 |
|
|
#slog 3, "accepted @$connections ".scalar(@pool); |
59 |
root |
1.3 |
$::NOW = time; |
60 |
root |
1.1 |
if (@pool) { |
61 |
|
|
(pop @pool)->ready; |
62 |
|
|
} else { |
63 |
|
|
async \&handler; |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
} |
67 |
|
|
}; |
68 |
|
|
|
69 |
|
|
loop; |
70 |
|
|
print "ende\n";#d# |
71 |
|
|
|
72 |
|
|
package conn; |
73 |
|
|
|
74 |
|
|
use Socket; |
75 |
|
|
use HTTP::Date; |
76 |
root |
1.2 |
use Convert::Scalar 'weaken'; |
77 |
|
|
|
78 |
root |
1.3 |
our %conn; # $conn{ip}{fh} => connobj |
79 |
|
|
our %blocked; |
80 |
root |
1.1 |
|
81 |
|
|
sub new { |
82 |
|
|
my $class = shift; |
83 |
|
|
my $fh = shift; |
84 |
root |
1.2 |
my $self = bless { fh => $fh }, $class; |
85 |
root |
1.1 |
my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
86 |
|
|
or $self->err(500, "unable to get peername"); |
87 |
root |
1.3 |
$self->{remote_addr} = inet_ntoa $iaddr; |
88 |
root |
1.2 |
|
89 |
|
|
# enter ourselves into various lists |
90 |
root |
1.3 |
weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
91 |
|
|
|
92 |
|
|
print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n"; |
93 |
root |
1.2 |
$self; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
sub DESTROY { |
97 |
|
|
my $self = shift; |
98 |
root |
1.3 |
delete $conn{$self->{remote_addr}}{$self*1}; |
99 |
|
|
delete $uri{$self->{uri}}{$self*1}; |
100 |
root |
1.1 |
} |
101 |
|
|
|
102 |
|
|
sub slog { |
103 |
|
|
main::slog(@_); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
sub print_response { |
107 |
|
|
my ($self, $code, $msg, $hdr, $content) = @_; |
108 |
|
|
my $res = "HTTP/1.0 $code $msg\015\012"; |
109 |
|
|
|
110 |
root |
1.3 |
$hdr->{Date} = time2str $::NOW; # slow? nah. |
111 |
root |
1.1 |
|
112 |
|
|
while (my ($h, $v) = each %$hdr) { |
113 |
|
|
$res .= "$h: $v\015\012" |
114 |
|
|
} |
115 |
|
|
$res .= "\015\012$content" if defined $content; |
116 |
|
|
|
117 |
root |
1.3 |
print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# |
118 |
root |
1.2 |
|
119 |
root |
1.1 |
print {$self->{fh}} $res; |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
sub err { |
123 |
|
|
my $self = shift; |
124 |
|
|
my ($code, $msg, $hdr, $content) = @_; |
125 |
|
|
|
126 |
|
|
unless (defined $content) { |
127 |
|
|
$content = "$code $msg"; |
128 |
|
|
$hdr->{"Content-Type"} = "text/plain"; |
129 |
|
|
$hdr->{"Content-Length"} = length $content; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
$self->slog($msg) if $code; |
133 |
|
|
|
134 |
|
|
$self->print_response($code, $msg, $hdr, $content); |
135 |
|
|
|
136 |
|
|
die bless {}, err::; |
137 |
|
|
} |
138 |
|
|
|
139 |
root |
1.3 |
sub err_blocked { |
140 |
|
|
my $self = shift; |
141 |
|
|
my $ip = $self->{remote_addr}; |
142 |
|
|
my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; |
143 |
|
|
$self->err(403, "too many connections", |
144 |
|
|
{ "Retry-After" => $::BLOCKTIME }, |
145 |
|
|
<<EOF); |
146 |
|
|
You have been blocked because you opened too many connections. You |
147 |
|
|
may retry at $time. Until then, |
148 |
|
|
every new access will renew the block. |
149 |
|
|
EOF |
150 |
|
|
} |
151 |
|
|
|
152 |
root |
1.1 |
sub handle { |
153 |
|
|
my $self = shift; |
154 |
|
|
my $fh = $self->{fh}; |
155 |
|
|
|
156 |
|
|
#while() { |
157 |
|
|
$self->{h} = {}; |
158 |
|
|
|
159 |
|
|
# read request and parse first line |
160 |
|
|
$fh->timeout($::REQ_TIMEOUT); |
161 |
|
|
my $req = $fh->readline("\015\012\015\012"); |
162 |
|
|
$fh->timeout($::RES_TIMEOUT); |
163 |
|
|
|
164 |
|
|
defined $req or |
165 |
|
|
$self->err(408, "request timeout"); |
166 |
|
|
|
167 |
root |
1.3 |
my $ip = $self->{remote_addr}; |
168 |
|
|
|
169 |
|
|
if ($blocked{$ip}) { |
170 |
|
|
$self->err_blocked($blocked{$ip}) |
171 |
|
|
if $blocked{$ip} > $::NOW; |
172 |
|
|
|
173 |
|
|
delete $blocked{$ip}; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
if (%{$conn{$ip}} > $::MAX_CONN_IP) { |
177 |
|
|
$self->slog("blocked ip $ip"); |
178 |
|
|
$self->err_blocked; |
179 |
|
|
} |
180 |
|
|
|
181 |
root |
1.1 |
$req =~ /^(?:\015\012)? |
182 |
|
|
(GET|HEAD) \040+ |
183 |
|
|
([^\040]+) \040+ |
184 |
|
|
HTTP\/([0-9]+\.[0-9]+) |
185 |
|
|
\015\012/gx |
186 |
|
|
or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
187 |
|
|
|
188 |
|
|
$2 ne "1.0" |
189 |
|
|
or $self->err(506, "http protocol version not supported"); |
190 |
|
|
|
191 |
|
|
$self->{method} = $1; |
192 |
|
|
$self->{uri} = $2; |
193 |
|
|
|
194 |
|
|
# parse headers |
195 |
|
|
{ |
196 |
|
|
my (%hdr, $h, $v); |
197 |
|
|
|
198 |
|
|
$hdr{lc $1} .= ",$2" |
199 |
|
|
while $req =~ /\G |
200 |
|
|
([^:\000-\040]+): |
201 |
|
|
[\008\040]* |
202 |
|
|
((?: [^\015\012]+ | \015\012[\008\040] )*) |
203 |
|
|
\015\012 |
204 |
|
|
/gxc; |
205 |
|
|
|
206 |
|
|
$req =~ /\G\015\012$/ |
207 |
|
|
or $self->err(400, "bad request"); |
208 |
|
|
|
209 |
|
|
$self->{h}{$h} = substr $v, 1 |
210 |
|
|
while ($h, $v) = each %hdr; |
211 |
|
|
} |
212 |
|
|
|
213 |
|
|
$self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
214 |
root |
1.3 |
|
215 |
|
|
weaken ($uri{$self->{uri}}{$self*1} = $self); |
216 |
root |
1.1 |
|
217 |
|
|
$self->map_uri; |
218 |
root |
1.2 |
|
219 |
|
|
Coro::Event::do_timer(after => 5); |
220 |
|
|
|
221 |
root |
1.1 |
$self->respond; |
222 |
|
|
#} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
# uri => path mapping |
226 |
|
|
sub map_uri { |
227 |
|
|
my $self = shift; |
228 |
|
|
my $host = $self->{h}{host} || "default"; |
229 |
|
|
my $uri = $self->{uri}; |
230 |
|
|
|
231 |
|
|
# some massaging, also makes it more secure |
232 |
|
|
$uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
233 |
|
|
$uri =~ s%//+%/%g; |
234 |
|
|
$uri =~ s%/\.(?=/|$)%%g; |
235 |
|
|
1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
236 |
|
|
|
237 |
|
|
$uri =~ m%^/?\.\.(?=/|$)% |
238 |
|
|
and $self->err(400, "bad request"); |
239 |
|
|
|
240 |
|
|
$self->{name} = $uri; |
241 |
|
|
|
242 |
|
|
# now do the path mapping |
243 |
|
|
$self->{path} = "$::DOCROOT/$host$uri"; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
sub server_address { |
247 |
|
|
my $self = shift; |
248 |
|
|
my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
249 |
|
|
or $self->err(500, "unable to get socket name"); |
250 |
|
|
((inet_ntoa $iaddr), $port); |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
sub server_host { |
254 |
|
|
my $self = shift; |
255 |
|
|
if (exists $self->{h}{host}) { |
256 |
|
|
return $self->{h}{host}; |
257 |
|
|
} else { |
258 |
|
|
return (($self->server_address)[0]); |
259 |
|
|
} |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
sub server_hostport { |
263 |
|
|
my $self = shift; |
264 |
|
|
my ($host, $port); |
265 |
|
|
if (exists $self->{h}{host}) { |
266 |
|
|
($host, $port) = ($self->{h}{host}, $self->{server_port}); |
267 |
|
|
} else { |
268 |
|
|
($host, $port) = $self->server_address; |
269 |
|
|
} |
270 |
|
|
$port = $port == 80 ? "" : ":$port"; |
271 |
|
|
$host.$port; |
272 |
|
|
} |
273 |
|
|
|
274 |
|
|
sub _cgi { |
275 |
|
|
my $self = shift; |
276 |
|
|
my $path = shift; |
277 |
|
|
my $fh; |
278 |
|
|
|
279 |
|
|
# no two-way xxx supported |
280 |
|
|
if (0 == fork) { |
281 |
|
|
open STDOUT, ">&".fileno($self->{fh}); |
282 |
|
|
if (chdir $::DOCROOT) { |
283 |
|
|
$ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
284 |
|
|
$ENV{HTTP_HOST} = $self->server_host; |
285 |
|
|
$ENV{HTTP_PORT} = $self->{server_host}; |
286 |
|
|
$ENV{SCRIPT_NAME} = $self->{name}; |
287 |
|
|
exec $::INDEXPROG; |
288 |
|
|
} |
289 |
|
|
Coro::State::_exit(0); |
290 |
|
|
} else { |
291 |
|
|
} |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
sub respond { |
295 |
|
|
my $self = shift; |
296 |
|
|
my $path = $self->{path}; |
297 |
|
|
|
298 |
|
|
stat $path |
299 |
|
|
or $self->err(404, "not found"); |
300 |
|
|
|
301 |
|
|
# idiotic netscape sends idiotic headers AGAIN |
302 |
|
|
my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
303 |
|
|
? str2time $1 : 0; |
304 |
|
|
|
305 |
|
|
if (-d _ && -r _) { |
306 |
|
|
# directory |
307 |
|
|
if ($path !~ /\/$/) { |
308 |
|
|
# create a redirect to get the trailing "/" |
309 |
|
|
my $host = $self->server_hostport; |
310 |
|
|
$self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
311 |
|
|
} else { |
312 |
|
|
$ims < (stat _)[9] |
313 |
|
|
or $self->err(304, "not modified"); |
314 |
|
|
|
315 |
|
|
if ($self->{method} eq "GET") { |
316 |
|
|
if (-r "$path/index.html") { |
317 |
|
|
$self->{path} .= "/index.html"; |
318 |
|
|
$self->handle_file; |
319 |
|
|
} else { |
320 |
|
|
$self->handle_dir; |
321 |
|
|
} |
322 |
|
|
} |
323 |
|
|
} |
324 |
|
|
} elsif (-f _ && -r _) { |
325 |
|
|
-x _ and $self->err(403, "forbidden"); |
326 |
|
|
$self->handle_file; |
327 |
|
|
} else { |
328 |
|
|
$self->err(404, "not found"); |
329 |
|
|
} |
330 |
|
|
} |
331 |
|
|
|
332 |
|
|
sub handle_dir { |
333 |
|
|
my $self = shift; |
334 |
|
|
$self->_cgi($::INDEXPROG); |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
sub handle_file { |
338 |
|
|
my $self = shift; |
339 |
|
|
my $length = -s _; |
340 |
|
|
my $hdr = { |
341 |
|
|
"Last-Modified" => time2str ((stat _)[9]), |
342 |
|
|
}; |
343 |
|
|
|
344 |
|
|
my @code = (200, "ok"); |
345 |
|
|
my ($l, $h); |
346 |
|
|
|
347 |
|
|
if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
348 |
|
|
for (split /,/, $1) { |
349 |
|
|
if (/^-(\d+)$/) { |
350 |
|
|
($l, $h) = ($length - $1, $length - 1); |
351 |
|
|
} elsif (/^(\d+)-(\d*)$/) { |
352 |
|
|
($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
353 |
|
|
} else { |
354 |
|
|
($l, $h) = (0, $length - 1); |
355 |
|
|
goto ignore; |
356 |
|
|
} |
357 |
|
|
goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
358 |
|
|
} |
359 |
|
|
$hdr->{"Content-Range"} = "bytes */$length"; |
360 |
|
|
$self->err(416, "not satisfiable", $hdr); |
361 |
|
|
|
362 |
|
|
satisfiable: |
363 |
|
|
$hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
364 |
|
|
@code = (206, "partial content"); |
365 |
|
|
$length = $h - $l + 1; |
366 |
|
|
|
367 |
|
|
ignore: |
368 |
|
|
} else { |
369 |
|
|
($l, $h) = (0, $length - 1); |
370 |
|
|
} |
371 |
|
|
|
372 |
|
|
if ($self->{path} =~ /\.html$/) { |
373 |
|
|
$hdr->{"Content-Type"} = "text/html"; |
374 |
|
|
} else { |
375 |
|
|
$hdr->{"Content-Type"} = "application/octet-stream"; |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
$hdr->{"Content-Length"} = $length; |
379 |
|
|
|
380 |
|
|
$self->print_response(@code, $hdr, ""); |
381 |
|
|
|
382 |
|
|
if ($self->{method} eq "GET") { |
383 |
|
|
my ($fh, $buf); |
384 |
|
|
open $fh, "<", $self->{path} |
385 |
|
|
or die "$self->{path}: late open failure ($!)"; |
386 |
|
|
|
387 |
|
|
if ($l) { |
388 |
|
|
sysseek $fh, $l, 0 |
389 |
|
|
or die "$self->{path}: cannot seek to $l ($!)"; |
390 |
|
|
} |
391 |
|
|
|
392 |
|
|
$h -= $l - 1; |
393 |
|
|
|
394 |
|
|
while ($h > 0) { |
395 |
|
|
$h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
396 |
|
|
print {$self->{fh}} $buf |
397 |
|
|
or last; |
398 |
|
|
} |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
close $fh; |
402 |
|
|
} |
403 |
root |
1.2 |
|
404 |
|
|
1; |