package httpheadimport ()// ScanCookie scans cookie pairs from data using DefaultCookieScanner.Scan()// method.func ( []byte, func(, []byte) bool) bool {returnDefaultCookieScanner.Scan(, )}// DefaultCookieScanner is a CookieScanner which is used by ScanCookie().// Note that it is intended to have the same behavior as http.Request.Cookies()// has.varDefaultCookieScanner = CookieScanner{}// CookieScanner contains options for scanning cookie pairs.// See https://tools.ietf.org/html/rfc6265#section-4.1.1typeCookieScannerstruct {// DisableNameValidation disables name validation of a cookie. If false, // only RFC2616 "tokens" are accepted. DisableNameValidation bool// DisableValueValidation disables value validation of a cookie. If false, // only RFC6265 "cookie-octet" characters are accepted. // // Note that Strict option also affects validation of a value. // // If Strict is false, then scanner begins to allow space and comma // characters inside the value for better compatibility with non standard // cookies implementations. DisableValueValidation bool// BreakOnPairError sets scanner to immediately return after first pair syntax // validation error. // If false, scanner will try to skip invalid pair bytes and go ahead. BreakOnPairError bool// Strict enables strict RFC6265 mode scanning. It affects name and value // validation, as also some other rules. // If false, it is intended to bring the same behavior as // http.Request.Cookies(). Strict bool}// Scan maps data to name and value pairs. Usually data represents value of the// Cookie header.func ( CookieScanner) ( []byte, func(, []byte) bool) bool { := &Scanner{data: }const ( = iota ) := for .Buffered() > 0 {switch {case :// Pairs separated by ";" and space, according to the RFC6265: // cookie-pair *( ";" SP cookie-pair ) // // Cookie pairs MUST be separated by (";" SP). So our only option // here is to fail as syntax error. , := .Peek2()if != ';' {returnfalse } = := 1if == ' ' { ++ } elseif .Strict {returnfalse } .Advance()case :if !.FetchUntil(';') {returnfalse }var []byte := .Bytes()if := bytes.IndexByte(, '='); != -1 { = [+1:] = [:] } elseif .Strict {if !.BreakOnPairError {goto }returnfalse }if !.Strict {trimLeft() }if !.DisableNameValidation && !ValidCookieName() {if !.BreakOnPairError {goto }returnfalse }if !.Strict { = trimRight() } = stripQuotes()if !.DisableValueValidation && !ValidCookieValue(, .Strict) {if !.BreakOnPairError {goto }returnfalse }if !(, ) {returntrue } : = } }returntrue}// ValidCookieValue reports whether given value is a valid RFC6265// "cookie-octet" bytes.//// cookie-octet = %x21 / %x23-2B / %x2D-3A / %x3C-5B / %x5D-7E// ; US-ASCII characters excluding CTLs,// ; whitespace DQUOTE, comma, semicolon,// ; and backslash//// Note that the false strict parameter disables errors on space 0x20 and comma// 0x2c. This could be useful to bring some compatibility with non-compliant// clients/servers in the real world.// It acts the same as standard library cookie parser if strict is false.func ( []byte, bool) bool {iflen() == 0 {returntrue }for , := range {switch {case'"', ';', '\\':returnfalsecase',', ' ':if {returnfalse }default:if <= 0x20 {returnfalse }if >= 0x7f {returnfalse } } }returntrue}// ValidCookieName reports wheter given bytes is a valid RFC2616 "token" bytes.func ( []byte) bool {for , := range {if !OctetTypes[].IsToken() {returnfalse } }returntrue}func stripQuotes( []byte) []byte {if := len() - 1; > 0 && [0] == '"' && [] == '"' {return [1:] }return}func trimLeft( []byte) []byte {varintfor < len() && OctetTypes[[]].IsSpace() { ++ }return [:]}func trimRight( []byte) []byte { := len()for > 0 && OctetTypes[[-1]].IsSpace() { -- }return [:]}
The pages are generated with Goldsv0.8.2. (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.