Debian Codegraph @

examples/client-io-socket-ssl.pl

raw
1 use strict;
2 use warnings;
3
4 use Protocol::HTTP2::Client;
5 use IO::Socket::SSL;
6 use IO::Select;
7
8 my $host = 'example.com';
9 my $port = 443;
10
11 # POST request
12 my $h2_client = Protocol::HTTP2::Client->new->request(
13
14 # HTTP/2-headers
15 ':method' => 'POST',
16 ':path' => '/api/datas',
17 ':scheme' => 'https',
18 ':authority' => $host . ':' . $port,
19
20 # HTTP-headers
21 headers => [
22 'user-agent' => 'Protocol::HTTP2',
23 'content-type' => 'application/json'
24 ],
25
26 # do something useful with data
27 on_done => sub {
28 my ( $headers, $data ) = @_;
29
30 },
31
32 # POST body
33 data => '{ "data" : "test" }',
34 );
35
36 # TLS transport socket
37 my $client = IO::Socket::SSL->new(
38 PeerHost => $host,
39 PeerPort => $port,
40
41 # openssl 1.0.1 support only NPN
42 #SSL_npn_protocols => ['h2'],
43
44 # openssl 1.0.2 also have ALPN
45 SSL_alpn_protocols => ['h2'],
46 ) or die $!;
47
48 # non blocking
49 $client->blocking(0);
50
51 my $sel = IO::Select->new($client);
52
53 # send/recv frames until request is done
54 while ( !$h2_client->shutdown ) {
55 $sel->can_write;
56 while ( my $frame = $h2_client->next_frame ) {
57 syswrite $client, $frame;
58 }
59
60 $sel->can_read;
61 while ( sysread $client, my $data, 4096 ) {
62 $h2_client->feed($data);
63 }
64 }
65