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.12 by root, Sat Aug 11 23:10:56 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
104 $self->{time} = $::NOW; 117 $self->{time} = $::NOW;
105 118
106 # enter ourselves into various lists 119 # enter ourselves into various lists
107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 120 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
108 121
122 $::conns++;
123
109 $self; 124 $self;
110} 125}
111 126
112sub DESTROY { 127sub DESTROY {
113 my $self = shift; 128 my $self = shift;
129
130 $::conns--;
131
114 delete $conn{$self->{remote_addr}}{$self*1}; 132 delete $conn{$self->{remote_addr}}{$self*1};
115 delete $uri{$self->{uri}}{$self*1}; 133 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
116} 134}
117 135
118sub slog { 136sub slog {
119 my $self = shift; 137 my $self = shift;
120 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 138 main::slog($_[0], "$self->{remote_addr}> $_[1]");
130 while (my ($h, $v) = each %$hdr) { 148 while (my ($h, $v) = each %$hdr) {
131 $res .= "$h: $v\015\012" 149 $res .= "$h: $v\015\012"
132 } 150 }
133 $res .= "\015\012"; 151 $res .= "\015\012";
134 152
135 $res .= $content if defined $content and $self->{method} eq "GET"; 153 $res .= $content if defined $content and $self->{method} ne "HEAD";
136 154
137 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#
138 156
139 $self->{written} += 157 $self->{written} +=
140 print {$self->{fh}} $res; 158 print {$self->{fh}} $res;
158sub err_blocked { 176sub err_blocked {
159 my $self = shift; 177 my $self = shift;
160 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
161 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
162 180
163 Coro::Event::do_timer(after => 5); 181 Coro::Event::do_timer(after => 15);
164 182
165 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
166 { 184 {
167 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
168 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME
169 }, 187 },
170 <<EOF); 188 <<EOF);
212 $req =~ /^(?:\015\012)? 230 $req =~ /^(?:\015\012)?
213 (GET|HEAD) \040+ 231 (GET|HEAD) \040+
214 ([^\040]+) \040+ 232 ([^\040]+) \040+
215 HTTP\/([0-9]+\.[0-9]+) 233 HTTP\/([0-9]+\.[0-9]+)
216 \015\012/gx 234 \015\012/gx
217 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
218 236
219 $2 ne "1.0" 237 $2 ne "1.0"
220 or $self->err(506, "http protocol version not supported"); 238 or $self->err(506, "http protocol version not supported");
221 239
222 $self->{method} = $1; 240 $self->{method} = $1;
241 while ($h, $v) = each %hdr; 259 while ($h, $v) = each %hdr;
242 } 260 }
243 261
244 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 262 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
245 263
246 weaken ($uri{$self->{uri}}{$self*1} = $self); 264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
247 265
248 $self->map_uri; 266 $self->map_uri;
249 $self->respond; 267 $self->respond;
250 #} 268 #}
251} 269}
399 $self->err(416, "not satisfiable", $hdr); 417 $self->err(416, "not satisfiable", $hdr);
400 418
401satisfiable: 419satisfiable:
402 # check for segmented downloads 420 # check for segmented downloads
403 if ($l && $::NO_SEGMENTED) { 421 if ($l && $::NO_SEGMENTED) {
404 if (%{$uri{$self->{uri}}} > 1) { 422 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
423 Coro::Event::do_timer(after => 15);
424
405 $self->err(400, "segmented downloads are not allowed"); 425 $self->err(400, "segmented downloads are not allowed");
406 } 426 }
407 } 427 }
408 428
409 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 429 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
420 $hdr->{"Content-Length"} = $length; 440 $hdr->{"Content-Length"} = $length;
421 441
422 $self->response(@code, $hdr, ""); 442 $self->response(@code, $hdr, "");
423 443
424 if ($self->{method} eq "GET") { 444 if ($self->{method} eq "GET") {
425 my ($fh, $buf); 445 my ($fh, $buf, $r);
446 my $current = $Coro::current;
426 open $fh, "<", $self->{path} 447 open $fh, "<", $self->{path}
427 or die "$self->{path}: late open failure ($!)"; 448 or die "$self->{path}: late open failure ($!)";
428 449
429 if ($l) {
430 sysseek $fh, $l, 0
431 or die "$self->{path}: cannot seek to $l ($!)";
432 }
433
434 $h -= $l - 1; 450 $h -= $l - 1;
435 451
436 while ($h > 0) { 452 while ($h > 0) {
437 $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;
438 my $w = $self->{fh}->syswrite($buf) 460 my $w = $self->{fh}->syswrite($buf)
439 or last; 461 or last;
440 $::written += $w; 462 $::written += $w;
441 $self->{written} += $w; 463 $self->{written} += $w;
464 $l += $r;
442 } 465 }
443 } 466 }
444 467
445 close $fh; 468 close $fh;
446} 469}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines