ghttp_request_param.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package ghttp
  7. import (
  8. "bytes"
  9. "fmt"
  10. "io/ioutil"
  11. "mime/multipart"
  12. "reflect"
  13. "strings"
  14. "github.com/gogf/gf/container/gvar"
  15. "github.com/gogf/gf/encoding/gjson"
  16. "github.com/gogf/gf/encoding/gurl"
  17. "github.com/gogf/gf/encoding/gxml"
  18. "github.com/gogf/gf/errors/gcode"
  19. "github.com/gogf/gf/errors/gerror"
  20. "github.com/gogf/gf/internal/json"
  21. "github.com/gogf/gf/internal/utils"
  22. "github.com/gogf/gf/text/gregex"
  23. "github.com/gogf/gf/text/gstr"
  24. "github.com/gogf/gf/util/gconv"
  25. "github.com/gogf/gf/util/gvalid"
  26. )
  27. const (
  28. parseTypeRequest = 0
  29. parseTypeQuery = 1
  30. parseTypeForm = 2
  31. )
  32. var (
  33. // xmlHeaderBytes is the most common XML format header.
  34. xmlHeaderBytes = []byte("<?xml")
  35. )
  36. // Parse is the most commonly used function, which converts request parameters to struct or struct
  37. // slice. It also automatically validates the struct or every element of the struct slice according
  38. // to the validation tag of the struct.
  39. //
  40. // The parameter <pointer> can be type of: *struct/**struct/*[]struct/*[]*struct.
  41. //
  42. // It supports single and multiple struct convertion:
  43. // 1. Single struct, post content like: {"id":1, "name":"john"} or ?id=1&name=john
  44. // 2. Multiple struct, post content like: [{"id":1, "name":"john"}, {"id":, "name":"smith"}]
  45. //
  46. // TODO: Improve the performance by reducing duplicated reflect usage on the same variable across packages.
  47. func (r *Request) Parse(pointer interface{}) error {
  48. return r.doParse(pointer, parseTypeRequest)
  49. }
  50. // ParseQuery performs like function Parse, but only parses the query parameters.
  51. func (r *Request) ParseQuery(pointer interface{}) error {
  52. return r.doParse(pointer, parseTypeQuery)
  53. }
  54. // ParseForm performs like function Parse, but only parses the form parameters or the body content.
  55. func (r *Request) ParseForm(pointer interface{}) error {
  56. return r.doParse(pointer, parseTypeForm)
  57. }
  58. // doParse parses the request data to struct/structs according to request type.
  59. func (r *Request) doParse(pointer interface{}, requestType int) error {
  60. var (
  61. reflectVal1 = reflect.ValueOf(pointer)
  62. reflectKind1 = reflectVal1.Kind()
  63. )
  64. if reflectKind1 != reflect.Ptr {
  65. return fmt.Errorf(
  66. "parameter should be type of *struct/**struct/*[]struct/*[]*struct, but got: %v",
  67. reflectKind1,
  68. )
  69. }
  70. var (
  71. reflectVal2 = reflectVal1.Elem()
  72. reflectKind2 = reflectVal2.Kind()
  73. )
  74. switch reflectKind2 {
  75. // Single struct, post content like:
  76. // 1. {"id":1, "name":"john"}
  77. // 2. ?id=1&name=john
  78. case reflect.Ptr, reflect.Struct:
  79. var (
  80. err error
  81. data map[string]interface{}
  82. )
  83. // Converting.
  84. switch requestType {
  85. case parseTypeQuery:
  86. if data, err = r.doGetQueryStruct(pointer); err != nil {
  87. return err
  88. }
  89. case parseTypeForm:
  90. if data, err = r.doGetFormStruct(pointer); err != nil {
  91. return err
  92. }
  93. default:
  94. if data, err = r.doGetRequestStruct(pointer); err != nil {
  95. return err
  96. }
  97. }
  98. // Validation.
  99. if err := gvalid.CheckStructWithData(r.Context(), pointer, data, nil); err != nil {
  100. return err
  101. }
  102. // Multiple struct, it only supports JSON type post content like:
  103. // [{"id":1, "name":"john"}, {"id":, "name":"smith"}]
  104. case reflect.Array, reflect.Slice:
  105. // If struct slice conversion, it might post JSON/XML content,
  106. // so it uses `gjson` for the conversion.
  107. j, err := gjson.LoadContent(r.GetBody())
  108. if err != nil {
  109. return err
  110. }
  111. if err := j.GetStructs(".", pointer); err != nil {
  112. return err
  113. }
  114. for i := 0; i < reflectVal2.Len(); i++ {
  115. if err := gvalid.CheckStructWithData(
  116. r.Context(),
  117. reflectVal2.Index(i),
  118. j.GetMap(gconv.String(i)),
  119. nil,
  120. ); err != nil {
  121. return err
  122. }
  123. }
  124. }
  125. return nil
  126. }
  127. // Get is alias of GetRequest, which is one of the most commonly used functions for
  128. // retrieving parameter.
  129. // See r.GetRequest.
  130. func (r *Request) Get(key string, def ...interface{}) interface{} {
  131. return r.GetRequest(key, def...)
  132. }
  133. // GetVar is alis of GetRequestVar.
  134. // See GetRequestVar.
  135. func (r *Request) GetVar(key string, def ...interface{}) *gvar.Var {
  136. return r.GetRequestVar(key, def...)
  137. }
  138. // GetRaw is alias of GetBody.
  139. // See GetBody.
  140. // Deprecated, use GetBody instead.
  141. func (r *Request) GetRaw() []byte {
  142. return r.GetBody()
  143. }
  144. // GetRawString is alias of GetBodyString.
  145. // See GetBodyString.
  146. // Deprecated, use GetBodyString instead.
  147. func (r *Request) GetRawString() string {
  148. return r.GetBodyString()
  149. }
  150. // GetBody retrieves and returns request body content as bytes.
  151. // It can be called multiple times retrieving the same body content.
  152. func (r *Request) GetBody() []byte {
  153. if r.bodyContent == nil {
  154. r.bodyContent, _ = ioutil.ReadAll(r.Body)
  155. r.Body = utils.NewReadCloser(r.bodyContent, true)
  156. }
  157. return r.bodyContent
  158. }
  159. // GetBodyString retrieves and returns request body content as string.
  160. // It can be called multiple times retrieving the same body content.
  161. func (r *Request) GetBodyString() string {
  162. return string(r.GetBody())
  163. }
  164. // GetJson parses current request content as JSON format, and returns the JSON object.
  165. // Note that the request content is read from request BODY, not from any field of FORM.
  166. func (r *Request) GetJson() (*gjson.Json, error) {
  167. return gjson.LoadJson(r.GetBody())
  168. }
  169. // GetString is an alias and convenient function for GetRequestString.
  170. // See GetRequestString.
  171. func (r *Request) GetString(key string, def ...interface{}) string {
  172. return r.GetRequestString(key, def...)
  173. }
  174. // GetBool is an alias and convenient function for GetRequestBool.
  175. // See GetRequestBool.
  176. func (r *Request) GetBool(key string, def ...interface{}) bool {
  177. return r.GetRequestBool(key, def...)
  178. }
  179. // GetInt is an alias and convenient function for GetRequestInt.
  180. // See GetRequestInt.
  181. func (r *Request) GetInt(key string, def ...interface{}) int {
  182. return r.GetRequestInt(key, def...)
  183. }
  184. // GetInt32 is an alias and convenient function for GetRequestInt32.
  185. // See GetRequestInt32.
  186. func (r *Request) GetInt32(key string, def ...interface{}) int32 {
  187. return r.GetRequestInt32(key, def...)
  188. }
  189. // GetInt64 is an alias and convenient function for GetRequestInt64.
  190. // See GetRequestInt64.
  191. func (r *Request) GetInt64(key string, def ...interface{}) int64 {
  192. return r.GetRequestInt64(key, def...)
  193. }
  194. // GetInts is an alias and convenient function for GetRequestInts.
  195. // See GetRequestInts.
  196. func (r *Request) GetInts(key string, def ...interface{}) []int {
  197. return r.GetRequestInts(key, def...)
  198. }
  199. // GetUint is an alias and convenient function for GetRequestUint.
  200. // See GetRequestUint.
  201. func (r *Request) GetUint(key string, def ...interface{}) uint {
  202. return r.GetRequestUint(key, def...)
  203. }
  204. // GetUint32 is an alias and convenient function for GetRequestUint32.
  205. // See GetRequestUint32.
  206. func (r *Request) GetUint32(key string, def ...interface{}) uint32 {
  207. return r.GetRequestUint32(key, def...)
  208. }
  209. // GetUint64 is an alias and convenient function for GetRequestUint64.
  210. // See GetRequestUint64.
  211. func (r *Request) GetUint64(key string, def ...interface{}) uint64 {
  212. return r.GetRequestUint64(key, def...)
  213. }
  214. // GetFloat32 is an alias and convenient function for GetRequestFloat32.
  215. // See GetRequestFloat32.
  216. func (r *Request) GetFloat32(key string, def ...interface{}) float32 {
  217. return r.GetRequestFloat32(key, def...)
  218. }
  219. // GetFloat64 is an alias and convenient function for GetRequestFloat64.
  220. // See GetRequestFloat64.
  221. func (r *Request) GetFloat64(key string, def ...interface{}) float64 {
  222. return r.GetRequestFloat64(key, def...)
  223. }
  224. // GetFloats is an alias and convenient function for GetRequestFloats.
  225. // See GetRequestFloats.
  226. func (r *Request) GetFloats(key string, def ...interface{}) []float64 {
  227. return r.GetRequestFloats(key, def...)
  228. }
  229. // GetArray is an alias and convenient function for GetRequestArray.
  230. // See GetRequestArray.
  231. func (r *Request) GetArray(key string, def ...interface{}) []string {
  232. return r.GetRequestArray(key, def...)
  233. }
  234. // GetStrings is an alias and convenient function for GetRequestStrings.
  235. // See GetRequestStrings.
  236. func (r *Request) GetStrings(key string, def ...interface{}) []string {
  237. return r.GetRequestStrings(key, def...)
  238. }
  239. // GetInterfaces is an alias and convenient function for GetRequestInterfaces.
  240. // See GetRequestInterfaces.
  241. func (r *Request) GetInterfaces(key string, def ...interface{}) []interface{} {
  242. return r.GetRequestInterfaces(key, def...)
  243. }
  244. // GetMap is an alias and convenient function for GetRequestMap.
  245. // See GetRequestMap.
  246. func (r *Request) GetMap(def ...map[string]interface{}) map[string]interface{} {
  247. return r.GetRequestMap(def...)
  248. }
  249. // GetMapStrStr is an alias and convenient function for GetRequestMapStrStr.
  250. // See GetRequestMapStrStr.
  251. func (r *Request) GetMapStrStr(def ...map[string]interface{}) map[string]string {
  252. return r.GetRequestMapStrStr(def...)
  253. }
  254. // GetStruct is an alias and convenient function for GetRequestStruct.
  255. // See GetRequestStruct.
  256. func (r *Request) GetStruct(pointer interface{}, mapping ...map[string]string) error {
  257. return r.GetRequestStruct(pointer, mapping...)
  258. }
  259. // parseQuery parses query string into r.queryMap.
  260. func (r *Request) parseQuery() {
  261. if r.parsedQuery {
  262. return
  263. }
  264. r.parsedQuery = true
  265. if r.URL.RawQuery != "" {
  266. var err error
  267. r.queryMap, err = gstr.Parse(r.URL.RawQuery)
  268. if err != nil {
  269. panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, ""))
  270. }
  271. }
  272. }
  273. // parseBody parses the request raw data into r.rawMap.
  274. // Note that it also supports JSON data from client request.
  275. func (r *Request) parseBody() {
  276. if r.parsedBody {
  277. return
  278. }
  279. r.parsedBody = true
  280. // There's no data posted.
  281. if r.ContentLength == 0 {
  282. return
  283. }
  284. if body := r.GetBody(); len(body) > 0 {
  285. // Trim space/new line characters.
  286. body = bytes.TrimSpace(body)
  287. // JSON format checks.
  288. if body[0] == '{' && body[len(body)-1] == '}' {
  289. _ = json.UnmarshalUseNumber(body, &r.bodyMap)
  290. }
  291. // XML format checks.
  292. if len(body) > 5 && bytes.EqualFold(body[:5], xmlHeaderBytes) {
  293. r.bodyMap, _ = gxml.DecodeWithoutRoot(body)
  294. }
  295. if body[0] == '<' && body[len(body)-1] == '>' {
  296. r.bodyMap, _ = gxml.DecodeWithoutRoot(body)
  297. }
  298. // Default parameters decoding.
  299. if r.bodyMap == nil {
  300. r.bodyMap, _ = gstr.Parse(r.GetBodyString())
  301. }
  302. }
  303. }
  304. // parseForm parses the request form for HTTP method PUT, POST, PATCH.
  305. // The form data is pared into r.formMap.
  306. //
  307. // Note that if the form was parsed firstly, the request body would be cleared and empty.
  308. func (r *Request) parseForm() {
  309. if r.parsedForm {
  310. return
  311. }
  312. r.parsedForm = true
  313. // There's no data posted.
  314. if r.ContentLength == 0 {
  315. return
  316. }
  317. if contentType := r.Header.Get("Content-Type"); contentType != "" {
  318. var err error
  319. if gstr.Contains(contentType, "multipart/") {
  320. // multipart/form-data, multipart/mixed
  321. if err = r.ParseMultipartForm(r.Server.config.FormParsingMemory); err != nil {
  322. panic(gerror.WrapCode(gcode.CodeInvalidRequest, err, ""))
  323. }
  324. } else if gstr.Contains(contentType, "form") {
  325. // application/x-www-form-urlencoded
  326. if err = r.Request.ParseForm(); err != nil {
  327. panic(gerror.WrapCode(gcode.CodeInvalidRequest, err, ""))
  328. }
  329. }
  330. if len(r.PostForm) > 0 {
  331. // Re-parse the form data using united parsing way.
  332. params := ""
  333. for name, values := range r.PostForm {
  334. // Invalid parameter name.
  335. // Only allow chars of: '\w', '[', ']', '-'.
  336. if !gregex.IsMatchString(`^[\w\-\[\]]+$`, name) && len(r.PostForm) == 1 {
  337. // It might be JSON/XML content.
  338. if s := gstr.Trim(name + strings.Join(values, " ")); len(s) > 0 {
  339. if s[0] == '{' && s[len(s)-1] == '}' || s[0] == '<' && s[len(s)-1] == '>' {
  340. r.bodyContent = []byte(s)
  341. params = ""
  342. break
  343. }
  344. }
  345. }
  346. if len(values) == 1 {
  347. if len(params) > 0 {
  348. params += "&"
  349. }
  350. params += name + "=" + gurl.Encode(values[0])
  351. } else {
  352. if len(name) > 2 && name[len(name)-2:] == "[]" {
  353. name = name[:len(name)-2]
  354. for _, v := range values {
  355. if len(params) > 0 {
  356. params += "&"
  357. }
  358. params += name + "[]=" + gurl.Encode(v)
  359. }
  360. } else {
  361. if len(params) > 0 {
  362. params += "&"
  363. }
  364. params += name + "=" + gurl.Encode(values[len(values)-1])
  365. }
  366. }
  367. }
  368. if params != "" {
  369. if r.formMap, err = gstr.Parse(params); err != nil {
  370. panic(gerror.WrapCode(gcode.CodeInvalidParameter, err, ""))
  371. }
  372. }
  373. }
  374. }
  375. // It parses the request body without checking the Content-Type.
  376. if r.formMap == nil {
  377. if r.Method != "GET" {
  378. r.parseBody()
  379. }
  380. if len(r.bodyMap) > 0 {
  381. r.formMap = r.bodyMap
  382. }
  383. }
  384. }
  385. // GetMultipartForm parses and returns the form as multipart form.
  386. func (r *Request) GetMultipartForm() *multipart.Form {
  387. r.parseForm()
  388. return r.MultipartForm
  389. }
  390. // GetMultipartFiles parses and returns the post files array.
  391. // Note that the request form should be type of multipart.
  392. func (r *Request) GetMultipartFiles(name string) []*multipart.FileHeader {
  393. form := r.GetMultipartForm()
  394. if form == nil {
  395. return nil
  396. }
  397. if v := form.File[name]; len(v) > 0 {
  398. return v
  399. }
  400. // Support "name[]" as array parameter.
  401. if v := form.File[name+"[]"]; len(v) > 0 {
  402. return v
  403. }
  404. // Support "name[0]","name[1]","name[2]", etc. as array parameter.
  405. key := ""
  406. files := make([]*multipart.FileHeader, 0)
  407. for i := 0; ; i++ {
  408. key = fmt.Sprintf(`%s[%d]`, name, i)
  409. if v := form.File[key]; len(v) > 0 {
  410. files = append(files, v[0])
  411. } else {
  412. break
  413. }
  414. }
  415. if len(files) > 0 {
  416. return files
  417. }
  418. return nil
  419. }