httpsfv

package module
v0.0.0-...-2d567a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 12 Imported by: 0

README

httpsfv Go Reference Lint Test

Package httpsfv implements parsing and serializing of HTTP Structured Field Values as specified in RFC 9651.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Overview

Package httpsfv implements parsing and serializing of HTTP Structured Field Values as specified in RFC 9651.

Headers can be parsed using either Parse, when exactly one header line is given, or ParseLines for zero or more header lines. Both functions take the expected field type (a Dictionary, Item or List) as type parameter.

Example:

dict, err := httpsfv.ParseLines[httpsfv.Dictionary](req.Header["No-Vary-Search"])

Values can be serialized using the AppendText method implements by all types.

text, err := dict.AppendText(nil)

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidBareItem is returned when parsing a bare item fails.
	ErrInvalidBareItem = errors.New("invalid bare item")

	// ErrInvalidBoolean is returned when parsing a boolean fails.
	ErrInvalidBoolean = errors.New("invalid boolean")

	// ErrInvalidByteSequence is returned when parsing a byte sequence fails.
	ErrInvalidByteSequence = errors.New("invalid byte sequence")

	// ErrInvalidDate is returned when parsing a date fails.
	ErrInvalidDate = errors.New("invalid date")

	// ErrInvalidDictionary is returned when parsing a dictionary fails.
	ErrInvalidDictionary = errors.New("invalid dictionary")

	// ErrInvalidDisplayString is returned when parsing a display string fails.
	ErrInvalidDisplayString = errors.New("invalid display string")

	// ErrInvalidInnerList is returned when parsing an inner list fails.
	ErrInvalidInnerList = errors.New("invalid inner list")

	// ErrInvalidIntegerOrDecimal is returned when parsing an integer or decimal fails.
	ErrInvalidIntegerOrDecimal = errors.New("invalid integer or decimal")

	// ErrInvalidItem is returned when parsing an item fails.
	ErrInvalidItem = errors.New("invalid item")

	// ErrInvalidKey is returned when parsing a key fails.
	ErrInvalidKey = errors.New("invalid key")

	// ErrInvalidList is returned when parsing a list fails.
	ErrInvalidList = errors.New("invalid list")

	// ErrInvalidParameters is returned when parsing parameters fails.
	ErrInvalidParameters = errors.New("invalid parameters")

	// ErrInvalidString is returned when parsing a string fails.
	ErrInvalidString = errors.New("invalid string")

	// ErrInvalidToken is returned when parsing a token fails.
	ErrInvalidToken = errors.New("invalid token")

	// ErrNonAsciiInput is returned when parsing a structured field containing non-ASCII characters.
	ErrNonAsciiInput = errors.New("non-ascii input")

	// ErrTrailingData is returned when parsing a structured field containing trailing data.
	ErrTrailingData = errors.New("trailing data")
)

Functions

func Parse

func Parse[T Dictionary | Item | List](inputString string) (T, error)

Parse parses the given string and returns as either Dictionary, Item or List based on the generic type parameter T, as specified in RFC 9651 section 4.2.

An empty input returns a zero value and a nil error.

func ParseLines

func ParseLines[T Dictionary | Item | List](inputStrings []string) (T, error)

ParseLines is like Parse but validates takes a list of inputs, generally each specified header line for the given header, and validates that each string can be parsed without the other strings.

If any input string cannot be parsed, an error is returned.

Otherwise, the result of parsing the strings as one, joined by a comma and a space, is returned.

Types

type BareItem

type BareItem struct {
	// contains filtered or unexported fields
}

BareItem represents a simple item without parameters.

It acts as a tagged union, with BareItem.Type returning the type of the item.

func BareItemBoolean

func BareItemBoolean(b bool) BareItem

BareItemBoolean returns a BareItem holding the given boolean value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func BareItemByteSequence

func BareItemByteSequence(bs []byte) BareItem

BareItemByteSequence returns a BareItem holding the given byte sequence value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

Note that bs is not copied. Modifications to the underlying value will affect the returned bare item.

func BareItemDate

func BareItemDate(d int64) BareItem

BareItemDate returns a BareItem holding the given date value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func BareItemDecimal

func BareItemDecimal(d float64) BareItem

BareItemDecimal returns a BareItem holding the given decimal value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func BareItemDisplayString

func BareItemDisplayString(d string) BareItem

BareItemDisplayString returns a BareItem holding the given display string value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func BareItemInteger

func BareItemInteger(i int64) BareItem

BareItemInteger returns a BareItem holding the given integer value.

func BareItemString

func BareItemString(s string) BareItem

BareItemString returns a BareItem holding the given string value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func BareItemToken

func BareItemToken(t string) BareItem

BareItemToken returns a BareItem holding the given token value.

No validation is performed on the value, and serializing the value may fail if it is not valid.

func (*BareItem) AppendText

func (b *BareItem) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

It panics if b's type is not a valid value.

func (*BareItem) Boolean

func (b *BareItem) Boolean() bool

Boolean returns the underlying boolean value.

It panics if b is not of type BareItemTypeBoolean.

func (*BareItem) ByteSequence

func (b *BareItem) ByteSequence() []byte

ByteSequence returns the underlying byte sequence value.

It panics if b is not of type BareItemTypeByteSequence.

func (*BareItem) Date

func (b *BareItem) Date() int64

Date returns the underlying date value.

It panics if b is not of type BareItemTypeDate.

func (*BareItem) Decimal

func (b *BareItem) Decimal() float64

Decimal returns the underlying decimal value.

It panics if b is not of type BareItemTypeDecimal.

func (*BareItem) DisplayString

func (b *BareItem) DisplayString() string

DisplayString returns the underlying display string value.

It panics if b is not of type BareItemTypeDate.

func (*BareItem) Integer

func (b *BareItem) Integer() int64

Integer returns the underlying integer value.

It panics if b is not of type BareItemTypeInteger.

func (*BareItem) String

func (b *BareItem) String() string

String returns the underlying string value.

It panics if b is not of type BareItemTypeString.

func (*BareItem) Token

func (b *BareItem) Token() string

Token returns the underlying token value.

It panics if b is not of type BareItemTypeToken.

func (*BareItem) Type

func (b *BareItem) Type() BareItemType

Type returns the type of the bare item.

type BareItemType

type BareItemType uint8

BareItemType is an enum of types for a BareItem.

const (
	// BareItemTypeInvalid is the zero value of [BareItemType] and not a valid value.
	BareItemTypeInvalid BareItemType = iota

	// BareItemTypeBoolean denotes a boolean value as specified in RFC 9651 section 3.3.6.
	BareItemTypeBoolean

	// BareItemTypeByteSequence denotes a byte sequence as specified in RFC 9651 section 3.3.5.
	BareItemTypeByteSequence

	// BareItemTypeDate denotes a date as specified in RFC 9651 section 3.3.7.
	BareItemTypeDate

	// BareItemTypeDecimal denotes a decimal value as specified in RFC 9651 section 3.3.3.
	BareItemTypeDecimal

	// BareItemTypeDisplayString denotes a display string as specified in RFC 9651 section 3.3.8.
	BareItemTypeDisplayString

	// BareItemTypeInteger denotes an integer value as specified in RFC 9651 section 3.3.1.
	BareItemTypeInteger

	// BareItemTypeString denotes a string as specified in RFC 9651 section 3.3.3.
	BareItemTypeString

	// BareItemTypeToken denotes a token as specified in RFC 9651 section 3.3.4.
	BareItemTypeToken
)

func (BareItemType) String

func (t BareItemType) String() string

String returns the type name, which is the name of the constant minus the type prefix.

type Dictionary

type Dictionary struct {
	ordered.Map[string, ItemOrInnerList]
}

Dictionary represents an ordered map of key-values pairs as specified in RFC 9651 section 3.2.

func (*Dictionary) AppendText

func (d *Dictionary) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

type InnerList

type InnerList struct {
	// Members contains the members of the list.
	Members []Item

	// Parameters optionally contains the parameters specified for the value.
	Parameters Parameters
}

InnerList represents an inner list as specified in RFC 9651 section 3.1.1.

func (*InnerList) AppendText

func (il *InnerList) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

type Item

type Item struct {
	// BareItem contains the bare item without parameters.
	BareItem

	// Parameters optionally contains the parameters specified for the value.
	Parameters Parameters
}

Item represents a single item with optional parameters as specified in RFC 9651 section 3.3.

func (*Item) AppendText

func (i *Item) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

type ItemOrInnerList

type ItemOrInnerList struct {
	// contains filtered or unexported fields
}

ItemOrInnerList contains either an Item or InnerList as part of a Dictionary or List.

It acts as a tagged union, with ItemOrInnerList.Type specifying the type and ItemOrInnerList.InnerList or ItemOrInnerList.Item returning the value.

func ItemOrInnerListFrom

func ItemOrInnerListFrom[T BareItem | InnerList | Item](t T) ItemOrInnerList

ItemOrInnerListFrom wraps t in the tagged union ItemOrInnerList.

A BareItem will be wrapped in an Item with empty Item.Parameters.

func (ItemOrInnerList) InnerList

func (t ItemOrInnerList) InnerList() InnerList

InnerList returns the underlying InnerList.

It panics if t is not of type ItemOrInnerListTypeInnerList.

func (ItemOrInnerList) Item

func (t ItemOrInnerList) Item() Item

Item returns the underlying Item.

It panics if t is not of type ItemOrInnerListTypeItem.

func (ItemOrInnerList) Type

Type returns the type of the underlying value.

type ItemOrInnerListType

type ItemOrInnerListType uint8

ItemOrInnerListType is an enum of types that a ItemOrInnerList can contain.

const (
	// ItemOrInnerListTypeInnerList denotes an ItemOrInnerList containing an InnerList.
	ItemOrInnerListTypeInnerList ItemOrInnerListType = iota

	// ItemOrInnerListTypeItem denotes an ItemOrInnerList containing an Item.
	ItemOrInnerListTypeItem
)

func (ItemOrInnerListType) String

func (t ItemOrInnerListType) String() string

String returns the type name, which is the name of the constant minus the type prefix.

type List

type List struct {
	// Members is a slice of the members of the list.
	Members []ItemOrInnerList
}

List represents a list of items with optional parameters as specified in RFC 9651 section 3.1.

func (*List) AppendText

func (l *List) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

type Parameters

type Parameters struct {
	ordered.Map[string, BareItem]
}

Parameters is an ordered map of parameters that can be added to a InnerList, Item or List.

func (*Parameters) AppendText

func (p *Parameters) AppendText(text []byte) ([]byte, error)

AppendText implements the encoding.TextAppender interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL