| 1 | package Net::HTTP2::Client::Connection::IOAsync; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =encoding utf-8 | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Net::HTTP2::Client::Connection::IOAsync | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $loop = IO::Async::Loop->new(); | ||
| 15 | |||
| 16 | my $h2 = Net::HTTP2::Client::Connection::IOAsync->new( $loop, 'perl.org' ); | ||
| 17 | |||
| 18 | $h2->request("GET", "/")->then( | ||
| 19 | sub ($resp) { | ||
| 20 | print $resp->content(); | ||
| 21 | }, | ||
| 22 | )->finally( sub { $loop->stop() } ); | ||
| 23 | |||
| 24 | $loop->run(); | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | This class extends L<Net::HTTP2::Client::Connection> to work with L<IO::Async>. | ||
| 29 | |||
| 30 | It requires L<IO::Async::SSL>, which as of this writing is a separate | ||
| 31 | distribution. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | #---------------------------------------------------------------------- | ||
| 36 | |||
| 37 | use parent 'Net::HTTP2::Client::Connection'; | ||
| 38 | |||
| 39 | use IO::Async::SSL (); | ||
| 40 | use Scalar::Util (); | ||
| 41 | |||
| 42 | use Net::HTTP2::IOSocketSSL (); | ||
| 43 | |||
| 44 | # perl -I ../p5-X-Tiny/lib -MIO::Async::Loop -MData::Dumper -MAnyEvent -I ../p5-IO-SigGuard/lib -I ../p5-Promise-ES6/lib -Ilib -MNet::HTTP2::Client::Connection::IOAsync -e'my $loop = IO::Async::Loop->new(); my $pool = Net::HTTP2::Client::Connection::IOAsync->new($loop, "google.com"); $pool->request("GET", "/")->then( sub { print Dumper shift } )->finally(sub { $loop->stop() }); $loop->run()' | ||
| 45 | |||
| 46 | #$IO::Socket::SSL::DEBUG = 5; | ||
| 47 | #$Net::SSLeay::trace = 8; | ||
| 48 | |||
| 49 | sub _parse_args { | ||
| 50 | my ($class, $loop) = splice @_, 0, 2; | ||
| 51 | |||
| 52 | return ( | ||
| 53 | $class->SUPER::_parse_args(@_), | ||
| 54 | loop => $loop, | ||
| 55 | ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub _start_io_if_needed { | ||
| 59 | my ($self, $host, $port, $h2) = @_; | ||
| 60 | |||
| 61 | if (!$self->{'_io_started'}) { | ||
| 62 | $self->{'_io_started'} = 1; | ||
| 63 | |||
| 64 | my $stream_sr = \$self->{'stream'}; | ||
| 65 | |||
| 66 | my $prebuf_sr = \$self->{'prebuf'}; | ||
| 67 | |||
| 68 | my $weak_self = $self; | ||
| 69 | Scalar::Util::weaken($weak_self); | ||
| 70 | |||
| 71 | $self->{'loop'}->SSL_connect( | ||
| 72 | host => $host, | ||
| 73 | service => $port, | ||
| 74 | |||
| 75 | Net::HTTP2::IOSocketSSL::tls_proto_args(), | ||
| 76 | Net::HTTP2::IOSocketSSL::verify_args_from_boolean($self->{'tls_verify_yn'}), | ||
| 77 | |||
| 78 | SSL_hostname => $host, | ||
| 79 | |||
| 80 | on_stream => sub { | ||
| 81 | my $stream = $_[0]; | ||
| 82 | |||
| 83 | $stream_sr = \$stream; | ||
| 84 | |||
| 85 | $stream->configure( | ||
| 86 | on_read => sub { | ||
| 87 | $h2->feed( | ||
| 88 | substr( ${ $_[1] }, 0, length ${ $_[1] }, q<> ), | ||
| 89 | ); | ||
| 90 | }, | ||
| 91 | |||
| 92 | on_read_eof => sub { | ||
| 93 | $weak_self->_on_stream_close() if $weak_self; | ||
| 94 | }, | ||
| 95 | |||
| 96 | on_read_error => sub { | ||
| 97 | my $err = shift; | ||
| 98 | $weak_self->_on_stream_error($err); | ||
| 99 | }, | ||
| 100 | |||
| 101 | on_write_error => sub { | ||
| 102 | my $err = shift; | ||
| 103 | $weak_self->_on_stream_error($err); | ||
| 104 | }, | ||
| 105 | ); | ||
| 106 | |||
| 107 | $stream->write($$prebuf_sr) if $$prebuf_sr; | ||
| 108 | |||
| 109 | $self->{'loop'}->add($stream); | ||
| 110 | }, | ||
| 111 | |||
| 112 | on_resolve_error => sub { | ||
| 113 | print STDERR Dumper( resolve => @_ ); | ||
| 114 | }, | ||
| 115 | on_connect_error => sub { | ||
| 116 | use Data::Dumper; | ||
| 117 | print STDERR Dumper( connect => @_ ); | ||
| 118 | }, | ||
| 119 | on_ssl_error => sub { | ||
| 120 | use Data::Dumper; | ||
| 121 | print STDERR Dumper( tls => @_ ); | ||
| 122 | }, | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | |||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _write_frame { | ||
| 130 | if (my $stream = $_[0]{'stream'}) { | ||
| 131 | $stream->write($_[1]); | ||
| 132 | } | ||
| 133 | else { | ||
| 134 | $_[0]{'prebuf'} .= $_[1]; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; |