binding.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
  30. // but it reads the body from supplied bytes instead of req.Body.
  31. type BindingBody interface {
  32. Binding
  33. BindBody([]byte, interface{}) error
  34. }
  35. // BindingUri adds BindUri method to Binding. BindUri is similar with Bind,
  36. // but it read the Params.
  37. type BindingUri interface {
  38. Name() string
  39. BindUri(map[string][]string, interface{}) error
  40. }
  41. // These implement the Binding interface and can be used to bind the data
  42. // present in the request to struct instances.
  43. var (
  44. JSON = jsonBinding{}
  45. XML = xmlBinding{}
  46. Form = formBinding{}
  47. Query = queryBinding{}
  48. FormPost = formPostBinding{}
  49. FormMultipart = formMultipartBinding{}
  50. ProtoBuf = protobufBinding{}
  51. MsgPack = msgpackBinding{}
  52. YAML = yamlBinding{}
  53. Uri = uriBinding{}
  54. Header = headerBinding{}
  55. )
  56. // Default returns the appropriate Binding instance based on the HTTP method
  57. // and the content type.
  58. func Default(method, contentType string) Binding {
  59. if method == http.MethodGet {
  60. return Form
  61. }
  62. switch contentType {
  63. case MIMEJSON:
  64. return JSON
  65. case MIMEXML, MIMEXML2:
  66. return XML
  67. case MIMEPROTOBUF:
  68. return ProtoBuf
  69. case MIMEMSGPACK, MIMEMSGPACK2:
  70. return MsgPack
  71. case MIMEYAML:
  72. return YAML
  73. case MIMEMultipartPOSTForm:
  74. return FormMultipart
  75. default: // case MIMEPOSTForm:
  76. return Form
  77. }
  78. }
  79. func validate(obj interface{}) error {
  80. return nil
  81. }
  82. // Bind checks the Content-Type to select a binding engine automatically,
  83. // Depending the "Content-Type" header different bindings are used:
  84. // "application/json" --> JSON binding
  85. // "application/xml" --> XML binding
  86. // otherwise --> returns an error.
  87. // It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
  88. // It decodes the json payload into the struct specified as a pointer.
  89. // It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
  90. func Bind(req *http.Request, obj interface{}) error {
  91. b := Default(req.Method, ContentType(req))
  92. return MustBindWith(req, obj, b)
  93. }
  94. // MustBindWith binds the passed struct pointer using the specified binding engine.
  95. // It will abort the request with HTTP 400 if any error occurs.
  96. // See the binding package.
  97. func MustBindWith(req *http.Request, obj interface{}, b Binding) (err error) {
  98. return b.Bind(req, obj)
  99. }
  100. // ContentType returns the Content-Type header of the request.
  101. func ContentType(req *http.Request) string {
  102. return filterFlags(req.Header.Get("Content-Type"))
  103. }
  104. func filterFlags(content string) string {
  105. for i, char := range content {
  106. if char == ' ' || char == ';' {
  107. return content[:i]
  108. }
  109. }
  110. return content
  111. }