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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines