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.

126 lines
3.7 KiB

  1. // Copyright 2015 go-swagger maintainers
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package middleware
  15. import (
  16. "mime"
  17. "net/http"
  18. "strings"
  19. "github.com/go-openapi/errors"
  20. "github.com/go-openapi/swag"
  21. "github.com/go-openapi/runtime"
  22. )
  23. type validation struct {
  24. context *Context
  25. result []error
  26. request *http.Request
  27. route *MatchedRoute
  28. bound map[string]interface{}
  29. }
  30. // ContentType validates the content type of a request
  31. func validateContentType(allowed []string, actual string) error {
  32. debugLog("validating content type for %q against [%s]", actual, strings.Join(allowed, ", "))
  33. if len(allowed) == 0 {
  34. return nil
  35. }
  36. mt, _, err := mime.ParseMediaType(actual)
  37. if err != nil {
  38. return errors.InvalidContentType(actual, allowed)
  39. }
  40. if swag.ContainsStringsCI(allowed, mt) {
  41. return nil
  42. }
  43. if swag.ContainsStringsCI(allowed, "*/*") {
  44. return nil
  45. }
  46. parts := strings.Split(actual, "/")
  47. if len(parts) == 2 && swag.ContainsStringsCI(allowed, parts[0]+"/*") {
  48. return nil
  49. }
  50. return errors.InvalidContentType(actual, allowed)
  51. }
  52. func validateRequest(ctx *Context, request *http.Request, route *MatchedRoute) *validation {
  53. debugLog("validating request %s %s", request.Method, request.URL.EscapedPath())
  54. validate := &validation{
  55. context: ctx,
  56. request: request,
  57. route: route,
  58. bound: make(map[string]interface{}),
  59. }
  60. validate.contentType()
  61. if len(validate.result) == 0 {
  62. validate.responseFormat()
  63. }
  64. if len(validate.result) == 0 {
  65. validate.parameters()
  66. }
  67. return validate
  68. }
  69. func (v *validation) parameters() {
  70. debugLog("validating request parameters for %s %s", v.request.Method, v.request.URL.EscapedPath())
  71. if result := v.route.Binder.Bind(v.request, v.route.Params, v.route.Consumer, v.bound); result != nil {
  72. if result.Error() == "validation failure list" {
  73. for _, e := range result.(*errors.Validation).Value.([]interface{}) {
  74. v.result = append(v.result, e.(error))
  75. }
  76. return
  77. }
  78. v.result = append(v.result, result)
  79. }
  80. }
  81. func (v *validation) contentType() {
  82. if len(v.result) == 0 && runtime.HasBody(v.request) {
  83. debugLog("validating body content type for %s %s", v.request.Method, v.request.URL.EscapedPath())
  84. ct, _, req, err := v.context.ContentType(v.request)
  85. if err != nil {
  86. v.result = append(v.result, err)
  87. } else {
  88. v.request = req
  89. }
  90. if len(v.result) == 0 {
  91. if err := validateContentType(v.route.Consumes, ct); err != nil {
  92. v.result = append(v.result, err)
  93. }
  94. }
  95. if ct != "" && v.route.Consumer == nil {
  96. cons, ok := v.route.Consumers[ct]
  97. if !ok {
  98. v.result = append(v.result, errors.New(500, "no consumer registered for %s", ct))
  99. } else {
  100. v.route.Consumer = cons
  101. }
  102. }
  103. }
  104. }
  105. func (v *validation) responseFormat() {
  106. // if the route provides values for Produces and no format could be identify then return an error.
  107. // if the route does not specify values for Produces then treat request as valid since the API designer
  108. // choose not to specify the format for responses.
  109. if str, rCtx := v.context.ResponseFormat(v.request, v.route.Produces); str == "" && len(v.route.Produces) > 0 {
  110. v.request = rCtx
  111. v.result = append(v.result, errors.InvalidResponseFormat(v.request.Header.Get(runtime.HeaderAccept), v.route.Produces))
  112. }
  113. }