binding.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. //go:build !nomsgpack
  5. // +build !nomsgpack
  6. package binding
  7. import "net/http"
  8. // Content-Type MIME of the most common data formats.
  9. const (
  10. MIMEJSON = "application/json"
  11. MIMEHTML = "text/html"
  12. MIMEXML = "application/xml"
  13. MIMEXML2 = "text/xml"
  14. MIMEPlain = "text/plain"
  15. MIMEPOSTForm = "application/x-www-form-urlencoded"
  16. MIMEMultipartPOSTForm = "multipart/form-data"
  17. MIMEPROTOBUF = "application/x-protobuf"
  18. MIMEMSGPACK = "application/x-msgpack"
  19. MIMEMSGPACK2 = "application/msgpack"
  20. MIMEYAML = "application/x-yaml"
  21. )
  22. // Binding describes the interface which needs to be implemented for binding the
  23. // data present in the request such as JSON request body, query parameters or
  24. // the form POST.
  25. type Binding interface {
  26. Name() string
  27. Bind(*http.Request, interface{}) error
  28. }
  29. // These implement the Binding interface and can be used to bind the data
  30. // present in the request to struct instances.
  31. var (
  32. JSON = jsonBinding{}
  33. XML = xmlBinding{}
  34. Form = formBinding{}
  35. Query = queryBinding{}
  36. FormPost = formPostBinding{}
  37. FormMultipart = formMultipartBinding{}
  38. ProtoBuf = protobufBinding{}
  39. MsgPack = msgpackBinding{}
  40. YAML = yamlBinding{}
  41. URI = uriBinding{}
  42. Header = headerBinding{}
  43. )
  44. // Default returns the appropriate Binding instance based on the HTTP method
  45. // and the content type.
  46. func Default(method, contentType string) Binding {
  47. if method == http.MethodGet {
  48. return Form
  49. }
  50. switch contentType {
  51. case MIMEJSON:
  52. return JSON
  53. case MIMEXML, MIMEXML2:
  54. return XML
  55. case MIMEPROTOBUF:
  56. return ProtoBuf
  57. case MIMEMSGPACK, MIMEMSGPACK2:
  58. return MsgPack
  59. case MIMEYAML:
  60. return YAML
  61. case MIMEMultipartPOSTForm:
  62. return FormMultipart
  63. default: // case MIMEPOSTForm:
  64. return Form
  65. }
  66. }
  67. func validate(obj interface{}) error {
  68. return nil
  69. }
  70. // Bind checks the Content-Type to select a binding engine automatically,
  71. // Depending the "Content-Type" header different bindings are used:
  72. // "application/json" --> JSON binding
  73. // "application/xml" --> XML binding
  74. // otherwise --> returns an error.
  75. // It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
  76. // It decodes the json payload into the struct specified as a pointer.
  77. // It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
  78. func Bind(req *http.Request, obj interface{}) error {
  79. b := Default(req.Method, ContentType(req))
  80. return MustBindWith(req, obj, b)
  81. }
  82. // BindHeader bind with header
  83. func BindHeader(req *http.Request, obj interface{}) error {
  84. return Header.Bind(req, obj)
  85. }
  86. // MustBindWith binds the passed struct pointer using the specified binding engine.
  87. // It will abort the request with HTTP 400 if any error occurs.
  88. // See the binding package.
  89. func MustBindWith(req *http.Request, obj interface{}, b Binding) (err error) {
  90. return b.Bind(req, obj)
  91. }
  92. // ContentType returns the Content-Type header of the request.
  93. func ContentType(req *http.Request) string {
  94. return filterFlags(req.Header.Get("Content-Type"))
  95. }
  96. func filterFlags(content string) string {
  97. for i, char := range content {
  98. if char == ' ' || char == ';' {
  99. return content[:i]
  100. }
  101. }
  102. return content
  103. }