| 1 |
package Protocol::HTTP2::HeaderCompression; |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Protocol::HTTP2::Huffman; |
| 5 |
use Protocol::HTTP2::StaticTable; |
| 6 |
use Protocol::HTTP2::Constants qw(:errors :settings :limits); |
| 7 |
use Protocol::HTTP2::Trace qw(tracer bin2hex); |
| 8 |
use Exporter qw(import); |
| 9 |
our @EXPORT_OK = qw(int_encode int_decode str_encode str_decode headers_decode |
| 10 |
headers_encode); |
| 11 |
|
| 12 |
sub int_encode { |
| 13 |
my ( $int, $N ) = @_; |
| 14 |
$N ||= 7; |
| 15 |
my $ff = ( 1 << $N ) - 1; |
| 16 |
|
| 17 |
if ( $int < $ff ) { |
| 18 |
return pack 'C', $int; |
| 19 |
} |
| 20 |
|
| 21 |
my $res = pack 'C', $ff; |
| 22 |
$int -= $ff; |
| 23 |
|
| 24 |
while ( $int >= 0x80 ) { |
| 25 |
$res .= pack( 'C', ( $int & 0x7f ) | 0x80 ); |
| 26 |
$int >>= 7; |
| 27 |
} |
| 28 |
|
| 29 |
return $res . pack( 'C', $int ); |
| 30 |
} |
| 31 |
|
| 32 |
# int_decode() |
| 33 |
# |
| 34 |
# arguments: |
| 35 |
# buf_ref - ref to buffer with encoded data |
| 36 |
# buf_offset - offset in buffer |
| 37 |
# int_ref - ref to scalar where result will be stored |
| 38 |
# N - bits in first byte |
| 39 |
# |
| 40 |
# returns: count of read bytes of encoded integer |
| 41 |
# or undef on error (malformed data) |
| 42 |
|
| 43 |
sub int_decode { |
| 44 |
my ( $buf_ref, $buf_offset, $int_ref, $N ) = @_; |
| 45 |
return undef if length($$buf_ref) - $buf_offset <= 0; |
| 46 |
$N ||= 7; |
| 47 |
my $ff = ( 1 << $N ) - 1; |
| 48 |
|
| 49 |
$$int_ref = $ff & vec( $$buf_ref, $buf_offset, 8 ); |
| 50 |
return 1 if $$int_ref < $ff; |
| 51 |
|
| 52 |
my $l = length($$buf_ref) - $buf_offset - 1; |
| 53 |
|
| 54 |
for my $i ( 1 .. $l ) { |
| 55 |
return undef if $i > MAX_INT_SIZE; |
| 56 |
my $s = vec( $$buf_ref, $i + $buf_offset, 8 ); |
| 57 |
$$int_ref += ( $s & 0x7f ) << ( $i - 1 ) * 7; |
| 58 |
return $i + 1 if $s < 0x80; |
| 59 |
} |
| 60 |
|
| 61 |
return undef; |
| 62 |
} |
| 63 |
|
| 64 |
sub str_encode { |
| 65 |
my $str = shift; |
| 66 |
my $huff_str = huffman_encode($str); |
| 67 |
my $pack; |
| 68 |
if ( length($huff_str) < length($str) ) { |
| 69 |
$pack = int_encode( length($huff_str), 7 ); |
| 70 |
vec( $pack, 7, 1 ) = 1; |
| 71 |
$pack .= $huff_str; |
| 72 |
} |
| 73 |
else { |
| 74 |
$pack = int_encode( length($str), 7 ); |
| 75 |
$pack .= $str; |
| 76 |
} |
| 77 |
return $pack; |
| 78 |
} |
| 79 |
|
| 80 |
# str_decode() |
| 81 |
# arguments: |
| 82 |
# buf_ref - ref to buffer with encoded data |
| 83 |
# buf_offset - offset in buffer |
| 84 |
# str_ref - ref to scalar where result will be stored |
| 85 |
# returns: count of read bytes of encoded data |
| 86 |
|
| 87 |
sub str_decode { |
| 88 |
my ( $buf_ref, $buf_offset, $str_ref ) = @_; |
| 89 |
my $offset = int_decode( $buf_ref, $buf_offset, \my $l, 7 ); |
| 90 |
return undef |
| 91 |
unless defined $offset |
| 92 |
&& length($$buf_ref) - $buf_offset - $offset >= $l; |
| 93 |
|
| 94 |
$$str_ref = substr $$buf_ref, $offset + $buf_offset, $l; |
| 95 |
$$str_ref = huffman_decode($$str_ref) |
| 96 |
if vec( $$buf_ref, $buf_offset * 8 + 7, 1 ) == 1; |
| 97 |
return $offset + $l; |
| 98 |
} |
| 99 |
|
| 100 |
sub evict_ht { |
| 101 |
my ( $context, $size ) = @_; |
| 102 |
my @evicted; |
| 103 |
|
| 104 |
my $ht = $context->{header_table}; |
| 105 |
|
| 106 |
while ( $context->{ht_size} + $size > $context->{max_ht_size} ) { |
| 107 |
my $n = $#$ht; |
| 108 |
my $kv_ref = pop @$ht; |
| 109 |
$context->{ht_size} -= |
| 110 |
32 + length( $kv_ref->[0] ) + length( $kv_ref->[1] ); |
| 111 |
tracer->debug( sprintf "Evicted header [%i] %s = %s\n", |
| 112 |
$n + 1, @$kv_ref ); |
| 113 |
push @evicted, [ $n, @$kv_ref ]; |
| 114 |
} |
| 115 |
return @evicted; |
| 116 |
} |
| 117 |
|
| 118 |
sub add_to_ht { |
| 119 |
my ( $context, $key, $value ) = @_; |
| 120 |
my $size = length($key) + length($value) + 32; |
| 121 |
return () if $size > $context->{max_ht_size}; |
| 122 |
|
| 123 |
my @evicted = evict_ht( $context, $size ); |
| 124 |
|
| 125 |
my $ht = $context->{header_table}; |
| 126 |
my $kv_ref = [ $key, $value ]; |
| 127 |
|
| 128 |
unshift @$ht, $kv_ref; |
| 129 |
$context->{ht_size} += $size; |
| 130 |
return @evicted; |
| 131 |
} |
| 132 |
|
| 133 |
sub headers_decode { |
| 134 |
my ( $con, $buf_ref, $buf_offset, $length, $stream_id ) = @_; |
| 135 |
|
| 136 |
my $context = $con->decode_context; |
| 137 |
|
| 138 |
my $ht = $context->{header_table}; |
| 139 |
my $eh = $context->{emitted_headers}; |
| 140 |
|
| 141 |
my $offset = 0; |
| 142 |
my $hsize = 0; |
| 143 |
|
| 144 |
while ( $offset < $length ) { |
| 145 |
|
| 146 |
my $f = vec( $$buf_ref, $buf_offset + $offset, 8 ); |
| 147 |
tracer->debug( sprintf "\toffset: %d, byte: %02x\n", $offset, $f ); |
| 148 |
|
| 149 |
# Indexed Header |
| 150 |
if ( $f & 0x80 ) { |
| 151 |
my $size = |
| 152 |
int_decode( $buf_ref, $buf_offset + $offset, \my $index, 7 ); |
| 153 |
last unless $size; |
| 154 |
|
| 155 |
# DECODING ERROR |
| 156 |
if ( $index == 0 ) { |
| 157 |
tracer->error("Indexed header with zero index\n"); |
| 158 |
$con->error(COMPRESSION_ERROR); |
| 159 |
return undef; |
| 160 |
} |
| 161 |
|
| 162 |
tracer->debug("\tINDEXED($index) HEADER\t"); |
| 163 |
|
| 164 |
# Static table or Header Table entry |
| 165 |
if ( $index <= @stable ) { |
| 166 |
my ( $key, $value ) = @{ $stable[ $index - 1 ] }; |
| 167 |
$hsize += length($key) + length($value) + 32; |
| 168 |
push @$eh, $key, $value; |
| 169 |
tracer->debug("$key = $value\n"); |
| 170 |
} |
| 171 |
elsif ( $index > @stable + @$ht ) { |
| 172 |
tracer->error( |
| 173 |
"Indexed header with index out of header table: " |
| 174 |
. $index |
| 175 |
. "\n" ); |
| 176 |
$con->error(COMPRESSION_ERROR); |
| 177 |
return undef; |
| 178 |
} |
| 179 |
else { |
| 180 |
my $kv_ref = $ht->[ $index - @stable - 1 ]; |
| 181 |
|
| 182 |
$hsize += length( $kv_ref->[0] ) + length( $kv_ref->[1] ) + 32; |
| 183 |
push @$eh, @$kv_ref; |
| 184 |
tracer->debug("$kv_ref->[0] = $kv_ref->[1]\n"); |
| 185 |
} |
| 186 |
|
| 187 |
$offset += $size; |
| 188 |
} |
| 189 |
|
| 190 |
# Literal Header Field - New Name |
| 191 |
elsif ( $f == 0x40 || $f == 0x00 || $f == 0x10 ) { |
| 192 |
my $key_size = |
| 193 |
str_decode( $buf_ref, $buf_offset + $offset + 1, \my $key ); |
| 194 |
last unless $key_size; |
| 195 |
|
| 196 |
if ( $key_size == 1 ) { |
| 197 |
tracer->error("Empty literal header name"); |
| 198 |
$con->error(COMPRESSION_ERROR); |
| 199 |
return undef; |
| 200 |
} |
| 201 |
|
| 202 |
if ( $key =~ /[^a-z0-9\!\#\$\%\&\'\*\+\-\^\_\`]/ && $key !~ /^\:/ ) |
| 203 |
{ |
| 204 |
tracer->warning("Illegal characters in header name"); |
| 205 |
$con->stream_error( $stream_id, PROTOCOL_ERROR ); |
| 206 |
return undef; |
| 207 |
} |
| 208 |
|
| 209 |
my $value_size = |
| 210 |
str_decode( $buf_ref, $buf_offset + $offset + 1 + $key_size, |
| 211 |
\my $value ); |
| 212 |
last unless $value_size; |
| 213 |
|
| 214 |
# Emitting header |
| 215 |
$hsize += length($key) + length($value) + 32; |
| 216 |
push @$eh, $key, $value; |
| 217 |
|
| 218 |
# Add to index |
| 219 |
if ( $f == 0x40 ) { |
| 220 |
add_to_ht( $context, $key, $value ); |
| 221 |
} |
| 222 |
tracer->debug( sprintf "\tLITERAL(new) HEADER\t%s: %s\n", |
| 223 |
$key, substr( $value, 0, 30 ) ); |
| 224 |
|
| 225 |
$offset += 1 + $key_size + $value_size; |
| 226 |
} |
| 227 |
|
| 228 |
# Literal Header Field - Indexed Name |
| 229 |
elsif (( $f & 0xC0 ) == 0x40 |
| 230 |
|| ( $f & 0xF0 ) == 0x00 |
| 231 |
|| ( $f & 0xF0 ) == 0x10 ) |
| 232 |
{ |
| 233 |
my $size = int_decode( $buf_ref, $buf_offset + $offset, |
| 234 |
\my $index, ( $f & 0xC0 ) == 0x40 ? 6 : 4 ); |
| 235 |
last unless $size; |
| 236 |
|
| 237 |
my $value_size = |
| 238 |
str_decode( $buf_ref, $buf_offset + $offset + $size, \my $value ); |
| 239 |
last unless $value_size; |
| 240 |
|
| 241 |
my $key; |
| 242 |
|
| 243 |
if ( $index <= @stable ) { |
| 244 |
$key = $stable[ $index - 1 ]->[0]; |
| 245 |
} |
| 246 |
elsif ( $index > @stable + @$ht ) { |
| 247 |
tracer->error( |
| 248 |
"Literal header with index out of header table: " |
| 249 |
. $index |
| 250 |
. "\n" ); |
| 251 |
$con->error(COMPRESSION_ERROR); |
| 252 |
return undef; |
| 253 |
} |
| 254 |
else { |
| 255 |
$key = $ht->[ $index - @stable - 1 ]->[0]; |
| 256 |
} |
| 257 |
|
| 258 |
# Emitting header |
| 259 |
$hsize += length($key) + length($value) + 32; |
| 260 |
push @$eh, $key, $value; |
| 261 |
|
| 262 |
# Add to index |
| 263 |
if ( ( $f & 0xC0 ) == 0x40 ) { |
| 264 |
add_to_ht( $context, $key, $value ); |
| 265 |
} |
| 266 |
tracer->debug("\tLITERAL($index) HEADER\t$key: $value\n"); |
| 267 |
|
| 268 |
$offset += $size + $value_size; |
| 269 |
} |
| 270 |
|
| 271 |
# Encoding Context Update - Maximum Header Table Size change |
| 272 |
elsif ( ( $f & 0xE0 ) == 0x20 ) { |
| 273 |
my $size = |
| 274 |
int_decode( $buf_ref, $buf_offset + $offset, \my $ht_size, 5 ); |
| 275 |
last unless $size; |
| 276 |
|
| 277 |
# It's not possible to increase size of HEADER_TABLE |
| 278 |
if ( |
| 279 |
$ht_size > $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} ) |
| 280 |
{ |
| 281 |
tracer->error( "Peer attempt to increase " |
| 282 |
. "maximum header table size higher than current size: " |
| 283 |
. "$ht_size > " |
| 284 |
. $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE} ); |
| 285 |
$con->error(COMPRESSION_ERROR); |
| 286 |
return undef; |
| 287 |
} |
| 288 |
if (@$eh) { |
| 289 |
tracer->error( |
| 290 |
"Attempt to change header table size after headers"); |
| 291 |
$con->error(COMPRESSION_ERROR); |
| 292 |
return undef; |
| 293 |
} |
| 294 |
tracer->debug( "Update header table size from " |
| 295 |
. $context->{max_ht_size} . " to " |
| 296 |
. $ht_size ); |
| 297 |
$context->{max_ht_size} = $ht_size; |
| 298 |
evict_ht( $context, 0 ); |
| 299 |
$offset += $size; |
| 300 |
} |
| 301 |
|
| 302 |
# Encoding Error |
| 303 |
else { |
| 304 |
tracer->error( sprintf( "Unknown header type: %08b", $f ) ); |
| 305 |
$con->error(COMPRESSION_ERROR); |
| 306 |
return undef; |
| 307 |
} |
| 308 |
|
| 309 |
# Check header limit |
| 310 |
if ( $hsize > $context->{settings}->{&SETTINGS_MAX_HEADER_LIST_SIZE} ) { |
| 311 |
tracer->error( "Headers size has exceeded the allowed limit: " |
| 312 |
. $hsize |
| 313 |
. "\n" ); |
| 314 |
$con->error(ENHANCE_YOUR_CALM); |
| 315 |
return undef; |
| 316 |
} |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
if ( $offset != $length ) { |
| 321 |
tracer->error( |
| 322 |
"Headers decoding stopped at offset $offset of $length\n"); |
| 323 |
$con->error(COMPRESSION_ERROR); |
| 324 |
return undef; |
| 325 |
} |
| 326 |
|
| 327 |
return $offset; |
| 328 |
} |
| 329 |
|
| 330 |
sub headers_encode { |
| 331 |
my ( $context, $headers ) = @_; |
| 332 |
my $res = ''; |
| 333 |
my $ht = $context->{header_table}; |
| 334 |
my $sht = $context->{settings}->{&SETTINGS_HEADER_TABLE_SIZE}; |
| 335 |
|
| 336 |
# Encode dynamic table size update |
| 337 |
if ( $context->{max_ht_size} != $sht ) { |
| 338 |
$res .= int_encode( $sht, 5 ); |
| 339 |
vec( $res, 3, 2 ) = 0; |
| 340 |
vec( $res, 5, 1 ) = 1; |
| 341 |
$context->{max_ht_size} = $sht; |
| 342 |
} |
| 343 |
|
| 344 |
HLOOP: |
| 345 |
for my $n ( 0 .. $#$headers / 2 ) { |
| 346 |
my $header = lc( $headers->[ 2 * $n ] ); |
| 347 |
my $value = $headers->[ 2 * $n + 1 ]; |
| 348 |
my $hdr; |
| 349 |
|
| 350 |
tracer->debug("Encoding header: $header = $value\n"); |
| 351 |
|
| 352 |
for my $i ( 0 .. $#$ht ) { |
| 353 |
next |
| 354 |
unless $ht->[$i]->[0] eq $header |
| 355 |
&& $ht->[$i]->[1] eq $value; |
| 356 |
$hdr = int_encode( $i + @stable + 1, 7 ); |
| 357 |
vec( $hdr, 7, 1 ) = 1; |
| 358 |
$res .= $hdr; |
| 359 |
tracer->debug( |
| 360 |
"\talready in header table, index " . ( $i + 1 ) . "\n" ); |
| 361 |
next HLOOP; |
| 362 |
} |
| 363 |
|
| 364 |
# 7.1 Indexed header field representation |
| 365 |
if ( exists $rstable{ $header . ' ' . $value } ) { |
| 366 |
$hdr = int_encode( $rstable{ $header . ' ' . $value }, 7 ); |
| 367 |
vec( $hdr, 7, 1 ) = 1; |
| 368 |
tracer->debug( "\tIndexed header " |
| 369 |
. $rstable{ $header . ' ' . $value } |
| 370 |
. " from table\n" ); |
| 371 |
} |
| 372 |
|
| 373 |
# 7.2.1 Literal Header Field with Incremental Indexing |
| 374 |
# (Indexed Name) |
| 375 |
elsif ( exists $rstable{ $header . ' ' } ) { |
| 376 |
$hdr = int_encode( $rstable{ $header . ' ' }, 6 ); |
| 377 |
vec( $hdr, 3, 2 ) = 1; |
| 378 |
$hdr .= str_encode($value); |
| 379 |
add_to_ht( $context, $header, $value ); |
| 380 |
tracer->debug( "\tLiteral header " |
| 381 |
. $rstable{ $header . ' ' } |
| 382 |
. " indexed name\n" ); |
| 383 |
} |
| 384 |
|
| 385 |
# 7.2.1 Literal Header Field with Incremental Indexing |
| 386 |
# (New Name) |
| 387 |
else { |
| 388 |
$hdr = pack( 'C', 0x40 ); |
| 389 |
$hdr .= str_encode($header) . str_encode($value); |
| 390 |
add_to_ht( $context, $header, $value ); |
| 391 |
tracer->debug("\tLiteral header new name\n"); |
| 392 |
} |
| 393 |
|
| 394 |
$res .= $hdr; |
| 395 |
} |
| 396 |
|
| 397 |
return $res; |
| 398 |
} |
| 399 |
|
| 400 |
1; |