File Coverage

blib/lib/Net/HTTP2/Client/Connection/AnyEvent.pm
Criterion Covered Total %
statement 32 38 84.2
branch 1 4 25.0
condition 1 3 33.3
subroutine 10 12 83.3
pod n/a
total 44 57 77.1


line stmt bran cond sub pod time code
1             package Net::HTTP2::Client::Connection::AnyEvent;
2              
3 1     1   6 use strict;
  1         1  
  1         27  
4 1     1   5 use warnings;
  1         1  
  1         27  
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 to work with L.
29              
30             It requires that L support ALPN.
31              
32             =cut
33              
34             #----------------------------------------------------------------------
35              
36 1     1   4 use parent 'Net::HTTP2::Client::Connection';
  1         2  
  1         3  
37              
38 1     1   563 use AnyEvent::TLS ();
  1         29737  
  1         25  
39 1     1   635 use AnyEvent::Handle ();
  1         6593  
  1         21  
40 1     1   7 use Net::SSLeay ();
  1         1  
  1         12  
41 1     1   4 use Scalar::Util ();
  1         2  
  1         276  
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             sub _start_io_if_needed {
46 2     2   5 my ($self, $host, $port, $h2) = @_;
47              
48 2   33     10 $self->{'ae_handle'} ||= do {
49 2         3 my @tls_opts;
50              
51 2 50       7 if ($self->{'tls_verify_yn'}) {
52 0         0 push @tls_opts, (
53             verify => 1,
54             verify_peername => 'https',
55             );
56             }
57              
58 2         15 my $tls = AnyEvent::TLS->new(@tls_opts);
59              
60 2         9247 Net::SSLeay::CTX_set_alpn_protos($tls->ctx(), $self->_ALPN_PROTOS());
61              
62 2         29 my $weak_self = $self;
63 2         11 Scalar::Util::weaken($weak_self);
64              
65             AnyEvent::Handle->new(
66             connect => [$host, $port],
67             tls => 'connect',
68             tls_ctx => $tls,
69              
70             on_error => sub {
71 0     0   0 my ($handle, $fatal, $msg) = @_;
72 0         0 $weak_self->_on_stream_error($msg);
73 0         0 $handle->destroy();
74             },
75              
76             on_eof => sub {
77 0 0   0   0 $weak_self->_on_stream_close() if $weak_self;
78 0         0 shift()->destroy();
79             },
80              
81             on_read => sub {
82 47     47   232329 $h2->feed(
83             substr($_[0]->rbuf, 0, length $_[0]->rbuf, q<>),
84             );
85             },
86 2         45 );
87             };
88             }
89              
90             sub _write_frame {
91 12     12   35 $_[0]{'ae_handle'}->push_write($_[1]);
92             }
93              
94             1;