.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Protocol::HTTP2::Server 3pm" .TH Protocol::HTTP2::Server 3pm 2024-05-22 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Protocol::HTTP2::Server \- HTTP/2 server .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Protocol::HTTP2::Server; \& \& # You must create tcp server yourself \& use AnyEvent; \& use AnyEvent::Socket; \& use AnyEvent::Handle; \& \& my $w = AnyEvent\->condvar; \& \& # Plain\-text HTTP/2 connection \& tcp_server \*(Aqlocalhost\*(Aq, 8000, sub { \& my ( $fh, $peer_host, $peer_port ) = @_; \& my $handle; \& $handle = AnyEvent::Handle\->new( \& fh => $fh, \& autocork => 1, \& on_error => sub { \& $_[0]\->destroy; \& print "connection error\en"; \& }, \& on_eof => sub { \& $handle\->destroy; \& } \& ); \& \& # Create Protocol::HTTP2::Server object \& my $server; \& $server = Protocol::HTTP2::Server\->new( \& on_request => sub { \& my ( $stream_id, $headers, $data ) = @_; \& my $message = "hello, world!"; \& \& # Response to client \& $server\->response( \& \*(Aq:status\*(Aq => 200, \& stream_id => $stream_id, \& \& # HTTP/1.1 Headers \& headers => [ \& \*(Aqserver\*(Aq => \*(Aqperl\-Protocol\-HTTP2/0.13\*(Aq, \& \*(Aqcontent\-length\*(Aq => length($message), \& \*(Aqcache\-control\*(Aq => \*(Aqmax\-age=3600\*(Aq, \& \*(Aqdate\*(Aq => \*(AqFri, 18 Apr 2014 07:27:11 GMT\*(Aq, \& \*(Aqlast\-modified\*(Aq => \*(AqThu, 27 Feb 2014 10:30:37 GMT\*(Aq, \& ], \& \& # Content \& data => $message, \& ); \& }, \& ); \& \& # First send settings to peer \& while ( my $frame = $server\->next_frame ) { \& $handle\->push_write($frame); \& } \& \& # Receive clients frames \& # Reply to client \& $handle\->on_read( \& sub { \& my $handle = shift; \& \& $server\->feed( $handle\->{rbuf} ); \& \& $handle\->{rbuf} = undef; \& while ( my $frame = $server\->next_frame ) { \& $handle\->push_write($frame); \& } \& $handle\->push_shutdown if $server\->shutdown; \& } \& ); \& }; \& \& $w\->recv; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Protocol::HTTP2::Server is HTTP/2 server library. It's intended to make http2\-server implementations on top of your favorite event loop. .PP See also Shuvgey \- AnyEvent HTTP/2 Server for PSGI based on Protocol::HTTP2::Server. .SS METHODS .IX Subsection "METHODS" \fInew\fR .IX Subsection "new" .PP Initialize new server object .PP .Vb 1 \& my $server = Protocol::HTTP2::Client\->new( %options ); .Ve .PP Available options: .IP "on_request => sub {...}" 4 .IX Item "on_request => sub {...}" Callback invoked when receiving client's requests .Sp .Vb 3 \& on_request => sub { \& # Stream ID, headers array reference and body of request \& my ( $stream_id, $headers, $data ) = @_; \& \& my $message = "hello, world!"; \& $server\->response( \& \*(Aq:status\*(Aq => 200, \& stream_id => $stream_id, \& headers => [ \& \*(Aqserver\*(Aq => \*(Aqperl\-Protocol\-HTTP2/0.13\*(Aq, \& \*(Aqcontent\-length\*(Aq => length($message), \& ], \& data => $message, \& ); \& ... \& }, .Ve .IP "upgrade => 0|1" 4 .IX 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. .Sp See Starting HTTP/2 for "http" URIs .IP "on_error => sub {...}" 4 .IX Item "on_error => sub {...}" Callback invoked on protocol errors .Sp .Vb 4 \& on_error => sub { \& my $error = shift; \& ... \& }, .Ve .IP "on_change_state => sub {...}" 4 .IX Item "on_change_state => sub {...}" Callback invoked every time when http/2 streams change their state. See Stream States .Sp .Vb 4 \& on_change_state => sub { \& my ( $stream_id, $previous_state, $current_state ) = @_; \& ... \& }, .Ve .PP \fIresponse\fR .IX Subsection "response" .PP Prepare response .PP .Vb 2 \& my $message = "hello, world!"; \& $server\->response( \& \& # HTTP/2 status \& \*(Aq:status\*(Aq => 200, \& \& # Stream ID \& stream_id => $stream_id, \& \& # HTTP/1.1 headers \& headers => [ \& \*(Aqserver\*(Aq => \*(Aqperl\-Protocol\-HTTP2/0.01\*(Aq, \& \*(Aqcontent\-length\*(Aq => length($message), \& ], \& \& # Body of response \& data => $message, \& ); .Ve .PP \fIresponse_stream\fR .IX Subsection "response_stream" .PP If body of response is not yet ready or server will stream data .PP .Vb 3 \& # P::H::Server::Stream object \& my $server_stream; \& $server_stream = $server\->response_stream( \& \& # HTTP/2 status \& \*(Aq:status\*(Aq => 200, \& \& # Stream ID \& stream_id => $stream_id, \& \& # HTTP/1.1 headers \& headers => [ \& \*(Aqserver\*(Aq => \*(Aqperl\-Protocol\-HTTP2/0.01\*(Aq, \& ], \& \& # Callback if client abort this stream \& on_cancel => sub { \& ... \& } \& ); \& \& # Send partial data \& $server_stream\->send($chunk_of_data); \& $server_stream\->send($chunk_of_data); \& \& ## 3 ways to finish stream: \& # \& # The best: send last chunk and close stream in one action \& $server_stream\->last($chunk_of_data); \& \& # Close the stream (will send empty frame) \& $server_stream\->close(); \& \& # Destroy object (will send empty frame) \& undef $server_stream .Ve .PP \fIpush\fR .IX Subsection "push" .PP Prepare Push Promise. See Server Push .PP .Vb 4 \& # Example of push inside of on_request callback \& on_request => sub { \& my ( $stream_id, $headers, $data ) = @_; \& my %h = (@$headers); \& \& # Push promise (must be before response) \& if ( $h{\*(Aq:path\*(Aq} eq \*(Aq/index.html\*(Aq ) { \& \& # index.html contain styles.css resource, so server can push \& # "/style.css" to client before it request it to increase speed \& # of loading of whole page \& $server\->push( \& \*(Aq:authority\*(Aq => \*(Aqlocalhost:8000\*(Aq, \& \*(Aq:method\*(Aq => \*(AqGET\*(Aq, \& \*(Aq:path\*(Aq => \*(Aq/style.css\*(Aq, \& \*(Aq:scheme\*(Aq => \*(Aqhttp\*(Aq, \& stream_id => $stream_id, \& ); \& } \& \& $server\->response(...); \& ... \& } .Ve .PP \fIshutdown\fR .IX Subsection "shutdown" .PP Get connection status: .IP "0 \- active" 4 .IX Item "0 - active" .PD 0 .IP "1 \- closed (you can terminate connection)" 4 .IX Item "1 - closed (you can terminate connection)" .PD .PP \fInext_frame\fR .IX Subsection "next_frame" .PP get next frame to send over connection to client. Returns: .IP "undef \- on error" 4 .IX Item "undef - on error" .PD 0 .IP "0 \- nothing to send" 4 .IX Item "0 - nothing to send" .IP "binary string \- encoded frame" 4 .IX Item "binary string - encoded frame" .PD .PP .Vb 4 \& # Example \& while ( my $frame = $server\->next_frame ) { \& syswrite $fh, $frame; \& } .Ve .PP \fIfeed\fR .IX Subsection "feed" .PP Feed decoder with chunks of client's request .PP .Vb 2 \& sysread $fh, $binary_data, 4096; \& $server\->feed($binary_data); .Ve .PP \fIping\fR .IX Subsection "ping" .PP Send ping frame to client (to keep connection alive) .PP .Vb 1 \& $server\->ping .Ve .PP or .PP .Vb 1 \& $server\->ping($payload); .Ve .PP Payload can be arbitrary binary string and must contain 8 octets. If payload argument is omitted server will send random data.