| 1 | package Protocol::HTTP2::Frame; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use Protocol::HTTP2::Trace qw(tracer); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | use Protocol::HTTP2::Constants | ||||||||||||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | qw(const_name :frame_types :errors :preface :states :flags :limits :settings); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Protocol::HTTP2::Frame::Data; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Protocol::HTTP2::Frame::Headers; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use Protocol::HTTP2::Frame::Priority; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use Protocol::HTTP2::Frame::Rst_stream; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 13 | ▶ | use Protocol::HTTP2::Frame::Settings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 13 | ▶ | use Protocol::HTTP2::Frame::Push_promise; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 13 | ▶ | use Protocol::HTTP2::Frame::Ping; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use Protocol::HTTP2::Frame::Goaway; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 13 | ▶ | use Protocol::HTTP2::Frame::Window_update; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use Protocol::HTTP2::Frame::Continuation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | # Table of payload decoders | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 13 | 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 | 13 | map { $_ => \&{ 'Protocol::HTTP2::Frame::' . $frame_class{$_} . '::decode' } } | |||||||||||||||||||||||||||||||||||||||||
| 34 | keys %frame_class; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | my %encoder = | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 13 | map { $_ => \&{ 'Protocol::HTTP2::Frame::' . $frame_class{$_} . '::encode' } } | |||||||||||||||||||||||||||||||||||||||||
| 38 | keys %frame_class; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 228 | ▶ | sub frame_encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frame_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 228 | my ( $con, $type, $flags, $stream_id, $data_ref ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 228 | my $payload = $encoder{$type}->( $con, \$flags, $stream_id, $data_ref ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 228 | my $l = length $payload; | |||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 228 | pack( 'CnC2N', ( $l >> 16 ), ( $l & 0xFFFF ), $type, $flags, $stream_id ) | |||||||||||||||||||||||||||||||||||||||||
| 47 | . $payload; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 16 | ▶ | sub preface_decode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preface_decode: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 16 | my ( $con, $buf_ref, $buf_offset ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 16 | ▶ | return 0 if length($$buf_ref) - $buf_offset < length(PREFACE); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | return | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 16 | ▶ | index( $$buf_ref, PREFACE, $buf_offset ) == -1 ? undef : length(PREFACE); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 20 | ▶ | sub preface_encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
preface_encode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | PREFACE; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 326 | ▶ | sub frame_header_decode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frame_header_decode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 326 | my ( undef, $buf_ref, $buf_offset ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 326 | my ( $hl, $ll, $type, $flags, $stream_id ) = | |||||||||||||||||||||||||||||||||||||||||
| 65 | unpack( 'CnC2N', substr( $$buf_ref, $buf_offset, FRAME_HEADER_SIZE ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 326 | my $length = ( $hl << 16 ) + $ll; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 326 | $stream_id &= 0x7FFF_FFFF; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 326 | return $length, $type, $flags, $stream_id; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 433 | ▶ | sub frame_decode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
frame_decode: called CC=14 SCAR=32.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 433 | my ( $con, $buf_ref, $buf_offset ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 433 | ▶ | return 0 if length($$buf_ref) - $buf_offset < FRAME_HEADER_SIZE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 214 | my ( $length, $type, $flags, $stream_id ) = | |||||||||||||||||||||||||||||||||||||||||
| 77 | $con->frame_header_decode( $buf_ref, $buf_offset ); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 214 | ▶ | if ( $length > $con->dec_setting(SETTINGS_MAX_FRAME_SIZE) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | tracer->error("Frame is too large: $length\n"); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $con->error(FRAME_SIZE_ERROR); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 214 | ▶ | return 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 214 | ▶ | exists $frame_class{$type} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | ? const_name( "frame_types", $type ) | ||||||||||||||||||||||||||||||||||||||||||
| 93 | : "UNKNOWN", | ||||||||||||||||||||||||||||||||||||||||||
| 94 | $type, | ||||||||||||||||||||||||||||||||||||||||||
| 95 | $flags, | ||||||||||||||||||||||||||||||||||||||||||
| 96 | $stream_id, | ||||||||||||||||||||||||||||||||||||||||||
| 97 | $length | ||||||||||||||||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 214 | my $pending_stream_id = $con->pending_stream; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 214 | ▶ | if ( $pending_stream_id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$pending_stream_id and $type != 9 || $pending_stream_id != $stream_id
$type != 9 or $pending_stream_id != $stream_id
MC/DC0%0/3
$pending_stream_id and $type != 9 || $pending_stream_id != $stream_id
$pending_stream_id
$type != 9
$pending_stream_id != $stream_id
Truth table50%2/4
A and B or C
A= $pending_stream_idB= $type != 9C= $pending_stream_id != $stream_id
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | && ( $type != CONTINUATION || $pending_stream_id != $stream_id ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | tracer->debug("Expected CONTINUATION for stream $pending_stream_id"); | |||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $con->error(PROTOCOL_ERROR); | |||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # Unknown type of frame | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 214 | ▶ | if ( !exists $frame_class{$type} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | tracer->info("ignore unknown frame type $type"); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return FRAME_HEADER_SIZE + $length; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | $con->decode_context->{frame} = { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 214 | type => $type, | |||||||||||||||||||||||||||||||||||||||||
| 117 | flags => $flags, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | length => $length, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | stream => $stream_id, | ||||||||||||||||||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | # Try to create new stream structure | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 214 | ▶ | if ( $stream_id | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$stream_id and not $con->stream($stream_id) and not $con->new_peer_stream($stream_id)
$stream_id and not $con->stream($stream_id)
MC/DC0%0/3
$stream_id and not $con->stream($stream_id) and not $con->new_peer_stream($stream_id)
$stream_id
not $con->stream($stream_id)
not $con->new_peer_stream($stream_id)
Truth table75%3/4
A and not(B) and not(C)
A= $stream_idB= not $con->stream($stream_id)C= not $con->new_peer_stream($stream_id)
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | && !$con->stream($stream_id) | ||||||||||||||||||||||||||||||||||||||||||
| 125 | && !$con->new_peer_stream($stream_id) ) | ||||||||||||||||||||||||||||||||||||||||||
| 126 | { | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return $con->error ? undef : FRAME_HEADER_SIZE + $length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | return undef | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 214 | ▶ | unless defined $decoder{$type} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | ->( $con, $buf_ref, $buf_offset + FRAME_HEADER_SIZE, $length ); | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # Arrived frame may change state of stream | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 212 | $con->state_machine( 'recv', $type, $flags, $stream_id ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 212 | 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 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||