binding.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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 and PUT 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" || 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. if exists {
  219. numElems := len(inputValue)
  220. if structField.Kind() == reflect.Slice && numElems > 0 {
  221. sliceOf := structField.Type().Elem().Kind()
  222. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  223. for i := 0; i < numElems; i++ {
  224. setWithProperType(sliceOf, inputValue[i], slice.Index(i), inputFieldName, errors)
  225. }
  226. formStruct.Field(i).Set(slice)
  227. } else {
  228. setWithProperType(typeField.Type.Kind(), inputValue[0], structField, inputFieldName, errors)
  229. }
  230. continue
  231. }
  232. inputFile, exists := formfile[inputFieldName]
  233. if !exists {
  234. continue
  235. }
  236. fhType := reflect.TypeOf((*multipart.FileHeader)(nil))
  237. numElems := len(inputFile)
  238. if structField.Kind() == reflect.Slice && numElems > 0 && structField.Type().Elem() == fhType {
  239. slice := reflect.MakeSlice(structField.Type(), numElems, numElems)
  240. for i := 0; i < numElems; i++ {
  241. slice.Index(i).Set(reflect.ValueOf(inputFile[i]))
  242. }
  243. structField.Set(slice)
  244. } else if structField.Type() == fhType {
  245. structField.Set(reflect.ValueOf(inputFile[0]))
  246. }
  247. }
  248. }
  249. }
  250. // ErrorHandler simply counts the number of errors in the
  251. // context and, if more than 0, writes a response with an
  252. // error code and a JSON payload describing the errors.
  253. // The response will have a JSON content-type.
  254. // Middleware remaining on the stack will not even see the request
  255. // if, by this point, there are any errors.
  256. // This is a "default" handler, of sorts, and you are
  257. // welcome to use your own instead. The Bind middleware
  258. // invokes this automatically for convenience.
  259. func ErrorHandler(errs Errors, resp http.ResponseWriter) {
  260. if len(errs) > 0 {
  261. resp.Header().Set("Content-Type", jsonContentType)
  262. if errs.Has(DeserializationError) {
  263. resp.WriteHeader(http.StatusBadRequest)
  264. } else if errs.Has(ContentTypeError) {
  265. resp.WriteHeader(http.StatusUnsupportedMediaType)
  266. } else {
  267. resp.WriteHeader(StatusUnprocessableEntity)
  268. }
  269. errOutput, _ := json.Marshal(errs)
  270. resp.Write(errOutput)
  271. return
  272. }
  273. }
  274. // This sets the value in a struct of an indeterminate type to the
  275. // matching value from the request (via Form middleware) in the
  276. // same type, so that not all deserialized values have to be strings.
  277. // Supported types are string, int, float, and bool.
  278. func setWithProperType(valueKind reflect.Kind, val string, structField reflect.Value, nameInTag string, errors Errors) {
  279. switch valueKind {
  280. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  281. if val == "" {
  282. val = "0"
  283. }
  284. intVal, err := strconv.ParseInt(val, 10, 64)
  285. if err != nil {
  286. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as integer")
  287. } else {
  288. structField.SetInt(intVal)
  289. }
  290. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  291. if val == "" {
  292. val = "0"
  293. }
  294. uintVal, err := strconv.ParseUint(val, 10, 64)
  295. if err != nil {
  296. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as unsigned integer")
  297. } else {
  298. structField.SetUint(uintVal)
  299. }
  300. case reflect.Bool:
  301. if val == "" {
  302. val = "false"
  303. }
  304. boolVal, err := strconv.ParseBool(val)
  305. if err != nil {
  306. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as boolean")
  307. } else {
  308. structField.SetBool(boolVal)
  309. }
  310. case reflect.Float32:
  311. if val == "" {
  312. val = "0.0"
  313. }
  314. floatVal, err := strconv.ParseFloat(val, 32)
  315. if err != nil {
  316. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 32-bit float")
  317. } else {
  318. structField.SetFloat(floatVal)
  319. }
  320. case reflect.Float64:
  321. if val == "" {
  322. val = "0.0"
  323. }
  324. floatVal, err := strconv.ParseFloat(val, 64)
  325. if err != nil {
  326. errors.Add([]string{nameInTag}, TypeError, "Value could not be parsed as 64-bit float")
  327. } else {
  328. structField.SetFloat(floatVal)
  329. }
  330. case reflect.String:
  331. structField.SetString(val)
  332. }
  333. }
  334. // Don't pass in pointers to bind to. Can lead to bugs. See:
  335. // https://github.com/codegangsta/martini-contrib/pull/34#issuecomment-29683659
  336. func ensureNotPointer(obj interface{}) {
  337. if reflect.TypeOf(obj).Kind() == reflect.Ptr {
  338. panic("Pointers are not accepted as binding models")
  339. }
  340. }
  341. // Performs validation and combines errors from validation
  342. // with errors from deserialization, then maps both the
  343. // resulting struct and the errors to the context.
  344. func validateAndMap(obj reflect.Value, context martini.Context, errors Errors, ifacePtr ...interface{}) {
  345. context.Invoke(Validate(obj.Interface()))
  346. errors = append(errors, getErrors(context)...)
  347. context.Map(errors)
  348. context.Map(obj.Elem().Interface())
  349. if len(ifacePtr) > 0 {
  350. context.MapTo(obj.Elem().Interface(), ifacePtr[0])
  351. }
  352. }
  353. // getErrors simply gets the errors from the context (it's kind of a chore)
  354. func getErrors(context martini.Context) Errors {
  355. return context.Get(reflect.TypeOf(Errors{})).Interface().(Errors)
  356. }
  357. type (
  358. // Implement the Validator interface to handle some rudimentary
  359. // request validation logic so your application doesn't have to.
  360. Validator interface {
  361. // Validate validates that the request is OK. It is recommended
  362. // that validation be limited to checking values for syntax and
  363. // semantics, enough to know that you can make sense of the request
  364. // in your application. For example, you might verify that a credit
  365. // card number matches a valid pattern, but you probably wouldn't
  366. // perform an actual credit card authorization here.
  367. Validate(Errors, *http.Request) Errors
  368. }
  369. )
  370. var (
  371. // Maximum amount of memory to use when parsing a multipart form.
  372. // Set this to whatever value you prefer; default is 10 MB.
  373. MaxMemory = int64(1024 * 1024 * 10)
  374. )
  375. const (
  376. jsonContentType = "application/json; charset=utf-8"
  377. StatusUnprocessableEntity = 422
  378. )