You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

161 lines
4.2 KiB

  1. /*
  2. Copyright (c) 2011, Open Knowledge Foundation Ltd.
  3. All rights reserved.
  4. HTTP Content-Type Autonegotiation.
  5. The functions in this package implement the behaviour specified in
  6. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
  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. // For internal use, so that we can use the sort interface
  45. type accept_slice []Accept
  46. func (accept accept_slice) Len() int {
  47. slice := []Accept(accept)
  48. return len(slice)
  49. }
  50. func (accept accept_slice) Less(i, j int) bool {
  51. slice := []Accept(accept)
  52. ai, aj := slice[i], slice[j]
  53. if ai.Q > aj.Q {
  54. return true
  55. }
  56. if ai.Type != "*" && aj.Type == "*" {
  57. return true
  58. }
  59. if ai.SubType != "*" && aj.SubType == "*" {
  60. return true
  61. }
  62. return false
  63. }
  64. func (accept accept_slice) Swap(i, j int) {
  65. slice := []Accept(accept)
  66. slice[i], slice[j] = slice[j], slice[i]
  67. }
  68. // Parse an Accept Header string returning a sorted list
  69. // of clauses
  70. func ParseAccept(header string) (accept []Accept) {
  71. parts := strings.Split(header, ",")
  72. accept = make([]Accept, 0, len(parts))
  73. for _, part := range parts {
  74. part := strings.Trim(part, " ")
  75. a := Accept{}
  76. a.Params = make(map[string]string)
  77. a.Q = 1.0
  78. mrp := strings.Split(part, ";")
  79. media_range := mrp[0]
  80. sp := strings.Split(media_range, "/")
  81. a.Type = strings.Trim(sp[0], " ")
  82. switch {
  83. case len(sp) == 1 && a.Type == "*":
  84. a.SubType = "*"
  85. case len(sp) == 2:
  86. a.SubType = strings.Trim(sp[1], " ")
  87. default:
  88. continue
  89. }
  90. if len(mrp) == 1 {
  91. accept = append(accept, a)
  92. continue
  93. }
  94. for _, param := range mrp[1:] {
  95. sp := strings.SplitN(param, "=", 2)
  96. if len(sp) != 2 {
  97. continue
  98. }
  99. token := strings.Trim(sp[0], " ")
  100. if token == "q" {
  101. a.Q, _ = strconv.ParseFloat(sp[1], 32)
  102. } else {
  103. a.Params[token] = strings.Trim(sp[1], " ")
  104. }
  105. }
  106. accept = append(accept, a)
  107. }
  108. slice := accept_slice(accept)
  109. sort.Sort(slice)
  110. return
  111. }
  112. // Negotiate the most appropriate content_type given the accept header
  113. // and a list of alternatives.
  114. func Negotiate(header string, alternatives []string) (content_type string) {
  115. asp := make([][]string, 0, len(alternatives))
  116. for _, ctype := range alternatives {
  117. asp = append(asp, strings.SplitN(ctype, "/", 2))
  118. }
  119. for _, clause := range ParseAccept(header) {
  120. for i, ctsp := range asp {
  121. if clause.Type == ctsp[0] && clause.SubType == ctsp[1] {
  122. content_type = alternatives[i]
  123. return
  124. }
  125. if clause.Type == ctsp[0] && clause.SubType == "*" {
  126. content_type = alternatives[i]
  127. return
  128. }
  129. if clause.Type == "*" && clause.SubType == "*" {
  130. content_type = alternatives[i]
  131. return
  132. }
  133. }
  134. }
  135. return
  136. }