Engine/Interface/Standalone.pm untested

filter: Statementstmt 0.0% 0 / 127 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 234 CC 21 SCAR 32.8
CC
21
Cov
0%
run
72.0
_handler
30.0
_parse_header
20.0
CRAP 26.5
1 package HTTP::Engine::Interface::Standalone;
2 use HTTP::Engine::Interface
3 builder => 'NoEnv',
4 writer => {
5 response_line => 1,
6 before => {
7 finalize => sub {
8 my($self, $req, $res) = @_;
9  
10 $res->headers->date(time);
11  
12 if ($req->_connection->{keepalive_available}) {
13 $res->headers->header( Connection => 'keep-alive' );
14 } else {
15 $res->headers->header( Connection => 'close' );
16 }
17 }
18 }
19 }
20 ;
21  
22  
23 use Socket qw(:all);
24 use IO::Socket::INET ();
25 use IO::Select ();
26  
27 BEGIN {
28 if ( $ENV{SMART_COMMENTS} ) {
29 Any::Moose::load_class('Smart::Comments');
30 Smart::Comments->import;
31 }
32 }
33  
34 has host => (
35 is => 'ro',
36 isa => 'Str',
37 default => '127.0.0.1',
38 );
39  
40 has port => (
41 is => 'ro',
42 isa => 'Int',
43 default => 1978,
44 );
45  
46 has keepalive => (
47 is => 'ro',
48 isa => 'Bool',
49 default => 0,
50 );
51  
52 has keepalive_timeout => (
53 is => 'ro',
54 isa => 'Int',
55 default => 5,
56 );
57  
58 # fixme add preforking support using Parallel::Prefork
59 has fork => (
60 is => 'ro',
61 isa => 'Bool',
62 default => 0,
63 );
64  
65 has allowed => (
66 is => 'rw',
67 isa => 'HashRef',
68 default => sub { { '127.0.0.1' => '255.255.255.255' } },
69 );
70  
71 has argv => (
72 is => 'ro',
73 isa => 'ArrayRef',
74 default => sub { [] },
75 );
76  
77 sub run {
78 my ( $self ) = @_;
79  
80 if ($self->keepalive && !$self->fork) {
81 Carp::croak "set fork=1 if you want to work with keepalive!";
82 }
83  
84 # Setup socket
85 my $daemon = IO::Socket::INET->new(
86 Listen => SOMAXCONN,
87 LocalAddr => $self->host,
88 LocalPort => $self->port,
89 Proto => 'tcp',
90 ReuseAddr => 1,
91 Type => SOCK_STREAM,
92 ) or die "Couldn't create daemon: $!";
93  
94 my $restart = 0;
95 my $parent = $$;
96 my $pid = undef;
97 local $SIG{CHLD} = 'IGNORE';
98  
99 ### start server
100 while (my ($remote, $peername) = $daemon->accept) {
101 ### accept : $remote->fileno
102 # TODO (Catalyst): get while ( my $remote = $daemon->accept ) to work
103 next unless my($method, $uri, $protocol) = $self->_parse_request_line($remote);
104 unless (uc $method eq 'RESTART') {
105 # Fork
106 next if $self->fork && ($pid = fork);
107 $self->_handler($remote, $method, $uri, $protocol, $peername);
108 if (defined $pid) {
109 $daemon->close;
110 exit();
111 }
112 } else {
113 ### RESTART
114 if ($self->_can_restart($peername)) {
115 $restart = 1;
116 last;
117 }
118 }
119 } continue {
120 close $remote;
121 }
122 $daemon->close;
123  
124 if ($restart) {
125 $SIG{CHLD} = 'DEFAULT';
126 wait;
127 exec $^X, $0, @{ $self->argv };
128 }
129  
130 exit;
131 }
132  
133 sub _handler {
134 my($self, $remote, $method, $uri, $protocol, $peername) = @_;
135  
136 # Ignore broken pipes as an HTTP server should
137 local $SIG{PIPE} = sub { close $remote };
138  
139 # We better be careful and just use 1.0
140 $protocol = '1.0'; # XXX I don't know about why this needed.
141  
142 my $select = IO::Select->new($remote);
143  
144 $remote->autoflush(1);
145  
146 while (1) {
147 # FIXME refactor an HTTP push parser
148  
149 my $headers = $self->_parse_header($remote, $protocol);
150  
151 my $connection = lc $headers->header("Connection");
152 ### connection: $connection
153  
154 my $keepalive_available = $self->keepalive
155 && index( $connection, 'keep-alive' ) > -1
156 ;
157 ### keepalive_available: $keepalive_available
158  
159 $self->_handle_one($remote, $method, $uri, $protocol, $peername, $headers, $keepalive_available);
160  
161 if ($keepalive_available) {
162 ### waiting keepalive timeout
163 last unless $select->can_read($self->keepalive_timeout);
164  
165 ### GO! keep alive!
166 last unless ($method, $uri, $protocol) = $self->_parse_request_line($remote, 1);
167 } else {
168 last;
169 }
170 }
171  
172 $remote->read(my $buf, 4096) if $select->can_read(0); # IE hack
173  
174 ### close connection
175 $remote->close();
176 }
177  
178 sub _parse_request_line {
179 my($self, $handle, $is_keepalive) = @_;
180  
181 # Parse request line
182 my $line = $self->_get_line($handle);
183 if ($is_keepalive && ($line eq '' || $line eq "\015")) {
184 $line = $self->_get_line($handle);
185 }
186 return ()
187 unless my($method, $uri, $protocol) =
188 $line =~ m/\A(\w+)\s+(\S+)(?:\s+HTTP\/(\d+(?:\.\d+)?))?\z/;
189 return ($method, $uri, $protocol);
190 }
191  
192 sub _peeraddr {
193 my ($self, $peername) = @_;
194  
195 my (undef, $iaddr) = sockaddr_in($peername);
196 return inet_ntoa($iaddr) || "127.0.0.1";
197 }
198  
199 sub _get_line {
200 my($self, $handle) = @_;
201  
202 # FIXME use bufferred but nonblocking IO? this is a lot of calls =(
203 my $line = '';
204 while ($handle->read(my $byte, 1)) {
205 last if $byte eq "\012"; # eol
206 $line .= $byte;
207 }
208  
209 # strip \r, \n was already stripped
210 $line =~ s/\015$//s;
211  
212 $line;
213 }
214  
215 # Parse headers
216 # taken from HTTP::Message, which is unfortunately not really reusable
217 sub _parse_header {
218 my ($self, $remote, $protocol) = @_;
219  
220 if ( $protocol >= 1 ) {
221 my @hdr;
222 while ( length( my $line = $self->_get_line($remote) ) ) {
223 if ( $line =~ s/^([^\s:]+)[ \t]*: ?(.*)// ) {
224 push( @hdr, $1, $2 );
225 }
226 elsif ( @hdr && $line =~ s/^([ \t].*)// ) {
227 $hdr[-1] .= "\n$1";
228 }
229 else {
230 last;
231 }
232 }
233 HTTP::Headers::Fast->new(@hdr);
234 }
235 else {
236 HTTP::Headers::Fast->new;
237 }
238 }
239  
240 sub _handle_one {
241 my($self, $remote, $method, $uri, $protocol, $peername, $headers, $keepalive_available) = @_;
242  
243 local *STDOUT = $remote;
244 $self->handle_request(
245 uri => URI::WithBase->new(
246 do {
247 my $u = URI->new($uri);
248 $u->scheme('http');
249 $u->host($headers->header('Host') || $self->host);
250 $u->port($self->port);
251 $u->path('/') if $uri =~ m!^https?://!i;
252 my $b = $u->clone;
253 $b->path_query('/');
254 ($u, $b);
255 },
256 ),
257 headers => $headers,
258 _connection => {
259 input_handle => $remote,
260 output_handle => $remote,
261 env => {},
262 keepalive_available => $keepalive_available,
263 },
264 connection_info => {
265 method => $method,
266 address => $self->_peeraddr($peername),
267 port => $self->port,
268 protocol => "HTTP/$protocol",
269 user => undef,
270 _https_info => undef,
271 request_uri => $uri,
272 },
273 );
274 }
275  
276 sub _can_restart {
277 my ($self, $peername) = @_;
278  
279 my $peeraddr = _inet_addr($self->_peeraddr($peername));
280 my $allowed = $self->allowed;
281 for my $ip (keys %{ $allowed }) {
282 my $mask = $allowed->{$ip};
283 if (($peeraddr & _inet_addr($mask)) == _inet_addr($ip)) {
284 return 1
285 }
286 }
287 return 0;
288 }
289  
290 sub _inet_addr { unpack "N*", inet_aton($_[0]) }
291  
292 __INTERFACE__
293  
294 __END__