Copyright (c) 2011, Open Knowledge Foundation Ltd.All rights reserved.
HTTP Content-Type Autonegotiation.
The functions in this package implement the behaviour specified inhttp:www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
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 goautoneg

import (
	
	
	
)
Structure to represent a clause in an HTTP Accept Header
For internal use, so that we can use the sort interface
type accept_slice []Accept

func ( accept_slice) () int {
	 := []Accept()
	return len()
}

func ( accept_slice) (,  int) bool {
	 := []Accept()
	,  := [], []
	if .Q > .Q {
		return true
	}
	if .Type != "*" && .Type == "*" {
		return true
	}
	if .SubType != "*" && .SubType == "*" {
		return true
	}
	return false
}

func ( accept_slice) (,  int) {
	 := []Accept()
	[], [] = [], []
}
Parse an Accept Header string returning a sorted list of clauses
func ( string) ( []Accept) {
	 := strings.Split(, ",")
	 = make([]Accept, 0, len())
	for ,  := range  {
		 := strings.Trim(, " ")

		 := Accept{}
		.Params = make(map[string]string)
		.Q = 1.0

		 := strings.Split(, ";")

		 := [0]
		 := strings.Split(, "/")
		.Type = strings.Trim([0], " ")

		switch {
		case len() == 1 && .Type == "*":
			.SubType = "*"
		case len() == 2:
			.SubType = strings.Trim([1], " ")
		default:
			continue
		}

		if len() == 1 {
			 = append(, )
			continue
		}

		for ,  := range [1:] {
			 := strings.SplitN(, "=", 2)
			if len() != 2 {
				continue
			}
			 := strings.Trim([0], " ")
			if  == "q" {
				.Q, _ = strconv.ParseFloat([1], 32)
			} else {
				.Params[] = strings.Trim([1], " ")
			}
		}

		 = append(, )
	}

	 := accept_slice()
	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 ,  := range ParseAccept() {
		for ,  := range  {
			if .Type == [0] && .SubType == [1] {
				 = []
				return
			}
			if .Type == [0] && .SubType == "*" {
				 = []
				return
			}
			if .Type == "*" && .SubType == "*" {
				 = []
				return
			}
		}
	}
	return