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.40 by root, Mon Sep 3 00:06:06 2001 UTC

42my @pool; 42my @pool;
43 43
44# one "execution thread" 44# one "execution thread"
45sub handler { 45sub handler {
46 while () { 46 while () {
47 my $new = pop @newcons;
48 if ($new) { 47 if (@newcons) {
49 eval { 48 eval {
50 conn->new(@$new)->handle; 49 conn->new(@{pop @newcons})->handle;
51 }; 50 };
52 slog 1, "$@" if $@ && !ref $@; 51 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 52 $connections->up;
54 } else { 53 } else {
55 last if @pool >= $MAX_POOL; 54 last if @pool >= $MAX_POOL;
57 schedule; 56 schedule;
58 } 57 }
59 } 58 }
60} 59}
61 60
61sub listen_on {
62 my $listen = $_[0];
63
64 push @listen_sockets, $listen;
65
66 # the "main thread"
67 async {
68 slog 1, "accepting connections";
69 while () {
70 $connections->down;
71 push @newcons, [$listen->accept];
72 #slog 3, "accepted @$connections ".scalar(@pool);
73 if (@pool) {
74 (pop @pool)->ready;
75 } else {
76 async \&handler;
77 }
78
79 }
80 };
81}
82
62my $http_port = new Coro::Socket 83my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 84 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 85 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 86 ReuseAddr => 1,
66 Listen => 50, 87 Listen => 50,
67 or die "unable to start server"; 88 or die "unable to start server";
68 89
69push @listen_sockets, $http_port; 90listen_on $http_port;
91
92my $http_port = new Coro::Socket
93 LocalAddr => $SERVER_HOST,
94 LocalPort => $SERVER_PORT2,
95 ReuseAddr => 1,
96 Listen => 50,
97 or die "unable to start server";
98
99listen_on $http_port;
70 100
71our $NOW; 101our $NOW;
72our $HTTP_NOW; 102our $HTTP_NOW;
73 103
74Event->timer(interval => 1, hard => 1, cb => sub { 104Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 105 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 106 $HTTP_NOW = time2str $NOW;
77})->now; 107})->now;
78 108
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; 109package conn;
96 110
97use Socket; 111use Socket;
98use HTTP::Date; 112use HTTP::Date;
99use Convert::Scalar 'weaken'; 113use Convert::Scalar 'weaken';
100use Linux::AIO; 114use Linux::AIO;
101 115
102Linux::AIO::min_parallel $::AIO_PARALLEL; 116Linux::AIO::min_parallel $::AIO_PARALLEL;
103
104my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
105 117
106Event->io(fd => Linux::AIO::poll_fileno, 118Event->io(fd => Linux::AIO::poll_fileno,
107 poll => 'r', async => 1, 119 poll => 'r', async => 1,
108 cb => \&Linux::AIO::poll_cb); 120 cb => \&Linux::AIO::poll_cb);
109 121
136 or $self->err(500, "unable to decode peername"); 148 or $self->err(500, "unable to decode peername");
137 149
138 $self->{remote_addr} = inet_ntoa $iaddr; 150 $self->{remote_addr} = inet_ntoa $iaddr;
139 $self->{time} = $::NOW; 151 $self->{time} = $::NOW;
140 152
141 # enter ourselves into various lists
142 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
143
144 $::conns++; 153 $::conns++;
145 154
146 $self; 155 $self;
147} 156}
148 157
149sub DESTROY { 158sub DESTROY {
150 my $self = shift; 159 my $self = shift;
151
152 $::conns--; 160 $::conns--;
153
154 $self->eoconn; 161 $self->eoconn;
155 delete $conn{$self->{remote_addr}}{$self*1};
156} 162}
157 163
158# end of connection 164# end of connection
159sub eoconn { 165sub eoconn {
160 my $self = shift; 166 my $self = shift;
167
168 # clean up hints
169 delete $conn{$self->{remote_id}}{$self*1};
161 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 170 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
162} 171}
163 172
164sub slog { 173sub slog {
165 my $self = shift; 174 my $self = shift;
166 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 175 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
228 } 237 }
229 238
230 $self->{h} = {}; 239 $self->{h} = {};
231 240
232 $fh->timeout($::RES_TIMEOUT); 241 $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 242
255 $req =~ /^(?:\015\012)? 243 $req =~ /^(?:\015\012)?
256 (GET|HEAD) \040+ 244 (GET|HEAD) \040+
257 ([^\040]+) \040+ 245 ([^\040]+) \040+
258 HTTP\/([0-9]+\.[0-9]+) 246 HTTP\/([0-9]+\.[0-9]+)
283 271
284 $self->{h}{$h} = substr $v, 1 272 $self->{h}{$h} = substr $v, 1
285 while ($h, $v) = each %hdr; 273 while ($h, $v) = each %hdr;
286 } 274 }
287 275
276 # remote id should be unique per user
277 my $id = $self->{remote_addr};
278
279 if (exists $self->{h}{"client-ip"}) {
280 $id .= "[".$self->{h}{"client-ip"}."]";
281 } elsif (exists $self->{h}{"x-forwarded-for"}) {
282 $id .= "[".$self->{h}{"x-forwarded-for"}."]";
283 }
284
285 $self->{remote_id} = $id;
286
287 if ($blocked{$id}) {
288 $self->err_blocked($blocked{$id})
289 if $blocked{$id} > $::NOW;
290
291 delete $blocked{$id};
292 }
293
294 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
295 my $delay = $::PER_TIMEOUT + 15;
296 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 if ($delay <= 0) {
298 $self->slog(2, "blocked ip $id");
299 $self->err_blocked;
300 } else {
301 Coro::Event::do_timer(after => 4); $delay -= 4;
302 }
303 }
304 }
305
288 # find out server name and port 306 # find out server name and port
289 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { 307 if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
290 $host = $1; 308 $host = $1;
291 } else { 309 } else {
292 $host = $self->{h}{host}; 310 $host = $self->{h}{host};
301 $host = inet_ntoa $host; 319 $host = inet_ntoa $host;
302 } 320 }
303 321
304 $self->{server_name} = $host; 322 $self->{server_name} = $host;
305 323
306 # remote id should be unique per user 324 # enter ourselves into various lists
307 $self->{remote_id} = $self->{remote_addr}; 325 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); 326 weaken ($uri{$id}{$self->{uri}}{$self*1} = $self);
316 327
317 eval { 328 eval {
318 $self->map_uri; 329 $self->map_uri;
319 $self->respond; 330 $self->respond;
320 }; 331 };
457 $self->err(416, "not satisfiable", $hdr, ""); 468 $self->err(416, "not satisfiable", $hdr, "");
458 469
459satisfiable: 470satisfiable:
460 # check for segmented downloads 471 # check for segmented downloads
461 if ($l && $::NO_SEGMENTED) { 472 if ($l && $::NO_SEGMENTED) {
462 my $delay = 180; 473 my $delay = $::PER_TIMEOUT + 15;
463 while (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 474 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
464 if ($delay <= 0) { 475 if ($delay <= 0) {
465 $self->err_segmented_download; 476 $self->err_segmented_download;
466 } else { 477 } else {
467 Coro::Event::do_timer(after => 3); $delay -= 3; 478 Coro::Event::do_timer(after => 4); $delay -= 4;
468 } 479 }
469 } 480 }
470 } 481 }
471 482
472 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 483 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
518 while ($h > 0) { 529 while ($h > 0) {
519 if (0) { 530 if (0) {
520 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 531 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
521 or last; 532 or last;
522 } else { 533 } else {
523 undef $buf;
524 $aio_requests->down;
525 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 534 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
526 $buf, 0, sub { 535 $buf, 0, sub {
527 $r = $_[0]; 536 $r = $_[0];
528 $current->ready; 537 Coro::ready($current);
529 }); 538 });
530 &Coro::schedule; 539 &Coro::schedule;
531 $aio_requests->up;
532 last unless $r; 540 last unless $r;
533 } 541 }
534 my $w = $self->{fh}->syswrite($buf) 542 my $w = syswrite $self->{fh}, $buf
535 or last; 543 or last;
536 $::written += $w; 544 $::written += $w;
537 $self->{written} += $w; 545 $self->{written} += $w;
538 $l += $r; 546 $l += $r;
539 } 547 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines