// 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 httpheadimport ()// ScanTokens parses data in this form://// list = 1#token//// It returns false if data is malformed.func ( []byte, func([]byte) bool) bool { := &Scanner{data: }varboolfor .Next() {switch .Type() {caseItemToken: = trueif !(.Bytes()) {returntrue }caseItemSeparator:if !isComma(.Bytes()) {returnfalse }default:returnfalse } }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) {varint := -1return , ScanOptions(, func( int, , , []byte) Control {if != { = = len() = append(, Option{Name: }) }if != nil { [].Parameters.Set(, ) }returnControlContinue })}// SelectFlag encodes way of options selection.typeSelectFlagbyte// String represetns flag as string.func ( SelectFlag) () string {var [2]stringvarintif &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.SelectCopySelectFlag = 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.typeOptionSelectorstruct {// 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) {varOptionvarbool := -1 := .Allocif == nil { = defaultAlloc } := .Checkif == 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; -- {ifbytes.Equal([].Name, ) {returnControlSkip } } } = = Option{Name: } = true }if != nil { .Parameters.Set(, ) }returnControlContinue })if && () {if .Flags&SelectCopy != 0 { = .Copy((.Size())) } = append(, ) }return , }func defaultAlloc( int) []byte { returnmake([]byte, ) }func defaultCheck(Option) bool { returntrue }// Control represents operation that scanner should perform.typeControlbyteconst (// ControlContinue causes scanner to continue scan tokens.ControlContinueControl = 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: }varboolvarintconst ( = iota )var (int , , []bytebool )for .Next() {var (boolint ) := .Type() := .Bytes()switch {caseItemToken:switch {case , : = = = truecase : = = = truecase : = = = truedefault:returnfalse }caseItemString:if != {returnfalse } = = = truecaseItemSeparator:switch {caseisComma() && == :// Nothing to do.caseisComma() && == : = // 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 }caseisComma() && == : = = 1 = truecaseisSemicolon() && == : = caseisSemicolon() && == : = = truecaseisEquality() && == : = default:returnfalse }default:returnfalse }if {switch (, , , ) {caseControlBreak:// User want to stop to parsing parameters.returntruecaseControlSkip:// User want to skip current param. = .SkipEscaped(',')caseControlContinue:// 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 {returnlen() == 1 && [0] == ','}func isSemicolon( []byte) bool {returnlen() == 1 && [0] == ';'}func isEquality( []byte) bool {returnlen() == 1 && [0] == '='}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.