blib/lib/IO/Async/SSL.pm

filter: Statementstmt 96.8% 154 / 159 Branchbran 78.5% 55 / 70 Conditioncond 66.6% 38 / 57 MC/DCmcdc 21.8% 7 / 32 Subroutinesub 96.5% 28 / 29 Podpod 0.0% 0 / 6 totaltotal 79.8% 282 / 353 CC 55 SCAR 18.7
CC
55
Cov
86%
SSL_upgrade
14.1
SSL_connect
11.7
_SSL_args
6.3
CRAP 6.5
↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010-2022 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::SSL;
7  
8 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 11 our $VERSION = '0.24';
12 11 $VERSION = eval $VERSION;
13  
14 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 ▶use POSIX qw( EAGAIN EWOULDBLOCK );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 11 ▶use IO::Socket::SSL 2.003 qw( $SSL_ERROR SSL_WANT_READ SSL_WANT_WRITE ); # default_ca
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # require >= 2.003 for bugfixes - see RT#125220
20  
21 11 ▶use Future 0.33; # ->catch_with_f
Subroutine BEGIN: called CC=1 SCAR=0.0
22 11 ▶use IO::Async::Handle 0.29;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use IO::Async::Loop '0.61'; # new Listen API
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 NAME
26  
27 C<IO::Async::SSL> - use SSL/TLS with L<IO::Async>
28  
29 =head1 SYNOPSIS
30  
31 use IO::Async::Loop;
32 use IO::Async::SSL;
33  
34 my $loop = IO::Async::Loop->new();
35  
36 $loop->SSL_connect(
37 host => "www.example.com",
38 service => "https",
39  
40 on_stream => sub {
41 my ( $stream ) = @_;
42  
43 $stream->configure(
44 on_read => sub {
45 ...
46 },
47 );
48  
49 $loop->add( $stream );
50  
51 ...
52 },
53  
54 on_resolve_error => sub { print STDERR "Cannot resolve - $_[0]\n"; },
55 on_connect_error => sub { print STDERR "Cannot connect\n"; },
56 on_ssl_error => sub { print STDERR "Cannot negotiate SSL - $_[-1]\n"; },
57 );
58  
59 =head1 DESCRIPTION
60  
61 This module extends existing L<IO::Async> classes with extra methods to allow
62 the use of SSL or TLS-based connections using L<IO::Socket::SSL>. It does not
63 directly provide any methods or functions of its own.
64  
65 Primarily, it provides C<SSL_connect> and C<SSL_listen>, which yield
66 C<IO::Socket::SSL>-upgraded socket handles or L<IO::Async::Stream>
67 instances, and two forms of C<SSL_upgrade> to upgrade an existing TCP
68 connection to use SSL.
69  
70 As an additional convenience, if the C<SSL_verify_mode> and C<SSL_ca_*>
71 options are omitted, the module will attempt to provide them by querying the
72 result of L<IO::Socket::SSL>'s C<default_ca> function. Otherwise, the module
73 will print a warning and set C<SSL_VERIFY_NONE> instead.
74  
75 =cut
76  
77 11 my %SSL_ca_args = IO::Socket::SSL::default_ca();
78  
79 sub _SSL_args
80 {
81 33 ▶ my %args = @_;
Subroutine _SSL_args: called CC=6 SCAR=18.4
82  
83 # SSL clients (i.e. non-server) require a verify mode
84 33 ▶ if( !$args{SSL_server} and !defined $args{SSL_verify_mode} and
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}) 1 32 33
Condition88%8/9
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}
l!l&&r!l&&!r
3201
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"}
l!l&&r!l&&!r
3111
$args{"SSL_server"} or defined $args{"SSL_verify_mode"}
l!l&&r!l&&!r
16152
MC/DC75%3/4
$args{"SSL_server"} or defined $args{"SSL_verify_mode"} or defined $args{"SSL_ca_file"} or defined $args{"SSL_ca_path"}
$args{"SSL_server"} defined $args{"SSL_verify_mode"} defined $args{"SSL_ca_file"} defined $args{"SSL_ca_path"}
Truth table80%4/5
A or B or C or D
A= $args{"SSL_server"}B= defined $args{"SSL_verify_mode"}C= defined $args{"SSL_ca_file"}D= defined $args{"SSL_ca_path"}
ABCDresult
1XXX1
01XX1
001X1
00011
00000
85 !defined $args{SSL_ca_file} and !defined $args{SSL_ca_path} ) {
86 1 ▶ unless( %SSL_ca_args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%SSL_ca_args) 0 1 1
87 0 carp "Unable to set SSL_VERIFY_PEER because IO::Socket::SSL::default_ca() gives nothing";
88 0 $SSL_ca_args{SSL_verify_mode} = IO::Socket::SSL::SSL_VERIFY_NONE();
89 }
90  
91 1 %args = ( %SSL_ca_args, %args );
92 }
93  
94 33 return %args;
95 }
96  
97 sub sslread
98 {
99 293 ▶ my $stream = shift;
Subroutine sslread: called CC=5 SCAR=16.3
Pod undocumented
100 293 my ( $fh, undef, $len ) = @_;
101  
102 293 my $ret = $stream->_sysread( $fh, $_[1], $len );
103  
104 293 ▶ my $read_wants_write = !defined $ret &&
Condition66%6/9
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 110706477168456
!ll&&!rl&&r
278141
not defined $ret and $! == 11 || $! == 11
!ll&&!rl&&r
278015
$! == 11 or $! == 11
l!l&&r!l&&!r
1500
MC/DC50%2/4
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 110706477168456
not defined $ret $! == 11 $! == 11 $IO::Async::SSL::SSL_ERROR == 110706477168456
Truth table50%3/6
not(A) and B or C && D
A= not defined $retB= $! == 11C= $! == 11D= $IO::Async::SSL::SSL_ERROR == 110706477168456
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
105 ( $! == EAGAIN or $! == EWOULDBLOCK ) &&
106 $SSL_ERROR == SSL_WANT_WRITE;
107 293 $stream->want_writeready_for_read( $read_wants_write );
108  
109 # It's possible SSL_read took all the data out of the filehandle, thus
110 # making it not appear read-ready any more.
111 293 ▶ if( $fh->pending ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->pending) 16 277 293
112 16 ▶ $stream->loop->later( sub { $stream->on_read_ready } );
Subroutine __ANON__: called CC=1 SCAR=0.0
113 }
114  
115 293 return $ret;
116 }
117  
118 sub sslwrite
119 {
120 263 ▶ my $stream = shift;
Subroutine sslwrite: called CC=5 SCAR=17.0
Pod undocumented
121 263 my ( $fh, undef, $len ) = @_;
122  
123 # Placate RT98372
124 263 ▶ utf8::downgrade( $_[1] ) or
Branch: 1 branch
BranchTrueFalseTotal
utf8::downgrade($_[1]) or carp("Wide character in sslwrite") 0 263 263
125 carp "Wide character in sslwrite";
126  
127 263 my $ret = $stream->_syswrite( $fh, $_[1], $len );
128  
129 263 ▶ my $write_wants_read = !defined $ret &&
Condition55%5/9
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 110706477168360
!ll&&!rl&&r
26201
not defined $ret and $! == 11 || $! == 11
!ll&&!rl&&r
26201
$! == 11 or $! == 11
l!l&&r!l&&!r
100
MC/DC25%1/4
!defined($ret) && ($! == 11 || $! == 11) && $IO::Async::SSL::SSL_ERROR == 110706477168360
not defined $ret $! == 11 $! == 11 $IO::Async::SSL::SSL_ERROR == 110706477168360
Truth table33%2/6
not(A) and B or C && D
A= not defined $retB= $! == 11C= $! == 11D= $IO::Async::SSL::SSL_ERROR == 110706477168360
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
130 ( $! == EAGAIN or $! == EWOULDBLOCK ) &&
131 $SSL_ERROR == SSL_WANT_READ;
132 263 $stream->want_readready_for_write( $write_wants_read );
133 # If write wants read, there's no point waiting on writereadiness either
134 263 $stream->want_writeready_for_write( !$write_wants_read );
135  
136 263 return $ret;
137 }
138  
139 =head1 LOOP METHODS
140  
141 The following extra methods are added to L<IO::Async::Loop>.
142  
143 =cut
144  
145 =head2 SSL_upgrade
146  
147 ( $stream or $socket ) = $loop->SSL_upgrade( %params )->get;
148  
149 This method upgrades a given stream filehandle into an SSL-wrapped stream,
150 returning a future which will yield the given stream object or socket.
151  
152 Takes the following parameters:
153  
154 =over 8
155  
156 =item handle => IO::Async::Stream | IO
157  
158 The C<IO::Async::Stream> object containing the IO handle of an
159 already-established connection to act as the transport for SSL; or the plain
160 IO socket handle itself.
161  
162 If an C<IO::Async::Stream> is passed it will have the C<reader> and C<writer>
163 functions set on it suitable for SSL use, and will be returned as the result
164 from the future.
165  
166 If a plain socket handle is passed, that will be returned from the future
167 instead.
168  
169 =item SSL_server => BOOL
170  
171 If true, indicates this is the server side of the connection.
172  
173 =back
174  
175 In addition, any parameter whose name starts C<SSL_> will be passed to the
176 C<IO::Socket::SSL> constructor.
177  
178 The following legacy callback arguments are also supported, in case the
179 returned future is not used:
180  
181 =over 8
182  
183 =item on_upgraded => CODE
184  
185 A continuation that is invoked when the socket has been successfully upgraded
186 to SSL. It will be passed an instance of an C<IO::Socket::SSL>, which will
187 have appropriate SSL-compatible reader/writer functions attached.
188  
189 $on_upgraded->( $sslsocket )
190  
191 =item on_error => CODE
192  
193 A continuation that is invoked if C<IO::Socket::SSL> detects an error while
194 negotiating the upgrade.
195  
196 $on_error->( $! )
197  
198 =back
199  
200 =cut
201  
202 sub IO::Async::Loop::SSL_upgrade
203 {
204 22 ▶ my $loop = shift;
Subroutine SSL_upgrade: called CC=14 SCAR=26.5
Pod undocumented
205 22 my %params = @_;
206  
207 22 my $f = $loop->new_future;
208  
209 22 ▶ $params{handle} or croak "Expected 'handle'";
Branch: 1 branch
BranchTrueFalseTotal
$params{"handle"} or croak("Expected 'handle'") 0 22 22
210  
211 22 my $stream;
212 my $socket;
213 22 ▶ if( $params{handle}->isa( "IO::Async::Stream" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'handle'}->isa('IO::Async::Stream')) { } 16 6 22
214 16 $stream = delete $params{handle};
215 16 $socket = $stream->read_handle;
216 }
217 else {
218 6 $socket = delete $params{handle};
219 }
220  
221 {
222 22 my $on_upgraded = delete $params{on_upgraded} or defined wantarray
223 22 ▶ or croak "Expected 'on_upgraded' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray or croak("Expected 'on_upgraded' or to return a Future") 0 22 22
Condition66%2/3
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray
l!l&&r!l&&!r
6160
MC/DC0%0/2
my $on_upgraded = delete $params{"on_upgraded"} or defined wantarray
my $on_upgraded = delete $params{"on_upgraded"} defined wantarray
Truth table66%2/3
A or B
A= my $on_upgraded = delete $params{"on_upgraded"}B= defined wantarray
ABresult
1X1
011
000
224 my $on_error = delete $params{on_error} or defined wantarray
225 22 ▶ or croak "Expected 'on_error' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_error = delete $params{"on_error"} or defined wantarray or croak("Expected 'on_error' or to return a Future") 0 22 22
Condition66%2/3
my $on_error = delete $params{"on_error"} or defined wantarray
l!l&&r!l&&!r
6160
MC/DC0%0/2
my $on_error = delete $params{"on_error"} or defined wantarray
my $on_error = delete $params{"on_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_error = delete $params{"on_error"}B= defined wantarray
ABresult
1X1
011
000
226  
227 22 ▶ $f->on_done( $on_upgraded ) if $on_upgraded;
Branch: 1 branch
BranchTrueFalseTotal
if $on_upgraded 6 16 22
228 22 ▶ $f->on_fail( $on_error ) if $on_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_error 6 16 22
229 }
230  
231 22 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
232  
233 eval {
234 $socket = IO::Socket::SSL->start_SSL( $socket, _SSL_args
235 SSL_startHandshake => 0,
236  
237 # Required to make IO::Socket::SSL not ->close before we have a chance to remove it from the loop
238 ▶ SSL_error_trap => sub { },
Subroutine __ANON__: called CC=1 SCAR=0.0
239  
240 22 ▶ %ssl_params,
Branch: 1 branch
BranchTrueFalseTotal
$socket = "IO::Socket::SSL"->start_SSL($socket, _SSL_args("SSL_startHandshake", 0, "SSL_error_trap", sub { } , %ssl_params)) or die "IO::Socket::SSL"->errstr 1 21 22
241 ) or die IO::Socket::SSL->errstr;
242 22 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $socket = "IO::Socket::SSL"->start_SSL($socket, _SSL_args("SSL_startHandshake", 0, "SSL_error_trap", sub { } , %ssl_params)) or die "IO::Socket::SSL"->errstr } } or do { chomp(my $e = $@); return $f->fail($e, "ssl") } 1 21 22
243 1 chomp( my $e = $@ );
244 1 return $f->fail( $e, "ssl" );
245 };
246  
247 21 ▶ my $ready_method = $ssl_params{SSL_server} ? "accept_SSL" : "connect_SSL";
Branch: 1 branch
BranchTrueFalseTotal
$ssl_params{'SSL_server'} ? : 10 11 21
248  
249 my $ready = sub {
250 46 ▶ my ( $self ) = @_;
Subroutine __ANON__: called CC=6 SCAR=17.9
251 46 ▶ if( $socket->$ready_method ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socket->$ready_method) 17 29 46
252 17 $loop->remove( $self );
253  
254 17 ▶ if( $stream ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream) 15 2 17
255 15 $stream->configure(
256 handle => $socket,
257 reader => \&sslread,
258 writer => \&sslwrite,
259 );
260 }
261  
262 17 ▶ $f->done( $stream || $socket );
Condition66%2/3
$stream || $socket
l!l&&r!l&&!r
1520
MC/DC0%0/2
$stream || $socket
$stream $socket
Truth table66%2/3
A || B
A= $streamB= $socket
ABresult
1X1
011
000
263 17 return;
264 }
265  
266 29 ▶ if( $! != EAGAIN and $! != EWOULDBLOCK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! != 11 and $! != 11) 4 25 29
Condition66%2/3
$! != 11 and $! != 11
!ll&&!rl&&r
2504
MC/DC50%1/2
$! != 11 and $! != 11
$! != 11 $! != 11
Truth table66%2/3
A and B
A= $! != 11B= $! != 11
ABresult
0X0
100
111
267 4 my $errstr = IO::Socket::SSL::errstr();
268 4 $loop->remove( $self );
269 4 $f->fail( $errstr, "ssl" );
270 4 return;
271 }
272  
273 25 $self->want_readready ( $SSL_ERROR == SSL_WANT_READ );
274 25 $self->want_writeready( $SSL_ERROR == SSL_WANT_WRITE );
275 21 };
276  
277 # We're going to steal the IO handle from $stream, so we'll have to
278 # temporarily deconfigure it
279 21 ▶ $stream->configure( handle => undef ) if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 16 5 21
280  
281 21 $loop->add( my $handle = IO::Async::Handle->new(
282 handle => $socket,
283 on_read_ready => $ready,
284 on_write_ready => $ready,
285 ) );
286  
287 21 $ready->( $handle );
288  
289 21 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 19 2 21
290  
291 # Caller is not going to keep hold of the Future, so we have to ensure it
292 # stays alive somehow
293 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
294 }
295  
296 =head2 SSL_connect
297  
298 $stream = $loop->SSL_connect( %params )->get;
299  
300 This method performs a non-blocking connection to a given address or set of
301 addresses, upgrades the socket to SSL, then yields a C<IO::Async::Stream>
302 object when the SSL handshake is complete.
303  
304 It takes all the same arguments as C<IO::Async::Loop::connect()>. Any argument
305 whose name starts C<SSL_> will be passed on to the L<IO::Socket::SSL>
306 constructor rather than the Loop's C<connect> method. It is not required to
307 pass the C<socktype> option, as SSL implies this will be C<stream>.
308  
309 This method can also upgrade an existing C<IO::Async::Stream> or subclass
310 instance given as the C<handle> argument, by setting the C<reader> and
311 C<writer> functions.
312  
313 =head2 SSL_connect (void)
314  
315 $loop->SSL_connect( %params,
316 on_connected => sub { ... },
317 on_stream => sub { ... },
318 );
319  
320 When not returning a future, this method also supports the C<on_connected> and
321 C<on_stream> continuations.
322  
323 In addition, the following arguments are then required:
324  
325 =over 8
326  
327 =item on_ssl_error => CODE
328  
329 A continuation that is invoked if C<IO::Socket::SSL> detects an SSL-based
330 error once the actual stream socket is connected.
331  
332 =back
333  
334 If the C<on_connected> continuation is used, the socket handle it yields will
335 be a C<IO::Socket::SSL>, which must be wrapped in C<IO::Async::SSLStream> to
336 be used by C<IO::Async>. The C<on_stream> continuation will already yield such
337 an instance.
338  
339 =cut
340  
341 sub IO::Async::Loop::SSL_connect
342 {
343 6 ▶ my $loop = shift;
Subroutine SSL_connect: called CC=11 SCAR=24.6
Pod undocumented
344 6 my %params = @_;
345  
346 6 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
347  
348 6 my $on_done;
349 6 ▶ if( exists $params{on_connected} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $params{'on_connected'}) { } 1 5 6
elsif (exists $params{'on_stream'}) { } 1 4 5
350 1 my $on_connected = delete $params{on_connected};
351 $on_done = sub {
352 0 ▶ my ( $stream ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
353 0 $on_connected->( $stream->read_handle );
354 1 };
355 }
356 elsif( exists $params{on_stream} ) {
357 1 my $on_stream = delete $params{on_stream};
358 1 $on_done = $on_stream;
359 }
360 else {
361 4 ▶ croak "Expected 'on_connected' or 'on_stream' or to return a Future" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 4 4
362 }
363  
364 my $on_ssl_error = delete $params{on_ssl_error} or defined wantarray or
365 6 ▶ croak "Expected 'on_ssl_error' or to return a Future";
Branch: 1 branch
BranchTrueFalseTotal
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray or croak("Expected 'on_ssl_error' or to return a Future") 0 6 6
Condition66%2/3
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
l!l&&r!l&&!r
330
MC/DC0%0/2
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
my $on_ssl_error = delete $params{"on_ssl_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_ssl_error = delete $params{"on_ssl_error"}B= defined wantarray
ABresult
1X1
011
000
366  
367 6 ▶ my $stream = delete $params{handle} || do {
Condition66%2/3
delete $params{'handle'} || do { require IO::Async::Stream; 'IO::Async::Stream'->new }
l!l&&r!l&&!r
330
MC/DC0%0/2
delete $params{'handle'} || do { require IO::Async::Stream; 'IO::Async::Stream'->new }
delete $params{'handle'} do { require IO::Async::Stream; 'IO::Async::Stream'->new }
Truth table66%2/3
A || B
A= delete $params{'handle'}B= do { require IO::Async::Stream; 'IO::Async::Stream'->new }
ABresult
1X1
011
000
368 3 require IO::Async::Stream;
369 3 IO::Async::Stream->new;
370 };
371  
372 6 ▶ $stream->isa( "IO::Async::Stream" ) or
Branch: 1 branch
BranchTrueFalseTotal
$stream->isa("IO::Async::Stream") or croak("Can only SSL_connect a handle instance of IO::Async::Stream") 0 6 6
373 croak "Can only SSL_connect a handle instance of IO::Async::Stream";
374  
375 # Don't ->connect with the handle yet, because we'll first have to use the
376 # socket to perform SSL_upgrade on. We don't want to confuse the loop by
377 # giving it the same fd twice.
378  
379 my $f = $loop->connect(
380 socktype => 'stream', # SSL over DGRAM or RAW makes no sense
381 %params,
382 )->then( sub {
383 6 ▶ my ( $socket ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
384  
385 6 $stream->configure( handle => $socket );
386  
387 6 $loop->SSL_upgrade(
388 _SSL_args( %ssl_params ),
389 handle => $stream,
390 )
391 6 });
392  
393 6 ▶ $f->on_done( $on_done ) if $on_done;
Branch: 1 branch
BranchTrueFalseTotal
if $on_done 2 4 6
394 $f->on_fail( sub {
395 1 ▶ $on_ssl_error->( $_[0] ) if defined $_[1] and $_[1] eq "ssl";
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] and $_[1] eq "ssl" 1 0 1
Condition33%1/3
defined $_[1] and $_[1] eq "ssl"
!ll&&!rl&&r
001
MC/DC0%0/2
defined $_[1] and $_[1] eq "ssl"
defined $_[1] $_[1] eq "ssl"
Truth table33%1/3
A and B
A= defined $_[1]B= $_[1] eq "ssl"
ABresult
0X0
100
111
396 6 ▶ }) if $on_ssl_error;
Branch: 1 branch
BranchTrueFalseTotal
if $on_ssl_error 3 3 6
397  
398 6 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 4 2 6
399  
400 # Caller is not going to keep hold of the Future, so we have to ensure it
401 # stays alive somehow
402 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
403 }
404  
405 =head2 SSL_listen
406  
407 $loop->SSL_listen( %params )->get;
408  
409 This method sets up a listening socket using the addresses given, and will
410 invoke the callback each time a new connection is accepted on the socket and
411 the SSL handshake has been completed. This can be either the C<on_accept> or
412 C<on_stream> continuation; C<on_socket> is not supported.
413  
414 It takes all the same arguments as C<IO::Async::Loop::listen()>. Any argument
415 whose name starts C<SSL_> will be passed on to the L<IO::Socket::SSL>
416 constructor rather than the Loop's C<listen> method. It is not required to
417 pass the C<socktype> option, as SSL implies this will be C<stream>.
418  
419 In addition, the following arguments are rquired:
420  
421 =over 8
422  
423 =item on_ssl_error => CODE
424  
425 A continuation that is invoked if C<IO::Socket::SSL> detects an SSL-based
426 error once the actual stream socket is connected.
427  
428 =back
429  
430 The underlying L<IO::Socket::SSL> socket will also require the server key and
431 certificate for a server-mode socket. See its documentation for more details.
432  
433 If the C<on_accept> continuation is used, the socket handle it yields will be
434 a C<IO::Socket::SSL>, which must be wrapped in C<IO::Async::SSLStream> to be
435 used by C<IO::Async>. The C<on_stream> continuation will already yield such an
436 instance.
437  
438 =cut
439  
440 sub IO::Async::Loop::SSL_listen
441 {
442 5 ▶ my $loop = shift;
Subroutine SSL_listen: called CC=4 SCAR=14.0
Pod undocumented
443 5 my %params = @_;
444  
445 5 my %ssl_params = map { $_ => delete $params{$_} } grep m/^SSL_/, keys %params;
446 my $on_ssl_error = delete $params{on_ssl_error} or defined wantarray
447 5 ▶ or croak "Expected 'on_ssl_error'";
Branch: 1 branch
BranchTrueFalseTotal
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray or croak("Expected 'on_ssl_error'") 0 5 5
Condition66%2/3
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
l!l&&r!l&&!r
320
MC/DC0%0/2
my $on_ssl_error = delete $params{"on_ssl_error"} or defined wantarray
my $on_ssl_error = delete $params{"on_ssl_error"} defined wantarray
Truth table66%2/3
A or B
A= my $on_ssl_error = delete $params{"on_ssl_error"}B= defined wantarray
ABresult
1X1
011
000
448  
449 my $f = $loop->listen(
450 socktype => 'stream',
451 %params,
452 )->on_done( sub {
453 5 ▶ my $listener = shift;
Subroutine __ANON__: called CC=1 SCAR=0.1
454  
455 5 my $cleartext_acceptor = $listener->acceptor;
456 my $ssl_acceptor = sub {
457 5 ▶ my $listener = shift;
Subroutine __ANON__: called CC=3 SCAR=11.3
458 5 my ( $listen_sock, %params ) = @_;
459 5 my $stream = $params{handle};
460 5 ▶ !defined $stream or $stream->isa( "IO::Async::Stream" ) or
Branch: 1 branch
BranchTrueFalseTotal
not defined $stream or $stream->isa("IO::Async::Stream") or croak("Can only accept SSL on IO::Async::Stream handles") 0 5 5
Condition66%2/3
not defined $stream or $stream->isa("IO::Async::Stream")
l!l&&r!l&&!r
140
MC/DC0%0/2
not defined $stream or $stream->isa("IO::Async::Stream")
not defined $stream $stream->isa("IO::Async::Stream")
Truth table66%2/3
not(A) or B
A= not defined $streamB= $stream->isa("IO::Async::Stream")
ABresult
1X1
011
000
461 croak "Can only accept SSL on IO::Async::Stream handles";
462  
463 $listener->$cleartext_acceptor( $listen_sock )->then( sub {
464 5 ▶ my ( $socket ) = @_;
Subroutine __ANON__: called CC=4 SCAR=14.3
465  
466 5 ▶ return Future->done() unless $socket; # EAGAIN
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 0 5 5
467  
468 5 ▶ $stream->configure( handle => $socket ) if $stream;
Branch: 1 branch
BranchTrueFalseTotal
if $stream 4 1 5
469  
470 $loop->SSL_upgrade(
471 _SSL_args( SSL_server => 1, %ssl_params ),
472 handle => ( $stream || $socket ),
473 )->catch_with_f( ssl => sub {
474 1 ▶ my ( $f, $failure ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.4
475 1 ▶ if( $on_ssl_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_ssl_error) 1 0 1
476 1 $on_ssl_error->( $failure );
477 1 return Future->done; # eat it
478 }
479 0 return $f;
480 5 ▶ });
Condition66%2/3
$stream || $socket
l!l&&r!l&&!r
410
MC/DC0%0/2
$stream || $socket
$stream $socket
Truth table66%2/3
A || B
A= $streamB= $socket
ABresult
1X1
011
000
481 5 });
482 5 };
483  
484 5 $listener->configure( acceptor => $ssl_acceptor );
485 5 });
486  
487 5 ▶ return $f if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 3 2 5
488  
489 # Caller is not going to keep hold of the Future, so we have to ensure it
490 # stays alive somehow
491 2 ▶ $f->on_ready( sub { undef $f } ); # intentional cycle
Subroutine __ANON__: called CC=1 SCAR=0.0
492 }
493  
494 =head1 STREAM PROTOCOL METHODS
495  
496 The following extra methods are added to L<IO::Async::Protocol::Stream>.
497  
498 =cut
499  
500 =head2 SSL_upgrade
501  
502 $protocol->SSL_upgrade( %params )->get;
503  
504 A shortcut to calling C<< $loop->SSL_upgrade >>. This method will unconfigure
505 the C<transport> of the Protocol, upgrade its underlying filehandle to SSL,
506 then reconfigure it again with SSL reader and writer functions on it. It takes
507 the same arguments as C<< $loop->SSL_upgrade >>, except that the C<handle>
508 argument is not required as it's taken from the Protocol's C<transport>.
509  
510 =cut
511  
512 sub IO::Async::Protocol::Stream::SSL_upgrade
513 {
514 2 ▶ my $protocol = shift;
Subroutine SSL_upgrade: called CC=3 SCAR=11.1
Pod undocumented
515 2 my %params = @_;
516  
517 2 ▶ my $on_upgraded = delete $params{on_upgraded} or croak "Expected 'on_upgraded'";
Branch: 1 branch
BranchTrueFalseTotal
my $on_upgraded = delete $params{"on_upgraded"} or croak("Expected 'on_upgraded'") 0 2 2
518  
519 2 ▶ my $loop = $protocol->get_loop or croak "Expected to be a member of a Loop";
Branch: 1 branch
BranchTrueFalseTotal
my $loop = $protocol->get_loop or croak("Expected to be a member of a Loop") 0 2 2
520  
521 2 my $transport = $protocol->transport;
522  
523 2 $protocol->configure( transport => undef );
524  
525 $loop->SSL_upgrade(
526 handle => $transport,
527 on_upgraded => sub {
528 2 ▶ my ( $transport ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
529  
530 2 $protocol->configure( transport => $transport );
531  
532 2 $on_upgraded->();
533 },
534  
535 2 %params,
536 );
537 }
538  
539 =head1 AUTHOR
540  
541 Paul Evans <leonerd@leonerd.org.uk>
542  
543 =cut
544  
545 11 0x55AA;
↑ Summary Build »