… | |
… | |
18 | printf "---: $format\n", @_; |
18 | printf "---: $format\n", @_; |
19 | } |
19 | } |
20 | |
20 | |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
21 | my $connections = new Coro::Semaphore $MAX_CONNECTS; |
22 | |
22 | |
23 | my @fh; |
23 | my @newcons; |
24 | my @pool; |
24 | my @pool; |
25 | |
25 | |
26 | # one "execution thread" |
26 | # one "execution thread" |
27 | sub handler { |
27 | sub handler { |
28 | while () { |
28 | while () { |
29 | my $fh = pop @fh; |
29 | my $new = pop @newcons; |
30 | if ($fh) { |
30 | if ($new) { |
31 | eval { |
31 | eval { |
32 | conn->new($fh)->handle; |
32 | conn->new(@$new)->handle; |
33 | }; |
33 | }; |
34 | close $fh; |
|
|
35 | slog 1, "$@" if $@ && !ref $@; |
34 | slog 1, "$@" if $@ && !ref $@; |
36 | $connections->up; |
35 | $connections->up; |
37 | } else { |
36 | } else { |
38 | last if @pool >= $MAX_POOL; |
37 | last if @pool >= $MAX_POOL; |
39 | push @pool, $Coro::current; |
38 | push @pool, $Coro::current; |
… | |
… | |
54 | # the "main thread" |
53 | # the "main thread" |
55 | async { |
54 | async { |
56 | slog 1, "accepting connections"; |
55 | slog 1, "accepting connections"; |
57 | while () { |
56 | while () { |
58 | $connections->down; |
57 | $connections->down; |
59 | push @fh, $http_port->accept; |
58 | push @newcons, [$http_port->accept]; |
60 | #slog 3, "accepted @$connections ".scalar(@pool); |
59 | #slog 3, "accepted @$connections ".scalar(@pool); |
61 | $::NOW = time; |
60 | $::NOW = time; |
62 | if (@pool) { |
61 | if (@pool) { |
63 | (pop @pool)->ready; |
62 | (pop @pool)->ready; |
64 | } else { |
63 | } else { |
… | |
… | |
74 | use HTTP::Date; |
73 | use HTTP::Date; |
75 | use Convert::Scalar 'weaken'; |
74 | use Convert::Scalar 'weaken'; |
76 | |
75 | |
77 | our %conn; # $conn{ip}{fh} => connobj |
76 | our %conn; # $conn{ip}{fh} => connobj |
78 | our %blocked; |
77 | our %blocked; |
|
|
78 | our %mimetype; |
|
|
79 | |
|
|
80 | sub read_mimetypes { |
|
|
81 | local *M; |
|
|
82 | if (open M, "<mimetypes") { |
|
|
83 | while (<M>) { |
|
|
84 | if (/^([^#]\S+)\t+(\S+)$/) { |
|
|
85 | $mimetype{lc $1} = $2; |
|
|
86 | } |
|
|
87 | } |
|
|
88 | } else { |
|
|
89 | $self->slog(1, "cannot open mimetypes\n"); |
|
|
90 | } |
|
|
91 | } |
79 | |
92 | |
80 | sub new { |
93 | sub new { |
81 | my $class = shift; |
94 | my $class = shift; |
|
|
95 | my $peername = shift; |
82 | my $fh = shift; |
96 | my $fh = shift; |
83 | my $self = bless { fh => $fh }, $class; |
97 | my $self = bless { fh => $fh }, $class; |
84 | my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername |
98 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
85 | or $self->err(500, "unable to get peername"); |
99 | or $self->err(500, "unable to decode peername"); |
|
|
100 | |
86 | $self->{remote_addr} = inet_ntoa $iaddr; |
101 | $self->{remote_addr} = inet_ntoa $iaddr; |
87 | |
102 | |
88 | # enter ourselves into various lists |
103 | # enter ourselves into various lists |
89 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
104 | weaken ($conn{$self->{remote_addr}}{$self*1} = $self); |
90 | |
105 | |
… | |
… | |
244 | |
259 | |
245 | $self->{name} = $uri; |
260 | $self->{name} = $uri; |
246 | |
261 | |
247 | # now do the path mapping |
262 | # now do the path mapping |
248 | $self->{path} = "$::DOCROOT/$host$uri"; |
263 | $self->{path} = "$::DOCROOT/$host$uri"; |
|
|
264 | |
|
|
265 | $self->access_check; |
249 | } |
266 | } |
250 | |
267 | |
251 | sub server_address { |
268 | sub server_address { |
252 | my $self = shift; |
269 | my $self = shift; |
253 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
270 | my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname |
… | |
… | |
380 | ignore: |
397 | ignore: |
381 | } else { |
398 | } else { |
382 | ($l, $h) = (0, $length - 1); |
399 | ($l, $h) = (0, $length - 1); |
383 | } |
400 | } |
384 | |
401 | |
385 | if ($self->{path} =~ /\.html$/) { |
402 | $self->{path} =~ /\.([^.]+)$/; |
386 | $hdr->{"Content-Type"} = "text/html"; |
|
|
387 | } else { |
|
|
388 | $hdr->{"Content-Type"} = "application/octet-stream"; |
403 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
389 | } |
|
|
390 | |
|
|
391 | $hdr->{"Content-Length"} = $length; |
404 | $hdr->{"Content-Length"} = $length; |
392 | |
405 | |
393 | $self->response(@code, $hdr, ""); |
406 | $self->response(@code, $hdr, ""); |
394 | |
407 | |
395 | if ($self->{method} eq "GET") { |
408 | if ($self->{method} eq "GET") { |