| 1 |
package Protocol::HTTP2::Frame; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Protocol::HTTP2::Trace qw(tracer); |
| 5 |
use Protocol::HTTP2::Constants |
| 6 |
qw(const_name :frame_types :errors :preface :states :flags :limits :settings); |
| 7 |
use Protocol::HTTP2::Frame::Data; |
| 8 |
use Protocol::HTTP2::Frame::Headers; |
| 9 |
use Protocol::HTTP2::Frame::Priority; |
| 10 |
use Protocol::HTTP2::Frame::Rst_stream; |
| 11 |
use Protocol::HTTP2::Frame::Settings; |
| 12 |
use Protocol::HTTP2::Frame::Push_promise; |
| 13 |
use Protocol::HTTP2::Frame::Ping; |
| 14 |
use Protocol::HTTP2::Frame::Goaway; |
| 15 |
use Protocol::HTTP2::Frame::Window_update; |
| 16 |
use Protocol::HTTP2::Frame::Continuation; |
| 17 |
|
| 18 |
# Table of payload decoders |
| 19 |
my %frame_class = ( |
| 20 |
&DATA => 'Data', |
| 21 |
&HEADERS => 'Headers', |
| 22 |
&PRIORITY => 'Priority', |
| 23 |
&RST_STREAM => 'Rst_stream', |
| 24 |
&SETTINGS => 'Settings', |
| 25 |
&PUSH_PROMISE => 'Push_promise', |
| 26 |
&PING => 'Ping', |
| 27 |
&GOAWAY => 'Goaway', |
| 28 |
&WINDOW_UPDATE => 'Window_update', |
| 29 |
&CONTINUATION => 'Continuation', |
| 30 |
); |
| 31 |
|
| 32 |
my %decoder = |
| 33 |
map { $_ => \&{ 'Protocol::HTTP2::Frame::' . $frame_class{$_} . '::decode' } } |
| 34 |
keys %frame_class; |
| 35 |
|
| 36 |
my %encoder = |
| 37 |
map { $_ => \&{ 'Protocol::HTTP2::Frame::' . $frame_class{$_} . '::encode' } } |
| 38 |
keys %frame_class; |
| 39 |
|
| 40 |
sub frame_encode { |
| 41 |
my ( $con, $type, $flags, $stream_id, $data_ref ) = @_; |
| 42 |
|
| 43 |
my $payload = $encoder{$type}->( $con, \$flags, $stream_id, $data_ref ); |
| 44 |
my $l = length $payload; |
| 45 |
|
| 46 |
pack( 'CnC2N', ( $l >> 16 ), ( $l & 0xFFFF ), $type, $flags, $stream_id ) |
| 47 |
. $payload; |
| 48 |
} |
| 49 |
|
| 50 |
sub preface_decode { |
| 51 |
my ( $con, $buf_ref, $buf_offset ) = @_; |
| 52 |
return 0 if length($$buf_ref) - $buf_offset < length(PREFACE); |
| 53 |
return |
| 54 |
index( $$buf_ref, PREFACE, $buf_offset ) == -1 ? undef : length(PREFACE); |
| 55 |
} |
| 56 |
|
| 57 |
sub preface_encode { |
| 58 |
PREFACE; |
| 59 |
} |
| 60 |
|
| 61 |
sub frame_header_decode { |
| 62 |
my ( undef, $buf_ref, $buf_offset ) = @_; |
| 63 |
|
| 64 |
my ( $hl, $ll, $type, $flags, $stream_id ) = |
| 65 |
unpack( 'CnC2N', substr( $$buf_ref, $buf_offset, FRAME_HEADER_SIZE ) ); |
| 66 |
|
| 67 |
my $length = ( $hl << 16 ) + $ll; |
| 68 |
$stream_id &= 0x7FFF_FFFF; |
| 69 |
return $length, $type, $flags, $stream_id; |
| 70 |
} |
| 71 |
|
| 72 |
sub frame_decode { |
| 73 |
my ( $con, $buf_ref, $buf_offset ) = @_; |
| 74 |
return 0 if length($$buf_ref) - $buf_offset < FRAME_HEADER_SIZE; |
| 75 |
|
| 76 |
my ( $length, $type, $flags, $stream_id ) = |
| 77 |
$con->frame_header_decode( $buf_ref, $buf_offset ); |
| 78 |
|
| 79 |
if ( $length > $con->dec_setting(SETTINGS_MAX_FRAME_SIZE) ) { |
| 80 |
tracer->error("Frame is too large: $length\n"); |
| 81 |
$con->error(FRAME_SIZE_ERROR); |
| 82 |
return undef; |
| 83 |
} |
| 84 |
|
| 85 |
return 0 |
| 86 |
if length($$buf_ref) - $buf_offset - FRAME_HEADER_SIZE - $length < 0; |
| 87 |
|
| 88 |
tracer->debug( |
| 89 |
sprintf "TYPE = %s(%i), FLAGS = %08b, STREAM_ID = %i, " |
| 90 |
. "LENGTH = %i\n", |
| 91 |
exists $frame_class{$type} |
| 92 |
? const_name( "frame_types", $type ) |
| 93 |
: "UNKNOWN", |
| 94 |
$type, |
| 95 |
$flags, |
| 96 |
$stream_id, |
| 97 |
$length |
| 98 |
); |
| 99 |
|
| 100 |
my $pending_stream_id = $con->pending_stream; |
| 101 |
if ( $pending_stream_id |
| 102 |
&& ( $type != CONTINUATION || $pending_stream_id != $stream_id ) ) |
| 103 |
{ |
| 104 |
tracer->debug("Expected CONTINUATION for stream $pending_stream_id"); |
| 105 |
$con->error(PROTOCOL_ERROR); |
| 106 |
return undef; |
| 107 |
} |
| 108 |
|
| 109 |
# Unknown type of frame |
| 110 |
if ( !exists $frame_class{$type} ) { |
| 111 |
tracer->info("ignore unknown frame type $type"); |
| 112 |
return FRAME_HEADER_SIZE + $length; |
| 113 |
} |
| 114 |
|
| 115 |
$con->decode_context->{frame} = { |
| 116 |
type => $type, |
| 117 |
flags => $flags, |
| 118 |
length => $length, |
| 119 |
stream => $stream_id, |
| 120 |
}; |
| 121 |
|
| 122 |
# Try to create new stream structure |
| 123 |
if ( $stream_id |
| 124 |
&& !$con->stream($stream_id) |
| 125 |
&& !$con->new_peer_stream($stream_id) ) |
| 126 |
{ |
| 127 |
return $con->error ? undef : FRAME_HEADER_SIZE + $length; |
| 128 |
} |
| 129 |
|
| 130 |
return undef |
| 131 |
unless defined $decoder{$type} |
| 132 |
->( $con, $buf_ref, $buf_offset + FRAME_HEADER_SIZE, $length ); |
| 133 |
|
| 134 |
# Arrived frame may change state of stream |
| 135 |
$con->state_machine( 'recv', $type, $flags, $stream_id ); |
| 136 |
|
| 137 |
return FRAME_HEADER_SIZE + $length; |
| 138 |
} |
| 139 |
|
| 140 |
=pod |
| 141 |
|
| 142 |
=head1 NOTES |
| 143 |
|
| 144 |
=head2 Frame Types vs Flags and Stream ID |
| 145 |
|
| 146 |
Table represent possible combination of frame types and flags. |
| 147 |
Last column -- Stream ID of frame types (x -- sid >= 1, 0 -- sid = 0) |
| 148 |
|
| 149 |
|
| 150 |
+-END_STREAM 0x1 |
| 151 |
| +-ACK 0x1 |
| 152 |
| | +-END_HEADERS 0x4 |
| 153 |
| | | +-PADDED 0x8 |
| 154 |
| | | | +-PRIORITY 0x20 |
| 155 |
| | | | | +-stream id (value) |
| 156 |
| | | | | | |
| 157 |
| frame type\flag | V | V | V | V | V | | V | |
| 158 |
| --------------- |:-:|:-:|:-:|:-:|:-:| - |:---:| |
| 159 |
| DATA | x | | | x | | | x | |
| 160 |
| HEADERS | x | | x | x | x | | x | |
| 161 |
| PRIORITY | | | | | | | x | |
| 162 |
| RST_STREAM | | | | | | | x | |
| 163 |
| SETTINGS | | x | | | | | 0 | |
| 164 |
| PUSH_PROMISE | | | x | x | | | x | |
| 165 |
| PING | | x | | | | | 0 | |
| 166 |
| GOAWAY | | | | | | | 0 | |
| 167 |
| WINDOW_UPDATE | | | | | | | 0/x | |
| 168 |
| CONTINUATION | | | x | x | | | x | |
| 169 |
|
| 170 |
=cut |
| 171 |
|
| 172 |
1; |