Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions http2/hpack/hpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ type Decoder struct {
// to fully parse before. Unlike buf, we own this data.
saveBuf bytes.Buffer

firstField bool // processing the first field of the header block
firstField bool // processing the first field of the header block
allowMissingFields bool
}

// NewDecoder returns a new decoder with the provided maximum dynamic
Expand Down Expand Up @@ -123,6 +124,10 @@ func (d *Decoder) SetMaxStringLength(n int) {
d.maxStrLen = n
}

func (d *Decoder) SetAllowMissingFields(v bool) {
d.allowMissingFields = v
}
Comment on lines +127 to +129
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new public API function should have documentation explaining its purpose and behavior, similar to other public methods in this file.


// SetEmitFunc changes the callback used when new header fields
// are decoded.
// It must be non-nil. It does not affect EmitEnabled.
Expand Down Expand Up @@ -343,6 +348,10 @@ func (d *Decoder) parseFieldIndexed() error {
}
hf, ok := d.at(idx)
if !ok {
if d.allowMissingFields {
d.buf = buf
return d.callEmit(HeaderField{Name: fmt.Sprintf("missing-index-%d", idx), Value: "none"})
}
Comment on lines +351 to +354
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an inconsistency in how missing fields are handled. In parseFieldIndexed(), you set the value to "none", but in parseFieldLiteral(), no value is set for missing fields. Consider using a consistent approach for both cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is if complete header filed is indexed. (key and value)
For parseFieldLiteral the value is not indexed, only header key

return DecodingError{InvalidIndexError(idx)}
}
d.buf = buf
Expand All @@ -363,9 +372,13 @@ func (d *Decoder) parseFieldLiteral(n uint8, it indexType) error {
if nameIdx > 0 {
ihf, ok := d.at(nameIdx)
if !ok {
return DecodingError{InvalidIndexError(nameIdx)}
if !d.allowMissingFields {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have some unit tests to verify this logic.

return DecodingError{InvalidIndexError(nameIdx)}
}
hf.Name = fmt.Sprintf("missing-index-%d", nameIdx)
} else {
hf.Name = ihf.Name
}
hf.Name = ihf.Name
} else {
undecodedName, buf, err = d.readString(buf)
if err != nil {
Expand Down