File Coverage

blib/lib/Net/Server/MultiType.pm
Criterion Covered Total %
statement 35 51 68.6
branch 9 30 30.0
condition 3 10 30.0
subroutine 6 7 85.7
pod 1 3 33.3
total 54 101 53.4


line stmt bran cond sub pod time code
1             # -*- perl -*-
2             #
3             # Net::Server::MultiType - Net::Server personality
4             #
5             # Copyright (C) 2001-2022
6             #
7             # Paul Seamons
8             #
9             # This package may be distributed under the terms of either the
10             # GNU General Public License
11             # or the
12             # Perl Artistic License
13             #
14             # All rights reserved.
15             #
16             ################################################################
17              
18             package Net::Server::MultiType;
19              
20 6     6   13338 use strict;
  6         12  
  6         292  
21 6     6   48 use Carp qw(croak);
  6         10  
  6         552  
22 6     6   32 use base qw(Net::Server);
  6         12  
  6         5348  
23              
24             #sub net_server_type { shift->SUPER::net_server_type }; # not-needed
25              
26             sub options {
27 6     6 0 10 my $self = shift;
28 6         86 my $ref = $self->SUPER::options(@_);
29 6   100     125 $ref->{'server_type'} = $self->{'server'}->{'server_type'} ||= [];
30 6         23 return $ref;
31             }
32              
33 0     0 0 0 sub default_server_type { 'Fork' }
34              
35             sub run {
36 3 50   3 1 15718 my $self = ref($_[0]) ? shift() : shift->new;
37 3 50       663 $self->{'server'}->{'_run_args'} = [@_ == 1 ? %{$_[0]} : @_];
  0         0  
38 3         353 $self->_initialize;
39 3         26 my $prop = $self->{'server'};
40              
41 3 50 33     136 if (!defined $prop->{'server_type'} || ! @{ $prop->{'server_type'} }) {
  3         16  
42 0 0 0     0 if (my $ref = $self->can('default_server_type') && $self->default_server_type) {
43 0 0       0 $prop->{'server_type'} = ref($ref) ? $ref : [$ref];
44             }
45             }
46 3 50       6 foreach my $type (@{ $prop->{'server_type'} || []}) {
  3         22  
47 3 50       34 next if $type eq 'MultiType';
48 3 50       41 $type = ($type =~ /^(\w+)$/) ? $1 : next; # satisfy taint
49              
50 3 50       75 my $pkg = ($type =~ /::/) ? $type : "Net::Server::$type";
51 3         51 (my $file = "$pkg.pm") =~ s{::}{/}g;
52 3         148 eval { require $file };
  3         7049  
53 3 50       13 if ($@){
54 0         0 warn "Couldn't become server type \"$pkg\" [$@]\n";
55 0         0 next;
56             }
57              
58             # handle items like HTTP and PSGI that aren't true Net::Server flavors, but themselves are MultiType
59 3 50       74 if ($pkg->isa(__PACKAGE__)) {
60 0   0     0 my $type = $self->default_server_type || 'Single';
61 0 0       0 $type = ($type =~ /^(\w+)$/) ? $1 : next; # satisfy taint
62 0 0       0 my $_pkg = ($type =~ /::/) ? $type : "Net::Server::$type";
63 0         0 $prop->{'_recursive_multitype'} = $_pkg;
64 0         0 (my $file = "$_pkg.pm") =~ s{::}{/}g;
65 0 0       0 eval { require $file } or croak "Trouble becoming server type $pkg while loading default package $_pkg: $@";
  0         0  
66 0 0       0 croak "Recursive inheritance - Package $pkg inherits from $_pkg" if $_pkg->isa($pkg);
67 6     6   62 no strict 'refs';
  6         6  
  6         1048  
68 0         0 @{"${pkg}::ISA"} = ($_pkg);
  0         0  
69             }
70              
71             # kludgy - doesn't allow multiple Net::Server::MultiType servers within same process
72             # but it is probably better than modifying our child's class for it
73 3         492 @Net::Server::MultiType::ISA = ($pkg);
74 3         73 last;
75             }
76              
77             # now run as the new type of thingy
78             # passing self, instead of package, doesn't instantiate a new object
79 3         52 $self->SUPER::run(@_);
80             }
81              
82             1;
83              
84             __END__