binding.go 3.3 KB

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