autoneg.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. HTTP Content-Type Autonegotiation.
  3. The functions in this package implement the behaviour specified in
  4. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  5. Copyright (c) 2011, Open Knowledge Foundation Ltd.
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without
  8. modification, are permitted provided that the following conditions are
  9. met:
  10. Redistributions of source code must retain the above copyright
  11. notice, this list of conditions and the following disclaimer.
  12. Redistributions in binary form must reproduce the above copyright
  13. notice, this list of conditions and the following disclaimer in
  14. the documentation and/or other materials provided with the
  15. distribution.
  16. Neither the name of the Open Knowledge Foundation Ltd. nor the
  17. names of its contributors may be used to endorse or promote
  18. products derived from this software without specific prior written
  19. permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package goautoneg
  33. import (
  34. "sort"
  35. "strconv"
  36. "strings"
  37. )
  38. // Structure to represent a clause in an HTTP Accept Header
  39. type Accept struct {
  40. Type, SubType string
  41. Q float64
  42. Params map[string]string
  43. }
  44. // acceptSlice is defined to implement sort interface.
  45. type acceptSlice []Accept
  46. func (slice acceptSlice) Len() int {
  47. return len(slice)
  48. }
  49. func (slice acceptSlice) Less(i, j int) bool {
  50. ai, aj := slice[i], slice[j]
  51. if ai.Q > aj.Q {
  52. return true
  53. }
  54. if ai.Type != "*" && aj.Type == "*" {
  55. return true
  56. }
  57. if ai.SubType != "*" && aj.SubType == "*" {
  58. return true
  59. }
  60. return false
  61. }
  62. func (slice acceptSlice) Swap(i, j int) {
  63. slice[i], slice[j] = slice[j], slice[i]
  64. }
  65. func stringTrimSpaceCutset(r rune) bool {
  66. return r == ' '
  67. }
  68. func nextSplitElement(s, sep string) (item string, remaining string) {
  69. if index := strings.Index(s, sep); index != -1 {
  70. return s[:index], s[index+1:]
  71. }
  72. return s, ""
  73. }
  74. // Parse an Accept Header string returning a sorted list
  75. // of clauses
  76. func ParseAccept(header string) acceptSlice {
  77. partsCount := 0
  78. remaining := header
  79. for len(remaining) > 0 {
  80. partsCount++
  81. _, remaining = nextSplitElement(remaining, ",")
  82. }
  83. accept := make(acceptSlice, 0, partsCount)
  84. remaining = header
  85. var part string
  86. for len(remaining) > 0 {
  87. part, remaining = nextSplitElement(remaining, ",")
  88. part = strings.TrimFunc(part, stringTrimSpaceCutset)
  89. a := Accept{
  90. Q: 1.0,
  91. }
  92. sp, remainingPart := nextSplitElement(part, ";")
  93. sp0, spRemaining := nextSplitElement(sp, "/")
  94. a.Type = strings.TrimFunc(sp0, stringTrimSpaceCutset)
  95. switch {
  96. case len(spRemaining) == 0:
  97. if a.Type == "*" {
  98. a.SubType = "*"
  99. } else {
  100. continue
  101. }
  102. default:
  103. var sp1 string
  104. sp1, spRemaining = nextSplitElement(spRemaining, "/")
  105. if len(spRemaining) > 0 {
  106. continue
  107. }
  108. a.SubType = strings.TrimFunc(sp1, stringTrimSpaceCutset)
  109. }
  110. if len(remainingPart) == 0 {
  111. accept = append(accept, a)
  112. continue
  113. }
  114. a.Params = make(map[string]string)
  115. for len(remainingPart) > 0 {
  116. sp, remainingPart = nextSplitElement(remainingPart, ";")
  117. sp0, spRemaining = nextSplitElement(sp, "=")
  118. if len(spRemaining) == 0 {
  119. continue
  120. }
  121. var sp1 string
  122. sp1, spRemaining = nextSplitElement(spRemaining, "=")
  123. if len(spRemaining) != 0 {
  124. continue
  125. }
  126. token := strings.TrimFunc(sp0, stringTrimSpaceCutset)
  127. if token == "q" {
  128. a.Q, _ = strconv.ParseFloat(sp1, 32)
  129. } else {
  130. a.Params[token] = strings.TrimFunc(sp1, stringTrimSpaceCutset)
  131. }
  132. }
  133. accept = append(accept, a)
  134. }
  135. sort.Sort(accept)
  136. return accept
  137. }
  138. // Negotiate the most appropriate content_type given the accept header
  139. // and a list of alternatives.
  140. func Negotiate(header string, alternatives []string) (content_type string) {
  141. asp := make([][]string, 0, len(alternatives))
  142. for _, ctype := range alternatives {
  143. asp = append(asp, strings.SplitN(ctype, "/", 2))
  144. }
  145. for _, clause := range ParseAccept(header) {
  146. for i, ctsp := range asp {
  147. if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
  148. content_type = alternatives[i]
  149. return
  150. }
  151. if clause.Type == ctsp[0] && clause.SubType == "*" {
  152. content_type = alternatives[i]
  153. return
  154. }
  155. if clause.Type == "*" && clause.SubType == "*" {
  156. content_type = alternatives[i]
  157. return
  158. }
  159. }
  160. }
  161. return
  162. }