/usr/share/perl5/Protocol/HTTP2/Client.pm is in libprotocol-http2-perl 1.08-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 | package Protocol::HTTP2::Client;
use strict;
use warnings;
use Protocol::HTTP2::Connection;
use Protocol::HTTP2::Constants qw(:frame_types :flags :states :endpoints
:errors);
use Protocol::HTTP2::Trace qw(tracer);
use Carp;
use Scalar::Util ();
=encoding utf-8
=head1 NAME
Protocol::HTTP2::Client - HTTP/2 client
=head1 SYNOPSIS
use Protocol::HTTP2::Client;
# Create client object
my $client = Protocol::HTTP2::Client->new;
# Prepare first request
$client->request(
# HTTP/2 headers
':scheme' => 'http',
':authority' => 'localhost:8000',
':path' => '/',
':method' => 'GET',
# HTTP/1.1 headers
headers => [
'accept' => '*/*',
'user-agent' => 'perl-Protocol-HTTP2/0.13',
],
# Callback when receive server's response
on_done => sub {
my ( $headers, $data ) = @_;
...
},
);
# Protocol::HTTP2 is just HTTP/2 protocol decoder/encoder
# so you must create connection yourself
use AnyEvent;
use AnyEvent::Socket;
use AnyEvent::Handle;
my $w = AnyEvent->condvar;
# Plain-text HTTP/2 connection
tcp_connect 'localhost', 8000, sub {
my ($fh) = @_ or die "connection failed: $!\n";
my $handle;
$handle = AnyEvent::Handle->new(
fh => $fh,
autocork => 1,
on_error => sub {
$_[0]->destroy;
print "connection error\n";
$w->send;
},
on_eof => sub {
$handle->destroy;
$w->send;
}
);
# First write preface to peer
while ( my $frame = $client->next_frame ) {
$handle->push_write($frame);
}
# Receive servers frames
# Reply to server
$handle->on_read(
sub {
my $handle = shift;
$client->feed( $handle->{rbuf} );
$handle->{rbuf} = undef;
while ( my $frame = $client->next_frame ) {
$handle->push_write($frame);
}
# Terminate connection if all done
$handle->push_shutdown if $client->shutdown;
}
);
};
$w->recv;
=head1 DESCRIPTION
Protocol::HTTP2::Client is HTTP/2 client library. It's intended to make
http2-client implementations on top of your favorite event-loop.
=head2 METHODS
=head3 new
Initialize new client object
my $client = Procotol::HTTP2::Client->new( %options );
Available options:
=over
=item on_push => sub {...}
If server send push promise this callback will be invoked
on_push => sub {
# received PUSH PROMISE headers
my $pp_header = shift;
...
# if we want reject this push
# return undef
# if we want to accept pushed resource
# return callback to receive data
return sub {
my ( $headers, $data ) = @_;
...
}
},
=item upgrade => 0|1
Use HTTP/1.1 Upgrade to upgrade protocol from HTTP/1.1 to HTTP/2. Upgrade
possible only on plain (non-tls) connection. Default value is 0.
See
L<Starting HTTP/2 for "http" URIs|https://tools.ietf.org/html/rfc7540#section-3.2>
=item keepalive => 0|1
Keep connection alive after requests. Default value is 0. Don't forget to
explicitly call close method if set this to true.
=item on_error => sub {...}
Callback invoked on protocol errors
on_error => sub {
my $error = shift;
...
},
=item on_change_state => sub {...}
Callback invoked every time when http/2 streams change their state.
See
L<Stream States|https://tools.ietf.org/html/rfc7540#section-5.1>
on_change_state => sub {
my ( $stream_id, $previous_state, $current_state ) = @_;
...
},
=back
=cut
sub new {
my ( $class, %opts ) = @_;
my $self = {
con => undef,
input => '',
active_streams => 0,
keepalive => exists $opts{keepalive}
? delete $opts{keepalive}
: 0,
settings => exists $opts{settings} ? $opts{settings} : {},
};
if ( exists $opts{on_push} ) {
Scalar::Util::weaken( my $self = $self );
my $cb = delete $opts{on_push};
$opts{on_new_peer_stream} = sub {
my $stream_id = shift;
my $pp_headers;
$self->active_streams(+1);
$self->{con}->stream_cb(
$stream_id,
RESERVED,
sub {
my $res =
$cb->( $self->{con}->stream_pp_headers($stream_id) );
if ( $res && ref $cb eq 'CODE' ) {
$self->{con}->stream_cb(
$stream_id,
CLOSED,
sub {
$res->(
$self->{con}->stream_headers($stream_id),
$self->{con}->stream_data($stream_id),
);
$self->active_streams(-1);
}
);
}
else {
$self->{con}
->stream_error( $stream_id, REFUSED_STREAM );
$self->active_streams(-1);
}
}
);
};
}
$self->{con} = Protocol::HTTP2::Connection->new( CLIENT, %opts );
bless $self, $class;
}
sub active_streams {
my $self = shift;
my $add = shift || 0;
$self->{active_streams} += $add;
$self->{con}->finish
unless $self->{active_streams} > 0
|| $self->{keepalive};
}
=head3 request
Prepare HTTP/2 request.
$client->request(
# HTTP/2 headers
':scheme' => 'http',
':authority' => 'localhost:8000',
':path' => '/items',
':method' => 'POST',
# HTTP/1.1 headers
headers => [
'content-type' => 'application/x-www-form-urlencoded',
'user-agent' => 'perl-Protocol-HTTP2/0.06',
],
# Callback when receive server's response
on_done => sub {
my ( $headers, $data ) = @_;
...
},
# Callback when receive stream reset
on_error => sub {
my $error_code = shift;
},
# Body of POST request
data => "hello=world&test=done",
);
You can chaining request one by one:
$client->request( 1-st request )->request( 2-nd request );
Available callbacks:
=over
=item on_done => sub {...}
Invoked when full servers response is available
on_done => sub {
my ( $headers, $data ) = @_;
...
},
=item on_headers => sub {...}
Invoked as soon as headers have been successfully received from the server
on_headers => sub {
my $headers = shift;
...
# if we want reject any data
# return undef
# continue
return 1
}
=item on_data => sub {...}
If specified all data will be passed to this callback instead if on_done.
on_done will receive empty string.
on_data => sub {
my ( $partial_data, $headers ) = @_;
...
# if we want cancel download
# return undef
# continue downloading
return 1
}
=item on_error => sub {...}
Callback invoked on stream errors
on_error => sub {
my $error = shift;
...
}
=back
=cut
my @must = (qw(:authority :method :path :scheme));
sub request {
my ( $self, %h ) = @_;
my @miss = grep { !exists $h{$_} } @must;
croak "Missing fields in request: @miss" if @miss;
my $con = $self->{con};
my $stream_id = $con->new_stream;
unless ( defined $stream_id ) {
if ( exists $con->{on_error} ) {
$con->{on_error}->(PROTOCOL_ERROR);
return $self;
}
else {
croak "Can't create new stream, connection is closed";
}
}
$self->active_streams(+1);
if ( $con->upgrade && !exists $self->{sent_upgrade} ) {
$con->enqueue_raw(
$con->upgrade_request(
( map { $_ => $h{$_} } @must ),
headers => exists $h{headers} ? $h{headers} : []
)
);
$self->{sent_upgrade} = 1;
$con->stream_state( $stream_id, HALF_CLOSED );
}
else {
if ( !$con->preface ) {
$con->enqueue_raw( $con->preface_encode ),
$con->enqueue( SETTINGS, 0, 0, $self->{settings} );
$con->preface(1);
}
$con->send_headers(
$stream_id,
[
( map { $_ => $h{$_} } @must ),
exists $h{headers} ? @{ $h{headers} } : ()
],
exists $h{data} ? 0 : 1
);
$con->send_data( $stream_id, $h{data}, 1 ) if exists $h{data};
}
Scalar::Util::weaken $self;
Scalar::Util::weaken $con;
$con->stream_cb(
$stream_id,
CLOSED,
sub {
if ( exists $h{on_error} && $con->stream_reset($stream_id) ) {
$h{on_error}->( $con->stream_reset($stream_id) );
}
else {
$h{on_done}->(
$con->stream_headers($stream_id),
$con->stream_data($stream_id),
);
}
$self->active_streams(-1);
}
) if exists $h{on_done};
$con->stream_frame_cb(
$stream_id,
HEADERS,
sub {
my $res = $h{on_headers}->( $_[0] );
return if $res;
$con->stream_error( $stream_id, REFUSED_STREAM );
}
) if exists $h{on_headers};
$con->stream_frame_cb(
$stream_id,
DATA,
sub {
my $res = $h{on_data}->( $_[0], $con->stream_headers($stream_id), );
return if $res;
$con->stream_error( $stream_id, REFUSED_STREAM );
}
) if exists $h{on_data};
return $self;
}
=head3 keepalive
Keep connection alive after requests
my $bool = $client->keepalive;
$client = $client->keepalive($bool);
=cut
sub keepalive {
my $self = shift;
return @_
? scalar( $self->{keepalive} = shift, $self )
: $self->{keepalive};
}
=head3 shutdown
Get connection status:
=over
=item 0 - active
=item 1 - closed (you can terminate connection)
=back
=cut
sub shutdown {
shift->{con}->shutdown;
}
=head3 close
Explicitly close connection (send GOAWAY frame). This is required if client
has keepalive option enabled.
=cut
sub close {
shift->{con}->finish;
}
=head3 next_frame
get next frame to send over connection to server.
Returns:
=over
=item undef - on error
=item 0 - nothing to send
=item binary string - encoded frame
=back
# Example
while ( my $frame = $client->next_frame ) {
syswrite $fh, $frame;
}
=cut
sub next_frame {
my $self = shift;
my $frame = $self->{con}->dequeue;
tracer->debug("send one frame to wire\n") if $frame;
return $frame;
}
=head3 feed
Feed decoder with chunks of server's response
sysread $fh, $binary_data, 4096;
$client->feed($binary_data);
=cut
sub feed {
my ( $self, $chunk ) = @_;
$self->{input} .= $chunk;
my $offset = 0;
my $len;
my $con = $self->{con};
tracer->debug( "got " . length($chunk) . " bytes on a wire\n" );
if ( $con->upgrade ) {
$len = $con->decode_upgrade_response( \$self->{input}, $offset );
$con->shutdown(1) unless defined $len;
return unless $len;
$offset += $len;
$con->upgrade(0);
$con->enqueue_raw( $con->preface_encode );
$con->preface(1);
}
while ( $len = $con->frame_decode( \$self->{input}, $offset ) ) {
tracer->debug("decoded frame at $offset, length $len\n");
$offset += $len;
}
substr( $self->{input}, 0, $offset ) = '' if $offset;
}
=head3 ping
Send ping frame to server (to keep connection alive)
$client->ping
or
$client->ping($payload);
Payload can be arbitrary binary string and must contain 8 octets. If payload argument
is omitted client will send random data.
=cut
sub ping {
shift->{con}->send_ping(@_);
}
1;
|