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.11 by root, Sat Aug 11 19:59:20 2001 UTC vs.
Revision 1.17 by root, Wed Aug 15 03:06:21 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', async => 1,
81 cb => \&Linux::AIO::poll_cb );
82
83Event->add_hooks(prepare => sub {
84 &Coro::cede;
85});
75 86
76our %conn; # $conn{ip}{fh} => connobj 87our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 88our %blocked;
78our %mimetype; 89our %mimetype;
79 90
104 $self->{time} = $::NOW; 115 $self->{time} = $::NOW;
105 116
106 # enter ourselves into various lists 117 # enter ourselves into various lists
107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 118 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
108 119
120 $::conns++;
121
109 $self; 122 $self;
110} 123}
111 124
112sub DESTROY { 125sub DESTROY {
113 my $self = shift; 126 my $self = shift;
127
128 $::conns--;
129
114 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
115 delete $uri{$self->{uri}}{$self*1}; 131 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
116} 132}
117 133
118sub slog { 134sub slog {
119 my $self = shift; 135 my $self = shift;
120 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 136 main::slog($_[0], "$self->{remote_addr}> $_[1]");
121} 137}
122 138
123sub response { 139sub response {
124 my ($self, $code, $msg, $hdr, $content) = @_; 140 my ($self, $code, $msg, $hdr, $content) = @_;
125 my $res = "HTTP/1.0 $code $msg\015\012"; 141 my $res = "HTTP/1.1 $code $msg\015\012";
126 142
127 $res .= "Connection: close\015\012"; 143 #$res .= "Connection: close\015\012";
128 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 144 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
129 145
130 while (my ($h, $v) = each %$hdr) { 146 while (my ($h, $v) = each %$hdr) {
131 $res .= "$h: $v\015\012" 147 $res .= "$h: $v\015\012"
132 } 148 }
133 $res .= "\015\012"; 149 $res .= "\015\012";
134 150
135 $res .= $content if defined $content and $self->{method} eq "GET"; 151 $res .= $content if defined $content and $self->{method} ne "HEAD";
136 152
137 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 153 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
138 154
139 $self->{written} += 155 $self->{written} +=
140 print {$self->{fh}} $res; 156 print {$self->{fh}} $res;
147 unless (defined $content) { 163 unless (defined $content) {
148 $content = "$code $msg"; 164 $content = "$code $msg";
149 $hdr->{"Content-Type"} = "text/plain"; 165 $hdr->{"Content-Type"} = "text/plain";
150 $hdr->{"Content-Length"} = length $content; 166 $hdr->{"Content-Length"} = length $content;
151 } 167 }
168 $hdr->{"Connection"} = "close";
152 169
153 $self->response($code, $msg, $hdr, $content); 170 $self->response($code, $msg, $hdr, $content);
154 171
155 die bless {}, err::; 172 die bless {}, err::;
156} 173}
158sub err_blocked { 175sub err_blocked {
159 my $self = shift; 176 my $self = shift;
160 my $ip = $self->{remote_addr}; 177 my $ip = $self->{remote_addr};
161 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 178 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
162 179
163 Coro::Event::do_timer(after => 5); 180 Coro::Event::do_timer(after => 15);
164 181
165 $self->err(403, "too many connections", 182 $self->err(401, "too many connections",
166 { 183 {
167 "Content-Type" => "text/html", 184 "Content-Type" => "text/html",
168 "Retry-After" => $::BLOCKTIME 185 "Retry-After" => $::BLOCKTIME
169 }, 186 },
170 <<EOF); 187 <<EOF);
182 199
183sub handle { 200sub handle {
184 my $self = shift; 201 my $self = shift;
185 my $fh = $self->{fh}; 202 my $fh = $self->{fh};
186 203
204 $fh->timeout($::REQ_TIMEOUT);
187 #while() { 205 while() {
206 $self->{reqs}++;
207
208 # read request and parse first line
209 my $req = $fh->readline("\015\012\015\012");
210
211 unless (defined $req) {
212 if (exists $self->{version}) {
213 last;
214 } else {
215 $self->err(408, "request timeout");
216 }
217 }
218
188 $self->{h} = {}; 219 $self->{h} = {};
189 220
190 # read request and parse first line
191 $fh->timeout($::REQ_TIMEOUT);
192 my $req = $fh->readline("\015\012\015\012");
193 $fh->timeout($::RES_TIMEOUT); 221 $fh->timeout($::RES_TIMEOUT);
194
195 defined $req or
196 $self->err(408, "request timeout");
197
198 my $ip = $self->{remote_addr}; 222 my $ip = $self->{remote_addr};
199 223
200 if ($blocked{$ip}) { 224 if ($blocked{$ip}) {
201 $self->err_blocked($blocked{$ip}) 225 $self->err_blocked($blocked{$ip})
202 if $blocked{$ip} > $::NOW; 226 if $blocked{$ip} > $::NOW;
203 227
204 delete $blocked{$ip}; 228 delete $blocked{$ip};
205 } 229 }
206 230
207 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 231 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
208 $self->slog("blocked ip $ip"); 232 $self->slog(2, "blocked ip $ip");
209 $self->err_blocked; 233 $self->err_blocked;
210 } 234 }
211 235
212 $req =~ /^(?:\015\012)? 236 $req =~ /^(?:\015\012)?
213 (GET|HEAD) \040+ 237 (GET|HEAD) \040+
214 ([^\040]+) \040+ 238 ([^\040]+) \040+
215 HTTP\/([0-9]+\.[0-9]+) 239 HTTP\/([0-9]+\.[0-9]+)
216 \015\012/gx 240 \015\012/gx
217 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 241 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
218
219 $2 ne "1.0"
220 or $self->err(506, "http protocol version not supported");
221 242
222 $self->{method} = $1; 243 $self->{method} = $1;
223 $self->{uri} = $2; 244 $self->{uri} = $2;
245 $self->{version} = $3;
246
247 $3 eq "1.0" or $3 eq "1.1"
248 or $self->err(506, "http protocol version $3 not supported");
224 249
225 # parse headers 250 # parse headers
226 { 251 {
227 my (%hdr, $h, $v); 252 my (%hdr, $h, $v);
228 253
241 while ($h, $v) = each %hdr; 266 while ($h, $v) = each %hdr;
242 } 267 }
243 268
244 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 269 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
245 270
246 weaken ($uri{$self->{uri}}{$self*1} = $self); 271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
247 272
248 $self->map_uri; 273 $self->map_uri;
249 $self->respond; 274 $self->respond;
275
276 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
277
278 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
279 $fh->timeout($::PER_TIMEOUT);
250 #} 280 }
251} 281}
252 282
253# uri => path mapping 283# uri => path mapping
254sub map_uri { 284sub map_uri {
255 my $self = shift; 285 my $self = shift;
399 $self->err(416, "not satisfiable", $hdr); 429 $self->err(416, "not satisfiable", $hdr);
400 430
401satisfiable: 431satisfiable:
402 # check for segmented downloads 432 # check for segmented downloads
403 if ($l && $::NO_SEGMENTED) { 433 if ($l && $::NO_SEGMENTED) {
404 if (%{$uri{$self->{uri}}} > 1) { 434 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
405 $self->slog("segmented download refused\n"); 435 Coro::Event::do_timer(after => 15);
436
406 $self->err(400, "segmented downloads are not allowed"); 437 $self->err(400, "segmented downloads are not allowed");
407 } 438 }
408 } 439 }
409 440
410 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 441 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
421 $hdr->{"Content-Length"} = $length; 452 $hdr->{"Content-Length"} = $length;
422 453
423 $self->response(@code, $hdr, ""); 454 $self->response(@code, $hdr, "");
424 455
425 if ($self->{method} eq "GET") { 456 if ($self->{method} eq "GET") {
426 my ($fh, $buf); 457 my ($fh, $buf, $r);
458 my $current = $Coro::current;
427 open $fh, "<", $self->{path} 459 open $fh, "<", $self->{path}
428 or die "$self->{path}: late open failure ($!)"; 460 or die "$self->{path}: late open failure ($!)";
429 461
430 if ($l) {
431 sysseek $fh, $l, 0
432 or die "$self->{path}: cannot seek to $l ($!)";
433 }
434
435 $h -= $l - 1; 462 $h -= $l - 1;
436 463
437 while ($h > 0) { 464 while ($h > 0) {
438 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 465 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
466 $buf, 0, sub {
467 $r = $_[0];
468 $current->ready;
469 });
470 &Coro::schedule;
471 last unless $r;
439 my $w = $self->{fh}->syswrite($buf) 472 my $w = $self->{fh}->syswrite($buf)
440 or last; 473 or last;
441 $::written += $w; 474 $::written += $w;
442 $self->{written} += $w; 475 $self->{written} += $w;
476 $l += $r;
443 } 477 }
444 } 478 }
445 479
446 close $fh; 480 close $fh;
447} 481}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines