| 1 |
package Protocol::HTTP2::Frame::Settings; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Protocol::HTTP2::Constants qw(const_name :flags :errors :limits :settings); |
| 5 |
use Protocol::HTTP2::Trace qw(tracer); |
| 6 |
|
| 7 |
my %s_check = ( |
| 8 |
&SETTINGS_MAX_FRAME_SIZE => { |
| 9 |
validator => sub { |
| 10 |
$_[0] <= MAX_PAYLOAD_SIZE && $_[0] >= DEFAULT_MAX_FRAME_SIZE; |
| 11 |
}, |
| 12 |
error => PROTOCOL_ERROR |
| 13 |
}, |
| 14 |
&SETTINGS_ENABLE_PUSH => { |
| 15 |
validator => sub { |
| 16 |
$_[0] == 0 || $_[0] == 1; |
| 17 |
}, |
| 18 |
error => PROTOCOL_ERROR |
| 19 |
}, |
| 20 |
&SETTINGS_INITIAL_WINDOW_SIZE => { |
| 21 |
validator => sub { |
| 22 |
$_[0] <= MAX_FCW_SIZE; |
| 23 |
}, |
| 24 |
error => FLOW_CONTROL_ERROR |
| 25 |
}, |
| 26 |
); |
| 27 |
|
| 28 |
my %s_action = ( |
| 29 |
&SETTINGS_INITIAL_WINDOW_SIZE => sub { |
| 30 |
my ( $con, $size ) = @_; |
| 31 |
$con->fcw_initial_change($size); |
| 32 |
} |
| 33 |
); |
| 34 |
|
| 35 |
sub decode { |
| 36 |
my ( $con, $buf_ref, $buf_offset, $length ) = @_; |
| 37 |
my $frame_ref = $con->decode_context->{frame}; |
| 38 |
|
| 39 |
if ( $frame_ref->{stream} != 0 ) { |
| 40 |
$con->error(PROTOCOL_ERROR); |
| 41 |
return undef; |
| 42 |
} |
| 43 |
|
| 44 |
# just ack for our previous settings |
| 45 |
if ( $frame_ref->{flags} & ACK ) { |
| 46 |
if ( $length != 0 ) { |
| 47 |
tracer->error( |
| 48 |
"ACK settings frame have non-zero ($length) payload\n"); |
| 49 |
$con->error(FRAME_SIZE_ERROR); |
| 50 |
return undef; |
| 51 |
} |
| 52 |
return 0 |
| 53 |
|
| 54 |
# received empty settings (default), accept it |
| 55 |
} |
| 56 |
elsif ( $length == 0 ) { |
| 57 |
$con->accept_settings(); |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
if ( $length % 6 != 0 ) { |
| 62 |
tracer->error("Settings frame payload is broken (length $length)\n"); |
| 63 |
$con->error(FRAME_SIZE_ERROR); |
| 64 |
return undef; |
| 65 |
} |
| 66 |
|
| 67 |
my @settings = unpack( '(nN)*', substr( $$buf_ref, $buf_offset, $length ) ); |
| 68 |
while ( my ( $key, $value ) = splice @settings, 0, 2 ) { |
| 69 |
if ( !defined $con->enc_setting($key) ) { |
| 70 |
tracer->debug("\tUnknown setting $key\n"); |
| 71 |
|
| 72 |
# ignore unknown setting |
| 73 |
next; |
| 74 |
} |
| 75 |
elsif ( exists $s_check{$key} |
| 76 |
&& !$s_check{$key}{validator}->($value) ) |
| 77 |
{ |
| 78 |
tracer->debug( "\tInvalid value of setting " |
| 79 |
. const_name( "settings", $key ) . ": " |
| 80 |
. $value ); |
| 81 |
$con->error( $s_check{$key}{error} ); |
| 82 |
return undef; |
| 83 |
} |
| 84 |
|
| 85 |
# Settings change may run some action |
| 86 |
$s_action{$key}->( $con, $value ) if exists $s_action{$key}; |
| 87 |
|
| 88 |
tracer->debug( |
| 89 |
"\tSettings " . const_name( "settings", $key ) . " = $value\n" ); |
| 90 |
$con->enc_setting( $key, $value ); |
| 91 |
} |
| 92 |
|
| 93 |
$con->accept_settings(); |
| 94 |
return $length; |
| 95 |
} |
| 96 |
|
| 97 |
sub encode { |
| 98 |
my ( $con, $flags_ref, $stream, $data ) = @_; |
| 99 |
my $payload = ''; |
| 100 |
for my $key ( sort keys %$data ) { |
| 101 |
tracer->debug( "\tSettings " |
| 102 |
. const_name( "settings", $key ) |
| 103 |
. " = $data->{$key}\n" ); |
| 104 |
$payload .= pack( 'nN', $key, $data->{$key} ); |
| 105 |
} |
| 106 |
return $payload; |
| 107 |
} |
| 108 |
|
| 109 |
1; |