requests.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package httpx
  2. import (
  3. "io"
  4. "net/http"
  5. "strings"
  6. "github.com/tal-tech/go-zero/core/mapping"
  7. "github.com/tal-tech/go-zero/rest/internal/context"
  8. )
  9. const (
  10. formKey = "form"
  11. pathKey = "path"
  12. emptyJson = "{}"
  13. maxMemory = 32 << 20 // 32MB
  14. maxBodyLen = 8 << 20 // 8MB
  15. separator = ";"
  16. tokensInAttribute = 2
  17. )
  18. var (
  19. formUnmarshaler = mapping.NewUnmarshaler(formKey, mapping.WithStringValues())
  20. pathUnmarshaler = mapping.NewUnmarshaler(pathKey, mapping.WithStringValues())
  21. )
  22. // Parse parses the request.
  23. func Parse(r *http.Request, v interface{}) error {
  24. if err := ParsePath(r, v); err != nil {
  25. return err
  26. }
  27. if err := ParseForm(r, v); err != nil {
  28. return err
  29. }
  30. return ParseJsonBody(r, v)
  31. }
  32. // ParseForm parses the form request.
  33. func ParseForm(r *http.Request, v interface{}) error {
  34. if err := r.ParseForm(); err != nil {
  35. return err
  36. }
  37. if err := r.ParseMultipartForm(maxMemory); err != nil {
  38. if err != http.ErrNotMultipart {
  39. return err
  40. }
  41. }
  42. params := make(map[string]interface{}, len(r.Form))
  43. for name := range r.Form {
  44. formValue := r.Form.Get(name)
  45. if len(formValue) > 0 {
  46. params[name] = formValue
  47. }
  48. }
  49. return formUnmarshaler.Unmarshal(params, v)
  50. }
  51. func ParseHeader(headerValue string) map[string]string {
  52. ret := make(map[string]string)
  53. fields := strings.Split(headerValue, separator)
  54. for _, field := range fields {
  55. field = strings.TrimSpace(field)
  56. if len(field) == 0 {
  57. continue
  58. }
  59. kv := strings.SplitN(field, "=", tokensInAttribute)
  60. if len(kv) != tokensInAttribute {
  61. continue
  62. }
  63. ret[kv[0]] = kv[1]
  64. }
  65. return ret
  66. }
  67. // ParseJsonBody parses the post request which contains json in body.
  68. func ParseJsonBody(r *http.Request, v interface{}) error {
  69. var reader io.Reader
  70. if withJsonBody(r) {
  71. reader = io.LimitReader(r.Body, maxBodyLen)
  72. } else {
  73. reader = strings.NewReader(emptyJson)
  74. }
  75. return mapping.UnmarshalJsonReader(reader, v)
  76. }
  77. // ParsePath parses the symbols reside in url path.
  78. // Like http://localhost/bag/:name
  79. func ParsePath(r *http.Request, v interface{}) error {
  80. vars := context.Vars(r)
  81. m := make(map[string]interface{}, len(vars))
  82. for k, v := range vars {
  83. m[k] = v
  84. }
  85. return pathUnmarshaler.Unmarshal(m, v)
  86. }
  87. func withJsonBody(r *http.Request) bool {
  88. return r.ContentLength > 0 && strings.Contains(r.Header.Get(ContentType), ApplicationJson)
  89. }