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.35 by root, Thu Aug 30 12:35:28 2001 UTC vs.
Revision 1.44 by root, Fri Sep 14 12:38:18 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],
42my @pool; 44my @pool;
43 45
44# one "execution thread" 46# one "execution thread"
45sub handler { 47sub handler {
46 while () { 48 while () {
47 my $new = pop @newcons;
48 if ($new) { 49 if (@newcons) {
49 eval { 50 eval {
50 conn->new(@$new)->handle; 51 conn->new(@{pop @newcons})->handle;
51 }; 52 };
52 slog 1, "$@" if $@ && !ref $@; 53 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 54 $connections->up;
54 } else { 55 } else {
55 last if @pool >= $MAX_POOL; 56 last if @pool >= $MAX_POOL;
57 schedule; 58 schedule;
58 } 59 }
59 } 60 }
60} 61}
61 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
62my $http_port = new Coro::Socket 85my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 86 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 87 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 88 ReuseAddr => 1,
66 Listen => 50, 89 Listen => 50,
67 or die "unable to start server"; 90 or die "unable to start server";
68 91
69push @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}
70 104
71our $NOW; 105our $NOW;
72our $HTTP_NOW; 106our $HTTP_NOW;
73 107
74Event->timer(interval => 1, hard => 1, cb => sub { 108Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 109 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 110 $HTTP_NOW = time2str $NOW;
77})->now; 111})->now;
78 112
79# the "main thread"
80async {
81 slog 1, "accepting connections";
82 while () {
83 $connections->down;
84 push @newcons, [$http_port->accept];
85 #slog 3, "accepted @$connections ".scalar(@pool);
86 if (@pool) {
87 (pop @pool)->ready;
88 } else {
89 async \&handler;
90 }
91
92 }
93};
94
95package conn; 113package conn;
96 114
97use Socket; 115use Socket;
98use HTTP::Date; 116use HTTP::Date;
99use Convert::Scalar 'weaken'; 117use Convert::Scalar 'weaken';
100use Linux::AIO; 118use Linux::AIO;
101 119
102Linux::AIO::min_parallel $::AIO_PARALLEL; 120Linux::AIO::min_parallel $::AIO_PARALLEL;
103
104my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
105 121
106Event->io(fd => Linux::AIO::poll_fileno, 122Event->io(fd => Linux::AIO::poll_fileno,
107 poll => 'r', async => 1, 123 poll => 'r', async => 1,
108 cb => \&Linux::AIO::poll_cb); 124 cb => \&Linux::AIO::poll_cb);
109 125
127 143
128read_mimetypes; 144read_mimetypes;
129 145
130sub new { 146sub new {
131 my $class = shift; 147 my $class = shift;
148 my $fh = shift;
132 my $peername = shift; 149 my $peername = shift;
133 my $fh = shift;
134 my $self = bless { fh => $fh }, $class; 150 my $self = bless { fh => $fh }, $class;
135 my (undef, $iaddr) = unpack_sockaddr_in $peername 151 my (undef, $iaddr) = unpack_sockaddr_in $peername
136 or $self->err(500, "unable to decode peername"); 152 or $self->err(500, "unable to decode peername");
137 153
138 $self->{remote_addr} = inet_ntoa $iaddr; 154 $self->{remote_addr} = inet_ntoa $iaddr;
139 $self->{time} = $::NOW; 155 $self->{time} = $::NOW;
140 156
141 # enter ourselves into various lists
142 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
143
144 $::conns++; 157 $::conns++;
145 158
146 $self; 159 $self;
147} 160}
148 161
149sub DESTROY { 162sub DESTROY {
150 my $self = shift; 163 my $self = shift;
151
152 $::conns--; 164 $::conns--;
153
154 $self->eoconn; 165 $self->eoconn;
155 delete $conn{$self->{remote_addr}}{$self*1};
156} 166}
157 167
158# end of connection 168# end of connection
159sub eoconn { 169sub eoconn {
160 my $self = shift; 170 my $self = shift;
171
172 # clean up hints
173 delete $conn{$self->{remote_id}}{$self*1};
161 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 174 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
175
176 $httpevent->broadcast;
162} 177}
163 178
164sub slog { 179sub slog {
165 my $self = shift; 180 my $self = shift;
166 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
168 183
169sub response { 184sub response {
170 my ($self, $code, $msg, $hdr, $content) = @_; 185 my ($self, $code, $msg, $hdr, $content) = @_;
171 my $res = "HTTP/1.1 $code $msg\015\012"; 186 my $res = "HTTP/1.1 $code $msg\015\012";
172 187
173 $self->{h}{connection} ||= $hdr->{Connection}; 188 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/;
174 189
175 $res .= "Date: $HTTP_NOW\015\012"; 190 $res .= "Date: $HTTP_NOW\015\012";
176 191
177 while (my ($h, $v) = each %$hdr) { 192 while (my ($h, $v) = each %$hdr) {
178 $res .= "$h: $v\015\012" 193 $res .= "$h: $v\015\012"
228 } 243 }
229 244
230 $self->{h} = {}; 245 $self->{h} = {};
231 246
232 $fh->timeout($::RES_TIMEOUT); 247 $fh->timeout($::RES_TIMEOUT);
233 my $ip = $self->{remote_addr};
234
235 if ($blocked{$ip}) {
236 $self->err_blocked($blocked{$ip})
237 if $blocked{$ip} > $::NOW;
238
239 delete $blocked{$ip};
240 }
241
242 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
243 my $delay = 120;
244 while (%{$conn{$ip}} > $::MAX_CONN_IP) {
245 if ($delay <= 0) {
246 $self->slog(2, "blocked ip $ip");
247 $self->err_blocked;
248 } else {
249 Coro::Event::do_timer(after => 3);
250 $delay -= 3;
251 }
252 }
253 }
254 248
255 $req =~ /^(?:\015\012)? 249 $req =~ /^(?:\015\012)?
256 (GET|HEAD) \040+ 250 (GET|HEAD) \040+
257 ([^\040]+) \040+ 251 ([^\040]+) \040+
258 HTTP\/([0-9]+\.[0-9]+) 252 HTTP\/([0-9]+\.[0-9]+)
283 277
284 $self->{h}{$h} = substr $v, 1 278 $self->{h}{$h} = substr $v, 1
285 while ($h, $v) = each %hdr; 279 while ($h, $v) = each %hdr;
286 } 280 }
287 281
282 # remote id should be unique per user
283 my $id = $self->{remote_addr};
284
285 if (exists $self->{h}{"client-ip"}) {
286 $id .= "[".$self->{h}{"client-ip"}."]";
287 } elsif (exists $self->{h}{"x-forwarded-for"}) {
288 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
289 }
290
291 $self->{remote_id} = $id;
292
293 if ($blocked{$id}) {
294 $self->err_blocked($blocked{$id})
295 if $blocked{$id} > $::NOW;
296
297 delete $blocked{$id};
298 }
299
300 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
301 my $delay = $::PER_TIMEOUT + $::NOW + 15;
302 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
303 if ($delay < $::NOW) {
304 $self->slog(2, "blocked ip $id");
305 $self->err_blocked;
306 } else {
307 $httpevent->wait;
308 }
309 }
310 }
311
288 # find out server name and port 312 # find out server name and port
289 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 313 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
290 $host = $1; 314 $host = $1;
291 } else { 315 } else {
292 $host = $self->{h}{host}; 316 $host = $self->{h}{host};
294 318
295 if (defined $host) { 319 if (defined $host) {
296 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 320 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
297 } else { 321 } else {
298 ($self->{server_port}, $host) 322 ($self->{server_port}, $host)
299 = unpack_sockaddr_in $self->{fh}->getsockname 323 = unpack_sockaddr_in $self->{fh}->sockname
300 or $self->err(500, "unable to get socket name"); 324 or $self->err(500, "unable to get socket name");
301 $host = inet_ntoa $host; 325 $host = inet_ntoa $host;
302 } 326 }
303 327
304 $self->{server_name} = $host; 328 $self->{server_name} = $host;
305 329
306 # remote id should be unique per user 330 # enter ourselves into various lists
307 $self->{remote_id} = $self->{remote_addr}; 331 weaken ($conn{$id}{$self*1} = $self);
308
309 if (exists $self->{h}{"client-ip"}) {
310 $self->{remote_id} .= "[".$self->{h}{"client-ip"}."]";
311 } elsif (exists $self->{h}{"x-forwarded-for"}) {
312 $self->{remote_id} .= "[".$self->{h}{"x-forwarded-for"}."]";
313 }
314
315 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 332 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
316 333
317 eval { 334 eval {
318 $self->map_uri; 335 $self->map_uri;
319 $self->respond; 336 $self->respond;
320 }; 337 };
322 $self->eoconn; 339 $self->eoconn;
323 340
324 die if $@ && !ref $@; 341 die if $@ && !ref $@;
325 342
326 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 343 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
344
345 $httpevent->broadcast;
327 346
328 $fh->timeout($::PER_TIMEOUT); 347 $fh->timeout($::PER_TIMEOUT);
329 } 348 }
330} 349}
331 350
457 $self->err(416, "not satisfiable", $hdr, ""); 476 $self->err(416, "not satisfiable", $hdr, "");
458 477
459satisfiable: 478satisfiable:
460 # check for segmented downloads 479 # check for segmented downloads
461 if ($l && $::NO_SEGMENTED) { 480 if ($l && $::NO_SEGMENTED) {
462 my $delay = 180; 481 my $delay = $::NOW + $::PER_TIMEOUT + 15;
463 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 482 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
464 if ($delay <= 0) { 483 if ($delay <= $::NOW) {
465 $self->err_segmented_download; 484 $self->err_segmented_download;
466 } else { 485 } else {
467 Coro::Event::do_timer(after => 3); $delay -= 3; 486 $httpevent->broadcast;
468 } 487 }
469 } 488 }
470 } 489 }
471 490
472 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 491 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
518 while ($h > 0) { 537 while ($h > 0) {
519 if (0) { 538 if (0) {
520 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 539 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
521 or last; 540 or last;
522 } else { 541 } else {
523 undef $buf;
524 $aio_requests->down;
525 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 542 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
526 $buf, 0, sub { 543 $buf, 0, sub {
527 $r = $_[0]; 544 $r = $_[0];
528 $current->ready; 545 Coro::ready($current);
529 }); 546 });
530 &Coro::schedule; 547 &Coro::schedule;
531 $aio_requests->up;
532 last unless $r; 548 last unless $r;
533 } 549 }
534 my $w = $self->{fh}->syswrite($buf) 550 my $w = syswrite $self->{fh}, $buf
535 or last; 551 or last;
536 $::written += $w; 552 $::written += $w;
537 $self->{written} += $w; 553 $self->{written} += $w;
538 $l += $r; 554 $l += $r;
539 } 555 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines