… | |
… | |
42 | my @pool; |
42 | my @pool; |
43 | |
43 | |
44 | # one "execution thread" |
44 | # one "execution thread" |
45 | sub handler { |
45 | sub 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 | |
|
|
61 | sub 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 | |
62 | my $http_port = new Coro::Socket |
83 | my $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 | |
69 | push @listen_sockets, $http_port; |
90 | listen_on $http_port; |
|
|
91 | |
|
|
92 | if ($SERVER_PORT2) { |
|
|
93 | my $http_port = new Coro::Socket |
|
|
94 | LocalAddr => $SERVER_HOST, |
|
|
95 | LocalPort => $SERVER_PORT2, |
|
|
96 | ReuseAddr => 1, |
|
|
97 | Listen => 50, |
|
|
98 | or die "unable to start server"; |
|
|
99 | |
|
|
100 | listen_on $http_port; |
|
|
101 | } |
70 | |
102 | |
71 | our $NOW; |
103 | our $NOW; |
72 | our $HTTP_NOW; |
104 | our $HTTP_NOW; |
73 | |
105 | |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
106 | Event->timer(interval => 1, hard => 1, cb => sub { |
75 | $NOW = time; |
107 | $NOW = time; |
76 | $HTTP_NOW = time2str $NOW; |
108 | $HTTP_NOW = time2str $NOW; |
77 | })->now; |
109 | })->now; |
78 | |
|
|
79 | # the "main thread" |
|
|
80 | async { |
|
|
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 | |
110 | |
95 | package conn; |
111 | package conn; |
96 | |
112 | |
97 | use Socket; |
113 | use Socket; |
98 | use HTTP::Date; |
114 | use HTTP::Date; |
… | |
… | |
125 | |
141 | |
126 | read_mimetypes; |
142 | read_mimetypes; |
127 | |
143 | |
128 | sub new { |
144 | sub new { |
129 | my $class = shift; |
145 | my $class = shift; |
|
|
146 | my $fh = shift; |
130 | my $peername = shift; |
147 | my $peername = shift; |
131 | my $fh = shift; |
|
|
132 | my $self = bless { fh => $fh }, $class; |
148 | my $self = bless { fh => $fh }, $class; |
133 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
149 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
134 | or $self->err(500, "unable to decode peername"); |
150 | or $self->err(500, "unable to decode peername"); |
135 | |
151 | |
136 | $self->{remote_addr} = inet_ntoa $iaddr; |
152 | $self->{remote_addr} = inet_ntoa $iaddr; |
… | |
… | |
141 | $self; |
157 | $self; |
142 | } |
158 | } |
143 | |
159 | |
144 | sub DESTROY { |
160 | sub DESTROY { |
145 | my $self = shift; |
161 | my $self = shift; |
146 | |
|
|
147 | $::conns--; |
162 | $::conns--; |
148 | |
|
|
149 | $self->eoconn; |
163 | $self->eoconn; |
150 | } |
164 | } |
151 | |
165 | |
152 | # end of connection |
166 | # end of connection |
153 | sub eoconn { |
167 | sub eoconn { |
… | |
… | |
165 | |
179 | |
166 | sub response { |
180 | sub response { |
167 | my ($self, $code, $msg, $hdr, $content) = @_; |
181 | my ($self, $code, $msg, $hdr, $content) = @_; |
168 | my $res = "HTTP/1.1 $code $msg\015\012"; |
182 | my $res = "HTTP/1.1 $code $msg\015\012"; |
169 | |
183 | |
170 | $self->{h}{connection} ||= $hdr->{Connection}; |
184 | $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; |
171 | |
185 | |
172 | $res .= "Date: $HTTP_NOW\015\012"; |
186 | $res .= "Date: $HTTP_NOW\015\012"; |
173 | |
187 | |
174 | while (my ($h, $v) = each %$hdr) { |
188 | while (my ($h, $v) = each %$hdr) { |
175 | $res .= "$h: $v\015\012" |
189 | $res .= "$h: $v\015\012" |
… | |
… | |
300 | |
314 | |
301 | if (defined $host) { |
315 | if (defined $host) { |
302 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
316 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
303 | } else { |
317 | } else { |
304 | ($self->{server_port}, $host) |
318 | ($self->{server_port}, $host) |
305 | = unpack_sockaddr_in $self->{fh}->getsockname |
319 | = unpack_sockaddr_in $self->{fh}->sockname |
306 | or $self->err(500, "unable to get socket name"); |
320 | or $self->err(500, "unable to get socket name"); |
307 | $host = inet_ntoa $host; |
321 | $host = inet_ntoa $host; |
308 | } |
322 | } |
309 | |
323 | |
310 | $self->{server_name} = $host; |
324 | $self->{server_name} = $host; |