File Coverage

blib/lib/Feersum.pm
Criterion Covered Total %
statement 55 60 91.6
branch 11 16 68.7
condition 8 17 47.0
subroutine 11 13 84.6
pod 4 4 100.0
total 89 110 80.9


line stmt bran cond sub pod time code
1             package Feersum;
2 122     122   22903413 use 5.014;
  122         691  
3 122     122   816 use strict;
  122         242  
  122         3930  
4 122     122   718 use warnings;
  122         433  
  122         7512  
5 122     122   88831 use EV ();
  122         347157  
  122         4277  
6 122     122   966 use Carp ();
  122         227  
  122         2345  
7 122     122   2766 use Socket ();
  122         23813  
  122         11089  
8              
9             our $VERSION = '1.506_18';
10              
11             require Feersum::Connection;
12             require Feersum::Connection::Handle;
13             require XSLoader;
14             XSLoader::load('Feersum', $VERSION);
15              
16             # numify as per
17             # http://www.dagolden.com/index.php/369/version-numbers-should-be-boring/
18             $VERSION = eval $VERSION; ## no critic (StringyEval, ConstantVersion)
19              
20             our $INSTANCE;
21             my %_SOCKETS; # inside-out storage for socket refs (keyed by Scalar::Util::refaddr)
22              
23 122     122   756 use Scalar::Util ();
  122         231  
  122         2588  
24 122     122   658 use Exporter 'import';
  122         298  
  122         87852  
25             our @EXPORT_OK = qw(HEADER_NORM_SKIP HEADER_NORM_UPCASE HEADER_NORM_LOCASE HEADER_NORM_UPCASE_DASH HEADER_NORM_LOCASE_DASH);
26              
27             sub new {
28 169 100   169 1 26239650 unless ($INSTANCE) {
29 110         6883 $INSTANCE = __PACKAGE__->_xs_default_server();
30 110         2318 $SIG{PIPE} = 'IGNORE';
31             }
32 169         882 return $INSTANCE;
33             }
34             *endjinn = *new;
35              
36             sub new_instance {
37 62     62 1 46156556 my $class = shift;
38 62         1090 $SIG{PIPE} = 'IGNORE';
39 62         1415 return $class->_xs_new_server();
40             }
41              
42             sub DESTROY {
43 0     0   0 my $self = shift;
44 0         0 my $addr = Scalar::Util::refaddr($self);
45 0         0 delete $_SOCKETS{$addr};
46             # XS DESTROY is renamed to _xs_destroy and called here
47 0         0 $self->_xs_destroy();
48             }
49              
50             sub use_socket {
51 190     190 1 2911174 my ($self, $sock) = @_;
52 190         490 my $addr = Scalar::Util::refaddr($self);
53 190         445 push @{$_SOCKETS{$addr}}, $sock; # keep ref to prevent GC
  190         1001  
54 190         614 my $fd = fileno $sock;
55 190 50       961 Carp::croak "Invalid socket: fileno returned undef" unless defined $fd;
56 190         11622 $self->accept_on_fd($fd);
57              
58             # Try socket methods first, fall back to getsockname() for raw sockets
59 190         851 my ($host, $port) = ('localhost', 80);
60 190 100       2960 if ($sock->can('sockhost')) {
61 187   50     449 $host = eval { $sock->sockhost() } || 'localhost';
62 187   50     10772 $port = eval { $sock->sockport() } || 80; ## no critic (MagicNumbers)
63             } else {
64             # Raw socket (e.g., from Runner with SO_REUSEPORT) - use getsockname
65 3         22 my $sockaddr = getsockname($sock);
66 3 50       11 if ($sockaddr) {
67 3         6 my $family = eval { Socket::sockaddr_family($sockaddr) };
  3         12  
68 3 100 66     25 if (defined $family && $family == Socket::AF_INET()) {
    50 33        
      33        
69 2         6 (my $packed_port, my $packed_addr) = Socket::sockaddr_in($sockaddr);
70 2   50     25 $host = Socket::inet_ntoa($packed_addr) || 'localhost';
71             # Use defined check - port 0 is valid (OS-assigned dynamic port)
72 2 50       14 $port = defined($packed_port) ? $packed_port : 80;
73 1         10 } elsif (defined $family && eval { Socket::AF_INET6() } && $family == Socket::AF_INET6()) {
74 1         7 (my $packed_port, my $packed_addr) = Socket::sockaddr_in6($sockaddr);
75 1   50     24 $host = Socket::inet_ntop(Socket::AF_INET6(), $packed_addr) || 'localhost';
76             # Use defined check - port 0 is valid (OS-assigned dynamic port)
77 1 50       5 $port = defined($packed_port) ? $packed_port : 80;
78             }
79             }
80             }
81 190         8061 $self->set_server_name_and_port($host,$port);
82 190         599 return;
83             }
84              
85             # overload this to catch Feersum errors and exceptions thrown by request
86             # callbacks.
87 0     0 1   sub DIED { Carp::confess "DIED: $@"; }
88              
89             1;
90             __END__