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

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.10 by root, Sat Aug 11 16:34:47 2001 UTC vs.
Revision 1.16 by root, Tue Aug 14 19:38:25 2001 UTC

43 43
44my $http_port = new Coro::Socket 44my $http_port = new Coro::Socket
45 LocalAddr => $SERVER_HOST, 45 LocalAddr => $SERVER_HOST,
46 LocalPort => $SERVER_PORT, 46 LocalPort => $SERVER_PORT,
47 ReuseAddr => 1, 47 ReuseAddr => 1,
48 Listen => 1, 48 Listen => 50,
49 or die "unable to start server"; 49 or die "unable to start server";
50 50
51push @listen_sockets, $http_port; 51push @listen_sockets, $http_port;
52 52
53# the "main thread" 53# the "main thread"
70package conn; 70package conn;
71 71
72use Socket; 72use Socket;
73use HTTP::Date; 73use HTTP::Date;
74use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
75use Linux::AIO;
76
77Linux::AIO::min_parallel $::AIO_PARALLEL;
78
79Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r',
81 async => 1,
82 cb => \&Linux::AIO::poll_cb );
83
84Event->add_hooks(prepare => sub {
85 &Coro::cede while &Coro::nready;
86 1e6;
87});
75 88
76our %conn; # $conn{ip}{fh} => connobj 89our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 90our %blocked;
78our %mimetype; 91our %mimetype;
79 92
99 my $self = bless { fh => $fh }, $class; 112 my $self = bless { fh => $fh }, $class;
100 my (undef, $iaddr) = unpack_sockaddr_in $peername 113 my (undef, $iaddr) = unpack_sockaddr_in $peername
101 or $self->err(500, "unable to decode peername"); 114 or $self->err(500, "unable to decode peername");
102 115
103 $self->{remote_addr} = inet_ntoa $iaddr; 116 $self->{remote_addr} = inet_ntoa $iaddr;
117 $self->{time} = $::NOW;
104 118
105 # enter ourselves into various lists 119 # enter ourselves into various lists
106 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 120 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
107 121
122 $::conns++;
123
108 $self; 124 $self;
109} 125}
110 126
111sub DESTROY { 127sub DESTROY {
112 my $self = shift; 128 my $self = shift;
129
130 $::conns--;
131
113 delete $conn{$self->{remote_addr}}{$self*1}; 132 delete $conn{$self->{remote_addr}}{$self*1};
114 delete $uri{$self->{uri}}{$self*1}; 133 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
115} 134}
116 135
117sub slog { 136sub slog {
118 my $self = shift; 137 my $self = shift;
119 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 138 main::slog($_[0], "$self->{remote_addr}> $_[1]");
129 while (my ($h, $v) = each %$hdr) { 148 while (my ($h, $v) = each %$hdr) {
130 $res .= "$h: $v\015\012" 149 $res .= "$h: $v\015\012"
131 } 150 }
132 $res .= "\015\012"; 151 $res .= "\015\012";
133 152
134 $res .= $content if defined $content and $self->{method} eq "GET"; 153 $res .= $content if defined $content and $self->{method} ne "HEAD";
135 154
136 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 155 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
137 156
157 $self->{written} +=
138 print {$self->{fh}} $res; 158 print {$self->{fh}} $res;
139} 159}
140 160
141sub err { 161sub err {
142 my $self = shift; 162 my $self = shift;
143 my ($code, $msg, $hdr, $content) = @_; 163 my ($code, $msg, $hdr, $content) = @_;
156sub err_blocked { 176sub err_blocked {
157 my $self = shift; 177 my $self = shift;
158 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
159 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
160 180
161 Coro::Event::do_timer(after => 5); 181 Coro::Event::do_timer(after => 15);
162 182
163 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
164 { 184 {
165 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
166 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME
167 }, 187 },
168 <<EOF); 188 <<EOF);
201 221
202 delete $blocked{$ip}; 222 delete $blocked{$ip};
203 } 223 }
204 224
205 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 225 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
206 $self->slog("blocked ip $ip"); 226 $self->slog(2, "blocked ip $ip");
207 $self->err_blocked; 227 $self->err_blocked;
208 } 228 }
209 229
210 $req =~ /^(?:\015\012)? 230 $req =~ /^(?:\015\012)?
211 (GET|HEAD) \040+ 231 (GET|HEAD) \040+
212 ([^\040]+) \040+ 232 ([^\040]+) \040+
213 HTTP\/([0-9]+\.[0-9]+) 233 HTTP\/([0-9]+\.[0-9]+)
214 \015\012/gx 234 \015\012/gx
215 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
216 236
217 $2 ne "1.0" 237 $2 ne "1.0"
218 or $self->err(506, "http protocol version not supported"); 238 or $self->err(506, "http protocol version not supported");
219 239
220 $self->{method} = $1; 240 $self->{method} = $1;
239 while ($h, $v) = each %hdr; 259 while ($h, $v) = each %hdr;
240 } 260 }
241 261
242 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 262 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
243 263
244 weaken ($uri{$self->{uri}}{$self*1} = $self); 264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
245 265
246 $self->map_uri; 266 $self->map_uri;
247 $self->respond; 267 $self->respond;
248 #} 268 #}
249} 269}
397 $self->err(416, "not satisfiable", $hdr); 417 $self->err(416, "not satisfiable", $hdr);
398 418
399satisfiable: 419satisfiable:
400 # check for segmented downloads 420 # check for segmented downloads
401 if ($l && $::NO_SEGMENTED) { 421 if ($l && $::NO_SEGMENTED) {
402 if (%{$uri{$self->{uri}}} > 1) { 422 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
403 $self->slog("segmented download refused\n"); 423 Coro::Event::do_timer(after => 15);
424
404 $self->err(400, "segmented downloads are not allowed"); 425 $self->err(400, "segmented downloads are not allowed");
405 } 426 }
406 } 427 }
407 428
408 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 429 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
419 $hdr->{"Content-Length"} = $length; 440 $hdr->{"Content-Length"} = $length;
420 441
421 $self->response(@code, $hdr, ""); 442 $self->response(@code, $hdr, "");
422 443
423 if ($self->{method} eq "GET") { 444 if ($self->{method} eq "GET") {
424 my ($fh, $buf); 445 my ($fh, $buf, $r);
446 my $current = $Coro::current;
425 open $fh, "<", $self->{path} 447 open $fh, "<", $self->{path}
426 or die "$self->{path}: late open failure ($!)"; 448 or die "$self->{path}: late open failure ($!)";
427 449
428 if ($l) {
429 sysseek $fh, $l, 0
430 or die "$self->{path}: cannot seek to $l ($!)";
431 }
432
433 $h -= $l - 1; 450 $h -= $l - 1;
434 451
435 while ($h > 0) { 452 while ($h > 0) {
436 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 453 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
454 $buf, 0, sub {
455 $r = $_[0];
456 $current->ready;
457 });
458 &Coro::schedule;
459 last unless $r;
437 $self->{fh}->syswrite($buf) 460 my $w = $self->{fh}->syswrite($buf)
438 or last; 461 or last;
462 $::written += $w;
463 $self->{written} += $w;
464 $l += $r;
439 } 465 }
440 } 466 }
441 467
442 close $fh; 468 close $fh;
443} 469}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines