1 | #!/usr/bin/perl |
1 | #!/usr/bin/perl |
2 | |
2 | |
3 | # this is a relatively small web-server, using coroutines |
3 | # this is a relatively small web-server, using coroutines for connections. |
4 | # for connections. play around with it but do not use |
4 | # play around with it but do not use it in production without checking it |
5 | # it in production. ask myhttpd@plan9.de for a better |
5 | # works for you. ask myhttpd@plan9.de in case of problems, or if you are |
6 | # version that's more usable and bugfixed. |
6 | # interested in a newer version (more useless features). |
7 | |
7 | |
8 | use Coro; |
8 | use Coro; |
9 | use Coro::Semaphore; |
9 | use Coro::Semaphore; |
10 | use Coro::Event; |
10 | use Coro::Event; |
11 | use Coro::Socket; |
11 | use Coro::Socket; |
… | |
… | |
151 | ([^\040]+) \040+ |
151 | ([^\040]+) \040+ |
152 | HTTP\/([0-9]+\.[0-9]+) |
152 | HTTP\/([0-9]+\.[0-9]+) |
153 | \015\012/gx |
153 | \015\012/gx |
154 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
154 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
155 | |
155 | |
156 | $2 >= 2 |
156 | $2 < 2 |
157 | or $self->err(506, "http protocol version not supported"); |
157 | or $self->err(506, "http protocol version not supported"); |
158 | |
158 | |
159 | $self->{method} = $1; |
159 | $self->{method} = $1; |
160 | $self->{uri} = $2; |
160 | $self->{uri} = $2; |
161 | |
161 | |
… | |
… | |
164 | my (%hdr, $h, $v); |
164 | my (%hdr, $h, $v); |
165 | |
165 | |
166 | $hdr{lc $1} .= ",$2" |
166 | $hdr{lc $1} .= ",$2" |
167 | while $req =~ /\G |
167 | while $req =~ /\G |
168 | ([^:\000-\040]+): |
168 | ([^:\000-\040]+): |
169 | [\010\040]* |
169 | [\011\040]* |
170 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
170 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
171 | \015\012 |
171 | \015\012 |
172 | /gxc; |
172 | /gxc; |
173 | |
173 | |
174 | $req =~ /\G\015\012$/ |
174 | $req =~ /\G\015\012$/ |
175 | or $self->err(400, "bad request"); |
175 | or $self->err(400, "bad request"); |
… | |
… | |
317 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
317 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
318 | } else { |
318 | } else { |
319 | ($l, $h) = (0, $length - 1); |
319 | ($l, $h) = (0, $length - 1); |
320 | goto ignore; |
320 | goto ignore; |
321 | } |
321 | } |
322 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
322 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
323 | } |
323 | } |
324 | $hdr->{"Content-Range"} = "bytes */$length"; |
324 | $hdr->{"Content-Range"} = "bytes */$length"; |
325 | $self->err(416, "not satisfiable", $hdr); |
325 | $self->err(416, "not satisfiable", $hdr); |
326 | |
326 | |
327 | satisfiable: |
327 | satisfiable: |
… | |
… | |
344 | |
344 | |
345 | $self->print_response(@code, $hdr, ""); |
345 | $self->print_response(@code, $hdr, ""); |
346 | |
346 | |
347 | if ($self->{method} eq "GET") { |
347 | if ($self->{method} eq "GET") { |
348 | my ($fh, $buf); |
348 | my ($fh, $buf); |
349 | cpen $fh, "<", $self->{path} |
349 | open $fh, "<", $self->{path} |
350 | or die "$self->{path}: late open failure ($!)"; |
350 | or die "$self->{path}: late open failure ($!)"; |
351 | |
351 | |
352 | if ($l) { |
352 | if ($l) { |
353 | sysseek $fh, $l, 0 |
353 | sysseek $fh, $l, 0 |
354 | or die "$self->{path}: cannot seek to $l ($!)"; |
354 | or die "$self->{path}: cannot seek to $l ($!)"; |