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 ¶
- Variables
- func Parse[T Dictionary | Item | List](inputString string) (T, error)
- func ParseLines[T Dictionary | Item | List](inputStrings []string) (T, error)
- type BareItem
- func BareItemBoolean(b bool) BareItem
- func BareItemByteSequence(bs []byte) BareItem
- func BareItemDate(d int64) BareItem
- func BareItemDecimal(d float64) BareItem
- func BareItemDisplayString(d string) BareItem
- func BareItemInteger(i int64) BareItem
- func BareItemString(s string) BareItem
- func BareItemToken(t string) BareItem
- func (b *BareItem) AppendText(text []byte) ([]byte, error)
- func (b *BareItem) Boolean() bool
- func (b *BareItem) ByteSequence() []byte
- func (b *BareItem) Date() int64
- func (b *BareItem) Decimal() float64
- func (b *BareItem) DisplayString() string
- func (b *BareItem) Integer() int64
- func (b *BareItem) String() string
- func (b *BareItem) Token() string
- func (b *BareItem) Type() BareItemType
- type BareItemType
- type Dictionary
- type InnerList
- type Item
- type ItemOrInnerList
- type ItemOrInnerListType
- type List
- type Parameters
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
BareItemInteger returns a BareItem holding the given integer value.
func BareItemString ¶
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 ¶
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 ¶
AppendText implements the encoding.TextAppender interface.
It panics if b's type is not a valid value.
func (*BareItem) Boolean ¶
Boolean returns the underlying boolean value.
It panics if b is not of type BareItemTypeBoolean.
func (*BareItem) ByteSequence ¶
ByteSequence returns the underlying byte sequence value.
It panics if b is not of type BareItemTypeByteSequence.
func (*BareItem) Date ¶
Date returns the underlying date value.
It panics if b is not of type BareItemTypeDate.
func (*BareItem) Decimal ¶
Decimal returns the underlying decimal value.
It panics if b is not of type BareItemTypeDecimal.
func (*BareItem) DisplayString ¶
DisplayString returns the underlying display string value.
It panics if b is not of type BareItemTypeDate.
func (*BareItem) Integer ¶
Integer returns the underlying integer value.
It panics if b is not of type BareItemTypeInteger.
func (*BareItem) String ¶
String returns the underlying string value.
It panics if b is not of type BareItemTypeString.
func (*BareItem) Token ¶
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 ¶
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 ¶
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 ¶
func (t ItemOrInnerList) Type() ItemOrInnerListType
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 ¶
AppendText implements the encoding.TextAppender interface.
type Parameters ¶
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.