ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/eg/myhttpd
(Generate patch)

Comparing Coro/eg/myhttpd (file contents):
Revision 1.2 by root, Sat Aug 11 19:59:19 2001 UTC vs.
Revision 1.10 by root, Wed Apr 11 02:50:01 2007 UTC

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
8use Coro; 8use Coro;
9use Coro::Semaphore; 9use Coro::Semaphore;
10use Coro::Event; 10use Coro::Event;
11use Coro::Socket; 11use Coro::Socket;
62 schedule; 62 schedule;
63 } 63 }
64 } 64 }
65} 65}
66 66
67async { 67# move the event main loop into a coroutine
68async { loop };
69
68 slog 1, "accepting connections"; 70slog 1, "accepting connections";
69 while () { 71while () {
70 $connections->down; 72 $connections->down;
71 push @fh, $port->accept; 73 push @fh, $port->accept;
72 #slog 3, "accepted @$connections ".scalar(@pool); 74 #slog 3, "accepted @$connections ".scalar(@pool);
73 if (@pool) { 75 if (@pool) {
74 (pop @pool)->ready; 76 (pop @pool)->ready;
75 } else { 77 } else {
76 async \&handler; 78 async \&handler;
77 }
78
79 } 79 }
80};
81 80
82loop; 81}
83print "ende\n";#d#
84 82
85package conn; 83package conn;
86 84
87use Socket; 85use Socket;
88use HTTP::Date; 86use HTTP::Date;
89 87
90sub new { 88sub new {
91 my $class = shift; 89 my $class = shift;
92 my $fh = shift; 90 my $fh = shift;
93 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 91 my (undef, $iaddr) = unpack_sockaddr_in $fh->peername
94 or $self->err(500, "unable to get peername"); 92 or $self->err(500, "unable to get peername");
95 $self->{remote_address} = inet_ntoa $iaddr; 93 $self->{remote_address} = inet_ntoa $iaddr;
96 bless { fh => $fh }, $class; 94 bless { fh => $fh }, $class;
97} 95}
98 96
151 ([^\040]+) \040+ 149 ([^\040]+) \040+
152 HTTP\/([0-9]+\.[0-9]+) 150 HTTP\/([0-9]+\.[0-9]+)
153 \015\012/gx 151 \015\012/gx
154 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 152 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" });
155 153
156 $2 ne "1.0" 154 $2 < 2
157 or $self->err(506, "http protocol version not supported"); 155 or $self->err(506, "http protocol version not supported");
158 156
159 $self->{method} = $1; 157 $self->{method} = $1;
160 $self->{uri} = $2; 158 $self->{uri} = $2;
161 159
164 my (%hdr, $h, $v); 162 my (%hdr, $h, $v);
165 163
166 $hdr{lc $1} .= ",$2" 164 $hdr{lc $1} .= ",$2"
167 while $req =~ /\G 165 while $req =~ /\G
168 ([^:\000-\040]+): 166 ([^:\000-\040]+):
169 [\008\040]* 167 [\011\040]*
170 ((?: [^\015\012]+ | \015\012[\008\040] )*) 168 ((?: [^\015\012]+ | \015\012[\011\040] )*)
171 \015\012 169 \015\012
172 /gxc; 170 /gxc;
173 171
174 $req =~ /\G\015\012$/ 172 $req =~ /\G\015\012$/
175 or $self->err(400, "bad request"); 173 or $self->err(400, "bad request");
206 $self->{path} = "$::DOCROOT/$host$uri"; 204 $self->{path} = "$::DOCROOT/$host$uri";
207} 205}
208 206
209sub server_address { 207sub server_address {
210 my $self = shift; 208 my $self = shift;
211 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 209 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->sockname
212 or $self->err(500, "unable to get socket name"); 210 or $self->err(500, "unable to get socket name");
213 ((inet_ntoa $iaddr), $port); 211 ((inet_ntoa $iaddr), $port);
214} 212}
215 213
216sub server_host { 214sub server_host {
317 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 315 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
318 } else { 316 } else {
319 ($l, $h) = (0, $length - 1); 317 ($l, $h) = (0, $length - 1);
320 goto ignore; 318 goto ignore;
321 } 319 }
322 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 320 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
323 } 321 }
324 $hdr->{"Content-Range"} = "bytes */$length"; 322 $hdr->{"Content-Range"} = "bytes */$length";
325 $self->err(416, "not satisfiable", $hdr); 323 $self->err(416, "not satisfiable", $hdr);
326 324
327satisfiable: 325satisfiable:
363 } 361 }
364 } 362 }
365 363
366 close $fh; 364 close $fh;
367} 365}
366

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines