Debian Codegraph @

examples/client-tls-anyevent.pl

raw
1 use strict;
2 use warnings;
3 use AnyEvent;
4 use AnyEvent::Socket;
5 use AnyEvent::Handle;
6 use Net::SSLeay;
7 use AnyEvent::TLS;
8
9 use Protocol::HTTP2;
10 use Protocol::HTTP2::Client;
11 use Protocol::HTTP2::Constants qw(const_name);
12
13 Net::SSLeay::initialize();
14
15 my $client = Protocol::HTTP2::Client->new(
16 on_change_state => sub {
17 my ( $stream_id, $previous_state, $current_state ) = @_;
18 printf "Stream %i changed state from %s to %s\n",
19 $stream_id, const_name( "states", $previous_state ),
20 const_name( "states", $current_state );
21 },
22 on_push => sub {
23 my ($push_headers) = @_;
24
25 # If we accept PUSH_PROMISE
26 # return callback to receive promised data
27 # return undef otherwise
28 print "Server want to push some resource to us\n";
29
30 return sub {
31 my ( $headers, $data ) = @_;
32 print "Received promised resource\n";
33 }
34 },
35 on_error => sub {
36 my $error = shift;
37 printf "Error occurred: %s\n", const_name( "errors", $error );
38 }
39 );
40
41 my $host = '127.0.0.1';
42 my $port = 8000;
43
44 # Prepare http/2 request
45 $client->request(
46 ':scheme' => "https",
47 ':authority' => $host . ":" . $port,
48 ':path' => "/minil.toml",
49 ':method' => "GET",
50 headers => [
51 'accept' => '*/*',
52 'user-agent' => 'perl-Protocol-HTTP2/0.01',
53 ],
54 on_done => sub {
55 my ( $headers, $data ) = @_;
56 printf "Get headers. Count: %i\n", scalar(@$headers) / 2;
57 printf "Get data. Length: %i\n", length($data);
58 print $data;
59 },
60 );
61
62 my $w = AnyEvent->condvar;
63
64 tcp_connect $host, $port, sub {
65 my ($fh) = @_ or do {
66 print "connection failed: $!\n";
67 $w->send;
68 return;
69 };
70
71 my $tls;
72 eval {
73 $tls = AnyEvent::TLS->new( method => "TLSv1_2", );
74
75 # ALPN (Net-SSLeay > 1.55, openssl >= 1.0.2)
76 if ( exists &Net::SSLeay::CTX_set_alpn_protos ) {
77 Net::SSLeay::CTX_set_alpn_protos( $tls->ctx,
78 [Protocol::HTTP2::ident_tls] );
79 }
80
81 # NPN (Net-SSLeay > 1.45, openssl >= 1.0.1)
82 elsif ( exists &Net::SSLeay::CTX_set_next_proto_select_cb ) {
83 Net::SSLeay::CTX_set_next_proto_select_cb( $tls->ctx,
84 [Protocol::HTTP2::ident_tls] );
85 }
86 else {
87 die "ALPN and NPN is not supported\n";
88 }
89 };
90 if ($@) {
91 print "Some problem with SSL CTX: $@\n";
92 $w->send;
93 return;
94 }
95
96 my $handle;
97 $handle = AnyEvent::Handle->new(
98 fh => $fh,
99 tls => "connect",
100 tls_ctx => $tls,
101 autocork => 1,
102 on_error => sub {
103 $_[0]->destroy;
104 print "connection error\n";
105 $w->send;
106 },
107 on_eof => sub {
108 $handle->destroy;
109 $w->send;
110 }
111 );
112
113 # First write preface to peer
114 while ( my $frame = $client->next_frame ) {
115 $handle->push_write($frame);
116 }
117
118 $handle->on_read(
119 sub {
120 my $handle = shift;
121
122 $client->feed( $handle->{rbuf} );
123
124 $handle->{rbuf} = undef;
125 while ( my $frame = $client->next_frame ) {
126 $handle->push_write($frame);
127 }
128 $handle->push_shutdown if $client->shutdown;
129 }
130 );
131 };
132
133 $w->recv;