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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines