| 1 |
package Protocol::HTTP2::Frame::Data; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Protocol::HTTP2::Constants qw(:flags :errors :settings :limits); |
| 5 |
use Protocol::HTTP2::Trace qw(tracer); |
| 6 |
|
| 7 |
sub decode { |
| 8 |
my ( $con, $buf_ref, $buf_offset, $length ) = @_; |
| 9 |
my ( $pad, $offset ) = ( 0, 0 ); |
| 10 |
my $frame_ref = $con->decode_context->{frame}; |
| 11 |
|
| 12 |
# Protocol errors |
| 13 |
if ( |
| 14 |
# DATA frames MUST be associated with a stream |
| 15 |
$frame_ref->{stream} == 0 |
| 16 |
) |
| 17 |
{ |
| 18 |
$con->error(PROTOCOL_ERROR); |
| 19 |
return undef; |
| 20 |
} |
| 21 |
|
| 22 |
if ( $frame_ref->{flags} & PADDED ) { |
| 23 |
$pad = unpack( 'C', substr( $$buf_ref, $buf_offset ) ); |
| 24 |
$offset += 1; |
| 25 |
} |
| 26 |
|
| 27 |
my $dblock_size = $length - $offset - $pad; |
| 28 |
if ( $dblock_size < 0 ) { |
| 29 |
tracer->error("Not enough space for data block\n"); |
| 30 |
$con->error(PROTOCOL_ERROR); |
| 31 |
return undef; |
| 32 |
} |
| 33 |
|
| 34 |
my $fcw = $con->fcw_recv( -$length ); |
| 35 |
my $stream_fcw = $con->stream_fcw_recv( $frame_ref->{stream}, -$length ); |
| 36 |
if ( $fcw < 0 || $stream_fcw < 0 ) { |
| 37 |
tracer->warning( |
| 38 |
"received data overflow flow control window: $fcw|$stream_fcw\n"); |
| 39 |
$con->stream_error( $frame_ref->{stream}, FLOW_CONTROL_ERROR ); |
| 40 |
return $length; |
| 41 |
} |
| 42 |
$con->fcw_update() if $fcw < $con->dec_setting(SETTINGS_MAX_FRAME_SIZE); |
| 43 |
$con->stream_fcw_update( $frame_ref->{stream} ) |
| 44 |
if $stream_fcw < $con->dec_setting(SETTINGS_MAX_FRAME_SIZE) |
| 45 |
&& !( $frame_ref->{flags} & END_STREAM ); |
| 46 |
|
| 47 |
return $length unless $dblock_size; |
| 48 |
|
| 49 |
my $data = substr $$buf_ref, $buf_offset + $offset, $dblock_size; |
| 50 |
|
| 51 |
# Update stream data container |
| 52 |
$con->stream_data( $frame_ref->{stream}, $data ); |
| 53 |
|
| 54 |
# Check length of data matched content-length in header |
| 55 |
if ( $frame_ref->{flags} & END_STREAM ) { |
| 56 |
my $slen = $con->stream_length( $frame_ref->{stream} ); |
| 57 |
if ( defined $slen && defined $con->stream_data( $frame_ref->{stream} ) |
| 58 |
&& $slen != length $con->stream_data( $frame_ref->{stream} ) ) |
| 59 |
{ |
| 60 |
tracer->warning( |
| 61 |
"content-length header don't match data frames size\n"); |
| 62 |
$con->stream_error( $frame_ref->{stream}, PROTOCOL_ERROR ); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return $length; |
| 67 |
} |
| 68 |
|
| 69 |
sub encode { |
| 70 |
my ( $con, $flags_ref, $stream_id, $data_ref ) = @_; |
| 71 |
|
| 72 |
return $$data_ref; |
| 73 |
} |
| 74 |
|
| 75 |
1; |