Perlbal/SocketSSL.pm

filter: Statementstmt 45.0% 41 / 91 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 61.9% 13 / 21 Podpod 66.6% 4 / 6 totaltotal 41.1% 58 / 141 CC 12 SCAR 23.7
CC
12
Cov
36%
try_accept
30.0
__ANON__
20.0
close
20.0
CRAP 10.7
« Perlbal/Plugin/Stats.pm ↑ Summary Perlbal/AIO.pm »
1 # Base class for SSL sockets.
2 #
3 # This is a simple class that extends Danga::Socket and contains an IO::Socket::SSL
4 # for the purpose of allowing non-blocking SSL in Perlbal.
5 #
6 # Copyright 2007, Mark Smith <mark@plogs.net>.
7 #
8 # This file is licensed under the same terms as Perl itself.
9  
10 package Perlbal::SocketSSL;
11  
12 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶no warnings qw(deprecated);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 22 ▶use Danga::Socket 1.44;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use IO::Socket::SSL 0.98;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 22 ▶use Errno qw( EAGAIN );
Subroutine BEGIN: called CC=1 SCAR=0.0
19 22 ▶use Perlbal::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 22 ▶use base 'Danga::Socket';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 22 ▶use fields qw( listener create_time alive_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 0 ▶Perlbal::Socket->set_socket_idle_handler('Perlbal::SocketSSL' => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
25 0 my Perlbal::SocketSSL $v = shift;
26  
27 0 ▶ my $max_age = eval { $v->max_idle_time } || 0;
Condition0%0/2
eval { do { $v->max_idle_time } } || 0
l!l
00
MC/DC0%0/1
eval { do { $v->max_idle_time } } || 0
eval { do { $v->max_idle_time } }
Truth table0%0/2
A
A= eval { do { $v->max_idle_time } }
Aresult
11
00
28 0 ▶ return unless $max_age;
Branch: 1 branch
BranchTrueFalseTotal
unless $max_age 0 0 0
29  
30 # Attributes are in another class, don't violate object boundaries.
31 $v->{sock}->close(SSL_no_shutdown => 1, SSL_ctx_free => 1)
32 0 ▶ if $v->{alive_time} < $Perlbal::tick_time - $max_age;
Branch: 1 branch
BranchTrueFalseTotal
if $v->{"alive_time"} < $Perlbal::tick_time - $max_age 0 0 0
33 22 });
34  
35 # called: CLASS->new( $sock, $tcplistener )
36 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
37 0 my Perlbal::SocketSSL $self = shift;
38 0 ▶ $self = fields::new( $self ) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
39  
40 0 Perlbal::objctor($self);
41  
42 0 my ($sock, $listener) = @_;
43  
44 0 ${*$sock}->{_danga_socket} = $self;
45 0 $self->{listener} = $listener;
46 0 $self->{alive_time} = $self->{create_time} = time;
47  
48 0 $self->SUPER::new($sock);
49  
50 # TODO: would be good to have an overall timeout so that we can
51 # kill sockets that are open and just sitting there. "ssl_handshake_timeout"
52 # or something like that...
53  
54 0 return $self;
55 }
56  
57 # this is nonblocking, it attempts to setup SSL and if it can't then
58 # it returns whether it needs to read or write. we then setup to wait
59 # for the event it indicates and then wait. when that event fires, we
60 # call down again, and repeat the process until we have setup the
61 # SSL connection.
62 0 ▶sub try_accept {
Subroutine try_accept: not called CC=5 SCAR=34.0
Pod undocumented
63 0 my Perlbal::SocketSSL $self = shift;
64  
65 0 my $sock = $self->{sock}->accept_SSL;
66  
67 0 ▶ if (defined $sock) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sock) 0 0 0
68 # looks like we got it! let's steal it from ourselves
69 # so Danga::Socket gives up on it and we can send
70 # it out to someone else. (we discard the return value
71 # as we already have it in $sock)
72 #
73 # of course, life isn't as simple as that. we have to do
74 # some trickery with the ordering here to ensure that we
75 # don't setup the new class until after the Perlbal::SocketSSL
76 # goes away according to Danga::Socket.
77 #
78 # if we don't do it this way, we get nasty errors because
79 # we (this object) still exists in the DescriptorMap of
80 # Danga::Socket when the new Perlbal::ClientXX tries to
81 # insert itself there.
82  
83 # removes us from the active polling, closes up shop, but
84 # save our fd first!
85 0 my $fd = $self->{fd};
86 0 $self->steal_socket;
87  
88 # finish blowing us away
89 0 my $ref = Danga::Socket->DescriptorMap();
90 0 delete $ref->{$fd};
91  
92 # now stick the new one in
93 0 my Perlbal::ClientHTTPBase $cb = $self->{listener}->class_new_socket($sock);
94 0 $cb->{is_ssl} = 1;
95 0 return;
96 }
97  
98 # nope, let's see if we can continue the process
99 0 ▶ if ($! == EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 11) { } 0 0 0
100 0 ▶ if ($SSL_ERROR == SSL_WANT_READ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Perlbal::SocketSSL::SSL_ERROR == 103817265787016) { } 0 0 0
elsif ($Perlbal::SocketSSL::SSL_ERROR == 103817265787352) { } 0 0 0
101 0 $self->watch_read(1);
102 } elsif ($SSL_ERROR == SSL_WANT_WRITE) {
103 0 $self->watch_write(1);
104 } else {
105 0 $self->close('invalid_ssl_state');
106 }
107 } else {
108 0 $self->close('invalid_ssl_error');
109 }
110 }
111  
112 0 ▶sub event_read {
Subroutine event_read: not called CC=1 SCAR=6.9
Pod documented
113 0 $_[0]->watch_read(0);
114 0 $_[0]->{alive_time} = $Perlbal::tick_time;
115 0 $_[0]->try_accept;
116 }
117  
118 0 ▶sub event_write {
Subroutine event_write: not called CC=1 SCAR=6.9
Pod documented
119 0 $_[0]->watch_write(0);
120 0 $_[0]->{alive_time} = $Perlbal::tick_time;
121 0 $_[0]->try_accept;
122 }
123  
124 0 ▶sub event_err {
Subroutine event_err: not called CC=1 SCAR=6.9
Pod documented
125 0 $_[0]->close('invalid_ssl_state');
126 }
127  
128 # You can tuna-fish, but you can't tune a Perlbal::SocketSSL
129 0 ▶sub max_idle_time {
Subroutine max_idle_time: not called CC=1 SCAR=6.9
Pod undocumented
130 0 return 60;
131 }
132  
133 package Perlbal::SocketSSL2;
134  
135 22 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 22 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
137  
138 22 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 22 ▶use base 'IO::Socket::SSL';
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 0 ▶sub close {
Subroutine close: not called CC=4 SCAR=30.0
143 0 ▶ my $self = shift
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return IO::Socket::SSL::_invalid_object() 0 0 0
144 or return IO::Socket::SSL::_invalid_object();
145  
146 # If we our Danga::Socket sibling has a sock then we're being called for the first time.
147 # NOTE: this isn't strictly safe, ->close can get called on a sock multiple times. We
148 # really could use a safe way to know if this handle is being called from the post-
149 # event-loop cleanup code in Danga::Socket.
150 0 ▶ if (my $ds = ${*$self}->{_danga_socket}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ds = ${*$self;}->{'_danga_socket'}) { } 0 0 0
151 0 ${*$self}->{__close_args} = [ @_ ];
152 0 delete ${*$self}->{_danga_socket};
153 0 ▶ $ds->close('intercepted_ssl_close')
Branch: 1 branch
BranchTrueFalseTotal
if $ds->sock 0 0 0
154 if $ds->sock;
155 } else {
156 0 return $self->SUPER::close(@{${*$self}->{__close_args}});
157 }
158 }
159  
160 22 1;
« Perlbal/Plugin/Stats.pm ↑ Summary Perlbal/AIO.pm »