File: Settings.pm

package info (click to toggle)
libprotocol-http2-perl 1.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 448 kB
  • sloc: perl: 3,370; makefile: 7
file content (109 lines) | stat: -rw-r--r-- 2,997 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package Protocol::HTTP2::Frame::Settings;
use strict;
use warnings;
use Protocol::HTTP2::Constants qw(const_name :flags :errors :limits :settings);
use Protocol::HTTP2::Trace qw(tracer);

my %s_check = (
    &SETTINGS_MAX_FRAME_SIZE => {
        validator => sub {
            $_[0] <= MAX_PAYLOAD_SIZE && $_[0] >= DEFAULT_MAX_FRAME_SIZE;
        },
        error => PROTOCOL_ERROR
    },
    &SETTINGS_ENABLE_PUSH => {
        validator => sub {
            $_[0] == 0 || $_[0] == 1;
        },
        error => PROTOCOL_ERROR
    },
    &SETTINGS_INITIAL_WINDOW_SIZE => {
        validator => sub {
            $_[0] <= MAX_FCW_SIZE;
        },
        error => FLOW_CONTROL_ERROR
    },
);

my %s_action = (
    &SETTINGS_INITIAL_WINDOW_SIZE => sub {
        my ( $con, $size ) = @_;
        $con->fcw_initial_change($size);
    }
);

sub decode {
    my ( $con, $buf_ref, $buf_offset, $length ) = @_;
    my $frame_ref = $con->decode_context->{frame};

    if ( $frame_ref->{stream} != 0 ) {
        $con->error(PROTOCOL_ERROR);
        return undef;
    }

    # just ack for our previous settings
    if ( $frame_ref->{flags} & ACK ) {
        if ( $length != 0 ) {
            tracer->error(
                "ACK settings frame have non-zero ($length) payload\n");
            $con->error(FRAME_SIZE_ERROR);
            return undef;
        }
        return 0

          # received empty settings (default), accept it
    }
    elsif ( $length == 0 ) {
        $con->accept_settings();
        return 0;
    }

    if ( $length % 6 != 0 ) {
        tracer->error("Settings frame payload is broken (length $length)\n");
        $con->error(FRAME_SIZE_ERROR);
        return undef;
    }

    my @settings = unpack( '(nN)*', substr( $$buf_ref, $buf_offset, $length ) );
    while ( my ( $key, $value ) = splice @settings, 0, 2 ) {
        if ( !defined $con->enc_setting($key) ) {
            tracer->debug("\tUnknown setting $key\n");

            # ignore unknown setting
            next;
        }
        elsif ( exists $s_check{$key}
            && !$s_check{$key}{validator}->($value) )
        {
            tracer->debug( "\tInvalid value of setting "
                  . const_name( "settings", $key ) . ": "
                  . $value );
            $con->error( $s_check{$key}{error} );
            return undef;
        }

        # Settings change may run some action
        $s_action{$key}->( $con, $value ) if exists $s_action{$key};

        tracer->debug(
            "\tSettings " . const_name( "settings", $key ) . " = $value\n" );
        $con->enc_setting( $key, $value );
    }

    $con->accept_settings();
    return $length;
}

sub encode {
    my ( $con, $flags_ref, $stream, $data ) = @_;
    my $payload = '';
    for my $key ( sort keys %$data ) {
        tracer->debug( "\tSettings "
              . const_name( "settings", $key )
              . " = $data->{$key}\n" );
        $payload .= pack( 'nN', $key, $data->{$key} );
    }
    return $payload;
}

1;