HTTP2/Frame.pm

filter: Statementstmt 89.5% 77 / 86 Branchbran 54.5% 12 / 22 Conditioncond 66.6% 8 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 100% 19 / 19 Podpod 0.0% 0 / 5 totaltotal 77.3% 116 / 150 CC 16 SCAR 24.5
CC
16
Cov
81%
frame_decode
26.2
preface_decode
3.2
BEGIN
1.0
CRAP 11.6
« HTTP2/Frame/Window_update.pm ↑ Summary HTTP2/Frame/Settings.pm »
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
BranchTrueFalseTotal
if length($$buf_ref) - $buf_offset < 24 0 16 16
53 return
54 16 ▶ index( $$buf_ref, PREFACE, $buf_offset ) == -1 ? undef : length(PREFACE);
Branch: 1 branch
BranchTrueFalseTotal
index($$buf_ref, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", $buf_offset) == -1 ? : 0 16 16
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
BranchTrueFalseTotal
if length($$buf_ref) - $buf_offset < 9 219 214 433
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
BranchTrueFalseTotal
if ($length > $con->dec_setting(5)) 0 214 214
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
BranchTrueFalseTotal
if length($$buf_ref) - $buf_offset - 9 - $length < 0 0 214 214
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
BranchTrueFalseTotal
exists $frame_class{$type} ? : 214 0 214
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
BranchTrueFalseTotal
if ($pending_stream_id and $type != 9 || $pending_stream_id != $stream_id) 0 214 214
Condition50%3/6
$pending_stream_id and $type != 9 || $pending_stream_id != $stream_id
!ll&&!rl&&r
21310
$type != 9 or $pending_stream_id != $stream_id
l!l&&r!l&&!r
001
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
ABCresult
0XX0
1000
11X1
1011
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
BranchTrueFalseTotal
unless (exists $frame_class{$type}) 0 214 214
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
BranchTrueFalseTotal
if ($stream_id and not $con->stream($stream_id) and not $con->new_peer_stream($stream_id)) 0 214 214
Condition83%5/6
$stream_id and not $con->stream($stream_id) and not $con->new_peer_stream($stream_id)
!ll&&!rl&&r
177370
$stream_id and not $con->stream($stream_id)
!ll&&!rl&&r
829537
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)
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
$con->error ? : 0 0 0
128 }
129  
130 return undef
131 214 ▶ unless defined $decoder{$type}
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decoder{$type}->($con, $buf_ref, $buf_offset + 9, $length) 2 212 214
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;
« HTTP2/Frame/Window_update.pm ↑ Summary HTTP2/Frame/Settings.pm »