/*
HTTP Content-Type Autonegotiation.

The functions in this package implement the behaviour specified in
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Copyright (c) 2011, Open Knowledge Foundation Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    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 NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package goautoneg

import (
	
	
	
)

// Structure to represent a clause in an HTTP Accept Header
type Accept struct {
	Type, SubType string
	Q             float64
	Params        map[string]string
}

// acceptSlice is defined to implement sort interface.
type acceptSlice []Accept

func ( acceptSlice) () int {
	return len()
}

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

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 clauses
func ( string) acceptSlice {
	 := 0
	 := 
	for len() > 0 {
		++
		_,  = nextSplitElement(, ",")
	}
	 := make(acceptSlice, 0, )

	 = 
	var  string
	for len() > 0 {
		,  = nextSplitElement(, ",")
		 = strings.TrimFunc(, stringTrimSpaceCutset)

		 := Accept{
			Q: 1.0,
		}

		,  := nextSplitElement(, ";")

		,  := nextSplitElement(, "/")
		.Type = strings.TrimFunc(, stringTrimSpaceCutset)

		switch {
		case len() == 0:
			if .Type == "*" {
				.SubType = "*"
			} else {
				continue
			}
		default:
			var  string
			,  = nextSplitElement(, "/")
			if len() > 0 {
				continue
			}
			.SubType = strings.TrimFunc(, stringTrimSpaceCutset)
		}

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

		.Params = make(map[string]string)
		for len() > 0 {
			,  = nextSplitElement(, ";")
			,  = nextSplitElement(, "=")
			if len() == 0 {
				continue
			}
			var  string
			,  = nextSplitElement(, "=")
			if len() != 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 ,  := range ParseAccept() {
		for ,  := range  {
			if .Type == [0] && .SubType == [1] {
				 = []
				return
			}
			if .Type == [0] && .SubType == "*" {
				 = []
				return
			}
			if .Type == "*" && .SubType == "*" {
				 = []
				return
			}
		}
	}
	return
}