ghttp_request_param.go 13 KB

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