Debian Codegraph @

t/lib/PH2ClientServerTest.pm

raw
1 package PH2ClientServerTest;
2 use strict;
3 use warnings;
4 use AnyEvent;
5 use AnyEvent::Socket;
6 use AnyEvent::Handle;
7 use Net::SSLeay;
8 use AnyEvent::TLS;
9
10 use Protocol::HTTP2;
11 use Protocol::HTTP2::Client;
12 use Protocol::HTTP2::Server;
13 use Protocol::HTTP2::Constants qw(const_name);
14
15 use Exporter qw(import);
16 our @EXPORT = qw(client server check_tls);
17 use Carp;
18
19 sub check_tls {
20 my (%opts) = @_;
21 return
22 exists $opts{npn} ? exists &Net::SSLeay::P_next_proto_negotiated
23 : exists $opts{alpn} ? exists &Net::SSLeay::P_alpn_selected
24 : 1;
25 }
26
27 sub server {
28 my (%h) = @_;
29
30 my $cb = delete $h{test_cb} or croak "no servers test_cb";
31 my $port = delete $h{port} or croak "no port available";
32 my $host = delete $h{host};
33 my $tls_crt = delete $h{"tls_crt"};
34 my $tls_key = delete $h{"tls_key"};
35
36 my $w = AnyEvent->condvar;
37
38 tcp_server $host, $port, sub {
39 my ( $fh, $host, $port ) = @_;
40 my $handle;
41 my $tls;
42
43 if ( !$h{upgrade} && ( $h{npn} || $h{alpn} ) ) {
44 eval {
45 $tls = AnyEvent::TLS->new(
46 cert_file => $tls_crt,
47 key_file => $tls_key,
48 );
49
50 if ( $h{npn} ) {
51
52 # NPN (Net-SSLeay > 1.45, openssl >= 1.0.1)
53 Net::SSLeay::CTX_set_next_protos_advertised_cb( $tls->ctx,
54 [Protocol::HTTP2::ident_tls] );
55 }
56 if ( $h{alpn} ) {
57
58 # ALPN (Net-SSLeay > 1.55, openssl >= 1.0.2)
59 Net::SSLeay::CTX_set_alpn_select_cb( $tls->ctx,
60 [Protocol::HTTP2::ident_tls] );
61 }
62 };
63 if ($@) {
64 croak "Some problem with SSL CTX: $@" . Net::SSLeay::print_errs();
65 }
66 }
67
68 $handle = AnyEvent::Handle->new(
69 fh => $fh,
70 autocork => 1,
71 defined $tls
72 ? (
73 tls => "accept",
74 tls_ctx => $tls
75 )
76 : (),
77 on_error => sub {
78 $_[0]->destroy;
79 print STDERR "connection error: $_[2]: $!\n";
80 },
81 on_eof => sub {
82 $handle->destroy;
83 }
84 );
85
86 my $server = Protocol::HTTP2::Server->new(%h);
87 $cb->($server);
88
89 # First send settings to peer
90 while ( my $frame = $server->next_frame ) {
91 $handle->push_write($frame);
92 }
93
94 $handle->on_read(
95 sub {
96 my $handle = shift;
97
98 $server->feed( $handle->{rbuf} );
99
100 $handle->{rbuf} = undef;
101 while ( my $frame = $server->next_frame ) {
102 $handle->push_write($frame);
103 }
104 $handle->push_shutdown if $server->shutdown;
105 }
106 );
107 };
108 my $res = $w->recv;
109 croak("error occurred\n") unless $res;
110 }
111
112 sub client {
113 my (%h) = @_;
114 my $port = delete $h{port} or croak "no port available";
115 my $tls;
116
117 my $host = delete $h{host};
118
119 if ( delete $h{upgrade} ) {
120 $h{upgrade} = 1;
121 }
122 elsif ( $h{npn} || $h{alpn} ) {
123 eval {
124 $tls = AnyEvent::TLS->new();
125
126 if ( delete $h{npn} ) {
127
128 # NPN (Net-SSLeay > 1.45, openssl >= 1.0.1)
129 Net::SSLeay::CTX_set_next_proto_select_cb( $tls->ctx,
130 [Protocol::HTTP2::ident_tls] );
131 }
132 if ( delete $h{alpn} ) {
133
134 # ALPN (Net-SSLeay > 1.55, openssl >= 1.0.2)
135 Net::SSLeay::CTX_set_alpn_protos( $tls->ctx,
136 [Protocol::HTTP2::ident_tls] );
137 }
138 };
139 if ($@) {
140 croak "Some problem with SSL CTX: $@\n";
141 }
142 }
143
144 my $cb = delete $h{test_cb} or croak "no clients test_cb";
145
146 my $client = Protocol::HTTP2::Client->new(%h);
147 $cb->($client);
148
149 my $w = AnyEvent->condvar;
150
151 tcp_connect $host, $port, sub {
152 my ($fh) = @_ or do {
153 print "connection failed: $!\n";
154 $w->send(0);
155 return;
156 };
157
158 my $handle;
159 $handle = AnyEvent::Handle->new(
160 fh => $fh,
161 defined $tls
162 ? (
163 tls => "connect",
164 tls_ctx => $tls,
165 )
166 : (),
167 autocork => 1,
168 on_error => sub {
169 $_[0]->destroy;
170 print STDERR "connection error: $_[2]: $!\n";
171 $w->send(0);
172 },
173 on_eof => sub {
174 $handle->destroy;
175 $w->send(1);
176 }
177 );
178
179 # First write preface to peer
180 while ( my $frame = $client->next_frame ) {
181 $handle->push_write($frame);
182 }
183
184 $handle->on_read(
185 sub {
186 my $handle = shift;
187
188 $client->feed( $handle->{rbuf} );
189
190 $handle->{rbuf} = undef;
191 while ( my $frame = $client->next_frame ) {
192 $handle->push_write($frame);
193 }
194 $handle->push_shutdown if $client->shutdown;
195 }
196 );
197 };
198
199 my $res = $w->recv;
200 croak("error occurred\n") unless $res;
201 }
202
203 1;