.\" Automatically generated by Pod::Man 4.14 (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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . 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 .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Protocol::HTTP2::Server 3pm" .TH Protocol::HTTP2::Server 3pm "2022-12-04" "perl v5.36.0" "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 \s-1HTTP/2\s0 server library. It's intended to make http2\-server implementations on top of your favorite event loop. .PP See also Shuvgey \- AnyEvent \s-1HTTP/2\s0 Server for \s-1PSGI\s0 based on Protocol::HTTP2::Server. .SS "\s-1METHODS\s0" .IX Subsection "METHODS" \fInew\fR .IX Subsection "new" .PP Initialize new server object .PP .Vb 1 \& my $server = Procotol::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 \s-1HTTP/1.1\s0 Upgrade to upgrade protocol from \s-1HTTP/1.1\s0 to \s-1HTTP/2.\s0 Upgrade possible only on plain (non-tls) connection. .Sp See Starting \s-1HTTP/2\s0 for \*(L"http\*(R" 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 => \*(Aqlocahost: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.