// Package httphead contains utils for parsing HTTP and HTTP-grammar compatible // text protocols headers. // // That is, this package first aim is to bring ability to easily parse // constructions, described here https://tools.ietf.org/html/rfc2616#section-2
package httphead import ( ) // ScanTokens parses data in this form: // // list = 1#token // // It returns false if data is malformed. func ( []byte, func([]byte) bool) bool { := &Scanner{data: } var bool for .Next() { switch .Type() { case ItemToken: = true if !(.Bytes()) { return true } case ItemSeparator: if !isComma(.Bytes()) { return false } default: return false } } return && !.err } // ParseOptions parses all header options and appends it to given slice of // Option. It returns flag of successful (wellformed input) parsing. // // Note that appended options are all consist of subslices of data. That is, // mutation of data will mutate appended options. func ( []byte, []Option) ([]Option, bool) { var int := -1 return , ScanOptions(, func( int, , , []byte) Control { if != { = = len() = append(, Option{Name: }) } if != nil { [].Parameters.Set(, ) } return ControlContinue }) } // SelectFlag encodes way of options selection. type SelectFlag byte // String represetns flag as string. func ( SelectFlag) () string { var [2]string var int if &SelectCopy != 0 { [] = "copy" ++ } if &SelectUnique != 0 { [] = "unique" ++ } return "[" + strings.Join([:], "|") + "]" } const ( // SelectCopy causes selector to copy selected option before appending it // to resulting slice. // If SelectCopy flag is not passed to selector, then appended options will // contain sub-slices of the initial data. SelectCopy SelectFlag = 1 << iota // SelectUnique causes selector to append only not yet existing option to // resulting slice. Unique is checked by comparing option names. SelectUnique ) // OptionSelector contains configuration for selecting Options from header value. type OptionSelector struct { // Check is a filter function that applied to every Option that possibly // could be selected. // If Check is nil all options will be selected. Check func(Option) bool // Flags contains flags for options selection. Flags SelectFlag // Alloc used to allocate slice of bytes when selector is configured with // SelectCopy flag. It will be called with number of bytes needed for copy // of single Option. // If Alloc is nil make is used. Alloc func(n int) []byte } // Select parses header data and appends it to given slice of Option. // It also returns flag of successful (wellformed input) parsing. func ( OptionSelector) ( []byte, []Option) ([]Option, bool) { var Option var bool := -1 := .Alloc if == nil { = defaultAlloc } := .Check if == nil { = defaultCheck } := ScanOptions(, func( int, , , []byte) Control { if != { if && () { if .Flags&SelectCopy != 0 { = .Copy((.Size())) } = append(, ) = false } if .Flags&SelectUnique != 0 { for := len() - 1; >= 0; -- { if bytes.Equal([].Name, ) { return ControlSkip } } } = = Option{Name: } = true } if != nil { .Parameters.Set(, ) } return ControlContinue }) if && () { if .Flags&SelectCopy != 0 { = .Copy((.Size())) } = append(, ) } return , } func defaultAlloc( int) []byte { return make([]byte, ) } func defaultCheck(Option) bool { return true } // Control represents operation that scanner should perform. type Control byte const ( // ControlContinue causes scanner to continue scan tokens. ControlContinue Control = iota // ControlBreak causes scanner to stop scan tokens. ControlBreak // ControlSkip causes scanner to skip current entity. ControlSkip ) // ScanOptions parses data in this form: // // values = 1#value // value = token *( ";" param ) // param = token [ "=" (token | quoted-string) ] // // It calls given callback with the index of the option, option itself and its // parameter (attribute and its value, both could be nil). Index is useful when // header contains multiple choises for the same named option. // // Given callback should return one of the defined Control* values. // ControlSkip means that passed key is not in caller's interest. That is, all // parameters of that key will be skipped. // ControlBreak means that no more keys and parameters should be parsed. That // is, it must break parsing immediately. // ControlContinue means that caller want to receive next parameter and its // value or the next key. // // It returns false if data is malformed. func ( []byte, func( int, , , []byte) Control) bool { := &Scanner{data: } var bool var int const ( = iota ) var ( int , , []byte bool ) for .Next() { var ( bool int ) := .Type() := .Bytes() switch { case ItemToken: switch { case , : = = = true case : = = = true case : = = = true default: return false } case ItemString: if != { return false } = = = true case ItemSeparator: switch { case isComma() && == : // Nothing to do. case isComma() && == : = // Make call only if we have not called this key yet. = if ! { // If we have already called callback with the key // that just ended. ++ } else { // Else grow the index after calling callback. = 1 } case isComma() && == : = = 1 = true case isSemicolon() && == : = case isSemicolon() && == : = = true case isEquality() && == : = default: return false } default: return false } if { switch (, , , ) { case ControlBreak: // User want to stop to parsing parameters. return true case ControlSkip: // User want to skip current param. = .SkipEscaped(',') case ControlContinue: // User is interested in rest of parameters. // Nothing to do. default: panic("unexpected control value") } = true = nil = nil = false += } } if { = true (, , , ) } return && !.err } func isComma( []byte) bool { return len() == 1 && [0] == ',' } func isSemicolon( []byte) bool { return len() == 1 && [0] == ';' } func isEquality( []byte) bool { return len() == 1 && [0] == '=' }