File Coverage

blib/lib/Any/Daemon/HTTP.pm
Criterion Covered Total %
statement 57 247 23.0
branch 0 104 0.0
condition 0 70 0.0
subroutine 19 41 46.3
pod 13 16 81.2
total 89 478 18.6


line stmt bran cond sub pod time code
1             # Copyrights 2013-2019 by [Mark Overmeer].
2             # For other contributors see ChangeLog.
3             # See the manual pages for details on the licensing terms.
4             # Pod stripped from pm file by OODoc 2.02.
5             # This code is part of distribution Any-Daemon-HTTP. Meta-POD processed
6             # with OODoc into POD and HTML manual-pages. See README.md
7             # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8              
9             package Any::Daemon::HTTP;
10 1     1   2232 use vars '$VERSION';
  1         2  
  1         41  
11             $VERSION = '0.29';
12              
13              
14 1     1   5 use Log::Report 'any-daemon-http';
  1         1  
  1         5  
15 1     1   170 use parent 'Any::Daemon';
  1         2  
  1         6  
16              
17 1     1   4629 use warnings;
  1         2  
  1         20  
18 1     1   4 use strict;
  1         2  
  1         15  
19              
20 1     1   398 use Any::Daemon::HTTP::VirtualHost ();
  1         3  
  1         31  
21 1     1   408 use Any::Daemon::HTTP::Session ();
  1         2  
  1         17  
22 1     1   6 use Any::Daemon::HTTP::Proxy ();
  1         2  
  1         13  
23              
24 1     1   451 use HTTP::Daemon ();
  1         18948  
  1         31  
25 1     1   10 use HTTP::Status qw/:constants :is/;
  1         2  
  1         374  
26 1     1   7 use Socket qw/inet_aton PF_INET AF_INET/;
  1         2  
  1         52  
27 1     1   5 use IO::Socket qw/SOCK_STREAM SOMAXCONN SOL_SOCKET SO_LINGER/;
  1         2  
  1         7  
28 1     1   70 use IO::Socket::IP ();
  1         2  
  1         11  
29 1     1   4 use IO::Select ();
  1         3  
  1         15  
30 1     1   4 use File::Basename qw/basename/;
  1         2  
  1         35  
31 1     1   5 use File::Spec ();
  1         2  
  1         13  
32 1     1   5 use Scalar::Util qw/blessed/;
  1         2  
  1         37  
33 1     1   5 use Errno qw/EADDRINUSE/;
  1         2  
  1         37  
34              
35             use constant # default ports
36 1         2670 { PORT_HTTP => 80
37             , PORT_HTTPS => 443
38 1     1   4 };
  1         1  
39              
40             # To support IPv6, replace ::INET by ::IP
41             @HTTP::Daemon::ClientConn::ISA = qw(IO::Socket::IP);
42              
43              
44 0 0   0     sub _to_list($) { ref $_[0] eq 'ARRAY' ? @{$_[0]} : defined $_[0] ? $_[0] : () }
  0 0          
45              
46             sub init($)
47 0     0 0   { my ($self, $args) = @_;
48 0           $self->SUPER::init($args);
49              
50 0   0       my $listen = $args->{listen} || $args->{socket} || $args->{host};
51 0           my (@sockets, @hosts);
52 0           foreach my $conn (_to_list $listen)
53             { my ($socket, @host) = $self->_create_socket($conn
54             , protocol => $args->{protocol}
55 0           );
56              
57 0 0         push @sockets, $socket if $socket;
58 0           push @hosts, @host;
59             }
60              
61 0 0         @sockets or error __x"host or socket required for {pkg}::new()"
62             , pkg => ref $self;
63              
64 0           $self->{ADH_sockets} = \@sockets;
65 0           $self->{ADH_hosts} = \@hosts;
66              
67             $self->{ADH_session_class}
68 0   0       = $args->{session_class} || 'Any::Daemon::HTTP::Session';
69             $self->{ADH_vhost_class}
70 0   0       = $args->{vhost_class} || 'Any::Daemon::HTTP::VirtualHost';
71             $self->{ADH_proxy_class}
72 0   0       = $args->{proxy_class} || 'Any::Daemon::HTTP::Proxy';
73              
74 0           $self->{ADH_vhosts} = {};
75 0   0       $self->addVirtualHost($_) for _to_list($args->{vhosts} || $args->{vhost});
76              
77 0           $self->{ADH_proxies} = [];
78 0   0       $self->addProxy($_) for _to_list($args->{proxies} || $args->{proxy});
79              
80             !$args->{docroot}
81 0 0         or error __x"docroot parameter has been removed in v0.11";
82              
83 0   0       $self->{ADH_server} = $args->{server_id} || basename($0);
84 0   0       $self->{ADH_headers} = $args->{standard_headers} || [];
85 0   0 0     $self->{ADH_error} = $args->{on_error} || sub { $_[1] };
  0            
86 0 0         $self->{ADH_show_ps} = exists $args->{show_in_ps} ? $args->{show_in_ps} : 1;
87              
88             # "handlers" is probably a common typo
89 0   0       my $handler = $args->{handlers} || $args->{handler};
90              
91 0           my $host = shift @hosts;
92             $self->addVirtualHost
93             ( name => $host
94             , aliases => [@hosts, 'default']
95             , documents => $args->{documents}
96             , handler => $handler
97 0 0 0       ) if $args->{documents} || $handler;
98              
99 0           $self;
100             }
101              
102             sub _create_socket($%)
103 0     0     { my ($self, $listen, %args) = @_;
104 0 0         defined $listen or return;
105              
106 0 0 0       return ($listen, $listen->sockhost.':'.$listen->sockport)
107             if blessed $listen && $listen->isa('IO::Socket');
108              
109 0 0         my $port = $listen =~ s/\:([0-9]+)$// ? $1 : PORT_HTTP;
110 0           my $host = $listen;
111             my $proto = $self->{ADH_protocol}
112 0   0       = $args{protocol} || ($port==PORT_HTTPS ? 'HTTPS' : 'HTTP');
113              
114 0           my $sock_class;
115 0 0         if($proto eq 'HTTPS')
    0          
    0          
116 0           { $sock_class = 'IO::Socket::SSL';
117 0 0         eval "require IO::Socket::SSL; require HTTP::Daemon::SSL" or panic $@;
118             }
119             elsif($proto eq 'HTTP')
120 0           { $sock_class = 'IO::Socket::IP';
121             }
122             elsif($proto eq 'FCGI')
123 0           { $sock_class = 'IO::Socket::IP';
124 0 0         eval "require Any::Daemon::FCGI" or panic $@;
125             }
126             else
127 0           { error __x"Unsupported protocol '{proto}'", proto => $proto;
128             }
129              
130             # Wait max 60 seconds to get the socket
131             # You should be able to reduce the time to wait by setting linger
132             # on the socket in the process which has opened the socket before.
133 0           my ($socket, $elapse);
134 0           foreach my $retry (1..60)
135 0           { $elapse = $retry -1;
136              
137 0           $socket = $sock_class->new
138             ( LocalHost => $host
139             , LocalPort => $port
140             , Listen => SOMAXCONN
141             , Reuse => 1
142             , Type => SOCK_STREAM
143             , Proto => 'tcp'
144             );
145              
146 0 0 0       last if $socket || $! != EADDRINUSE;
147              
148 0 0         notice __x"waiting for socket at {address} to become available"
149             , address => "$host:$port"
150             if $retry==1;
151              
152 0           sleep 1;
153             }
154              
155             $socket
156 0 0         or fault __x"cannot create socket at {address}"
157             , address => "$host:$port";
158              
159 0 0         notice __x"got socket after {secs} seconds", secs => $elapse
160             if $elapse;
161              
162 0           ($socket, "$listen:$port", $socket->sockhost.':'.$socket->sockport);
163             }
164              
165             #----------------
166              
167 0     0 1   sub sockets() { @{shift->{ADH_sockets}} }
  0            
168 0     0 1   sub hosts() { @{shift->{ADH_hosts}} }
  0            
169 0     0 1   sub protocol() { shift->{ADH_protocol} }
170              
171             #-------------
172              
173             sub addVirtualHost(@)
174 0     0 1   { my $self = shift;
175 0 0         my $config = @_ > 1 ? +{@_} : !defined $_[0] ? return : shift;
    0          
176              
177 0           my $vhost;
178 0 0 0       if(blessed $config && $config->isa('Any::Daemon::HTTP::VirtualHost'))
    0          
179 0           { $vhost = $config }
180             elsif(ref $config eq 'HASH')
181 0           { $vhost = $self->{ADH_vhost_class}->new($config) }
182 0           else { error __x"virtual host configuration not a valid object nor HASH" }
183              
184 0           info __x"adding virtual host {name}", name => $vhost->name;
185              
186             $self->{ADH_vhosts}{$_} = $vhost
187 0           for $vhost->name, $vhost->aliases;
188              
189 0           $vhost;
190             }
191              
192              
193             sub addProxy(@)
194 0     0 1   { my $self = shift;
195 0 0         my $config = @_ > 1 ? +{@_} : !defined $_[0] ? return : shift;
    0          
196 0           my $proxy;
197 0 0         if(UNIVERSAL::isa($config, 'Any::Daemon::HTTP::Proxy'))
    0          
198 0           { $proxy = $config }
199             elsif(UNIVERSAL::isa($config, 'HASH'))
200 0           { $proxy = $self->{ADH_proxy_class}->new($config) }
201 0           else { error __x"proxy configuration not a valid object nor HASH" }
202              
203 0 0         $proxy->forwardMap
204             or error __x"proxy {name} has no map, so needs inside vhost"
205             , name => $proxy->name;
206              
207 0           info __x"adding proxy {name}", name => $proxy->name;
208              
209 0           push @{$self->{ADH_proxies}}, $proxy;
  0            
210             }
211              
212              
213             sub removeVirtualHost($)
214 0     0 1   { my ($self, $id) = @_;
215 0 0 0       my $vhost = blessed $id && $id->isa('Any::Daemon::HTTP::VirtualHost')
216             ? $id : $self->virtualHost($id);
217 0 0         defined $vhost or return;
218              
219             delete $self->{ADH_vhosts}{$_}
220 0           for $vhost->name, $vhost->aliases;
221 0           $vhost;
222             }
223              
224              
225 0     0 1   sub virtualHost($) { $_[0]->{ADH_vhosts}{$_[1]} }
226              
227              
228 0     0 1   sub proxies() { @{shift->{ADH_proxies}} }
  0            
229              
230              
231             sub findProxy($$$)
232 0     0 1   { my ($self, $session, $req, $host) = @_;
233 0           my $uri = $req->uri->abs("http://$host");
234 0           foreach my $proxy ($self->proxies)
235 0 0         { my $mapped = $proxy->forwardRewrite($session, $req, $uri) or next;
236 0           return ($proxy, $mapped);
237             }
238              
239 0           ();
240             }
241              
242             #-------------------
243              
244             sub _connection($$)
245 0     0     { my ($self, $client, $args) = @_;
246              
247 0           my $nr_req = 0;
248 0   0       my $max_req = $args->{max_req_per_conn} ||= 100;
249 0           my $start = time;
250 0   0       my $deadline = $start + ($args->{max_time_per_conn} ||= 120);
251 0   0       my $bonus = $args->{req_time_bonus} //= 2;
252              
253 0           my $conn;
254 0 0         if($self->protocol eq 'FCGI')
255 0           { $args->{socket} = $client;
256 0           $conn = Any::Daemon::FCGI::ClientConn->new($args);
257             }
258             else
259             { # Ugly hack, steal HTTP::Daemon's HTTP/1.1 implementation
260 0 0         $conn = bless $client, $client->isa('IO::Socket::SSL')
261             ? 'HTTP::Daemon::ClientConn::SSL'
262             : 'HTTP::Daemon::ClientConn';
263              
264 0           ${*$conn}{httpd_daemon} = $self;
  0            
265             }
266              
267 0           my $ip = $client->peerhost;
268 0   0       my $host =
269             ( $client->sockdomain == PF_INET
270             ? gethostbyaddr inet_aton($ip), AF_INET
271             : undef
272             ) || $ip;
273              
274 0           my $session = $self->{ADH_session_class}->new;
275 0           $session->set(peer => { ip => $ip, host => $host });
276 0           info __x"new client from {host} on {ip}" , host => $host, ip => $ip;
277              
278 0           my $init_conn = $args->{new_connection};
279 0           $self->$init_conn($session);
280              
281             # Change title in ps-table
282 0 0         my $title = $0 =~ /^(\S+)/ ? basename($1) : $0;
283 0           $self->psTitle("$title http from $host");
284              
285             $SIG{ALRM} = sub {
286 0     0     notice __x"connection from {host} lasted too long, killed after {time%d} seconds"
287             , host => $host, time => $deadline - $start;
288 0           exit 0;
289 0           };
290              
291 0           alarm $deadline - time;
292 0           while(my $req = $conn->get_request)
293 0   0       { my $vhostn = $req->header('Host') || 'default';
294 0           my $vhost = $self->virtualHost($vhostn);
295              
296             # Fallback to vhost without specific port number
297 0 0 0       $vhost ||= $self->virtualHost($1)
298             if $vhostn =~ /(.*)\:[0-9]+$/;
299              
300 0           my $resp;
301 0 0         if($vhost)
    0          
    0          
302             { $self->{ADH_host_base}
303 0 0         = (ref($conn) =~ /SSL/ ? 'https' : 'http').'://'.$vhost->name;
304 0           $resp = $vhost->handleRequest($self, $session, $req);
305             }
306             elsif(my ($proxy, $where) = $self->findProxy($session, $req, $vhostn))
307 0           { $resp = $proxy->forwardRequest($session, $req, $where);
308             }
309             elsif(my $default = $self->virtualHost('default'))
310 0           { $resp = HTTP::Response->new(HTTP_TEMPORARY_REDIRECT);
311 0           $resp->header(Location => 'http://'.$default->name);
312             }
313             else
314 0           { $resp = HTTP::Response->new(HTTP_NOT_ACCEPTABLE,
315             "virtual host $vhostn is not available");
316             }
317              
318 0 0         unless($resp)
319 0           { notice __x"no response produced for {uri}", uri => $req->uri;
320 0           $resp = HTTP::Response->new(HTTP_SERVICE_UNAVAILABLE);
321             }
322              
323 0           $resp->push_header(@{$self->{ADH_headers}});
  0            
324 0           $resp->request($req);
325              
326             # No content, then produce something better than an empty page.
327 0 0         if(is_error($resp->code))
328 0           { $resp = $self->{ADH_error}->($self, $resp, $session, $req);
329 0 0         $resp->content or $resp->content($resp->status_line);
330             }
331 0           $deadline += $bonus;
332 0           alarm $deadline - time;
333              
334 0           my $close = $nr_req++ >= $max_req;
335              
336 0 0         $resp->header(Connection => ($close ? 'close' : 'open'));
337 0           $conn->send_response($resp);
338              
339 0 0         last if $close;
340             }
341              
342 0           alarm 0;
343 0           $nr_req;
344             }
345              
346             sub run(%)
347 0     0 1   { my ($self, %args) = @_;
348              
349 0   0       my $new_child = $args{new_child} || 'newChild';
350 0   0       $args{new_connection} ||= 'newConnection';
351              
352 0           my $vhosts = $self->{ADH_vhosts};
353 0 0         unless(keys %$vhosts)
354 0           { my ($host, @aliases) = $self->hosts;
355 0           $self->addVirtualHost(name => $host, aliases => ['default', @aliases]);
356             }
357              
358             # option handle_request is deprecated in 0.11
359 0 0         if(my $handler = delete $args{handle_request})
360 0           { my (undef, $first) = %$vhosts;
361 0           $first->addHandler('/' => $handler);
362             }
363              
364 0 0         my $title = $0 =~ /^(\S+)/ ? basename($1) : $0;
365              
366 0           my ($req_count, $conn_count) = (0, 0);
367 0   0       my $max_conn = $args{max_conn_per_child} || 10_000;
368 0 0         $max_conn = int(0.9 * $max_conn + rand(0.2 * $max_conn))
369             if $max_conn > 10;
370              
371 0   0       my $max_req = $args{max_req_per_child} || 100_000;
372 0           my $linger = $args{linger};
373              
374 0           $self->psTitle("$title manager\x00\x00");
375             $args{child_task} ||= sub {
376 0     0     $self->psTitle("$title not used yet");
377             # even with one port, we still select...
378 0           my $select = IO::Select->new($self->sockets);
379              
380 0           $self->$new_child($select);
381              
382             CONNECTION:
383 0           while(my @ready = $select->can_read)
384             {
385 0           foreach my $socket (@ready)
386 0 0         { my $client = $socket->accept or next;
387 0 0         $client->sockopt(SO_LINGER, (pack "II", 1, $linger))
388             if defined $linger;
389              
390 0           $self->psTitle("$title handling "
391             . $client->peerhost.":".$client->peerport . " at "
392             . $client->sockhost.':'.$client->sockport);
393              
394 0           $req_count += $self->_connection($client, \%args);
395 0           $client->close;
396              
397             last CONNECTION
398 0 0 0       if $conn_count++ >= $max_conn
399             || $req_count >= $max_req;
400             }
401 0           $self->psTitle("$title idle after $conn_count");
402             }
403 0           0;
404 0   0       };
405              
406 0           info __x"start running the webserver";
407 0           $self->SUPER::run(%args);
408             }
409              
410              
411             sub newConnection($)
412 0     0 1   { my ($self, $session) = @_;
413 0           return $self;
414             }
415              
416              
417             sub newChild($)
418 0     0 1   { my ($self, $select) = @_;
419 0           return $self;
420             }
421              
422              
423             sub psTitle($)
424 0     0 1   { my ($self, $string) = @_;
425 0 0         $0 = $string if $self->{ADH_show_ps};
426             }
427              
428             # HTTP::Daemon methods used by ::ClientConn. We steal that parent role,
429             # but need to mimic the object a little. The names are not compatible
430             # with MarkOv's convention, so hidden for the users of this module
431 0     0 0   sub url() { shift->{ADH_host_base} }
432 0     0 0   sub product_tokens() {shift->{ADH_server}}
433              
434             1;
435              
436             __END__