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.38 by root, Sun Sep 2 02:27:51 2001 UTC vs.
Revision 1.47 by root, Tue Nov 20 01:56:21 2001 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal;
5 6
6use HTTP::Date; 7use HTTP::Date;
8use POSIX ();
7 9
8no utf8; 10no utf8;
9use bytes; 11use bytes;
10 12
11# at least on my machine, this thingy serves files 13# at least on my machine, this thingy serves files
28 my $format = shift; 30 my $format = shift;
29 printf "---: $format\n", @_; 31 printf "---: $format\n", @_;
30} 32}
31 33
32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 34our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
35our $httpevent = new Coro::Signal;
33 36
34our $wait_factor = 0.95; 37our $wait_factor = 0.95;
35 38
36our @transfers = ( 39our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 40 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
56 schedule; 59 schedule;
57 } 60 }
58 } 61 }
59} 62}
60 63
64sub listen_on {
65 my $listen = $_[0];
66
67 push @listen_sockets, $listen;
68
69 # the "main thread"
70 async {
71 slog 1, "accepting connections";
72 while () {
73 $connections->down;
74 push @newcons, [$listen->accept];
75 #slog 3, "accepted @$connections ".scalar(@pool);
76 if (@pool) {
77 (pop @pool)->ready;
78 } else {
79 async \&handler;
80 }
81
82 }
83 };
84}
85
61my $http_port = new Coro::Socket 86my $http_port = new Coro::Socket
62 LocalAddr => $SERVER_HOST, 87 LocalAddr => $SERVER_HOST,
63 LocalPort => $SERVER_PORT, 88 LocalPort => $SERVER_PORT,
64 ReuseAddr => 1, 89 ReuseAddr => 1,
65 Listen => 50, 90 Listen => 50,
66 or die "unable to start server"; 91 or die "unable to start server";
67 92
68push @listen_sockets, $http_port; 93listen_on $http_port;
94
95if ($SERVER_PORT2) {
96 my $http_port = new Coro::Socket
97 LocalAddr => $SERVER_HOST,
98 LocalPort => $SERVER_PORT2,
99 ReuseAddr => 1,
100 Listen => 50,
101 or die "unable to start server";
102
103 listen_on $http_port;
104}
69 105
70our $NOW; 106our $NOW;
71our $HTTP_NOW; 107our $HTTP_NOW;
72 108
73Event->timer(interval => 1, hard => 1, cb => sub { 109Event->timer(interval => 1, hard => 1, cb => sub {
74 $NOW = time; 110 $NOW = time;
75 $HTTP_NOW = time2str $NOW; 111 $HTTP_NOW = time2str $NOW;
76})->now; 112})->now;
77
78# the "main thread"
79async {
80 slog 1, "accepting connections";
81 while () {
82 $connections->down;
83 push @newcons, [$http_port->accept];
84 #slog 3, "accepted @$connections ".scalar(@pool);
85 if (@pool) {
86 (pop @pool)->ready;
87 } else {
88 async \&handler;
89 }
90
91 }
92};
93 113
94package conn; 114package conn;
95 115
96use Socket; 116use Socket;
97use HTTP::Date; 117use HTTP::Date;
124 144
125read_mimetypes; 145read_mimetypes;
126 146
127sub new { 147sub new {
128 my $class = shift; 148 my $class = shift;
149 my $fh = shift;
129 my $peername = shift; 150 my $peername = shift;
130 my $fh = shift;
131 my $self = bless { fh => $fh }, $class; 151 my $self = bless { fh => $fh }, $class;
132 my (undef, $iaddr) = unpack_sockaddr_in $peername 152 my (undef, $iaddr) = unpack_sockaddr_in $peername
133 or $self->err(500, "unable to decode peername"); 153 or $self->err(500, "unable to decode peername");
134 154
135 $self->{remote_addr} = inet_ntoa $iaddr; 155 $self->{remote_addr} = inet_ntoa $iaddr;
140 $self; 160 $self;
141} 161}
142 162
143sub DESTROY { 163sub DESTROY {
144 my $self = shift; 164 my $self = shift;
145
146 $::conns--; 165 $::conns--;
147
148 $self->eoconn; 166 $self->eoconn;
149} 167}
150 168
151# end of connection 169# end of connection
152sub eoconn { 170sub eoconn {
153 my $self = shift; 171 my $self = shift;
154 172
155 # clean up hints 173 # clean up hints
156 delete $conn{$self->{remote_id}}{$self*1}; 174 delete $conn{$self->{remote_id}}{$self*1};
157 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; 175 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
176
177 $httpevent->broadcast;
158} 178}
159 179
160sub slog { 180sub slog {
161 my $self = shift; 181 my $self = shift;
162 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 182 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
164 184
165sub response { 185sub response {
166 my ($self, $code, $msg, $hdr, $content) = @_; 186 my ($self, $code, $msg, $hdr, $content) = @_;
167 my $res = "HTTP/1.1 $code $msg\015\012"; 187 my $res = "HTTP/1.1 $code $msg\015\012";
168 188
169 $self->{h}{connection} ||= $hdr->{Connection}; 189 if (exists $hdr->{Connection}) {
190 if ($hdr->{Connection} =~ /close/) {
191 $self->{h}{connection} = "close"
192 }
193 } else {
194 if ($self->{version} < 1.1) {
195 if ($self->{h}{connection} =~ /keep-alive/i) {
196 $hdr->{Connection} = "Keep-Alive";
197 } else {
198 $self->{h}{connection} = "close"
199 }
200 }
201 }
170 202
171 $res .= "Date: $HTTP_NOW\015\012"; 203 $res .= "Date: $HTTP_NOW\015\012";
172 204
173 while (my ($h, $v) = each %$hdr) { 205 while (my ($h, $v) = each %$hdr) {
174 $res .= "$h: $v\015\012" 206 $res .= "$h: $v\015\012"
175 } 207 }
176 $res .= "\015\012"; 208 $res .= "\015\012";
177 209
178 $res .= $content if defined $content and $self->{method} ne "HEAD"; 210 $res .= $content if defined $content and $self->{method} ne "HEAD";
179 211
212 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
180 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; 213 " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
181 214
182 print $accesslog $log if $accesslog; 215 print $accesslog $log if $accesslog;
183 print STDERR $log; 216 print STDERR $log;
184 217
185 $self->{written} += 218 $self->{written} +=
277 310
278 delete $blocked{$id}; 311 delete $blocked{$id};
279 } 312 }
280 313
281 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 314 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
282 my $delay = $::PER_TIMEOUT + 15; 315 my $delay = $::PER_TIMEOUT + $::NOW + 15;
283 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 316 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
284 if ($delay <= 0) { 317 if ($delay < $::NOW) {
285 $self->slog(2, "blocked ip $id"); 318 $self->slog(2, "blocked ip $id");
286 $self->err_blocked; 319 $self->err_blocked;
287 } else { 320 } else {
288 Coro::Event::do_timer(after => 4); $delay -= 4; 321 $httpevent->wait;
289 } 322 }
290 } 323 }
291 } 324 }
292 325
293 # find out server name and port 326 # find out server name and port
299 332
300 if (defined $host) { 333 if (defined $host) {
301 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 334 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
302 } else { 335 } else {
303 ($self->{server_port}, $host) 336 ($self->{server_port}, $host)
304 = unpack_sockaddr_in $self->{fh}->getsockname 337 = unpack_sockaddr_in $self->{fh}->sockname
305 or $self->err(500, "unable to get socket name"); 338 or $self->err(500, "unable to get socket name");
306 $host = inet_ntoa $host; 339 $host = inet_ntoa $host;
307 } 340 }
308 341
309 $self->{server_name} = $host; 342 $self->{server_name} = $host;
319 352
320 $self->eoconn; 353 $self->eoconn;
321 354
322 die if $@ && !ref $@; 355 die if $@ && !ref $@;
323 356
324 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 357 last if $self->{h}{connection} =~ /close/;
358
359 $httpevent->broadcast;
325 360
326 $fh->timeout($::PER_TIMEOUT); 361 $fh->timeout($::PER_TIMEOUT);
327 } 362 }
328} 363}
329 364
399 } else { 434 } else {
400 $ims < $self->{stat}[9] 435 $ims < $self->{stat}[9]
401 or $self->err(304, "not modified"); 436 or $self->err(304, "not modified");
402 437
403 if (-r "$path/index.html") { 438 if (-r "$path/index.html") {
404 $self->{path} .= "/index.html"; 439 # replace directory "size" by index.html filesize
440 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
405 $self->handle_file; 441 $self->handle_file;
406 } else { 442 } else {
407 $self->handle_dir; 443 $self->handle_dir;
408 } 444 }
409 } 445 }
421 457
422 $self->response(200, "ok", 458 $self->response(200, "ok",
423 { 459 {
424 "Content-Type" => "text/html", 460 "Content-Type" => "text/html",
425 "Content-Length" => length $idx, 461 "Content-Length" => length $idx,
462 "Last-Modified" => time2str ((stat _)[9]),
426 }, 463 },
427 $idx); 464 $idx);
428} 465}
429 466
430sub handle_file { 467sub handle_file {
455 $self->err(416, "not satisfiable", $hdr, ""); 492 $self->err(416, "not satisfiable", $hdr, "");
456 493
457satisfiable: 494satisfiable:
458 # check for segmented downloads 495 # check for segmented downloads
459 if ($l && $::NO_SEGMENTED) { 496 if ($l && $::NO_SEGMENTED) {
460 my $delay = $::PER_TIMEOUT + 15; 497 my $delay = $::NOW + $::PER_TIMEOUT + 15;
461 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 498 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
462 if ($delay <= 0) { 499 if ($delay <= $::NOW) {
463 $self->err_segmented_download; 500 $self->err_segmented_download;
464 } else { 501 } else {
465 Coro::Event::do_timer(after => 4); $delay -= 4; 502 $httpevent->wait;
466 } 503 }
467 } 504 }
468 } 505 }
469 506
470 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 507 $hdr->{"Content-Range"} = "bytes $l-$h/$length";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines