comparison h2.t @ 843:5e7845d36e54

Tests: simplified constructing HTTP/2 request body. No functional changes.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 04 Feb 2016 17:36:12 +0300
parents 60b3e0cfba77
children 3a334b20208e
comparison
equal deleted inserted replaced
842:60b3e0cfba77 843:5e7845d36e54
3135 3135
3136 $uri->{h2_continue} = 1; 3136 $uri->{h2_continue} = 1;
3137 return new_stream($ctx, $uri, $stream); 3137 return new_stream($ctx, $uri, $stream);
3138 } 3138 }
3139 3139
3140 sub pack_body {
3141 my ($ctx, $body, $flags, $extra) = @_;
3142
3143 my $pad = defined $extra->{body_padding} ? $extra->{body_padding} : 0;
3144 my $padlen = defined $extra->{body_padding} ? 1 : 0;
3145
3146 my $buf = pack_length(length($body) + $pad + $padlen);
3147 $flags |= 0x8 if $padlen;
3148 $buf .= pack 'CC', 0x0, $flags; # DATA, END_STREAM
3149 $buf .= pack 'N', $ctx->{last_stream};
3150 $buf .= pack 'C', $pad if $padlen; # DATA Pad Length?
3151 $buf .= $body;
3152 $buf .= pack "x$pad" if $padlen; # DATA Padding
3153 return $buf;
3154 }
3155
3140 sub new_stream { 3156 sub new_stream {
3141 my ($ctx, $uri, $stream) = @_; 3157 my ($ctx, $uri, $stream) = @_;
3142 my ($input, $buf); 3158 my ($input, $buf);
3143 my ($d, $status); 3159 my ($d, $status);
3144 3160
3150 my $path = $uri->{path} || '/'; 3166 my $path = $uri->{path} || '/';
3151 my $headers = $uri->{headers}; 3167 my $headers = $uri->{headers};
3152 my $body = $uri->{body}; 3168 my $body = $uri->{body};
3153 my $prio = $uri->{prio}; 3169 my $prio = $uri->{prio};
3154 my $dep = $uri->{dep}; 3170 my $dep = $uri->{dep};
3155 my $split = ref $uri->{continuation} && $uri->{continuation} || [];
3156 my $bsplit = ref $uri->{body_split} && $uri->{body_split} || [];
3157 3171
3158 my $pad = defined $uri->{padding} ? $uri->{padding} : 0; 3172 my $pad = defined $uri->{padding} ? $uri->{padding} : 0;
3159 my $padlen = defined $uri->{padding} ? 1 : 0; 3173 my $padlen = defined $uri->{padding} ? 1 : 0;
3160 my $bpad = defined $uri->{body_padding} ? $uri->{body_padding} : 0;
3161 my $bpadlen = defined $uri->{body_padding} ? 1 : 0;
3162 3174
3163 my $type = defined $uri->{h2_continue} ? 0x9 : 0x1; 3175 my $type = defined $uri->{h2_continue} ? 0x9 : 0x1;
3164 my $flags = defined $uri->{continuation} ? 0x0 : 0x4; 3176 my $flags = defined $uri->{continuation} ? 0x0 : 0x4;
3165 $flags |= 0x1 unless defined $body; 3177 $flags |= 0x1 unless defined $body;
3166 $flags |= 0x8 if $padlen; 3178 $flags |= 0x8 if $padlen;
3194 } 3206 }
3195 3207
3196 $input = pack("B*", '001' . ipack(5, $uri->{table_size})) . $input 3208 $input = pack("B*", '001' . ipack(5, $uri->{table_size})) . $input
3197 if defined $uri->{table_size}; 3209 if defined $uri->{table_size};
3198 3210
3211 my $split = ref $uri->{continuation} && $uri->{continuation} || [];
3199 my @input = map { substr $input, 0, $_, "" } @$split; 3212 my @input = map { substr $input, 0, $_, "" } @$split;
3200 push @input, $input; 3213 push @input, $input;
3201 3214
3202 # set length, attach headers, padding, priority 3215 # set length, attach headers, padding, priority
3203 3216
3219 $buf .= pack("CC", 0x9, $flags); 3232 $buf .= pack("CC", 0x9, $flags);
3220 $buf .= pack("N", $ctx->{last_stream}); 3233 $buf .= pack("N", $ctx->{last_stream});
3221 $buf .= $input; 3234 $buf .= $input;
3222 } 3235 }
3223 3236
3224 my @body = map { substr $body, 0, $_, "" } @$bsplit; 3237 $split = ref $uri->{body_split} && $uri->{body_split} || [];
3225 push @body, $body; 3238 for (@$split) {
3226 3239 $buf .= pack_body($ctx, substr($body, 0, $_, ""), 0x0, $uri);
3227 if (defined $body[0]) {
3228 $buf .= pack_length(length($body[0]) + $bpad + $bpadlen);
3229 my $flags = defined $uri->{body_split} ? 0x0 : 0x1;
3230 $flags |= 0x8 if $bpadlen;
3231 $buf .= pack 'CC', 0x0, $flags; # DATA, END_STREAM
3232 $buf .= pack 'N', $ctx->{last_stream};
3233 $buf .= pack 'C', $bpad if $bpadlen; # DATA Pad Length?
3234 $buf .= $body[0];
3235 $buf .= (pack 'C', 0) x $bpad if $bpadlen; # DATA Padding
3236 } 3240 }
3237 3241
3238 shift @body; 3242 $buf .= pack_body($ctx, $body, 0x1, $uri) if defined $body;
3239
3240 while (@body) {
3241 $body = shift @body;
3242 $buf .= pack_length(length($body) + $bpad + $bpadlen);
3243 my $flags = @body ? 0x0 : 0x1;
3244 $flags |= 0x8 if $bpadlen;
3245 $buf .= pack 'CC', 0x0, $flags;
3246 $buf .= pack 'N', $ctx->{last_stream};
3247 $buf .= pack 'C', $bpad if $bpadlen;
3248 $buf .= $body;
3249 $buf .= (pack 'C', 0) x $bpad if $bpadlen; # DATA Padding
3250 }
3251 3243
3252 $split = ref $uri->{split} && $uri->{split} || []; 3244 $split = ref $uri->{split} && $uri->{split} || [];
3253 for (@$split) { 3245 for (@$split) {
3254 raw_write($ctx->{socket}, substr($buf, 0, $_, "")); 3246 raw_write($ctx->{socket}, substr($buf, 0, $_, ""));
3255 goto done if $uri->{abort}; 3247 goto done if $uri->{abort};