binding.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // Package binding transforms a raw request into a struct
  2. // ready to be used your application. It can also perform
  3. // validation on the data and handle errors.
  4. package binding
  5. import (
  6. "encoding/json"
  7. "io"
  8. "mime/multipart"
  9. "net/http"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "github.com/go-martini/martini"
  14. )
  15. /*
  16. For the land of Middle-ware Earth:
  17. One func to rule them all,
  18. One func to find them,
  19. One func to bring them all,
  20. And in this package BIND them.
  21. */
  22. // Bind wraps up the functionality of the Form and Json middleware
  23. // according to the Content-Type and verb of the request.
  24. // A Content-Type is required for POST, PUT and PATCH requests.
  25. // Bind invokes the ErrorHandler middleware to bail out if errors
  26. // occurred. If you want to perform your own error handling, use
  27. // Form or Json middleware directly. An interface pointer can
  28. // be added as a second argument in order to map the struct to
  29. // a specific interface.
  30. func Bind(obj interface{}, ifacePtr ...interface{}) martini.Handler {
  31. return func(context martini.Context, req *http.Request) {
  32. contentType := req.Header.Get("Content-Type")
  33. if req.Method == "POST" || req.Method == "PUT" || req.Method == "PATCH" || contentType != "" {
  34. if strings.Contains(contentType, "form-urlencoded") {
  35. context.Invoke(Form(obj, ifacePtr...))
  36. } else if strings.Contains(contentType, "multipart/form-data") {
  37. context.Invoke(MultipartForm(obj, ifacePtr...))
  38. } else if strings.Contains(contentType, "json") {
  39. context.Invoke(Json(obj, ifacePtr...))
  40. } else {
  41. var errors Errors
  42. if contentType == "" {
  43. errors.Add([]string{}, ContentTypeError, "Empty Content-Type")
  44. } else {
  45. errors.Add([]string{}, ContentTypeError, "Unsupported Content-Type")
  46. }
  47. context.Map(errors)
  48. }
  49. } else {
  50. context.Invoke(Form(obj, ifacePtr...))
  51. }
  52. context.Invoke(ErrorHandler)
  53. }
  54. }
  55. // Form is middleware to deserialize form-urlencoded data from the request.
  56. // It gets data from the form-urlencoded body, if present, or from the
  57. // query string. It uses the http.Request.ParseForm() method
  58. // to perform deserialization, then reflection is used to map each field
  59. // into the struct with the proper type. Structs with primitive slice types
  60. // (bool, float, int, string) can support deserialization of repeated form
  61. // keys, for example: key=val1&key=val2&key=val3
  62. // An interface pointer can be added as a second argument in order
  63. // to map the struct to a specific interface.
  64. func Form(formStruct interface{}, ifacePtr ...interface{}) martini.Handler {
  65. return func(context martini.Context, req *http.Request) {
  66. var errors Errors
  67. ensureNotPointer(formStruct)
  68. formStruct := reflect.New(reflect.TypeOf(formStruct))
  69. parseErr := req.ParseForm()
  70. // Format validation of the request body or the URL would add considerable overhead,
  71. // and ParseForm does not complain when URL encoding is off.
  72. // Because an empty request body or url can also mean absence of all needed values,
  73. // it is not in all cases a bad request, so let's return 422.
  74. if parseErr != nil {
  75. errors.Add([]string{}, DeserializationError, parseErr.Error())
  76. }
  77. mapForm(formStruct, req.Form, nil, errors)
  78. validateAndMap(formStruct, context, errors, ifacePtr...)
  79. }
  80. }
  81. // MultipartForm works much like Form, except it can parse multipart forms
  82. // and handle file uploads. Like the other deserialization middleware handlers,
  83. // you can pass in an interface to make the interface available for injection
  84. // into other handlers later.
  85. func MultipartForm(formStruct interface{}, ifacePtr ...interface{}) martini.Handler {
  86. return func(context martini.Context, req *http.Request) {
  87. var errors Errors
  88. ensureNotPointer(formStruct)
  89. formStruct := reflect.New(reflect.TypeOf(formStruct))
  90. // This if check is necessary due to https://github.com/martini-contrib/csrf/issues/6
  91. if req.MultipartForm == nil {
  92. // Workaround for multipart forms returning nil instead of an error
  93. // when content is not multipart; see https://code.google.com/p/go/issues/detail?id=6334
  94. if multipartReader, err := req.MultipartReader(); err != nil {
  95. // TODO: Cover this and the next error check with tests
  96. errors.Add([]string{}, DeserializationError, err.Error())
  97. } else {
  98. form, parseErr := multipartReader.ReadForm(MaxMemory)
  99. if parseErr != nil {
  100. errors.Add([]string{}, DeserializationError, parseErr.Error())
  101. }
  102. req.MultipartForm = form
  103. }
  104. }
  105. mapForm(formStruct, req.MultipartForm.Value, req.MultipartForm.File, errors)
  106. validateAndMap(formStruct, context, errors, ifacePtr...)
  107. }
  108. }
  109. // Json is middleware to deserialize a JSON payload from the request
  110. // into the struct that is passed in. The resulting struct is then
  111. // validated, but no error handling is actually performed here.
  112. // An interface pointer can be added as a second argument in order
  113. // to map the struct to a specific interface.
  114. func Json(jsonStruct interface{}, ifacePtr ...interface{}) martini.Handler {
  115. return func(context martini.Context, req *http.Request) {
  116. var errors Errors
  117. ensureNotPointer(jsonStruct)
  118. jsonStruct := reflect.New(reflect.TypeOf(jsonStruct))
  119. if req.Body != nil {
  120. defer req.Body.Close()
  121. err := json.NewDecoder(req.Body).Decode(jsonStruct.Interface())
  122. if err != nil && err != io.EOF {
  123. errors.Add([]string{}, DeserializationError, err.Error())
  124. }
  125. }
  126. validateAndMap(jsonStruct, context, errors, ifacePtr...)
  127. }
  128. }
  129. // Validate is middleware to enforce required fields. If the struct
  130. // passed in implements Validator, then the user-defined Validate method
  131. // is executed, and its errors are mapped to the context. This middleware
  132. // performs no error handling: it merely detects errors and maps them.
  133. func Validate(obj interface{}) martini.Handler {
  134. return func(context martini.Context, req *http.Request) {
  135. var errors Errors
  136. v := reflect.ValueOf(obj)
  137. k := v.Kind()
  138. if k == reflect.Interface || k == reflect.Ptr {
  139. v = v.Elem()
  140. k = v.Kind()
  141. }
  142. if k == reflect.Slice || k == reflect.Array {
  143. for i := 0; i < v.Len(); i++ {
  144. e := v.Index(i).Interface()
  145. errors = validateStruct(errors, e)
  146. if validator, ok := e.(Validator); ok {
  147. errors = validator.Validate(errors, req)
  148. }
  149. }
  150. } else {
  151. errors = validateStruct(errors, obj)
  152. if validator, ok := obj.(Validator); ok {
  153. errors = validator.Validate(errors, req)
  154. }
  155. }
  156. context.Map(errors)
  157. }
  158. }
  159. // Performs required field checking on a struct
  160. func validateStruct(errors Errors, obj interface{}) Errors {
  161. typ := reflect.TypeOf(obj)
  162. val := reflect.ValueOf(obj)
  163. if typ.Kind() == reflect.Ptr {
  164. typ = typ.Elem()
  165. val = val.Elem()
  166. }
  167. for i := 0; i < typ.NumField(); i++ {
  168. field := typ.Field(i)
  169. // Skip ignored and unexported fields in the struct
  170. if field.Tag.Get("form") == "-" || !val.Field(i).CanInterface() {
  171. continue
  172. }
  173. fieldValue := val.Field(i).Interface()
  174. zero := reflect.Zero(field.Type).Interface()
  175. // Validate nested and embedded structs (if pointer, only do so if not nil)
  176. if field.Type.Kind() == reflect.Struct ||
  177. (field.Type.Kind() == reflect.Ptr && !reflect.DeepEqual(zero, fieldValue) &&
  178. field.Type.Elem().Kind() == reflect.Struct) {
  179. errors = validateStruct(errors, fieldValue)
  180. }
  181. if strings.Index(field.Tag.Get("binding"), "required") > -1 {
  182. if reflect.DeepEqual(zero, fieldValue) {
  183. name := field.Name
  184. if j := field.Tag.Get("json"); j != "" {
  185. name = j
  186. } else if f := field.Tag.Get("form"); f != "" {
  187. name = f
  188. }
  189. errors.Add([]string{name}, RequiredError, "Required")
  190. }
  191. }
  192. }
  193. return errors
  194. }
  195. // Takes values from the form data and puts them into a struct
  196. func mapForm(formStruct reflect.Value, form map[string][]string,
  197. formfile map[string][]*multipart.FileHeader, errors Errors) {
  198. if formStruct.Kind() == reflect.Ptr {
  199. formStruct = formStruct.Elem()
  200. }
  201. typ := formStruct.Type()
  202. for i := 0; i < typ.NumField(); i++ {
  203. typeField := typ.Field(i)
  204. structField := formStruct.Field(i)
  205. if typeField.Type.Kind() == reflect.Ptr && typeField.Anonymous {
  206. structField.Set(reflect.New(typeField.Type.Elem()))
  207. mapForm(structField.Elem(), form, formfile, errors)
  208. if reflect.DeepEqual(structField.Elem().Interface(), reflect.Zero(structField.Elem().Type()).Interface()) {
  209. structField.Set(reflect.Zero(structField.Type()))
  210. }
  211. } else if typeField.Type.Kind() == reflect.Struct {
  212. mapForm(structField, form, formfile, errors)
  213. } else if inputFieldName := typeField.Tag.Get("form"); inputFieldName != "" {
  214. if !structField.CanSet() {
  215. continue
  216. }
  217. inputValue, exists := form[inputFieldName]
  218. inputFile, existsFile := formfile[inputFieldName]
  219. if exists && !existsFile {
  220. numElems := len(inputValue)
  221. if structField.Kind() == reflect.Slice && numElems > 0 {
  222. sliceOf := structField.Type().Elem().Kind()
  223. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  224. for i := 0; i < numElems; i++ {
  225. setWithProperType(sliceOf, inputValue[i], slice.Index(i), inputFieldName, errors)
  226. }
  227. formStruct.Field(i).Set(slice)
  228. } else {
  229. kind := typeField.Type.Kind()
  230. if structField.Kind() == reflect.Ptr {
  231. structField.Set(reflect.New(typeField.Type.Elem()))
  232. structField = structField.Elem()
  233. kind = typeField.Type.Elem().Kind()
  234. }
  235. setWithProperType(kind, inputValue[0], structField, inputFieldName, errors)
  236. }
  237. continue
  238. }
  239. if !existsFile {
  240. continue
  241. }
  242. fhType := reflect.TypeOf((*multipart.FileHeader)(nil))
  243. numElems := len(inputFile)
  244. if structField.Kind() == reflect.Slice && numElems > 0 && structField.Type().Elem() == fhType {
  245. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  246. for i := 0; i < numElems; i++ {
  247. slice.Index(i).Set(reflect.ValueOf(inputFile[i]))
  248. }
  249. structField.Set(slice)
  250. } else if structField.Type() == fhType {
  251. structField.Set(reflect.ValueOf(inputFile[0]))
  252. }
  253. }
  254. }
  255. }
  256. // ErrorHandler simply counts the number of errors in the
  257. // context and, if more than 0, writes a response with an
  258. // error code and a JSON payload describing the errors.
  259. // The response will have a JSON content-type.
  260. // Middleware remaining on the stack will not even see the request
  261. // if, by this point, there are any errors.
  262. // This is a "default" handler, of sorts, and you are
  263. // welcome to use your own instead. The Bind middleware
  264. // invokes this automatically for convenience.
  265. func ErrorHandler(errs Errors, resp http.ResponseWriter) {
  266. if len(errs) > 0 {
  267. resp.Header().Set("Content-Type", jsonContentType)
  268. if errs.Has(DeserializationError) {
  269. resp.WriteHeader(http.StatusBadRequest)
  270. } else if errs.Has(ContentTypeError) {
  271. resp.WriteHeader(http.StatusUnsupportedMediaType)
  272. } else {
  273. resp.WriteHeader(StatusUnprocessableEntity)
  274. }
  275. errOutput, _ := json.Marshal(errs)
  276. resp.Write(errOutput)
  277. return
  278. }
  279. }
  280. // This sets the value in a struct of an indeterminate type to the
  281. // matching value from the request (via Form middleware) in the
  282. // same type, so that not all deserialized values have to be strings.
  283. // Supported types are string, int, float, and bool.
  284. func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
  285. switch valueKind {
  286. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  287. if val == "" {
  288. val = "0"
  289. }
  290. intVal, err := strconv.ParseInt(val, 10, 64)
  291. if err != nil {
  292. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as integer")
  293. } else {
  294. structField.SetInt(intVal)
  295. }
  296. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  297. if val == "" {
  298. val = "0"
  299. }
  300. uintVal, err := strconv.ParseUint(val, 10, 64)
  301. if err != nil {
  302. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as unsigned integer")
  303. } else {
  304. structField.SetUint(uintVal)
  305. }
  306. case reflect.Bool:
  307. if val == "" {
  308. val = "false"
  309. }
  310. boolVal, err := strconv.ParseBool(val)
  311. if err != nil {
  312. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as boolean")
  313. } else {
  314. structField.SetBool(boolVal)
  315. }
  316. case reflect.Float32:
  317. if val == "" {
  318. val = "0.0"
  319. }
  320. floatVal, err := strconv.ParseFloat(val, 32)
  321. if err != nil {
  322. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 32-bit float")
  323. } else {
  324. structField.SetFloat(floatVal)
  325. }
  326. case reflect.Float64:
  327. if val == "" {
  328. val = "0.0"
  329. }
  330. floatVal, err := strconv.ParseFloat(val, 64)
  331. if err != nil {
  332. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 64-bit float")
  333. } else {
  334. structField.SetFloat(floatVal)
  335. }
  336. case reflect.String:
  337. structField.SetString(val)
  338. }
  339. }
  340. // Don't pass in pointers to bind to. Can lead to bugs. See:
  341. // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
  342. func ensureNotPointer(obj interface{}) {
  343. if reflect.TypeOf(obj).Kind() == reflect.Ptr {
  344. panic("Pointers are not accepted as binding models")
  345. }
  346. }
  347. // Performs validation and combines errors from validation
  348. // with errors from deserialization, then maps both the
  349. // resulting struct and the errors to the context.
  350. func validateAndMap(obj reflect.Value, context martini.Context, errors Errors, ifacePtr ...interface{}) {
  351. context.Invoke(Validate(obj.Interface()))
  352. errors = append(errors, getErrors(context)...)
  353. context.Map(errors)
  354. context.Map(obj.Elem().Interface())
  355. if len(ifacePtr) > 0 {
  356. context.MapTo(obj.Elem().Interface(), ifacePtr[0])
  357. }
  358. }
  359. // getErrors simply gets the errors from the context (it's kind of a chore)
  360. func getErrors(context martini.Context) Errors {
  361. return context.Get(reflect.TypeOf(Errors{})).Interface().(Errors)
  362. }
  363. type (
  364. // Implement the Validator interface to handle some rudimentary
  365. // request validation logic so your application doesn't have to.
  366. Validator interface {
  367. // Validate validates that the request is OK. It is recommended
  368. // that validation be limited to checking values for syntax and
  369. // semantics, enough to know that you can make sense of the request
  370. // in your application. For example, you might verify that a credit
  371. // card number matches a valid pattern, but you probably wouldn't
  372. // perform an actual credit card authorization here.
  373. Validate(Errors, *http.Request) Errors
  374. }
  375. )
  376. var (
  377. // Maximum amount of memory to use when parsing a multipart form.
  378. // Set this to whatever value you prefer; default is 10 MB.
  379. MaxMemory = int64(1024 * 1024 * 10)
  380. )
  381. const (
  382. jsonContentType = "application/json; charset=utf-8"
  383. StatusUnprocessableEntity = 422
  384. )