HTTP2/Client/Connection/AnyEvent.pm

filter: Statementstmt 85.7% 36 / 42 Branchbran 25.0% 1 / 4 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 83.3% 10 / 12 Podpod n/a totaltotal 76.1% 48 / 63 CC 4 SCAR 8.2
CC
4
Cov
78%
__ANON__
6.0
_start_io_if_needed
3.7
__ANON__
2.0
CRAP 2.3
« HTTP2/RejectorRegistry.pm ↑ Summary HTTP2.pm »
1 package Net::HTTP2::Client::Connection::AnyEvent;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::HTTP2::Client::Connection::AnyEvent
11  
12 =head1 SYNOPSIS
13  
14 my $cv = AnyEvent->condvar();
15  
16 my $h2 = Net::HTTP2::Client::Connection::AnyEvent->new("perl.org");
17  
18 $h2->request("GET", "/")->then(
19 sub ($resp) {
20 print $resp->content();
21 },
22 )->finally($cv);
23  
24 $cv->recv();
25  
26 =head1 DESCRIPTION
27  
28 This class extends L<Net::HTTP2::Client::Connection> to work with L<AnyEvent>.
29  
30 It requires that L<Net::SSLeay> support ALPN.
31  
32 =cut
33  
34 #----------------------------------------------------------------------
35  
36 1 ▶use parent 'Net::HTTP2::Client::Connection';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use AnyEvent::TLS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶use AnyEvent::Handle ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use Net::SSLeay ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # perl -I ../p5-X-Tiny/lib -MData::Dumper -MAnyEvent -I ../p5-IO-SigGuard/lib -I ../p5-Promise-ES6/lib -Ilib -MNet::HTTP2::Client::Connection::AnyEvent -e'my $h2 = Net::HTTP2::Client::Connection::AnyEvent->new("google.com"); my $cv = AnyEvent->condvar(); $h2->request("GET", "/")->then( sub { print Dumper shift } )->finally($cv); $cv->recv();'
44  
45 2 ▶sub _start_io_if_needed {
Subroutine _start_io_if_needed: called CC=3 SCAR=13.0
46 2 my ($self, $host, $port, $h2) = @_;
47  
48 2 ▶ $self->{'ae_handle'} ||= do {
Condition33%1/3
$self->{"ae_handle"} ||= do { my @tls_opts; if ($self->{"tls_verify_yn"}) { push @tls_opts, ("verify", 1, "verify_peername", "https"); } ; my $tls = "AnyEvent::TLS"->new(@tls_opts); &Net::SSLeay::CTX_set_alpn_protos(scalar $tls->ctx, scalar $self->_ALPN_PROTOS); my $weak_self = $self; builtin::weaken($weak_self); my $read_chunk; "AnyEvent::Handle"->new("connect", [$host, $port], "tls", "connect", "tls_ctx", $tls, "on_error", sub { my($handle, $fatal, $msg) = @_; $weak_self->_on_stream_error($msg); $handle->destroy; } , "on_eof", sub { $weak_self->_on_stream_close if $weak_self; (shift())->destroy; } , "on_read", sub { $read_chunk = $_[0]->rbuf; $_[0]->rbuf = ""; $h2->feed($read_chunk); } ) }
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"ae_handle"} ||= do { my @tls_opts; if ($self->{"tls_verify_yn"}) { push @tls_opts, ("verify", 1, "verify_peername", "https"); } ; my $tls = "AnyEvent::TLS"->new(@tls_opts); &Net::SSLeay::CTX_set_alpn_protos(scalar $tls->ctx, scalar $self->_ALPN_PROTOS); my $weak_self = $self; builtin::weaken($weak_self); my $read_chunk; "AnyEvent::Handle"->new("connect", [$host, $port], "tls", "connect", "tls_ctx", $tls, "on_error", sub { my($handle, $fatal, $msg) = @_; $weak_self->_on_stream_error($msg); $handle->destroy; } , "on_eof", sub { $weak_self->_on_stream_close if $weak_self; (shift())->destroy; } , "on_read", sub { $read_chunk = $_[0]->rbuf; $_[0]->rbuf = ""; $h2->feed($read_chunk); } ) }
$self->{"ae_handle"} do { my @tls_opts; if ($self->{"tls_verify_yn"}) { push @tls_opts, ("verify", 1, "verify_peername", "https"); } ; my $tls = "AnyEvent::TLS"->new(@tls_opts); &Net::SSLeay::CTX_set_alpn_protos(scalar $tls->ctx, scalar $self->_ALPN_PROTOS); my $weak_self = $self; builtin::weaken($weak_self); my $read_chunk; "AnyEvent::Handle"->new("connect", [$host, $port], "tls", "connect", "tls_ctx", $tls, "on_error", sub { my($handle, $fatal, $msg) = @_; $weak_self->_on_stream_error($msg); $handle->destroy; } , "on_eof", sub { $weak_self->_on_stream_close if $weak_self; (shift())->destroy; } , "on_read", sub { $read_chunk = $_[0]->rbuf; $_[0]->rbuf = ""; $h2->feed($read_chunk); } ) }
Truth table33%1/3
A ||= B
A= $self->{"ae_handle"}B= do { my @tls_opts; if ($self->{"tls_verify_yn"}) { push @tls_opts, ("verify", 1, "verify_peername", "https"); } ; my $tls = "AnyEvent::TLS"->new(@tls_opts); &Net::SSLeay::CTX_set_alpn_protos(scalar $tls->ctx, scalar $self->_ALPN_PROTOS); my $weak_self = $self; builtin::weaken($weak_self); my $read_chunk; "AnyEvent::Handle"->new("connect", [$host, $port], "tls", "connect", "tls_ctx", $tls, "on_error", sub { my($handle, $fatal, $msg) = @_; $weak_self->_on_stream_error($msg); $handle->destroy; } , "on_eof", sub { $weak_self->_on_stream_close if $weak_self; (shift())->destroy; } , "on_read", sub { $read_chunk = $_[0]->rbuf; $_[0]->rbuf = ""; $h2->feed($read_chunk); } ) }
ABresult
1X1
011
000
49 2 my @tls_opts;
50  
51 2 ▶ if ($self->{'tls_verify_yn'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"tls_verify_yn"}) 0 2 2
52 0 push @tls_opts, (
53 verify => 1,
54 verify_peername => 'https',
55 );
56 }
57  
58 2 my $tls = AnyEvent::TLS->new(@tls_opts);
59  
60 2 Net::SSLeay::CTX_set_alpn_protos($tls->ctx(), $self->_ALPN_PROTOS());
61  
62 2 my $weak_self = $self;
63 2 Scalar::Util::weaken($weak_self);
64  
65 2 my $read_chunk;
66  
67 AnyEvent::Handle->new(
68 connect => [$host, $port],
69 tls => 'connect',
70 tls_ctx => $tls,
71  
72 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 my ($handle, $fatal, $msg) = @_;
74 0 $weak_self->_on_stream_error($msg);
75 0 $handle->destroy();
76 },
77  
78 0 ▶ on_eof => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
79 0 ▶ $weak_self->_on_stream_close() if $weak_self;
Branch: 1 branch
BranchTrueFalseTotal
if $weak_self 0 0 0
80 0 shift()->destroy();
81 },
82  
83 60 ▶ on_read => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 # NB: This doesn’t empty out rbuf in pre-5.16 perls:
86 # substr( $_[0]->rbuf, 0, length($_[0]->rbuf), q<> )
87 60 $read_chunk = $_[0]->rbuf();
88 60 $_[0]->rbuf() = q<>;
89  
90 60 $h2->feed($read_chunk);
91 },
92 2 );
93 };
94 }
95  
96 14 ▶sub _write_frame {
Subroutine _write_frame: called CC=1 SCAR=0.0
97 14 $_[0]{'ae_handle'}->push_write($_[1]);
98 }
99  
100 1 1;
« HTTP2/RejectorRegistry.pm ↑ Summary HTTP2.pm »