File Coverage

blib/lib/Net/Server/MultiType.pm
Criterion Covered Total %
statement 32 48 66.6
branch 9 30 30.0
condition 3 10 30.0
subroutine 5 6 83.3
pod 1 3 33.3
total 50 97 51.5


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   13276 use strict;
  6         16  
  6         176  
21 6     6   24 use base qw(Net::Server);
  6         114  
  6         3510  
22              
23             #sub net_server_type { shift->SUPER::net_server_type }; # not-needed
24              
25             sub options {
26 6     6 0 27 my $self = shift;
27 6         73 my $ref = $self->SUPER::options(@_);
28 6   100     101 $ref->{'server_type'} = $self->{'server'}->{'server_type'} ||= [];
29 6         21 return $ref;
30             }
31              
32 0     0 0 0 sub default_server_type { 'Fork' }
33              
34             sub run {
35 3 50   3 1 9784 my $self = ref($_[0]) ? shift() : shift->new;
36 3 50       241 $self->{'server'}->{'_run_args'} = [@_ == 1 ? %{$_[0]} : @_];
  0         0  
37 3         108 $self->_initialize;
38 3         22 my $prop = $self->{'server'};
39              
40 3 50 33     38 if (!defined $prop->{'server_type'} || ! @{ $prop->{'server_type'} }) {
  3         16  
41 0 0 0     0 if (my $ref = $self->can('default_server_type') && $self->default_server_type) {
42 0 0       0 $prop->{'server_type'} = ref($ref) ? $ref : [$ref];
43             }
44             }
45 3 50       7 foreach my $type (@{ $prop->{'server_type'} || []}) {
  3         37  
46 3 50       13 next if $type eq 'MultiType';
47 3 50       47 $type = ($type =~ /^(\w+)$/) ? $1 : next; # satisfy taint
48              
49 3 50       48 my $pkg = ($type =~ /::/) ? $type : "Net::Server::$type";
50 3         72 (my $file = "$pkg.pm") =~ s{::}{/}g;
51 3         16 eval { require $file };
  3         3753  
52 3 50       15 if ($@){
53 0         0 warn "Couldn't become server type \"$pkg\" [$@]\n";
54 0         0 next;
55             }
56              
57             # handle items like HTTP and PSGI that aren't true Net::Server flavors, but themselves are MultiType
58 3 50       47 if ($pkg->isa(__PACKAGE__)) {
59 0   0     0 my $type = $self->default_server_type || 'Single';
60 0 0       0 $type = ($type =~ /^(\w+)$/) ? $1 : next; # satisfy taint
61 0 0       0 my $_pkg = ($type =~ /::/) ? $type : "Net::Server::$type";
62 0         0 $prop->{'_recursive_multitype'} = $_pkg;
63 0         0 (my $file = "$_pkg.pm") =~ s{::}{/}g;
64 0 0       0 eval { require $file } or die "Trouble becoming server type $pkg while loading default package $_pkg: $@\n";
  0         0  
65 0 0       0 die "Recursive inheritance - Package $pkg inherits from $_pkg.\n" if $_pkg->isa($pkg);
66 6     6   54 no strict 'refs';
  6         10  
  6         664  
67 0         0 @{"${pkg}::ISA"} = ($_pkg);
  0         0  
68             }
69              
70             # kludgy - doesn't allow multiple Net::Server::MultiType servers within same process
71             # but it is probably better than modifying our child's class for it
72 3         251 @Net::Server::MultiType::ISA = ($pkg);
73 3         18 last;
74             }
75              
76             # now run as the new type of thingy
77             # passing self, instead of package, doesn't instantiate a new object
78 3         32 $self->SUPER::run(@_);
79             }
80              
81             1;
82              
83             __END__