1 |
#!/opt/bin/perl |
2 |
|
3 |
# this is a relatively small web-server, using coroutines for connections. |
4 |
# play around with it but do not use it in production without checking it |
5 |
# works for you. ask myhttpd@plan9.de in case of problems, or if you are |
6 |
# interested in a newer version (more useless features). |
7 |
|
8 |
use Coro; |
9 |
use Coro::Semaphore; |
10 |
use Coro::Event; |
11 |
use Coro::Socket; |
12 |
|
13 |
no utf8; |
14 |
use bytes; |
15 |
|
16 |
# at least on my machine, this thingy serves files |
17 |
# quite a bit faster than apache, ;) |
18 |
# and quite a bit slower than thttpd :( |
19 |
|
20 |
$MAX_CONNECTS = 500; # maximum simult. connects |
21 |
$REQ_TIMEOUT = 60; # request timeout |
22 |
$RES_TIMEOUT = 180; # response timeout |
23 |
$MAX_POOL = 20; # max. number of idle workers |
24 |
$DOCROOT = "/usr/www/htdocs"; # document root |
25 |
$INDEXPROG = "/usr/www/bin/dols"; # indexing program (nph-cgi script) |
26 |
$SERVER_HOST = "0.0.0.0"; # host to bind on |
27 |
$SERVER_PORT = 80; # port to listen on |
28 |
|
29 |
my $port = new Coro::Socket |
30 |
LocalAddr => $SERVER_HOST, |
31 |
LocalPort => $SERVER_PORT, |
32 |
ReuseAddr => 1, |
33 |
Listen => 1, |
34 |
or die "unable to start server"; |
35 |
|
36 |
$SIG{PIPE} = 'IGNORE'; |
37 |
|
38 |
sub slog { |
39 |
my $level = shift; |
40 |
my $format = shift; |
41 |
#printf "---: $format\n", @_; |
42 |
} |
43 |
|
44 |
my $connections = new Coro::Semaphore $MAX_CONNECTS; |
45 |
|
46 |
my @fh; |
47 |
|
48 |
# move the event main loop into a coroutine |
49 |
async { loop }; |
50 |
|
51 |
slog 1, "accepting connections"; |
52 |
while () { |
53 |
$connections->down; |
54 |
if (my $fh = $port->accept) { |
55 |
#slog 3, "accepted @$connections ".scalar(@pool); |
56 |
async_pool { |
57 |
eval { |
58 |
conn->new($fh)->handle; |
59 |
}; |
60 |
close $fh; |
61 |
slog 1, "$@" if $@ && !ref $@; |
62 |
$connections->up; |
63 |
}; |
64 |
} |
65 |
} |
66 |
|
67 |
package conn; |
68 |
|
69 |
use Socket; |
70 |
use HTTP::Date; |
71 |
|
72 |
sub new { |
73 |
my $class = shift; |
74 |
my $fh = shift; |
75 |
my (undef, $iaddr) = unpack_sockaddr_in $fh->peername |
76 |
or $self->err(500, "unable to get peername"); |
77 |
$self->{remote_address} = inet_ntoa $iaddr; |
78 |
bless { fh => $fh }, $class; |
79 |
} |
80 |
|
81 |
sub slog { |
82 |
main::slog(@_); |
83 |
} |
84 |
|
85 |
sub print_response { |
86 |
my ($self, $code, $msg, $hdr, $content) = @_; |
87 |
my $res = "HTTP/1.0 $code $msg\015\012"; |
88 |
|
89 |
$hdr->{Date} = time2str time; # slow? nah. |
90 |
|
91 |
while (my ($h, $v) = each %$hdr) { |
92 |
$res .= "$h: $v\015\012" |
93 |
} |
94 |
$res .= "\015\012$content" if defined $content; |
95 |
|
96 |
print {$self->{fh}} $res; |
97 |
} |
98 |
|
99 |
sub err { |
100 |
my $self = shift; |
101 |
my ($code, $msg, $hdr, $content) = @_; |
102 |
|
103 |
unless (defined $content) { |
104 |
$content = "$code $msg"; |
105 |
$hdr->{"Content-Type"} = "text/plain"; |
106 |
$hdr->{"Content-Length"} = length $content; |
107 |
} |
108 |
|
109 |
$self->slog($msg) if $code; |
110 |
|
111 |
$self->print_response($code, $msg, $hdr, $content); |
112 |
|
113 |
die bless {}, err::; |
114 |
} |
115 |
|
116 |
sub handle { |
117 |
my $self = shift; |
118 |
my $fh = $self->{fh}; |
119 |
|
120 |
#while() { |
121 |
$self->{h} = {}; |
122 |
|
123 |
# read request and parse first line |
124 |
$fh->timeout($::REQ_TIMEOUT); |
125 |
my $req = $fh->readline("\015\012\015\012"); |
126 |
$fh->timeout($::RES_TIMEOUT); |
127 |
|
128 |
defined $req or |
129 |
$self->err(408, "request timeout"); |
130 |
|
131 |
$req =~ /^(?:\015\012)? |
132 |
(GET|HEAD) \040+ |
133 |
([^\040]+) \040+ |
134 |
HTTP\/([0-9]+\.[0-9]+) |
135 |
\015\012/gx |
136 |
or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); |
137 |
|
138 |
$2 < 2 |
139 |
or $self->err(506, "http protocol version not supported"); |
140 |
|
141 |
$self->{method} = $1; |
142 |
$self->{uri} = $2; |
143 |
|
144 |
# parse headers |
145 |
{ |
146 |
my (%hdr, $h, $v); |
147 |
|
148 |
$hdr{lc $1} .= ",$2" |
149 |
while $req =~ /\G |
150 |
([^:\000-\040]+): |
151 |
[\011\040]* |
152 |
((?: [^\015\012]+ | \015\012[\011\040] )*) |
153 |
\015\012 |
154 |
/gxc; |
155 |
|
156 |
$req =~ /\G\015\012$/ |
157 |
or $self->err(400, "bad request"); |
158 |
|
159 |
$self->{h}{$h} = substr $v, 1 |
160 |
while ($h, $v) = each %hdr; |
161 |
} |
162 |
|
163 |
$self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; |
164 |
|
165 |
$self->map_uri; |
166 |
$self->respond; |
167 |
#} |
168 |
} |
169 |
|
170 |
# uri => path mapping |
171 |
sub map_uri { |
172 |
my $self = shift; |
173 |
my $host = $self->{h}{host} || "default"; |
174 |
my $uri = $self->{uri}; |
175 |
|
176 |
$host =~ /[\/\\]/ |
177 |
and $self->err(400, "bad request"); |
178 |
|
179 |
# some massaging, also makes it more secure |
180 |
$uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
181 |
$uri =~ s%//+%/%g; |
182 |
$uri =~ s%/\.(?=/|$)%%g; |
183 |
1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
184 |
|
185 |
$uri =~ m%^/?\.\.(?=/|$)% |
186 |
and $self->err(400, "bad request"); |
187 |
|
188 |
$self->{name} = $uri; |
189 |
|
190 |
# now do the path mapping |
191 |
$self->{path} = "$::DOCROOT/$host$uri"; |
192 |
} |
193 |
|
194 |
sub server_address { |
195 |
my $self = shift; |
196 |
my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->sockname |
197 |
or $self->err(500, "unable to get socket name"); |
198 |
((inet_ntoa $iaddr), $port); |
199 |
} |
200 |
|
201 |
sub server_host { |
202 |
my $self = shift; |
203 |
if (exists $self->{h}{host}) { |
204 |
return $self->{h}{host}; |
205 |
} else { |
206 |
return (($self->server_address)[0]); |
207 |
} |
208 |
} |
209 |
|
210 |
sub server_hostport { |
211 |
my $self = shift; |
212 |
my ($host, $port); |
213 |
if (exists $self->{h}{host}) { |
214 |
($host, $port) = ($self->{h}{host}, $self->{server_port}); |
215 |
} else { |
216 |
($host, $port) = $self->server_address; |
217 |
} |
218 |
$port = $port == 80 ? "" : ":$port"; |
219 |
$host.$port; |
220 |
} |
221 |
|
222 |
# no, this doesn't do cgi, but it's close enough |
223 |
# for the no-longer-used directory indexing script. |
224 |
sub _cgi { |
225 |
my $self = shift; |
226 |
my $path = shift; |
227 |
my $fh; |
228 |
|
229 |
# no two-way xxx supported |
230 |
if (0 == fork) { |
231 |
open STDOUT, ">&".fileno($self->{fh}); |
232 |
if (chdir $::DOCROOT) { |
233 |
$ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike |
234 |
$ENV{HTTP_HOST} = $self->server_host; |
235 |
$ENV{HTTP_PORT} = $self->{server_host}; |
236 |
$ENV{SCRIPT_NAME} = $self->{name}; |
237 |
exec $::INDEXPROG; |
238 |
} |
239 |
Coro::State::_exit(0); |
240 |
} else { |
241 |
} |
242 |
} |
243 |
|
244 |
sub respond { |
245 |
my $self = shift; |
246 |
my $path = $self->{path}; |
247 |
|
248 |
stat $path |
249 |
or $self->err(404, "not found"); |
250 |
|
251 |
# idiotic netscape sends idiotic headers AGAIN |
252 |
my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
253 |
? str2time $1 : 0; |
254 |
|
255 |
if (-d _ && -r _) { |
256 |
# directory |
257 |
if ($path !~ /\/$/) { |
258 |
# create a redirect to get the trailing "/" |
259 |
my $host = $self->server_hostport; |
260 |
$self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); |
261 |
} else { |
262 |
$ims < (stat _)[9] |
263 |
or $self->err(304, "not modified"); |
264 |
|
265 |
if ($self->{method} eq "GET") { |
266 |
if (-r "$path/index.html") { |
267 |
$self->{path} .= "/index.html"; |
268 |
$self->handle_file; |
269 |
} else { |
270 |
$self->handle_dir; |
271 |
} |
272 |
} |
273 |
} |
274 |
} elsif (-f _ && -r _) { |
275 |
-x _ and $self->err(403, "forbidden"); |
276 |
$self->handle_file; |
277 |
} else { |
278 |
$self->err(404, "not found"); |
279 |
} |
280 |
} |
281 |
|
282 |
sub handle_dir { |
283 |
my $self = shift; |
284 |
$self->_cgi($::INDEXPROG); |
285 |
} |
286 |
|
287 |
sub handle_file { |
288 |
my $self = shift; |
289 |
my $length = -s _; |
290 |
my $hdr = { |
291 |
"Last-Modified" => time2str ((stat _)[9]), |
292 |
}; |
293 |
|
294 |
my @code = (200, "ok"); |
295 |
my ($l, $h); |
296 |
|
297 |
if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
298 |
for (split /,/, $1) { |
299 |
if (/^-(\d+)$/) { |
300 |
($l, $h) = ($length - $1, $length - 1); |
301 |
} elsif (/^(\d+)-(\d*)$/) { |
302 |
($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
303 |
} else { |
304 |
($l, $h) = (0, $length - 1); |
305 |
goto ignore; |
306 |
} |
307 |
goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
308 |
} |
309 |
$hdr->{"Content-Range"} = "bytes */$length"; |
310 |
$self->err(416, "not satisfiable", $hdr); |
311 |
|
312 |
satisfiable: |
313 |
$hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
314 |
@code = (206, "partial content"); |
315 |
$length = $h - $l + 1; |
316 |
|
317 |
ignore: |
318 |
} else { |
319 |
($l, $h) = (0, $length - 1); |
320 |
} |
321 |
|
322 |
if ($self->{path} =~ /\.html$/) { |
323 |
$hdr->{"Content-Type"} = "text/html"; |
324 |
} else { |
325 |
$hdr->{"Content-Type"} = "application/octet-stream"; |
326 |
} |
327 |
|
328 |
$hdr->{"Content-Length"} = $length; |
329 |
|
330 |
$self->print_response(@code, $hdr, ""); |
331 |
|
332 |
if ($self->{method} eq "GET") { |
333 |
my ($fh, $buf); |
334 |
open $fh, "<", $self->{path} |
335 |
or die "$self->{path}: late open failure ($!)"; |
336 |
|
337 |
if ($l) { |
338 |
sysseek $fh, $l, 0 |
339 |
or die "$self->{path}: cannot seek to $l ($!)"; |
340 |
} |
341 |
|
342 |
$h -= $l - 1; |
343 |
|
344 |
while ($h > 0) { |
345 |
$h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; |
346 |
print {$self->{fh}} $buf |
347 |
or last; |
348 |
} |
349 |
} |
350 |
|
351 |
close $fh; |
352 |
} |
353 |
|