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