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.9 by root, Sat Aug 11 12:49:21 2001 UTC vs.
Revision 1.14 by root, Sun Aug 12 14:09:03 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"
77our %blocked; 77our %blocked;
78our %mimetype; 78our %mimetype;
79 79
80sub read_mimetypes { 80sub read_mimetypes {
81 local *M; 81 local *M;
82 if (open M, "<mimetypes") { 82 if (open M, "<mime_types") {
83 while (<M>) { 83 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) { 84 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2; 85 $mimetype{lc $1} = $2;
86 } 86 }
87 } 87 }
88 } else { 88 } else {
89 $self->slog(1, "cannot open mimetypes\n"); 89 print "cannot open mime_types\n";
90 } 90 }
91} 91}
92
93read_mimetypes;
92 94
93sub new { 95sub new {
94 my $class = shift; 96 my $class = shift;
95 my $peername = shift; 97 my $peername = shift;
96 my $fh = shift; 98 my $fh = shift;
97 my $self = bless { fh => $fh }, $class; 99 my $self = bless { fh => $fh }, $class;
98 my (undef, $iaddr) = unpack_sockaddr_in $peername 100 my (undef, $iaddr) = unpack_sockaddr_in $peername
99 or $self->err(500, "unable to decode peername"); 101 or $self->err(500, "unable to decode peername");
100 102
101 $self->{remote_addr} = inet_ntoa $iaddr; 103 $self->{remote_addr} = inet_ntoa $iaddr;
104 $self->{time} = $::NOW;
102 105
103 # enter ourselves into various lists 106 # enter ourselves into various lists
104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
105 108
109 $::conns++;
110
106 $self; 111 $self;
107} 112}
108 113
109sub DESTROY { 114sub DESTROY {
110 my $self = shift; 115 my $self = shift;
116
117 $::conns--;
118
111 delete $conn{$self->{remote_addr}}{$self*1}; 119 delete $conn{$self->{remote_addr}}{$self*1};
112 delete $uri{$self->{uri}}{$self*1}; 120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
113} 121}
114 122
115sub slog { 123sub slog {
116 my $self = shift; 124 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 125 main::slog($_[0], "$self->{remote_addr}> $_[1]");
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
126 134
127 while (my ($h, $v) = each %$hdr) { 135 while (my ($h, $v) = each %$hdr) {
128 $res .= "$h: $v\015\012" 136 $res .= "$h: $v\015\012"
129 } 137 }
138 $res .= "\015\012";
130 139
131 $res .= "\015\012$content" if defined $content; 140 $res .= $content if defined $content and $self->{method} ne "HEAD";
132 141
133 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 142 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
134 143
144 $self->{written} +=
135 print {$self->{fh}} $res; 145 print {$self->{fh}} $res;
136} 146}
137 147
138sub err { 148sub err {
139 my $self = shift; 149 my $self = shift;
140 my ($code, $msg, $hdr, $content) = @_; 150 my ($code, $msg, $hdr, $content) = @_;
152 162
153sub err_blocked { 163sub err_blocked {
154 my $self = shift; 164 my $self = shift;
155 my $ip = $self->{remote_addr}; 165 my $ip = $self->{remote_addr};
156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167
168 Coro::Event::do_timer(after => 15);
169
157 $self->err(403, "too many connections", 170 $self->err(401, "too many connections",
158 { 171 {
159 "Content-Type" => "text/html", 172 "Content-Type" => "text/html",
160 "Retry-After" => $::BLOCKTIME 173 "Retry-After" => $::BLOCKTIME
161 }, 174 },
162 <<EOF); 175 <<EOF);
187 defined $req or 200 defined $req or
188 $self->err(408, "request timeout"); 201 $self->err(408, "request timeout");
189 202
190 my $ip = $self->{remote_addr}; 203 my $ip = $self->{remote_addr};
191 204
205 $self->err_blocked($blocked{$ip});
192 if ($blocked{$ip}) { 206 if ($blocked{$ip}) {
193 $self->err_blocked($blocked{$ip}) 207 $self->err_blocked($blocked{$ip})
194 if $blocked{$ip} > $::NOW; 208 if $blocked{$ip} > $::NOW;
195 209
196 delete $blocked{$ip}; 210 delete $blocked{$ip};
197 } 211 }
198 212
199 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 213 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
200 $self->slog("blocked ip $ip"); 214 $self->slog(2, "blocked ip $ip");
201 $self->err_blocked; 215 $self->err_blocked;
202 } 216 }
203 217
204 $req =~ /^(?:\015\012)? 218 $req =~ /^(?:\015\012)?
205 (GET|HEAD) \040+ 219 (GET|HEAD) \040+
206 ([^\040]+) \040+ 220 ([^\040]+) \040+
207 HTTP\/([0-9]+\.[0-9]+) 221 HTTP\/([0-9]+\.[0-9]+)
208 \015\012/gx 222 \015\012/gx
209 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 223 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
210 224
211 $2 ne "1.0" 225 $2 ne "1.0"
212 or $self->err(506, "http protocol version not supported"); 226 or $self->err(506, "http protocol version not supported");
213 227
214 $self->{method} = $1; 228 $self->{method} = $1;
233 while ($h, $v) = each %hdr; 247 while ($h, $v) = each %hdr;
234 } 248 }
235 249
236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 250 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
237 251
238 weaken ($uri{$self->{uri}}{$self*1} = $self); 252 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
239 253
240 $self->map_uri; 254 $self->map_uri;
241 $self->respond; 255 $self->respond;
242 #} 256 #}
243} 257}
304 if (chdir $::DOCROOT) { 318 if (chdir $::DOCROOT) {
305 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 319 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
306 $ENV{HTTP_HOST} = $self->server_host; 320 $ENV{HTTP_HOST} = $self->server_host;
307 $ENV{HTTP_PORT} = $self->{server_host}; 321 $ENV{HTTP_PORT} = $self->{server_host};
308 $ENV{SCRIPT_NAME} = $self->{name}; 322 $ENV{SCRIPT_NAME} = $self->{name};
309 exec $::INDEXPROG; 323 exec $path;
310 } 324 }
311 Coro::State::_exit(0); 325 Coro::State::_exit(0);
312 } else { 326 } else {
313 } 327 }
314} 328}
317 my $self = shift; 331 my $self = shift;
318 my $path = $self->{path}; 332 my $path = $self->{path};
319 333
320 stat $path 334 stat $path
321 or $self->err(404, "not found"); 335 or $self->err(404, "not found");
336
337 $self->{stat} = [stat _];
322 338
323 # idiotic netscape sends idiotic headers AGAIN 339 # idiotic netscape sends idiotic headers AGAIN
324 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 340 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
325 ? str2time $1 : 0; 341 ? str2time $1 : 0;
326 342
329 if ($path !~ /\/$/) { 345 if ($path !~ /\/$/) {
330 # create a redirect to get the trailing "/" 346 # create a redirect to get the trailing "/"
331 my $host = $self->server_hostport; 347 my $host = $self->server_hostport;
332 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 348 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
333 } else { 349 } else {
334 $ims < (stat _)[9] 350 $ims < $self->{stat}[9]
335 or $self->err(304, "not modified"); 351 or $self->err(304, "not modified");
336 352
337 if ($self->{method} eq "GET") { 353 if ($self->{method} eq "GET") {
338 if (-r "$path/index.html") { 354 if (-r "$path/index.html") {
339 $self->{path} .= "/index.html"; 355 $self->{path} .= "/index.html";
351 } 367 }
352} 368}
353 369
354sub handle_dir { 370sub handle_dir {
355 my $self = shift; 371 my $self = shift;
356 $self->_cgi($::INDEXPROG); 372 my $idx = $self->diridx;
373
374 $self->response(200, "ok",
375 {
376 "Content-Type" => "text/html",
377 "Content-Length" => length $idx,
378 },
379 $idx);
357} 380}
358 381
359sub handle_file { 382sub handle_file {
360 my $self = shift; 383 my $self = shift;
361 my $length = -s _; 384 my $length = -s _;
381 $hdr->{"Content-Range"} = "bytes */$length"; 404 $hdr->{"Content-Range"} = "bytes */$length";
382 $self->err(416, "not satisfiable", $hdr); 405 $self->err(416, "not satisfiable", $hdr);
383 406
384satisfiable: 407satisfiable:
385 # check for segmented downloads 408 # check for segmented downloads
386 if ($l && $NO_SEGMENTED) { 409 if ($l && $::NO_SEGMENTED) {
387 if (%{$uri{$self->{uri}}} > 1) { 410 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
388 $self->slog("segmented download refused\n"); 411 Coro::Event::do_timer(after => 15);
412
389 $self->err(400, "segmented downloads are not allowed"); 413 $self->err(400, "segmented downloads are not allowed");
390 } 414 }
391 } 415 }
392 416
393 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 417 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
417 441
418 $h -= $l - 1; 442 $h -= $l - 1;
419 443
420 while ($h > 0) { 444 while ($h > 0) {
421 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 445 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
422 $self->{fh}->syswrite($buf) 446 my $w = $self->{fh}->syswrite($buf)
423 or last; 447 or last;
448 $::written += $w;
449 $self->{written} += $w;
424 } 450 }
425 } 451 }
426 452
427 close $fh; 453 close $fh;
428} 454}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines