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; |
… | |
… | |
42 | } |
42 | } |
43 | |
43 | |
44 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
44 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
45 | |
45 | |
46 | my @fh; |
46 | my @fh; |
47 | my @pool; |
|
|
48 | |
47 | |
49 | sub handler { |
48 | # move the event main loop into a coroutine |
|
|
49 | async { loop }; |
|
|
50 | |
|
|
51 | slog 1, "accepting connections"; |
50 | while () { |
52 | while () { |
51 | my $fh = pop @fh; |
53 | $connections->down; |
52 | if ($fh) { |
54 | if (my $fh = $port->accept) { |
|
|
55 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
56 | async_pool { |
53 | eval { |
57 | eval { |
54 | conn->new($fh)->handle; |
58 | conn->new($fh)->handle; |
55 | }; |
59 | }; |
56 | close $fh; |
60 | close $fh; |
57 | slog 1, "$@" if $@ && !ref $@; |
61 | slog 1, "$@" if $@ && !ref $@; |
58 | $connections->up; |
62 | $connections->up; |
59 | } else { |
|
|
60 | last if @pool >= $MAX_POOL; |
|
|
61 | push @pool, $Coro::current; |
|
|
62 | schedule; |
|
|
63 | } |
63 | }; |
64 | } |
64 | } |
65 | } |
65 | } |
66 | |
|
|
67 | async { |
|
|
68 | slog 1, "accepting connections"; |
|
|
69 | while () { |
|
|
70 | $connections->down; |
|
|
71 | push @fh, $port->accept; |
|
|
72 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
73 | if (@pool) { |
|
|
74 | (pop @pool)->ready; |
|
|
75 | } else { |
|
|
76 | async \&handler; |
|
|
77 | } |
|
|
78 | |
|
|
79 | } |
|
|
80 | }; |
|
|
81 | |
|
|
82 | loop; |
|
|
83 | print "ende\n";#d# |
|
|
84 | |
66 | |
85 | package conn; |
67 | package conn; |
86 | |
68 | |
87 | use Socket; |
69 | use Socket; |
88 | use HTTP::Date; |
70 | use HTTP::Date; |
… | |
… | |
151 | ([^\040]+) \040+ |
133 | ([^\040]+) \040+ |
152 | HTTP\/([0-9]+\.[0-9]+) |
134 | HTTP\/([0-9]+\.[0-9]+) |
153 | \015\012/gx |
135 | \015\012/gx |
154 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
136 | or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); |
155 | |
137 | |
156 | $2 ne "1.0" |
138 | $2 < 2 |
157 | or $self->err(506, "http protocol version not supported"); |
139 | or $self->err(506, "http protocol version not supported"); |
158 | |
140 | |
159 | $self->{method} = $1; |
141 | $self->{method} = $1; |
160 | $self->{uri} = $2; |
142 | $self->{uri} = $2; |
161 | |
143 | |
… | |
… | |
164 | my (%hdr, $h, $v); |
146 | my (%hdr, $h, $v); |
165 | |
147 | |
166 | $hdr{lc $1} .= ",$2" |
148 | $hdr{lc $1} .= ",$2" |
167 | while $req =~ /\G |
149 | while $req =~ /\G |
168 | ([^:\000-\040]+): |
150 | ([^:\000-\040]+): |
169 | [\008\040]* |
151 | [\011\040]* |
170 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
152 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
171 | \015\012 |
153 | \015\012 |
172 | /gxc; |
154 | /gxc; |
173 | |
155 | |
174 | $req =~ /\G\015\012$/ |
156 | $req =~ /\G\015\012$/ |
175 | or $self->err(400, "bad request"); |
157 | or $self->err(400, "bad request"); |
… | |
… | |
317 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
299 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
318 | } else { |
300 | } else { |
319 | ($l, $h) = (0, $length - 1); |
301 | ($l, $h) = (0, $length - 1); |
320 | goto ignore; |
302 | goto ignore; |
321 | } |
303 | } |
322 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; |
304 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
323 | } |
305 | } |
324 | $hdr->{"Content-Range"} = "bytes */$length"; |
306 | $hdr->{"Content-Range"} = "bytes */$length"; |
325 | $self->err(416, "not satisfiable", $hdr); |
307 | $self->err(416, "not satisfiable", $hdr); |
326 | |
308 | |
327 | satisfiable: |
309 | satisfiable: |
… | |
… | |
363 | } |
345 | } |
364 | } |
346 | } |
365 | |
347 | |
366 | close $fh; |
348 | close $fh; |
367 | } |
349 | } |
|
|
350 | |