| 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::Server; |
| 11 |
use Protocol::HTTP2::Constants qw(const_name); |
| 12 |
|
| 13 |
Net::SSLeay::initialize(); |
| 14 |
|
| 15 |
my $host = '127.0.0.1'; |
| 16 |
my $port = 8000; |
| 17 |
|
| 18 |
my $w = AnyEvent->condvar; |
| 19 |
|
| 20 |
tcp_server $host, $port, sub { |
| 21 |
my ( $fh, $host, $port ) = @_; |
| 22 |
my $handle; |
| 23 |
|
| 24 |
my $tls; |
| 25 |
eval { |
| 26 |
$tls = AnyEvent::TLS->new( |
| 27 |
method => "TLSv1_2", |
| 28 |
cert_file => "test.crt", |
| 29 |
key_file => "test.key", |
| 30 |
); |
| 31 |
|
| 32 |
# ECDH curve ( Net-SSLeay >= 1.56, openssl >= 1.0.0 ) |
| 33 |
if ( exists &Net::SSLeay::CTX_set_tmp_ecdh ) { |
| 34 |
my $curve = Net::SSLeay::OBJ_txt2nid('prime256v1'); |
| 35 |
my $ecdh = Net::SSLeay::EC_KEY_new_by_curve_name($curve); |
| 36 |
Net::SSLeay::CTX_set_tmp_ecdh( $tls->ctx, $ecdh ); |
| 37 |
Net::SSLeay::EC_KEY_free($ecdh); |
| 38 |
} |
| 39 |
|
| 40 |
# ALPN (Net-SSLeay > 1.55, openssl >= 1.0.2) |
| 41 |
if ( exists &Net::SSLeay::CTX_set_alpn_select_cb ) { |
| 42 |
Net::SSLeay::CTX_set_alpn_select_cb( $tls->ctx, |
| 43 |
[Protocol::HTTP2::ident_tls] ); |
| 44 |
} |
| 45 |
|
| 46 |
# NPN (Net-SSLeay > 1.45, openssl >= 1.0.1) |
| 47 |
elsif ( exists &Net::SSLeay::CTX_set_next_protos_advertised_cb ) { |
| 48 |
Net::SSLeay::CTX_set_next_protos_advertised_cb( $tls->ctx, |
| 49 |
[Protocol::HTTP2::ident_tls] ); |
| 50 |
} |
| 51 |
else { |
| 52 |
die "ALPN and NPN is not supported\n"; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
if ($@) { |
| 57 |
print "Some problem with SSL CTX: $@\n"; |
| 58 |
$w->send; |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
$handle = AnyEvent::Handle->new( |
| 63 |
fh => $fh, |
| 64 |
autocork => 1, |
| 65 |
tls => "accept", |
| 66 |
tls_ctx => $tls, |
| 67 |
on_error => sub { |
| 68 |
$_[0]->destroy; |
| 69 |
print "connection error\n"; |
| 70 |
}, |
| 71 |
on_eof => sub { |
| 72 |
$handle->destroy; |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
my $server; |
| 77 |
$server = Protocol::HTTP2::Server->new( |
| 78 |
on_change_state => sub { |
| 79 |
my ( $stream_id, $previous_state, $current_state ) = @_; |
| 80 |
printf "Stream %i changed state from %s to %s\n", |
| 81 |
$stream_id, const_name( "states", $previous_state ), |
| 82 |
const_name( "states", $current_state ); |
| 83 |
}, |
| 84 |
on_error => sub { |
| 85 |
my $error = shift; |
| 86 |
printf "Error occurred: %s\n", const_name( "errors", $error ); |
| 87 |
}, |
| 88 |
on_request => sub { |
| 89 |
my ( $stream_id, $headers, $data ) = @_; |
| 90 |
my %h = (@$headers); |
| 91 |
|
| 92 |
# Push promise (must be before response) |
| 93 |
if ( $h{':path'} eq '/minil.toml' ) { |
| 94 |
$server->push( |
| 95 |
':authority' => $host . ':' . $port, |
| 96 |
':method' => 'GET', |
| 97 |
':path' => '/css/style.css', |
| 98 |
':scheme' => 'https', |
| 99 |
stream_id => $stream_id, |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
my $message = "hello, world!"; |
| 104 |
$server->response( |
| 105 |
':status' => 200, |
| 106 |
stream_id => $stream_id, |
| 107 |
headers => [ |
| 108 |
'server' => 'perl-Protocol-HTTP2/0.01', |
| 109 |
'content-length' => length($message), |
| 110 |
'cache-control' => 'max-age=3600', |
| 111 |
'date' => 'Fri, 18 Apr 2014 07:27:11 GMT', |
| 112 |
'last-modified' => 'Thu, 27 Feb 2014 10:30:37 GMT', |
| 113 |
], |
| 114 |
data => $message, |
| 115 |
); |
| 116 |
}, |
| 117 |
); |
| 118 |
|
| 119 |
# First send settings to peer |
| 120 |
while ( my $frame = $server->next_frame ) { |
| 121 |
$handle->push_write($frame); |
| 122 |
} |
| 123 |
|
| 124 |
$handle->on_read( |
| 125 |
sub { |
| 126 |
my $handle = shift; |
| 127 |
|
| 128 |
$server->feed( $handle->{rbuf} ); |
| 129 |
|
| 130 |
$handle->{rbuf} = undef; |
| 131 |
while ( my $frame = $server->next_frame ) { |
| 132 |
$handle->push_write($frame); |
| 133 |
} |
| 134 |
$handle->push_shutdown if $server->shutdown; |
| 135 |
} |
| 136 |
); |
| 137 |
}; |
| 138 |
|
| 139 |
$w->recv; |