/*HTTP Content-Type Autonegotiation.The functions in this package implement the behaviour specified inhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec14.htmlCopyright (c) 2011, Open Knowledge Foundation Ltd.All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the Open Knowledge Foundation Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTLIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORA PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTHOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package goautonegimport ()// Structure to represent a clause in an HTTP Accept HeadertypeAcceptstruct { Type, SubType string Q float64 Params map[string]string}// acceptSlice is defined to implement sort interface.type acceptSlice []Acceptfunc ( acceptSlice) () int {returnlen()}func ( acceptSlice) (, int) bool { , := [], []if .Q > .Q {returntrue }if .Type != "*" && .Type == "*" {returntrue }if .SubType != "*" && .SubType == "*" {returntrue }returnfalse}func ( acceptSlice) (, int) { [], [] = [], []}func stringTrimSpaceCutset( rune) bool {return == ' '}func nextSplitElement(, string) ( string, string) {if := strings.Index(, ); != -1 {return [:], [+1:] }return , ""}// Parse an Accept Header string returning a sorted list// of clausesfunc ( string) acceptSlice { := 0 := forlen() > 0 { ++ _, = nextSplitElement(, ",") } := make(acceptSlice, 0, ) = varstringforlen() > 0 { , = nextSplitElement(, ",") = strings.TrimFunc(, stringTrimSpaceCutset) := Accept{Q: 1.0, } , := nextSplitElement(, ";") , := nextSplitElement(, "/") .Type = strings.TrimFunc(, stringTrimSpaceCutset)switch {caselen() == 0:if .Type == "*" { .SubType = "*" } else {continue }default:varstring , = nextSplitElement(, "/")iflen() > 0 {continue } .SubType = strings.TrimFunc(, stringTrimSpaceCutset) }iflen() == 0 { = append(, )continue } .Params = make(map[string]string)forlen() > 0 { , = nextSplitElement(, ";") , = nextSplitElement(, "=")iflen() == 0 {continue }varstring , = nextSplitElement(, "=")iflen() != 0 {continue } := strings.TrimFunc(, stringTrimSpaceCutset)if == "q" { .Q, _ = strconv.ParseFloat(, 32) } else { .Params[] = strings.TrimFunc(, stringTrimSpaceCutset) } } = append(, ) }sort.Sort()return}// Negotiate the most appropriate content_type given the accept header// and a list of alternatives.func ( string, []string) ( string) { := make([][]string, 0, len())for , := range { = append(, strings.SplitN(, "/", 2)) }for , := rangeParseAccept() {for , := range {if .Type == [0] && .SubType == [1] { = []return }if .Type == [0] && .SubType == "*" { = []return }if .Type == "*" && .SubType == "*" { = []return } } }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.