negotiate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package apiserver
  14. import (
  15. "mime"
  16. "net/http"
  17. "strconv"
  18. "strings"
  19. "bitbucket.org/ww/goautoneg"
  20. "k8s.io/kubernetes/pkg/runtime"
  21. )
  22. func negotiateOutput(req *http.Request, supported []string) (string, map[string]string, error) {
  23. acceptHeader := req.Header.Get("Accept")
  24. if len(acceptHeader) == 0 && len(supported) > 0 {
  25. acceptHeader = supported[0]
  26. }
  27. accept, ok := negotiate(acceptHeader, supported)
  28. if !ok {
  29. return "", nil, errNotAcceptable{supported}
  30. }
  31. pretty := isPrettyPrint(req)
  32. if _, ok := accept.Params["pretty"]; !ok && pretty {
  33. accept.Params["pretty"] = "1"
  34. }
  35. mediaType := accept.Type
  36. if len(accept.SubType) > 0 {
  37. mediaType += "/" + accept.SubType
  38. }
  39. return mediaType, accept.Params, nil
  40. }
  41. func negotiateOutputSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.SerializerInfo, error) {
  42. supported := ns.SupportedMediaTypes()
  43. mediaType, params, err := negotiateOutput(req, supported)
  44. if err != nil {
  45. return runtime.SerializerInfo{}, err
  46. }
  47. if s, ok := ns.SerializerForMediaType(mediaType, params); ok {
  48. return s, nil
  49. }
  50. return runtime.SerializerInfo{}, errNotAcceptable{supported}
  51. }
  52. func negotiateOutputStreamSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.StreamSerializerInfo, error) {
  53. supported := ns.SupportedMediaTypes()
  54. mediaType, params, err := negotiateOutput(req, supported)
  55. if err != nil {
  56. return runtime.StreamSerializerInfo{}, err
  57. }
  58. if s, ok := ns.StreamingSerializerForMediaType(mediaType, params); ok {
  59. return s, nil
  60. }
  61. return runtime.StreamSerializerInfo{}, errNotAcceptable{supported}
  62. }
  63. func negotiateInputSerializer(req *http.Request, s runtime.NegotiatedSerializer) (runtime.SerializerInfo, error) {
  64. supported := s.SupportedMediaTypes()
  65. mediaType := req.Header.Get("Content-Type")
  66. if len(mediaType) == 0 {
  67. mediaType = supported[0]
  68. }
  69. mediaType, options, err := mime.ParseMediaType(mediaType)
  70. if err != nil {
  71. return runtime.SerializerInfo{}, errUnsupportedMediaType{supported}
  72. }
  73. out, ok := s.SerializerForMediaType(mediaType, options)
  74. if !ok {
  75. return runtime.SerializerInfo{}, errUnsupportedMediaType{supported}
  76. }
  77. return out, nil
  78. }
  79. // isPrettyPrint returns true if the "pretty" query parameter is true or if the User-Agent
  80. // matches known "human" clients.
  81. func isPrettyPrint(req *http.Request) bool {
  82. // DEPRECATED: should be part of the content type
  83. if req.URL != nil {
  84. pp := req.URL.Query().Get("pretty")
  85. if len(pp) > 0 {
  86. pretty, _ := strconv.ParseBool(pp)
  87. return pretty
  88. }
  89. }
  90. userAgent := req.UserAgent()
  91. // This covers basic all browers and cli http tools
  92. if strings.HasPrefix(userAgent, "curl") || strings.HasPrefix(userAgent, "Wget") || strings.HasPrefix(userAgent, "Mozilla/5.0") {
  93. return true
  94. }
  95. return false
  96. }
  97. // negotiate the most appropriate content type given the accept header and a list of
  98. // alternatives.
  99. func negotiate(header string, alternatives []string) (goautoneg.Accept, bool) {
  100. alternates := make([][]string, 0, len(alternatives))
  101. for _, alternate := range alternatives {
  102. alternates = append(alternates, strings.SplitN(alternate, "/", 2))
  103. }
  104. for _, clause := range goautoneg.ParseAccept(header) {
  105. for _, alternate := range alternates {
  106. if clause.Type == alternate[0] && clause.SubType == alternate[1] {
  107. return clause, true
  108. }
  109. if clause.Type == alternate[0] && clause.SubType == "*" {
  110. clause.SubType = alternate[1]
  111. return clause, true
  112. }
  113. if clause.Type == "*" && clause.SubType == "*" {
  114. clause.Type = alternate[0]
  115. clause.SubType = alternate[1]
  116. return clause, true
  117. }
  118. }
  119. }
  120. return goautoneg.Accept{}, false
  121. }