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.3 by root, Fri Aug 10 02:28:28 2001 UTC vs.
Revision 1.4 by root, Fri Aug 10 04:16:41 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;
47 schedule; 40 schedule;
48 } 41 }
49 } 42 }
50} 43}
51 44
45my $http_port = new Coro::Socket
46 LocalAddr => $SERVER_HOST,
47 LocalPort => $SERVER_PORT,
48 ReuseAddr => 1,
49 Listen => 1,
50 or die "unable to start server";
51
52push @listen_sockets, $http_port;
53
52# the "main thread" 54# the "main thread"
53async { 55async {
54 slog 1, "accepting connections"; 56 slog 1, "accepting connections";
55 while () { 57 while () {
56 $connections->down; 58 $connections->down;
57 push @fh, $port->accept; 59 push @fh, $http_port->accept;
58 #slog 3, "accepted @$connections ".scalar(@pool); 60 #slog 3, "accepted @$connections ".scalar(@pool);
59 $::NOW = time; 61 $::NOW = time;
60 if (@pool) { 62 if (@pool) {
61 (pop @pool)->ready; 63 (pop @pool)->ready;
62 } else { 64 } else {
63 async \&handler; 65 async \&handler;
64 } 66 }
65 67
66 } 68 }
67}; 69};
68
69loop;
70print "ende\n";#d#
71 70
72package conn; 71package conn;
73 72
74use Socket; 73use Socket;
75use HTTP::Date; 74use HTTP::Date;
87 $self->{remote_addr} = inet_ntoa $iaddr; 86 $self->{remote_addr} = inet_ntoa $iaddr;
88 87
89 # enter ourselves into various lists 88 # enter ourselves into various lists
90 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
91 90
92 print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n";
93 $self; 91 $self;
94} 92}
95 93
96sub DESTROY { 94sub DESTROY {
97 my $self = shift; 95 my $self = shift;
98 delete $conn{$self->{remote_addr}}{$self*1}; 96 delete $conn{$self->{remote_addr}}{$self*1};
99 delete $uri{$self->{uri}}{$self*1}; 97 delete $uri{$self->{uri}}{$self*1};
100} 98}
101 99
102sub slog { 100sub slog {
103 main::slog(@_); 101 my $self = shift;
102 main::slog($_[0], "$self->{remote_addr}> $_[1]");
104} 103}
105 104
106sub print_response { 105sub response {
107 my ($self, $code, $msg, $hdr, $content) = @_; 106 my ($self, $code, $msg, $hdr, $content) = @_;
108 my $res = "HTTP/1.0 $code $msg\015\012"; 107 my $res = "HTTP/1.0 $code $msg\015\012";
109 108
110 $hdr->{Date} = time2str $::NOW; # slow? nah. 109 $res .= "Connection: close\015\012";
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 111
112 while (my ($h, $v) = each %$hdr) { 112 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 113 $res .= "$h: $v\015\012"
114 } 114 }
115
115 $res .= "\015\012$content" if defined $content; 116 $res .= "\015\012$content" if defined $content;
116 117
117 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
118 119
119 print {$self->{fh}} $res; 120 print {$self->{fh}} $res;
127 $content = "$code $msg"; 128 $content = "$code $msg";
128 $hdr->{"Content-Type"} = "text/plain"; 129 $hdr->{"Content-Type"} = "text/plain";
129 $hdr->{"Content-Length"} = length $content; 130 $hdr->{"Content-Length"} = length $content;
130 } 131 }
131 132
132 $self->slog($msg) if $code;
133
134 $self->print_response($code, $msg, $hdr, $content); 133 $self->response($code, $msg, $hdr, $content);
135 134
136 die bless {}, err::; 135 die bless {}, err::;
137} 136}
138 137
139sub err_blocked { 138sub err_blocked {
140 my $self = shift; 139 my $self = shift;
141 my $ip = $self->{remote_addr}; 140 my $ip = $self->{remote_addr};
142 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
143 $self->err(403, "too many connections", 142 $self->err(403, "too many connections",
143 {
144 "Content-Type" => "text/html",
144 { "Retry-After" => $::BLOCKTIME }, 145 "Retry-After" => $::BLOCKTIME
146 },
145 <<EOF); 147 <<EOF);
148<html><p>
146You have been blocked because you opened too many connections. You 149You have been blocked because you opened too many connections. You
147may retry at $time. Until then, 150may retry at</p>
148every new access will renew the block. 151
152 <p><blockquote>$time.</blockquote></p>
153
154<p>Until then, each new access will renew the block. You might want to have a
155look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
156</html>
149EOF 157EOF
150} 158}
151 159
152sub handle { 160sub handle {
153 my $self = shift; 161 my $self = shift;
358 } 366 }
359 $hdr->{"Content-Range"} = "bytes */$length"; 367 $hdr->{"Content-Range"} = "bytes */$length";
360 $self->err(416, "not satisfiable", $hdr); 368 $self->err(416, "not satisfiable", $hdr);
361 369
362satisfiable: 370satisfiable:
371 # check for segmented downloads
372 if ($l && $NO_SEGMENTED) {
373 if (%{$uri{$self->{uri}}} > 1) {
374 $self->slog("segmented download refused\n");
375 $self->err(400, "segmented downloads are not allowed");
376 }
377 }
378
363 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 379 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
364 @code = (206, "partial content"); 380 @code = (206, "partial content");
365 $length = $h - $l + 1; 381 $length = $h - $l + 1;
366 382
367ignore: 383ignore:
375 $hdr->{"Content-Type"} = "application/octet-stream"; 391 $hdr->{"Content-Type"} = "application/octet-stream";
376 } 392 }
377 393
378 $hdr->{"Content-Length"} = $length; 394 $hdr->{"Content-Length"} = $length;
379 395
380 $self->print_response(@code, $hdr, ""); 396 $self->response(@code, $hdr, "");
381 397
382 if ($self->{method} eq "GET") { 398 if ($self->{method} eq "GET") {
383 my ($fh, $buf); 399 my ($fh, $buf);
384 open $fh, "<", $self->{path} 400 open $fh, "<", $self->{path}
385 or die "$self->{path}: late open failure ($!)"; 401 or die "$self->{path}: late open failure ($!)";
390 } 406 }
391 407
392 $h -= $l - 1; 408 $h -= $l - 1;
393 409
394 while ($h > 0) { 410 while ($h > 0) {
395 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 411 $h -= sysread $fh, $buf, $h > 16384 ? 16384 : $h;
396 print {$self->{fh}} $buf 412 print {$self->{fh}} $buf
397 or last; 413 or last;
414 cede;
398 } 415 }
399 } 416 }
400 417
401 close $fh; 418 close $fh;
402} 419}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines