binding.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /*
  39. ProtoBuf = protobufBinding{}
  40. MsgPack = msgpackBinding{}
  41. YAML = yamlBinding{}
  42. //*/
  43. URI = uriBinding{}
  44. Header = headerBinding{}
  45. )
  46. // Default returns the appropriate Binding instance based on the HTTP method
  47. // and the content type.
  48. func Default(method, contentType string) Binding {
  49. if method == http.MethodGet {
  50. return Form
  51. }
  52. switch contentType {
  53. case MIMEJSON:
  54. return JSON
  55. case MIMEXML, MIMEXML2:
  56. return XML
  57. /*
  58. case MIMEPROTOBUF:
  59. return ProtoBuf
  60. case MIMEMSGPACK, MIMEMSGPACK2:
  61. return MsgPack
  62. case MIMEYAML:
  63. return YAML
  64. //*/
  65. case MIMEMultipartPOSTForm:
  66. return FormMultipart
  67. default: // case MIMEPOSTForm:
  68. return Form
  69. }
  70. }
  71. // Bind checks the Content-Type to select a binding engine automatically,
  72. // Depending the "Content-Type" header different bindings are used:
  73. //
  74. // "application/json" --> JSON binding
  75. // "application/xml" --> XML binding
  76. //
  77. // otherwise --> returns an error.
  78. // It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input.
  79. // It decodes the json payload into the struct specified as a pointer.
  80. // It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
  81. func Bind(req *http.Request, obj interface{}) error {
  82. b := Default(req.Method, ContentType(req))
  83. return MustBindWith(req, obj, b)
  84. }
  85. // BindHeader bind with header
  86. func BindHeader(req *http.Request, obj interface{}) error {
  87. return Header.Bind(req, obj)
  88. }
  89. // MustBindWith binds the passed struct pointer using the specified binding engine.
  90. // It will abort the request with HTTP 400 if any error occurs.
  91. // See the binding package.
  92. func MustBindWith(req *http.Request, obj interface{}, b Binding) (err error) {
  93. return b.Bind(req, obj)
  94. }
  95. // ContentType returns the Content-Type header of the request.
  96. func ContentType(req *http.Request) string {
  97. return filterFlags(req.Header.Get("Content-Type"))
  98. }
  99. func filterFlags(content string) string {
  100. for i, char := range content {
  101. if char == ' ' || char == ';' {
  102. return content[:i]
  103. }
  104. }
  105. return content
  106. }