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

Comparing Coro/myhttpd/httpd.pl (file contents):
Revision 1.16 by root, Tue Aug 14 19:38:25 2001 UTC vs.
Revision 1.18 by root, Wed Aug 15 03:13:36 2001 UTC

75use Linux::AIO; 75use Linux::AIO;
76 76
77Linux::AIO::min_parallel $::AIO_PARALLEL; 77Linux::AIO::min_parallel $::AIO_PARALLEL;
78 78
79Event->io(fd => Linux::AIO::poll_fileno, 79Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r', 80 poll => 'r', async => 1,
81 async => 1,
82 cb => \&Linux::AIO::poll_cb ); 81 cb => \&Linux::AIO::poll_cb );
83
84Event->add_hooks(prepare => sub {
85 &Coro::cede while &Coro::nready;
86 1e6;
87});
88 82
89our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{fh} => connobj
90our %blocked; 84our %blocked;
91our %mimetype; 85our %mimetype;
92 86
138 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 132 main::slog($_[0], "$self->{remote_addr}> $_[1]");
139} 133}
140 134
141sub response { 135sub response {
142 my ($self, $code, $msg, $hdr, $content) = @_; 136 my ($self, $code, $msg, $hdr, $content) = @_;
143 my $res = "HTTP/1.0 $code $msg\015\012"; 137 my $res = "HTTP/1.1 $code $msg\015\012";
144 138
145 $res .= "Connection: close\015\012"; 139 #$res .= "Connection: close\015\012";
146 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 140 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
147 141
148 while (my ($h, $v) = each %$hdr) { 142 while (my ($h, $v) = each %$hdr) {
149 $res .= "$h: $v\015\012" 143 $res .= "$h: $v\015\012"
150 } 144 }
165 unless (defined $content) { 159 unless (defined $content) {
166 $content = "$code $msg"; 160 $content = "$code $msg";
167 $hdr->{"Content-Type"} = "text/plain"; 161 $hdr->{"Content-Type"} = "text/plain";
168 $hdr->{"Content-Length"} = length $content; 162 $hdr->{"Content-Length"} = length $content;
169 } 163 }
164 $hdr->{"Connection"} = "close";
170 165
171 $self->response($code, $msg, $hdr, $content); 166 $self->response($code, $msg, $hdr, $content);
172 167
173 die bless {}, err::; 168 die bless {}, err::;
174} 169}
200 195
201sub handle { 196sub handle {
202 my $self = shift; 197 my $self = shift;
203 my $fh = $self->{fh}; 198 my $fh = $self->{fh};
204 199
200 $fh->timeout($::REQ_TIMEOUT);
205 #while() { 201 while() {
202 $self->{reqs}++;
203
204 # read request and parse first line
205 my $req = $fh->readline("\015\012\015\012");
206
207 unless (defined $req) {
208 if (exists $self->{version}) {
209 last;
210 } else {
211 $self->err(408, "request timeout");
212 }
213 }
214
206 $self->{h} = {}; 215 $self->{h} = {};
207 216
208 # read request and parse first line
209 $fh->timeout($::REQ_TIMEOUT);
210 my $req = $fh->readline("\015\012\015\012");
211 $fh->timeout($::RES_TIMEOUT); 217 $fh->timeout($::RES_TIMEOUT);
212
213 defined $req or
214 $self->err(408, "request timeout");
215
216 my $ip = $self->{remote_addr}; 218 my $ip = $self->{remote_addr};
217 219
218 if ($blocked{$ip}) { 220 if ($blocked{$ip}) {
219 $self->err_blocked($blocked{$ip}) 221 $self->err_blocked($blocked{$ip})
220 if $blocked{$ip} > $::NOW; 222 if $blocked{$ip} > $::NOW;
232 ([^\040]+) \040+ 234 ([^\040]+) \040+
233 HTTP\/([0-9]+\.[0-9]+) 235 HTTP\/([0-9]+\.[0-9]+)
234 \015\012/gx 236 \015\012/gx
235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 237 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
236 238
237 $2 ne "1.0"
238 or $self->err(506, "http protocol version not supported");
239
240 $self->{method} = $1; 239 $self->{method} = $1;
241 $self->{uri} = $2; 240 $self->{uri} = $2;
241 $self->{version} = $3;
242
243 $3 eq "1.0" or $3 eq "1.1"
244 or $self->err(506, "http protocol version $3 not supported");
242 245
243 # parse headers 246 # parse headers
244 { 247 {
245 my (%hdr, $h, $v); 248 my (%hdr, $h, $v);
246 249
263 266
264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 267 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
265 268
266 $self->map_uri; 269 $self->map_uri;
267 $self->respond; 270 $self->respond;
271
272 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
273
274 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
275 $fh->timeout($::PER_TIMEOUT);
268 #} 276 }
269} 277}
270 278
271# uri => path mapping 279# uri => path mapping
272sub map_uri { 280sub map_uri {
273 my $self = shift; 281 my $self = shift;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines