| 1 |
package Protocol::HTTP2::Frame::Headers; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Protocol::HTTP2::Constants qw(:flags :errors :states :limits); |
| 5 |
use Protocol::HTTP2::Trace qw(tracer); |
| 6 |
|
| 7 |
# 6.2 HEADERS |
| 8 |
sub decode { |
| 9 |
my ( $con, $buf_ref, $buf_offset, $length ) = @_; |
| 10 |
my ( $pad, $offset, $weight, $exclusive, $stream_dep ) = ( 0, 0 ); |
| 11 |
my $frame_ref = $con->decode_context->{frame}; |
| 12 |
|
| 13 |
# Protocol errors |
| 14 |
if ( |
| 15 |
# HEADERS frames MUST be associated with a stream |
| 16 |
$frame_ref->{stream} == 0 |
| 17 |
) |
| 18 |
{ |
| 19 |
$con->error(PROTOCOL_ERROR); |
| 20 |
return undef; |
| 21 |
} |
| 22 |
|
| 23 |
if ( $frame_ref->{flags} & PADDED ) { |
| 24 |
$pad = unpack( 'C', substr( $$buf_ref, $buf_offset, 1 ) ); |
| 25 |
$offset += 1; |
| 26 |
} |
| 27 |
|
| 28 |
if ( $frame_ref->{flags} & PRIORITY_FLAG ) { |
| 29 |
( $stream_dep, $weight ) = |
| 30 |
unpack( 'NC', substr( $$buf_ref, $buf_offset + $offset, 5 ) ); |
| 31 |
$exclusive = $stream_dep >> 31; |
| 32 |
$stream_dep &= 0x7FFF_FFFF; |
| 33 |
$weight++; |
| 34 |
|
| 35 |
$con->stream_weight( $frame_ref->{stream}, $weight ); |
| 36 |
unless ( |
| 37 |
$con->stream_reprio( |
| 38 |
$frame_ref->{stream}, $exclusive, $stream_dep |
| 39 |
) |
| 40 |
) |
| 41 |
{ |
| 42 |
tracer->error("Malformed HEADERS frame priority"); |
| 43 |
$con->error(PROTOCOL_ERROR); |
| 44 |
return undef; |
| 45 |
} |
| 46 |
|
| 47 |
$offset += 5; |
| 48 |
} |
| 49 |
|
| 50 |
# Not enough space for header block |
| 51 |
my $hblock_size = $length - $offset - $pad; |
| 52 |
if ( $hblock_size < 0 ) { |
| 53 |
$con->error(PROTOCOL_ERROR); |
| 54 |
return undef; |
| 55 |
} |
| 56 |
|
| 57 |
$con->stream_header_block( $frame_ref->{stream}, |
| 58 |
substr( $$buf_ref, $buf_offset + $offset, $hblock_size ) ); |
| 59 |
|
| 60 |
# Stream header block complete |
| 61 |
$con->stream_headers_done( $frame_ref->{stream} ) |
| 62 |
or return undef |
| 63 |
if $frame_ref->{flags} & END_HEADERS; |
| 64 |
|
| 65 |
return $length; |
| 66 |
} |
| 67 |
|
| 68 |
sub encode { |
| 69 |
my ( $con, $flags_ref, $stream, $data_ref ) = @_; |
| 70 |
my $res = ''; |
| 71 |
|
| 72 |
if ( exists $data_ref->{padding} ) { |
| 73 |
$$flags_ref |= PADDED; |
| 74 |
$res .= pack 'C', $data_ref->{padding}; |
| 75 |
} |
| 76 |
|
| 77 |
if ( exists $data_ref->{stream_dep} || exists $data_ref->{weight} ) { |
| 78 |
$$flags_ref |= PRIORITY_FLAG; |
| 79 |
my $weight = ( $data_ref->{weight} || DEFAULT_WEIGHT ) - 1; |
| 80 |
my $stream_dep = $data_ref->{stream_dep} || 0; |
| 81 |
$stream_dep |= ( 1 << 31 ) if $data_ref->{exclusive}; |
| 82 |
$res .= pack 'NC', $stream_dep, $weight; |
| 83 |
} |
| 84 |
|
| 85 |
return $res . ${ $data_ref->{hblock} }; |
| 86 |
} |
| 87 |
|
| 88 |
1; |