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.2 by root, Thu Aug 9 19:11:13 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);
60 $::NOW = time;
59 if (@pool) { 61 if (@pool) {
60 (pop @pool)->ready; 62 (pop @pool)->ready;
61 } else { 63 } else {
62 async \&handler; 64 async \&handler;
63 } 65 }
64 66
65 } 67 }
66}; 68};
67 69
68loop;
69print "ende\n";#d#
70
71package conn; 70package conn;
72 71
73use Socket; 72use Socket;
74use HTTP::Date; 73use HTTP::Date;
75use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
76 75
77my %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
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}
78 92
79sub new { 93sub new {
80 my $class = shift; 94 my $class = shift;
95 my $peername = shift;
81 my $fh = shift; 96 my $fh = shift;
82 my $self = bless { fh => $fh }, $class; 97 my $self = bless { fh => $fh }, $class;
83 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 98 my (undef, $iaddr) = unpack_sockaddr_in $peername
84 or $self->err(500, "unable to get peername"); 99 or $self->err(500, "unable to decode peername");
100
85 $self->{remote_address} = inet_ntoa $iaddr; 101 $self->{remote_addr} = inet_ntoa $iaddr;
86 102
87 # enter ourselves into various lists 103 # enter ourselves into various lists
88 weaken ($conn{$self->{remote_address}}{$self*1} = $self); 104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
89 print $self->{remote_address}.": ".($self*1)." > ".%{$conn{$self->{remote_address}}},"\n"; 105
90 $self; 106 $self;
91} 107}
92 108
93sub DESTROY { 109sub DESTROY {
94 my $self = shift; 110 my $self = shift;
95 delete $conn{$self->{remote_address}}{$self*1}; 111 delete $conn{$self->{remote_addr}}{$self*1};
112 delete $uri{$self->{uri}}{$self*1};
96} 113}
97 114
98sub slog { 115sub slog {
99 main::slog(@_); 116 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]");
100} 118}
101 119
102sub print_response { 120sub response {
103 my ($self, $code, $msg, $hdr, $content) = @_; 121 my ($self, $code, $msg, $hdr, $content) = @_;
104 my $res = "HTTP/1.0 $code $msg\015\012"; 122 my $res = "HTTP/1.0 $code $msg\015\012";
105 123
106 $hdr->{Date} = time2str time; # slow? nah. 124 $res .= "Connection: close\015\012";
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
107 126
108 while (my ($h, $v) = each %$hdr) { 127 while (my ($h, $v) = each %$hdr) {
109 $res .= "$h: $v\015\012" 128 $res .= "$h: $v\015\012"
110 } 129 }
130
111 $res .= "\015\012$content" if defined $content; 131 $res .= "\015\012$content" if defined $content;
112 132
113 print STDERR "$self->{remote_address} \"$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#
114 134
115 print {$self->{fh}} $res; 135 print {$self->{fh}} $res;
116} 136}
117 137
118sub err { 138sub err {
123 $content = "$code $msg"; 143 $content = "$code $msg";
124 $hdr->{"Content-Type"} = "text/plain"; 144 $hdr->{"Content-Type"} = "text/plain";
125 $hdr->{"Content-Length"} = length $content; 145 $hdr->{"Content-Length"} = length $content;
126 } 146 }
127 147
128 $self->slog($msg) if $code;
129
130 $self->print_response($code, $msg, $hdr, $content); 148 $self->response($code, $msg, $hdr, $content);
131 149
132 die bless {}, err::; 150 die bless {}, err::;
151}
152
153sub err_blocked {
154 my $self = shift;
155 my $ip = $self->{remote_addr};
156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
157 $self->err(403, "too many connections",
158 {
159 "Content-Type" => "text/html",
160 "Retry-After" => $::BLOCKTIME
161 },
162 <<EOF);
163<html><p>
164You have been blocked because you opened too many connections. You
165may retry at</p>
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>
172EOF
133} 173}
134 174
135sub handle { 175sub handle {
136 my $self = shift; 176 my $self = shift;
137 my $fh = $self->{fh}; 177 my $fh = $self->{fh};
144 my $req = $fh->readline("\015\012\015\012"); 184 my $req = $fh->readline("\015\012\015\012");
145 $fh->timeout($::RES_TIMEOUT); 185 $fh->timeout($::RES_TIMEOUT);
146 186
147 defined $req or 187 defined $req or
148 $self->err(408, "request timeout"); 188 $self->err(408, "request timeout");
189
190 my $ip = $self->{remote_addr};
191
192 if ($blocked{$ip}) {
193 $self->err_blocked($blocked{$ip})
194 if $blocked{$ip} > $::NOW;
195
196 delete $blocked{$ip};
197 }
198
199 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
200 $self->slog("blocked ip $ip");
201 $self->err_blocked;
202 }
149 203
150 $req =~ /^(?:\015\012)? 204 $req =~ /^(?:\015\012)?
151 (GET|HEAD) \040+ 205 (GET|HEAD) \040+
152 ([^\040]+) \040+ 206 ([^\040]+) \040+
153 HTTP\/([0-9]+\.[0-9]+) 207 HTTP\/([0-9]+\.[0-9]+)
179 while ($h, $v) = each %hdr; 233 while ($h, $v) = each %hdr;
180 } 234 }
181 235
182 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
183 237
238 weaken ($uri{$self->{uri}}{$self*1} = $self);
239
184 $self->map_uri; 240 $self->map_uri;
185
186 Coro::Event::do_timer(after => 5);
187
188 $self->respond; 241 $self->respond;
189 #} 242 #}
190} 243}
191 244
192# uri => path mapping 245# uri => path mapping
206 259
207 $self->{name} = $uri; 260 $self->{name} = $uri;
208 261
209 # now do the path mapping 262 # now do the path mapping
210 $self->{path} = "$::DOCROOT/$host$uri"; 263 $self->{path} = "$::DOCROOT/$host$uri";
264
265 $self->access_check;
211} 266}
212 267
213sub server_address { 268sub server_address {
214 my $self = shift; 269 my $self = shift;
215 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 270 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
325 } 380 }
326 $hdr->{"Content-Range"} = "bytes */$length"; 381 $hdr->{"Content-Range"} = "bytes */$length";
327 $self->err(416, "not satisfiable", $hdr); 382 $self->err(416, "not satisfiable", $hdr);
328 383
329satisfiable: 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
330 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 393 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
331 @code = (206, "partial content"); 394 @code = (206, "partial content");
332 $length = $h - $l + 1; 395 $length = $h - $l + 1;
333 396
334ignore: 397ignore:
335 } else { 398 } else {
336 ($l, $h) = (0, $length - 1); 399 ($l, $h) = (0, $length - 1);
337 } 400 }
338 401
339 if ($self->{path} =~ /\.html$/) { 402 $self->{path} =~ /\.([^.]+)$/;
340 $hdr->{"Content-Type"} = "text/html";
341 } else {
342 $hdr->{"Content-Type"} = "application/octet-stream"; 403 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
343 }
344
345 $hdr->{"Content-Length"} = $length; 404 $hdr->{"Content-Length"} = $length;
346 405
347 $self->print_response(@code, $hdr, ""); 406 $self->response(@code, $hdr, "");
348 407
349 if ($self->{method} eq "GET") { 408 if ($self->{method} eq "GET") {
350 my ($fh, $buf); 409 my ($fh, $buf);
351 open $fh, "<", $self->{path} 410 open $fh, "<", $self->{path}
352 or die "$self->{path}: late open failure ($!)"; 411 or die "$self->{path}: late open failure ($!)";
357 } 416 }
358 417
359 $h -= $l - 1; 418 $h -= $l - 1;
360 419
361 while ($h > 0) { 420 while ($h > 0) {
362 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 421 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
363 print {$self->{fh}} $buf 422 $self->{fh}->syswrite($buf)
364 or last; 423 or last;
365 } 424 }
366 } 425 }
367 426
368 close $fh; 427 close $fh;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines