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.3 by root, Fri Aug 10 02:28:28 2001 UTC vs.
Revision 1.9 by root, Sat Aug 11 12:49:21 2001 UTC

7use bytes; 7use bytes;
8 8
9# at least on my machine, this thingy serves files 9# at least on my machine, this thingy serves files
10# quite a bit faster than apache, ;) 10# quite a bit faster than apache, ;)
11# and quite a bit slower than thttpd :( 11# and quite a bit slower than thttpd :(
12
13my $port = new Coro::Socket
14 LocalAddr => $SERVER_HOST,
15 LocalPort => $SERVER_PORT,
16 ReuseAddr => 1,
17 Listen => 1,
18 or die "unable to start server";
19 12
20$SIG{PIPE} = 'IGNORE'; 13$SIG{PIPE} = 'IGNORE';
21 14
22sub slog { 15sub slog {
23 my $level = shift; 16 my $level = shift;
25 printf "---: $format\n", @_; 18 printf "---: $format\n", @_;
26} 19}
27 20
28my $connections = new Coro::Semaphore $MAX_CONNECTS; 21my $connections = new Coro::Semaphore $MAX_CONNECTS;
29 22
30my @fh; 23my @newcons;
31my @pool; 24my @pool;
32 25
33# one "execution thread" 26# one "execution thread"
34sub handler { 27sub handler {
35 while () { 28 while () {
36 my $fh = pop @fh; 29 my $new = pop @newcons;
37 if ($fh) { 30 if ($new) {
38 eval { 31 eval {
39 conn->new($fh)->handle; 32 conn->new(@$new)->handle;
40 }; 33 };
41 close $fh;
42 slog 1, "$@" if $@ && !ref $@; 34 slog 1, "$@" if $@ && !ref $@;
43 $connections->up; 35 $connections->up;
44 } else { 36 } else {
45 last if @pool >= $MAX_POOL; 37 last if @pool >= $MAX_POOL;
46 push @pool, $Coro::current; 38 push @pool, $Coro::current;
47 schedule; 39 schedule;
48 } 40 }
49 } 41 }
50} 42}
51 43
44my $http_port = new Coro::Socket
45 LocalAddr => $SERVER_HOST,
46 LocalPort => $SERVER_PORT,
47 ReuseAddr => 1,
48 Listen => 1,
49 or die "unable to start server";
50
51push @listen_sockets, $http_port;
52
52# the "main thread" 53# the "main thread"
53async { 54async {
54 slog 1, "accepting connections"; 55 slog 1, "accepting connections";
55 while () { 56 while () {
56 $connections->down; 57 $connections->down;
57 push @fh, $port->accept; 58 push @newcons, [$http_port->accept];
58 #slog 3, "accepted @$connections ".scalar(@pool); 59 #slog 3, "accepted @$connections ".scalar(@pool);
59 $::NOW = time; 60 $::NOW = time;
60 if (@pool) { 61 if (@pool) {
61 (pop @pool)->ready; 62 (pop @pool)->ready;
62 } else { 63 } else {
64 } 65 }
65 66
66 } 67 }
67}; 68};
68 69
69loop;
70print "ende\n";#d#
71
72package conn; 70package conn;
73 71
74use Socket; 72use Socket;
75use HTTP::Date; 73use HTTP::Date;
76use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
77 75
78our %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
79our %blocked; 77our %blocked;
78our %mimetype;
79
80sub read_mimetypes {
81 local *M;
82 if (open M, "<mimetypes") {
83 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2;
86 }
87 }
88 } else {
89 $self->slog(1, "cannot open mimetypes\n");
90 }
91}
80 92
81sub new { 93sub new {
82 my $class = shift; 94 my $class = shift;
95 my $peername = shift;
83 my $fh = shift; 96 my $fh = shift;
84 my $self = bless { fh => $fh }, $class; 97 my $self = bless { fh => $fh }, $class;
85 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 98 my (undef, $iaddr) = unpack_sockaddr_in $peername
86 or $self->err(500, "unable to get peername"); 99 or $self->err(500, "unable to decode peername");
100
87 $self->{remote_addr} = inet_ntoa $iaddr; 101 $self->{remote_addr} = inet_ntoa $iaddr;
88 102
89 # enter ourselves into various lists 103 # enter ourselves into various lists
90 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
91 105
92 print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n";
93 $self; 106 $self;
94} 107}
95 108
96sub DESTROY { 109sub DESTROY {
97 my $self = shift; 110 my $self = shift;
98 delete $conn{$self->{remote_addr}}{$self*1}; 111 delete $conn{$self->{remote_addr}}{$self*1};
99 delete $uri{$self->{uri}}{$self*1}; 112 delete $uri{$self->{uri}}{$self*1};
100} 113}
101 114
102sub slog { 115sub slog {
103 main::slog(@_); 116 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]");
104} 118}
105 119
106sub print_response { 120sub response {
107 my ($self, $code, $msg, $hdr, $content) = @_; 121 my ($self, $code, $msg, $hdr, $content) = @_;
108 my $res = "HTTP/1.0 $code $msg\015\012"; 122 my $res = "HTTP/1.0 $code $msg\015\012";
109 123
110 $hdr->{Date} = time2str $::NOW; # slow? nah. 124 $res .= "Connection: close\015\012";
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 126
112 while (my ($h, $v) = each %$hdr) { 127 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 128 $res .= "$h: $v\015\012"
114 } 129 }
130
115 $res .= "\015\012$content" if defined $content; 131 $res .= "\015\012$content" if defined $content;
116 132
117 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 133 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
118 134
119 print {$self->{fh}} $res; 135 print {$self->{fh}} $res;
127 $content = "$code $msg"; 143 $content = "$code $msg";
128 $hdr->{"Content-Type"} = "text/plain"; 144 $hdr->{"Content-Type"} = "text/plain";
129 $hdr->{"Content-Length"} = length $content; 145 $hdr->{"Content-Length"} = length $content;
130 } 146 }
131 147
132 $self->slog($msg) if $code;
133
134 $self->print_response($code, $msg, $hdr, $content); 148 $self->response($code, $msg, $hdr, $content);
135 149
136 die bless {}, err::; 150 die bless {}, err::;
137} 151}
138 152
139sub err_blocked { 153sub err_blocked {
140 my $self = shift; 154 my $self = shift;
141 my $ip = $self->{remote_addr}; 155 my $ip = $self->{remote_addr};
142 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
143 $self->err(403, "too many connections", 157 $self->err(403, "too many connections",
158 {
159 "Content-Type" => "text/html",
144 { "Retry-After" => $::BLOCKTIME }, 160 "Retry-After" => $::BLOCKTIME
161 },
145 <<EOF); 162 <<EOF);
163<html><p>
146You have been blocked because you opened too many connections. You 164You have been blocked because you opened too many connections. You
147may retry at $time. Until then, 165may retry at</p>
148every new access will renew the block. 166
167 <p><blockquote>$time.</blockquote></p>
168
169<p>Until then, each new access will renew the block. You might want to have a
170look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
171</html>
149EOF 172EOF
150} 173}
151 174
152sub handle { 175sub handle {
153 my $self = shift; 176 my $self = shift;
213 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
214 237
215 weaken ($uri{$self->{uri}}{$self*1} = $self); 238 weaken ($uri{$self->{uri}}{$self*1} = $self);
216 239
217 $self->map_uri; 240 $self->map_uri;
218
219 Coro::Event::do_timer(after => 5);
220
221 $self->respond; 241 $self->respond;
222 #} 242 #}
223} 243}
224 244
225# uri => path mapping 245# uri => path mapping
239 259
240 $self->{name} = $uri; 260 $self->{name} = $uri;
241 261
242 # now do the path mapping 262 # now do the path mapping
243 $self->{path} = "$::DOCROOT/$host$uri"; 263 $self->{path} = "$::DOCROOT/$host$uri";
264
265 $self->access_check;
244} 266}
245 267
246sub server_address { 268sub server_address {
247 my $self = shift; 269 my $self = shift;
248 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 270 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
358 } 380 }
359 $hdr->{"Content-Range"} = "bytes */$length"; 381 $hdr->{"Content-Range"} = "bytes */$length";
360 $self->err(416, "not satisfiable", $hdr); 382 $self->err(416, "not satisfiable", $hdr);
361 383
362satisfiable: 384satisfiable:
385 # check for segmented downloads
386 if ($l && $NO_SEGMENTED) {
387 if (%{$uri{$self->{uri}}} > 1) {
388 $self->slog("segmented download refused\n");
389 $self->err(400, "segmented downloads are not allowed");
390 }
391 }
392
363 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 393 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
364 @code = (206, "partial content"); 394 @code = (206, "partial content");
365 $length = $h - $l + 1; 395 $length = $h - $l + 1;
366 396
367ignore: 397ignore:
368 } else { 398 } else {
369 ($l, $h) = (0, $length - 1); 399 ($l, $h) = (0, $length - 1);
370 } 400 }
371 401
372 if ($self->{path} =~ /\.html$/) { 402 $self->{path} =~ /\.([^.]+)$/;
373 $hdr->{"Content-Type"} = "text/html";
374 } else {
375 $hdr->{"Content-Type"} = "application/octet-stream"; 403 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
376 }
377
378 $hdr->{"Content-Length"} = $length; 404 $hdr->{"Content-Length"} = $length;
379 405
380 $self->print_response(@code, $hdr, ""); 406 $self->response(@code, $hdr, "");
381 407
382 if ($self->{method} eq "GET") { 408 if ($self->{method} eq "GET") {
383 my ($fh, $buf); 409 my ($fh, $buf);
384 open $fh, "<", $self->{path} 410 open $fh, "<", $self->{path}
385 or die "$self->{path}: late open failure ($!)"; 411 or die "$self->{path}: late open failure ($!)";
390 } 416 }
391 417
392 $h -= $l - 1; 418 $h -= $l - 1;
393 419
394 while ($h > 0) { 420 while ($h > 0) {
395 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 421 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
396 print {$self->{fh}} $buf 422 $self->{fh}->syswrite($buf)
397 or last; 423 or last;
398 } 424 }
399 } 425 }
400 426
401 close $fh; 427 close $fh;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines