… | |
… | |
34 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
34 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
35 | our $httpevent = new Coro::Signal; |
35 | our $httpevent = new Coro::Signal; |
36 | |
36 | |
37 | our $wait_factor = 0.95; |
37 | our $wait_factor = 0.95; |
38 | |
38 | |
39 | our @transfers = ( |
39 | our $queue_small = new transferqueue $MAX_TRANSFERS_SMALL; |
40 | (new Coro::Semaphore $MAX_TRANSFERS_SMALL), |
40 | our $queue_large = new transferqueue $MAX_TRANSFERS_LARGE; |
41 | (new Coro::Semaphore $MAX_TRANSFERS_LARGE), |
41 | our $queue_index = new transferqueue 5; |
42 | ); |
|
|
43 | |
42 | |
44 | my @newcons; |
43 | my @newcons; |
45 | my @pool; |
44 | my @pool; |
46 | |
45 | |
47 | # one "execution thread" |
46 | # one "execution thread" |
… | |
… | |
150 | my $peername = shift; |
149 | my $peername = shift; |
151 | my $self = bless { fh => $fh }, $class; |
150 | my $self = bless { fh => $fh }, $class; |
152 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
151 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
153 | or $self->err(500, "unable to decode peername"); |
152 | or $self->err(500, "unable to decode peername"); |
154 | |
153 | |
|
|
154 | $self->{remote_addr} = |
155 | $self->{remote_addr} = inet_ntoa $iaddr; |
155 | $self->{remote_id} = inet_ntoa $iaddr; |
156 | $self->{time} = $::NOW; |
156 | $self->{time} = $::NOW; |
157 | |
157 | |
158 | weaken ($Coro::current->{conn} = $self); |
158 | weaken ($Coro::current->{conn} = $self); |
159 | |
159 | |
160 | $::conns++; |
160 | $::conns++; |
|
|
161 | $::maxconns = $::conns if $::conns > $::maxconns; |
161 | |
162 | |
162 | $self; |
163 | $self; |
163 | } |
164 | } |
164 | |
165 | |
165 | sub DESTROY { |
166 | sub DESTROY { |
… | |
… | |
179 | $httpevent->broadcast; |
180 | $httpevent->broadcast; |
180 | } |
181 | } |
181 | |
182 | |
182 | sub slog { |
183 | sub slog { |
183 | my $self = shift; |
184 | my $self = shift; |
184 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
185 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
185 | } |
186 | } |
186 | |
187 | |
187 | sub response { |
188 | sub response { |
188 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my $res = "HTTP/1.1 $code $msg\015\012"; |
190 | my $res = "HTTP/1.1 $code $msg\015\012"; |
… | |
… | |
210 | $res .= "\015\012"; |
211 | $res .= "\015\012"; |
211 | |
212 | |
212 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | |
214 | |
214 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
215 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
215 | " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
216 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
|
|
217 | " \"$self->{h}{referer}\"\n"; |
216 | |
218 | |
217 | print $accesslog $log if $accesslog; |
219 | print $accesslog $log if $accesslog; |
218 | print STDERR $log; |
220 | print STDERR $log; |
219 | |
221 | |
220 | $self->{written} += |
222 | $self->{written} += |
… | |
… | |
446 | or $self->err(304, "not modified"); |
448 | or $self->err(304, "not modified"); |
447 | |
449 | |
448 | if (-r "$path/index.html") { |
450 | if (-r "$path/index.html") { |
449 | # replace directory "size" by index.html filesize |
451 | # replace directory "size" by index.html filesize |
450 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
452 | $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7]; |
451 | $self->handle_file; |
453 | $self->handle_file($queue_index); |
452 | } else { |
454 | } else { |
453 | $self->handle_dir; |
455 | $self->handle_dir; |
454 | } |
456 | } |
455 | } |
457 | } |
456 | } elsif (-f _ && -r _) { |
458 | } elsif (-f _ && -r _) { |
457 | -x _ and $self->err(403, "forbidden"); |
459 | -x _ and $self->err(403, "forbidden"); |
458 | $self->handle_file; |
460 | $self->handle_file(-s _ >= $::TRANSFER_SMALL ? $queue_large : $queue_small); |
459 | } else { |
461 | } else { |
460 | $self->err(404, "not found"); |
462 | $self->err(404, "not found"); |
461 | } |
463 | } |
462 | } |
464 | } |
463 | } |
465 | } |
… | |
… | |
474 | }, |
476 | }, |
475 | $idx); |
477 | $idx); |
476 | } |
478 | } |
477 | |
479 | |
478 | sub handle_file { |
480 | sub handle_file { |
479 | my $self = shift; |
481 | my ($self, $queue) = @_; |
480 | my $length = $self->{stat}[7]; |
482 | my $length = $self->{stat}[7]; |
481 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
|
|
482 | my $hdr = { |
483 | my $hdr = { |
483 | "Last-Modified" => time2str ((stat _)[9]), |
484 | "Last-Modified" => time2str ((stat _)[9]), |
484 | }; |
485 | }; |
485 | |
486 | |
486 | my @code = (200, "ok"); |
487 | my @code = (200, "ok"); |
… | |
… | |
546 | if ($l) { |
547 | if ($l) { |
547 | sysseek $fh, $l, 0; |
548 | sysseek $fh, $l, 0; |
548 | } |
549 | } |
549 | } |
550 | } |
550 | |
551 | |
551 | my $transfer; # transfer guard |
552 | my $transfer = $queue->start_transfer; |
|
|
553 | my $locked; |
552 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
554 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
553 | |
555 | |
554 | $self->{time} = $::NOW; |
|
|
555 | |
|
|
556 | while ($h > 0) { |
556 | while ($h > 0) { |
557 | unless ($transfer) { |
557 | unless ($locked) { |
558 | if ($transfer ||= $queue->timed_guard($::WAIT_INTERVAL)) { |
558 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
559 | $bufsize = $::BUFSIZE; |
559 | $bufsize = $::BUFSIZE; |
560 | $self->{time} = $::NOW; |
560 | $self->{time} = $::NOW; |
561 | } |
561 | } |
562 | } |
562 | } |
563 | |
563 | |