forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/net/http2/hpack/hpack.go

924 lines
15 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2014 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
// Package hpack implements HPACK, a compression format for
2024-02-18 10:42:21 +00:00
// efficiently representing HTTP header fields in the context of HTTP/2.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// See http://tools.ietf.org/html/draft-ietf-httpbis-header-compression-09
2024-02-18 10:42:21 +00:00
package hpack
import (
"bytes"
"errors"
"fmt"
)
// A DecodingError is something the spec defines as a decoding error.
2024-02-18 10:42:21 +00:00
type DecodingError struct {
Err error
}
func (de DecodingError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("decoding error: %v", de.Err)
2024-02-18 10:42:21 +00:00
}
// An InvalidIndexError is returned when an encoder references a table
2024-02-18 10:42:21 +00:00
// entry before the static table or after the end of the dynamic table.
2024-02-18 10:42:21 +00:00
type InvalidIndexError int
func (e InvalidIndexError) Error() string {
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("invalid indexed representation index %d", int(e))
2024-02-18 10:42:21 +00:00
}
// A HeaderField is a name-value pair. Both the name and value are
2024-02-18 10:42:21 +00:00
// treated as opaque sequences of octets.
2024-02-18 10:42:21 +00:00
type HeaderField struct {
Name, Value string
// Sensitive means that this header field should never be
2024-02-18 10:42:21 +00:00
// indexed.
2024-02-18 10:42:21 +00:00
Sensitive bool
}
// IsPseudo reports whether the header field is an http2 pseudo header.
2024-02-18 10:42:21 +00:00
// That is, it reports whether it starts with a colon.
2024-02-18 10:42:21 +00:00
// It is not otherwise guaranteed to be a valid pseudo header field,
2024-02-18 10:42:21 +00:00
// though.
2024-02-18 10:42:21 +00:00
func (hf HeaderField) IsPseudo() bool {
2024-02-18 10:42:21 +00:00
return len(hf.Name) != 0 && hf.Name[0] == ':'
2024-02-18 10:42:21 +00:00
}
func (hf HeaderField) String() string {
2024-02-18 10:42:21 +00:00
var suffix string
2024-02-18 10:42:21 +00:00
if hf.Sensitive {
2024-02-18 10:42:21 +00:00
suffix = " (sensitive)"
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return fmt.Sprintf("header field %q = %q%s", hf.Name, hf.Value, suffix)
2024-02-18 10:42:21 +00:00
}
// Size returns the size of an entry per RFC 7541 section 4.1.
2024-02-18 10:42:21 +00:00
func (hf HeaderField) Size() uint32 {
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.4.1
2024-02-18 10:42:21 +00:00
// "The size of the dynamic table is the sum of the size of
2024-02-18 10:42:21 +00:00
// its entries. The size of an entry is the sum of its name's
2024-02-18 10:42:21 +00:00
// length in octets (as defined in Section 5.2), its value's
2024-02-18 10:42:21 +00:00
// length in octets (see Section 5.2), plus 32. The size of
2024-02-18 10:42:21 +00:00
// an entry is calculated using the length of the name and
2024-02-18 10:42:21 +00:00
// value without any Huffman encoding applied."
// This can overflow if somebody makes a large HeaderField
2024-02-18 10:42:21 +00:00
// Name and/or Value by hand, but we don't care, because that
2024-02-18 10:42:21 +00:00
// won't happen on the wire because the encoding doesn't allow
2024-02-18 10:42:21 +00:00
// it.
2024-02-18 10:42:21 +00:00
return uint32(len(hf.Name) + len(hf.Value) + 32)
2024-02-18 10:42:21 +00:00
}
// A Decoder is the decoding context for incremental processing of
2024-02-18 10:42:21 +00:00
// header blocks.
2024-02-18 10:42:21 +00:00
type Decoder struct {
dynTab dynamicTable
emit func(f HeaderField)
2024-02-18 10:42:21 +00:00
emitEnabled bool // whether calls to emit are enabled
maxStrLen int // 0 means unlimited
2024-02-18 10:42:21 +00:00
// buf is the unparsed buffer. It's only written to
2024-02-18 10:42:21 +00:00
// saveBuf if it was truncated in the middle of a header
2024-02-18 10:42:21 +00:00
// block. Because it's usually not owned, we can only
2024-02-18 10:42:21 +00:00
// process it under Write.
2024-02-18 10:42:21 +00:00
buf []byte // not owned; only valid during Write
// saveBuf is previous data passed to Write which we weren't able
2024-02-18 10:42:21 +00:00
// to fully parse before. Unlike buf, we own this data.
2024-02-18 10:42:21 +00:00
saveBuf bytes.Buffer
firstField bool // processing the first field of the header block
2024-02-18 10:42:21 +00:00
}
// NewDecoder returns a new decoder with the provided maximum dynamic
2024-02-18 10:42:21 +00:00
// table size. The emitFunc will be called for each valid field
2024-02-18 10:42:21 +00:00
// parsed, in the same goroutine as calls to Write, before Write returns.
2024-02-18 10:42:21 +00:00
func NewDecoder(maxDynamicTableSize uint32, emitFunc func(f HeaderField)) *Decoder {
2024-02-18 10:42:21 +00:00
d := &Decoder{
emit: emitFunc,
2024-02-18 10:42:21 +00:00
emitEnabled: true,
firstField: true,
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.dynTab.table.init()
2024-02-18 10:42:21 +00:00
d.dynTab.allowedMaxSize = maxDynamicTableSize
2024-02-18 10:42:21 +00:00
d.dynTab.setMaxSize(maxDynamicTableSize)
2024-02-18 10:42:21 +00:00
return d
2024-02-18 10:42:21 +00:00
}
// ErrStringLength is returned by Decoder.Write when the max string length
2024-02-18 10:42:21 +00:00
// (as configured by Decoder.SetMaxStringLength) would be violated.
2024-02-18 10:42:21 +00:00
var ErrStringLength = errors.New("hpack: string too long")
// SetMaxStringLength sets the maximum size of a HeaderField name or
2024-02-18 10:42:21 +00:00
// value string. If a string exceeds this length (even after any
2024-02-18 10:42:21 +00:00
// decompression), Write will return ErrStringLength.
2024-02-18 10:42:21 +00:00
// A value of 0 means unlimited and is the default from NewDecoder.
2024-02-18 10:42:21 +00:00
func (d *Decoder) SetMaxStringLength(n int) {
2024-02-18 10:42:21 +00:00
d.maxStrLen = n
2024-02-18 10:42:21 +00:00
}
// SetEmitFunc changes the callback used when new header fields
2024-02-18 10:42:21 +00:00
// are decoded.
2024-02-18 10:42:21 +00:00
// It must be non-nil. It does not affect EmitEnabled.
2024-02-18 10:42:21 +00:00
func (d *Decoder) SetEmitFunc(emitFunc func(f HeaderField)) {
2024-02-18 10:42:21 +00:00
d.emit = emitFunc
2024-02-18 10:42:21 +00:00
}
// SetEmitEnabled controls whether the emitFunc provided to NewDecoder
2024-02-18 10:42:21 +00:00
// should be called. The default is true.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// This facility exists to let servers enforce MAX_HEADER_LIST_SIZE
2024-02-18 10:42:21 +00:00
// while still decoding and keeping in-sync with decoder state, but
2024-02-18 10:42:21 +00:00
// without doing unnecessary decompression or generating unnecessary
2024-02-18 10:42:21 +00:00
// garbage for header fields past the limit.
2024-02-18 10:42:21 +00:00
func (d *Decoder) SetEmitEnabled(v bool) { d.emitEnabled = v }
// EmitEnabled reports whether calls to the emitFunc provided to NewDecoder
2024-02-18 10:42:21 +00:00
// are currently enabled. The default is true.
2024-02-18 10:42:21 +00:00
func (d *Decoder) EmitEnabled() bool { return d.emitEnabled }
// TODO: add method *Decoder.Reset(maxSize, emitFunc) to let callers re-use Decoders and their
2024-02-18 10:42:21 +00:00
// underlying buffers for garbage reasons.
func (d *Decoder) SetMaxDynamicTableSize(v uint32) {
2024-02-18 10:42:21 +00:00
d.dynTab.setMaxSize(v)
2024-02-18 10:42:21 +00:00
}
// SetAllowedMaxDynamicTableSize sets the upper bound that the encoded
2024-02-18 10:42:21 +00:00
// stream (via dynamic table size updates) may set the maximum size
2024-02-18 10:42:21 +00:00
// to.
2024-02-18 10:42:21 +00:00
func (d *Decoder) SetAllowedMaxDynamicTableSize(v uint32) {
2024-02-18 10:42:21 +00:00
d.dynTab.allowedMaxSize = v
2024-02-18 10:42:21 +00:00
}
type dynamicTable struct {
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.2.3.2
table headerFieldTable
size uint32 // in bytes
maxSize uint32 // current maxSize
2024-02-18 10:42:21 +00:00
allowedMaxSize uint32 // maxSize may go up to this, inclusive
2024-02-18 10:42:21 +00:00
}
func (dt *dynamicTable) setMaxSize(v uint32) {
2024-02-18 10:42:21 +00:00
dt.maxSize = v
2024-02-18 10:42:21 +00:00
dt.evict()
2024-02-18 10:42:21 +00:00
}
func (dt *dynamicTable) add(f HeaderField) {
2024-02-18 10:42:21 +00:00
dt.table.addEntry(f)
2024-02-18 10:42:21 +00:00
dt.size += f.Size()
2024-02-18 10:42:21 +00:00
dt.evict()
2024-02-18 10:42:21 +00:00
}
// If we're too big, evict old stuff.
2024-02-18 10:42:21 +00:00
func (dt *dynamicTable) evict() {
2024-02-18 10:42:21 +00:00
var n int
2024-02-18 10:42:21 +00:00
for dt.size > dt.maxSize && n < dt.table.len() {
2024-02-18 10:42:21 +00:00
dt.size -= dt.table.ents[n].Size()
2024-02-18 10:42:21 +00:00
n++
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
dt.table.evictOldest(n)
2024-02-18 10:42:21 +00:00
}
func (d *Decoder) maxTableIndex() int {
2024-02-18 10:42:21 +00:00
// This should never overflow. RFC 7540 Section 6.5.2 limits the size of
2024-02-18 10:42:21 +00:00
// the dynamic table to 2^32 bytes, where each entry will occupy more than
2024-02-18 10:42:21 +00:00
// one byte. Further, the staticTable has a fixed, small length.
2024-02-18 10:42:21 +00:00
return d.dynTab.table.len() + staticTable.len()
2024-02-18 10:42:21 +00:00
}
func (d *Decoder) at(i uint64) (hf HeaderField, ok bool) {
2024-02-18 10:42:21 +00:00
// See Section 2.3.3.
2024-02-18 10:42:21 +00:00
if i == 0 {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i <= uint64(staticTable.len()) {
2024-02-18 10:42:21 +00:00
return staticTable.ents[i-1], true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i > uint64(d.maxTableIndex()) {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// In the dynamic table, newer entries have lower indices.
2024-02-18 10:42:21 +00:00
// However, dt.ents[0] is the oldest entry. Hence, dt.ents is
2024-02-18 10:42:21 +00:00
// the reversed dynamic table.
2024-02-18 10:42:21 +00:00
dt := d.dynTab.table
2024-02-18 10:42:21 +00:00
return dt.ents[dt.len()-(int(i)-staticTable.len())], true
2024-02-18 10:42:21 +00:00
}
// DecodeFull decodes an entire block.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// TODO: remove this method and make it incremental later? This is
2024-02-18 10:42:21 +00:00
// easier for debugging now.
2024-02-18 10:42:21 +00:00
func (d *Decoder) DecodeFull(p []byte) ([]HeaderField, error) {
2024-02-18 10:42:21 +00:00
var hf []HeaderField
2024-02-18 10:42:21 +00:00
saveFunc := d.emit
2024-02-18 10:42:21 +00:00
defer func() { d.emit = saveFunc }()
2024-02-18 10:42:21 +00:00
d.emit = func(f HeaderField) { hf = append(hf, f) }
2024-02-18 10:42:21 +00:00
if _, err := d.Write(p); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err := d.Close(); err != nil {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return hf, nil
2024-02-18 10:42:21 +00:00
}
// Close declares that the decoding is complete and resets the Decoder
2024-02-18 10:42:21 +00:00
// to be reused again for a new header block. If there is any remaining
2024-02-18 10:42:21 +00:00
// data in the decoder's buffer, Close returns an error.
2024-02-18 10:42:21 +00:00
func (d *Decoder) Close() error {
2024-02-18 10:42:21 +00:00
if d.saveBuf.Len() > 0 {
2024-02-18 10:42:21 +00:00
d.saveBuf.Reset()
2024-02-18 10:42:21 +00:00
return DecodingError{errors.New("truncated headers")}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.firstField = true
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
func (d *Decoder) Write(p []byte) (n int, err error) {
2024-02-18 10:42:21 +00:00
if len(p) == 0 {
2024-02-18 10:42:21 +00:00
// Prevent state machine CPU attacks (making us redo
2024-02-18 10:42:21 +00:00
// work up to the point of finding out we don't have
2024-02-18 10:42:21 +00:00
// enough data)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
// Only copy the data if we have to. Optimistically assume
2024-02-18 10:42:21 +00:00
// that p will contain a complete header block.
2024-02-18 10:42:21 +00:00
if d.saveBuf.Len() == 0 {
2024-02-18 10:42:21 +00:00
d.buf = p
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
d.saveBuf.Write(p)
2024-02-18 10:42:21 +00:00
d.buf = d.saveBuf.Bytes()
2024-02-18 10:42:21 +00:00
d.saveBuf.Reset()
2024-02-18 10:42:21 +00:00
}
for len(d.buf) > 0 {
2024-02-18 10:42:21 +00:00
err = d.parseHeaderFieldRepr()
2024-02-18 10:42:21 +00:00
if err == errNeedMore {
2024-02-18 10:42:21 +00:00
// Extra paranoia, making sure saveBuf won't
2024-02-18 10:42:21 +00:00
// get too large. All the varint and string
2024-02-18 10:42:21 +00:00
// reading code earlier should already catch
2024-02-18 10:42:21 +00:00
// overlong things and return ErrStringLength,
2024-02-18 10:42:21 +00:00
// but keep this as a last resort.
2024-02-18 10:42:21 +00:00
const varIntOverhead = 8 // conservative
2024-02-18 10:42:21 +00:00
if d.maxStrLen != 0 && int64(len(d.buf)) > 2*(int64(d.maxStrLen)+varIntOverhead) {
2024-02-18 10:42:21 +00:00
return 0, ErrStringLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.saveBuf.Write(d.buf)
2024-02-18 10:42:21 +00:00
return len(p), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.firstField = false
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return len(p), err
2024-02-18 10:42:21 +00:00
}
// errNeedMore is an internal sentinel error value that means the
2024-02-18 10:42:21 +00:00
// buffer is truncated and we need to read more data before we can
2024-02-18 10:42:21 +00:00
// continue parsing.
2024-02-18 10:42:21 +00:00
var errNeedMore = errors.New("need more data")
type indexType int
const (
indexedTrue indexType = iota
2024-02-18 10:42:21 +00:00
indexedFalse
2024-02-18 10:42:21 +00:00
indexedNever
)
func (v indexType) indexed() bool { return v == indexedTrue }
2024-02-18 10:42:21 +00:00
func (v indexType) sensitive() bool { return v == indexedNever }
// returns errNeedMore if there isn't enough data available.
2024-02-18 10:42:21 +00:00
// any other error is fatal.
2024-02-18 10:42:21 +00:00
// consumes d.buf iff it returns nil.
2024-02-18 10:42:21 +00:00
// precondition: must be called with len(d.buf) > 0
2024-02-18 10:42:21 +00:00
func (d *Decoder) parseHeaderFieldRepr() error {
2024-02-18 10:42:21 +00:00
b := d.buf[0]
2024-02-18 10:42:21 +00:00
switch {
2024-02-18 10:42:21 +00:00
case b&128 != 0:
2024-02-18 10:42:21 +00:00
// Indexed representation.
2024-02-18 10:42:21 +00:00
// High bit set?
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.6.1
2024-02-18 10:42:21 +00:00
return d.parseFieldIndexed()
2024-02-18 10:42:21 +00:00
case b&192 == 64:
2024-02-18 10:42:21 +00:00
// 6.2.1 Literal Header Field with Incremental Indexing
2024-02-18 10:42:21 +00:00
// 0b10xxxxxx: top two bits are 10
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.1
2024-02-18 10:42:21 +00:00
return d.parseFieldLiteral(6, indexedTrue)
2024-02-18 10:42:21 +00:00
case b&240 == 0:
2024-02-18 10:42:21 +00:00
// 6.2.2 Literal Header Field without Indexing
2024-02-18 10:42:21 +00:00
// 0b0000xxxx: top four bits are 0000
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.2
2024-02-18 10:42:21 +00:00
return d.parseFieldLiteral(4, indexedFalse)
2024-02-18 10:42:21 +00:00
case b&240 == 16:
2024-02-18 10:42:21 +00:00
// 6.2.3 Literal Header Field never Indexed
2024-02-18 10:42:21 +00:00
// 0b0001xxxx: top four bits are 0001
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.6.2.3
2024-02-18 10:42:21 +00:00
return d.parseFieldLiteral(4, indexedNever)
2024-02-18 10:42:21 +00:00
case b&224 == 32:
2024-02-18 10:42:21 +00:00
// 6.3 Dynamic Table Size Update
2024-02-18 10:42:21 +00:00
// Top three bits are '001'.
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.6.3
2024-02-18 10:42:21 +00:00
return d.parseDynamicTableSizeUpdate()
2024-02-18 10:42:21 +00:00
}
return DecodingError{errors.New("invalid encoding")}
2024-02-18 10:42:21 +00:00
}
// (same invariants and behavior as parseHeaderFieldRepr)
2024-02-18 10:42:21 +00:00
func (d *Decoder) parseFieldIndexed() error {
2024-02-18 10:42:21 +00:00
buf := d.buf
2024-02-18 10:42:21 +00:00
idx, buf, err := readVarInt(7, buf)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf, ok := d.at(idx)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return DecodingError{InvalidIndexError(idx)}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.buf = buf
2024-02-18 10:42:21 +00:00
return d.callEmit(HeaderField{Name: hf.Name, Value: hf.Value})
2024-02-18 10:42:21 +00:00
}
// (same invariants and behavior as parseHeaderFieldRepr)
2024-02-18 10:42:21 +00:00
func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error {
2024-02-18 10:42:21 +00:00
buf := d.buf
2024-02-18 10:42:21 +00:00
nameIdx, buf, err := readVarInt(n, buf)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
var hf HeaderField
2024-02-18 10:42:21 +00:00
wantStr := d.emitEnabled || it.indexed()
2024-02-18 10:42:21 +00:00
var undecodedName undecodedString
2024-02-18 10:42:21 +00:00
if nameIdx > 0 {
2024-02-18 10:42:21 +00:00
ihf, ok := d.at(nameIdx)
2024-02-18 10:42:21 +00:00
if !ok {
2024-02-18 10:42:21 +00:00
return DecodingError{InvalidIndexError(nameIdx)}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf.Name = ihf.Name
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
undecodedName, buf, err = d.readString(buf)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
undecodedValue, buf, err := d.readString(buf)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if wantStr {
2024-02-18 10:42:21 +00:00
if nameIdx <= 0 {
2024-02-18 10:42:21 +00:00
hf.Name, err = d.decodeString(undecodedName)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf.Value, err = d.decodeString(undecodedValue)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.buf = buf
2024-02-18 10:42:21 +00:00
if it.indexed() {
2024-02-18 10:42:21 +00:00
d.dynTab.add(hf)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
hf.Sensitive = it.sensitive()
2024-02-18 10:42:21 +00:00
return d.callEmit(hf)
2024-02-18 10:42:21 +00:00
}
func (d *Decoder) callEmit(hf HeaderField) error {
2024-02-18 10:42:21 +00:00
if d.maxStrLen != 0 {
2024-02-18 10:42:21 +00:00
if len(hf.Name) > d.maxStrLen || len(hf.Value) > d.maxStrLen {
2024-02-18 10:42:21 +00:00
return ErrStringLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if d.emitEnabled {
2024-02-18 10:42:21 +00:00
d.emit(hf)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
// (same invariants and behavior as parseHeaderFieldRepr)
2024-02-18 10:42:21 +00:00
func (d *Decoder) parseDynamicTableSizeUpdate() error {
2024-02-18 10:42:21 +00:00
// RFC 7541, sec 4.2: This dynamic table size update MUST occur at the
2024-02-18 10:42:21 +00:00
// beginning of the first header block following the change to the dynamic table size.
2024-02-18 10:42:21 +00:00
if !d.firstField && d.dynTab.size > 0 {
2024-02-18 10:42:21 +00:00
return DecodingError{errors.New("dynamic table size update MUST occur at the beginning of a header block")}
2024-02-18 10:42:21 +00:00
}
buf := d.buf
2024-02-18 10:42:21 +00:00
size, buf, err := readVarInt(5, buf)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if size > uint64(d.dynTab.allowedMaxSize) {
2024-02-18 10:42:21 +00:00
return DecodingError{errors.New("dynamic table size update too large")}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
d.dynTab.setMaxSize(uint32(size))
2024-02-18 10:42:21 +00:00
d.buf = buf
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
}
var errVarintOverflow = DecodingError{errors.New("varint integer overflow")}
// readVarInt reads an unsigned variable length integer off the
2024-02-18 10:42:21 +00:00
// beginning of p. n is the parameter as described in
2024-02-18 10:42:21 +00:00
// https://httpwg.org/specs/rfc7541.html#rfc.section.5.1.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// n must always be between 1 and 8.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// The returned remain buffer is either a smaller suffix of p, or err != nil.
2024-02-18 10:42:21 +00:00
// The error is errNeedMore if p doesn't contain a complete integer.
2024-02-18 10:42:21 +00:00
func readVarInt(n byte, p []byte) (i uint64, remain []byte, err error) {
2024-02-18 10:42:21 +00:00
if n < 1 || n > 8 {
2024-02-18 10:42:21 +00:00
panic("bad n")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if len(p) == 0 {
2024-02-18 10:42:21 +00:00
return 0, p, errNeedMore
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
i = uint64(p[0])
2024-02-18 10:42:21 +00:00
if n < 8 {
2024-02-18 10:42:21 +00:00
i &= (1 << uint64(n)) - 1
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if i < (1<<uint64(n))-1 {
2024-02-18 10:42:21 +00:00
return i, p[1:], nil
2024-02-18 10:42:21 +00:00
}
origP := p
2024-02-18 10:42:21 +00:00
p = p[1:]
2024-02-18 10:42:21 +00:00
var m uint64
2024-02-18 10:42:21 +00:00
for len(p) > 0 {
2024-02-18 10:42:21 +00:00
b := p[0]
2024-02-18 10:42:21 +00:00
p = p[1:]
2024-02-18 10:42:21 +00:00
i += uint64(b&127) << m
2024-02-18 10:42:21 +00:00
if b&128 == 0 {
2024-02-18 10:42:21 +00:00
return i, p, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
m += 7
2024-02-18 10:42:21 +00:00
if m >= 63 { // TODO: proper overflow check. making this up.
2024-02-18 10:42:21 +00:00
return 0, origP, errVarintOverflow
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, origP, errNeedMore
2024-02-18 10:42:21 +00:00
}
// readString reads an hpack string from p.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// It returns a reference to the encoded string data to permit deferring decode costs
2024-02-18 10:42:21 +00:00
// until after the caller verifies all data is present.
2024-02-18 10:42:21 +00:00
func (d *Decoder) readString(p []byte) (u undecodedString, remain []byte, err error) {
2024-02-18 10:42:21 +00:00
if len(p) == 0 {
2024-02-18 10:42:21 +00:00
return u, p, errNeedMore
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
isHuff := p[0]&128 != 0
2024-02-18 10:42:21 +00:00
strLen, p, err := readVarInt(7, p)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return u, p, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if d.maxStrLen != 0 && strLen > uint64(d.maxStrLen) {
2024-02-18 10:42:21 +00:00
// Returning an error here means Huffman decoding errors
2024-02-18 10:42:21 +00:00
// for non-indexed strings past the maximum string length
2024-02-18 10:42:21 +00:00
// are ignored, but the server is returning an error anyway
2024-02-18 10:42:21 +00:00
// and because the string is not indexed the error will not
2024-02-18 10:42:21 +00:00
// affect the decoding state.
2024-02-18 10:42:21 +00:00
return u, nil, ErrStringLength
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if uint64(len(p)) < strLen {
2024-02-18 10:42:21 +00:00
return u, p, errNeedMore
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
u.isHuff = isHuff
2024-02-18 10:42:21 +00:00
u.b = p[:strLen]
2024-02-18 10:42:21 +00:00
return u, p[strLen:], nil
2024-02-18 10:42:21 +00:00
}
type undecodedString struct {
isHuff bool
b []byte
2024-02-18 10:42:21 +00:00
}
func (d *Decoder) decodeString(u undecodedString) (string, error) {
2024-02-18 10:42:21 +00:00
if !u.isHuff {
2024-02-18 10:42:21 +00:00
return string(u.b), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf := bufPool.Get().(*bytes.Buffer)
2024-02-18 10:42:21 +00:00
buf.Reset() // don't trust others
2024-02-18 10:42:21 +00:00
var s string
2024-02-18 10:42:21 +00:00
err := huffmanDecode(buf, d.maxStrLen, u.b)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
s = buf.String()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
buf.Reset() // be nice to GC
2024-02-18 10:42:21 +00:00
bufPool.Put(buf)
2024-02-18 10:42:21 +00:00
return s, err
2024-02-18 10:42:21 +00:00
}