HTTP2/Client/Connection/Mojo.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 100 CC 6 SCAR 23.7
CC
6
Cov
0%
_start_io_if_needed
30.0
_write_frame
6.0
_parse_event_args
2.0
CRAP 10.7
1 package Net::HTTP2::Client::Connection::Mojo;
2  
3 use strict;
4 use warnings;
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 Net::HTTP2::Client::Connection::Mojo
11  
12 =head1 SYNOPSIS
13  
14 my $h2 = Net::HTTP2::Client::Connection::Mojo->new("perl.org");
15  
16 $h2->request("GET", "/")->then(
17 sub ($resp) {
18 print $resp->content();
19 },
20 )->wait();
21  
22 =head1 DESCRIPTION
23  
24 This class extends L<Net::HTTP2::Client::Connection> to work with L<Mojolicious>.
25  
26 =head1 PROMISES
27  
28 This class’s returned promises are instances of L<Mojo::Promise> rather than
29 L<Promise::ES6>.
30  
31 =cut
32  
33 #----------------------------------------------------------------------
34  
35 use parent 'Net::HTTP2::Client::Connection';
36  
37 use Scalar::Util ();
38  
39 use Mojo::IOLoop::Client ();
40 use Mojo::Promise ();
41  
42 use Net::HTTP2::IOSocketSSL;
43  
44 use constant _PROMISE_CLASS => 'Mojo::Promise';
45  
46 # perl -I ../p5-X-Tiny/lib -MCarp::Always -MData::Dumper -I ../p5-Promise-ES6/lib -Ilib -MNet::HTTP2::Client::Connection::Mojo -e'my $h2 = Net::HTTP2::Client::Connection::Mojo->new("google.com"); $h2->request("GET", "/")->then( sub { print Dumper shift } )->wait()'
47  
48 #$IO::Socket::SSL::DEBUG = 5;
49 #$Net::SSLeay::trace = 8;
50  
51 use constant _INIT_OPTS => (
52 __PACKAGE__->SUPER::_INIT_OPTS(),
53 'reactor',
54 );
55  
56 sub _parse_event_args {
57 my ($self, %opts) = @_;
58  
59 return ( reactor => $opts{'reactor'} );
60 }
61  
62 sub _start_io_if_needed {
63 my ($self, $host, $port, $h2) = @_;
64  
65 $self->{'client'} ||= do {
66 my $stream_sr = \$self->{'stream'};
67 my $prebuf_sr = \$self->{'prebuf'};
68  
69 my $reactor = $self->{'reactor'};
70  
71 my $client = Mojo::IOLoop::Client->new();
72 $client->reactor($reactor) if $reactor;
73  
74 Scalar::Util::weaken($h2);
75  
76 my $weak_self = $self;
77 Scalar::Util::weaken($weak_self);
78  
79 my $rejectors = $self->{'rejectors'} or die 'no rejectors';
80  
81 $client->on( connect => sub {
82 $$stream_sr = Mojo::IOLoop::Stream->new(pop);
83 $$stream_sr->reactor($reactor) if $reactor;
84 $$stream_sr->start();
85  
86 $$stream_sr->on( error => sub {
87 my ($stream, $err) = @_;
88 $weak_self->_on_stream_error($err);
89 } );
90  
91 $$stream_sr->on( close => sub {
92  
93 # At global destruction $weak_self might no longer exist.
94 #
95 $weak_self->_on_stream_close() if $weak_self;
96 } );
97  
98 $$stream_sr->on( read => sub {
99 my ($stream) = @_;
100  
101 $h2->feed(pop);
102  
103 while ( my $frame = $h2->next_frame() ) {
104 $stream->write($frame);
105 }
106 } );
107  
108 $$stream_sr->write($$prebuf_sr) if $$prebuf_sr;
109 } );
110  
111 $client->on( error => sub {
112 my ($client, $err) = @_;
113  
114 $rejectors->reject_all($err);
115 } );
116  
117 $client->connect(
118 address => $host,
119 port => $port,
120 tls => 1,
121 tls_options => {
122 Net::HTTP2::IOSocketSSL::tls_proto_args(),
123 Net::HTTP2::IOSocketSSL::verify_args_from_boolean($self->{'tls_verify_yn'}),
124 },
125 );
126  
127 $client;
128 };
129  
130 return;
131 }
132  
133 sub _write_frame {
134 if (my $stream = $_[0]{'stream'}) {
135 $stream->write(@_[1, 2]);
136 }
137 else {
138 $_[0]{'prebuf'} .= $_[1];
139 }
140 }
141  
142 1;