2019-11-29 22:02:19 +08:00
|
|
|
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gogf/gf/container/gvar"
|
|
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/encoding/gurl"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
|
|
"io/ioutil"
|
|
|
|
"mime/multipart"
|
|
|
|
)
|
|
|
|
|
2019-12-01 21:05:46 +08:00
|
|
|
// Get is alias of GetRequest, which is one of the most commonly used functions for
|
|
|
|
// retrieving parameter.
|
|
|
|
// See GetRequest.
|
2019-11-29 22:02:19 +08:00
|
|
|
func (r *Request) Get(key string, def ...interface{}) interface{} {
|
|
|
|
return r.GetRequest(key, def...)
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:05:46 +08:00
|
|
|
// GetVar is alis of GetRequestVar.
|
|
|
|
// See GetRequestVar.
|
2019-11-29 22:02:19 +08:00
|
|
|
func (r *Request) GetVar(key string, def ...interface{}) *gvar.Var {
|
|
|
|
return r.GetRequestVar(key, def...)
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:05:46 +08:00
|
|
|
// GetRaw is alias of GetBody.
|
2019-11-30 09:42:07 +08:00
|
|
|
// See GetBody.
|
2019-11-29 22:02:19 +08:00
|
|
|
func (r *Request) GetRaw() []byte {
|
2019-11-30 09:42:07 +08:00
|
|
|
return r.GetBody()
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:05:46 +08:00
|
|
|
// GetRawString is alias of GetBodyString.
|
2019-11-30 09:42:07 +08:00
|
|
|
// See GetBodyString.
|
|
|
|
func (r *Request) GetRawString() string {
|
|
|
|
return r.GetBodyString()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRaw retrieves and returns request body content as bytes.
|
|
|
|
func (r *Request) GetBody() []byte {
|
|
|
|
if r.bodyContent == nil {
|
|
|
|
r.bodyContent, _ = ioutil.ReadAll(r.Body)
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
2019-11-30 09:42:07 +08:00
|
|
|
return r.bodyContent
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRawString retrieves and returns request body content as string.
|
2019-11-30 09:42:07 +08:00
|
|
|
func (r *Request) GetBodyString() string {
|
2019-11-29 22:02:19 +08:00
|
|
|
return gconv.UnsafeBytesToStr(r.GetRaw())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetJson parses current request content as JSON format, and returns the JSON object.
|
|
|
|
// Note that the request content is read from request BODY, not from any field of FORM.
|
|
|
|
func (r *Request) GetJson() (*gjson.Json, error) {
|
|
|
|
return gjson.LoadJson(r.GetRaw())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetString(key string, def ...interface{}) string {
|
|
|
|
return r.GetRequestString(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetBool(key string, def ...interface{}) bool {
|
|
|
|
return r.GetRequestBool(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetInt(key string, def ...interface{}) int {
|
|
|
|
return r.GetRequestInt(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetInt32(key string, def ...interface{}) int32 {
|
|
|
|
return r.GetRequestInt32(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetInt64(key string, def ...interface{}) int64 {
|
|
|
|
return r.GetRequestInt64(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetInts(key string, def ...interface{}) []int {
|
|
|
|
return r.GetRequestInts(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetUint(key string, def ...interface{}) uint {
|
|
|
|
return r.GetRequestUint(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetUint32(key string, def ...interface{}) uint32 {
|
|
|
|
return r.GetRequestUint32(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetUint64(key string, def ...interface{}) uint64 {
|
|
|
|
return r.GetRequestUint64(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetFloat32(key string, def ...interface{}) float32 {
|
|
|
|
return r.GetRequestFloat32(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetFloat64(key string, def ...interface{}) float64 {
|
|
|
|
return r.GetRequestFloat64(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetFloats(key string, def ...interface{}) []float64 {
|
|
|
|
return r.GetRequestFloats(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetArray(key string, def ...interface{}) []string {
|
|
|
|
return r.GetRequestArray(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetStrings(key string, def ...interface{}) []string {
|
|
|
|
return r.GetRequestStrings(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetInterfaces(key string, def ...interface{}) []interface{} {
|
|
|
|
return r.GetRequestInterfaces(key, def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetMap(def ...map[string]interface{}) map[string]interface{} {
|
|
|
|
return r.GetRequestMap(def...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Request) GetMapStrStr(def ...map[string]interface{}) map[string]string {
|
|
|
|
return r.GetRequestMapStrStr(def...)
|
|
|
|
}
|
|
|
|
|
2019-12-19 15:14:05 +08:00
|
|
|
// GetStruct is alias of GetRequestToStruct.
|
|
|
|
// See GetRequestToStruct.
|
|
|
|
func (r *Request) GetStruct(pointer interface{}, mapping ...map[string]string) error {
|
|
|
|
return r.GetRequestToStruct(pointer, mapping...)
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:05:46 +08:00
|
|
|
// GetToStruct is alias of GetRequestToStruct.
|
|
|
|
// See GetRequestToStruct.
|
2019-12-19 15:14:05 +08:00
|
|
|
// Deprecated.
|
2019-11-29 22:02:19 +08:00
|
|
|
func (r *Request) GetToStruct(pointer interface{}, mapping ...map[string]string) error {
|
|
|
|
return r.GetRequestToStruct(pointer, mapping...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseQuery parses query string into r.queryMap.
|
|
|
|
func (r *Request) ParseQuery() {
|
|
|
|
if r.parsedQuery {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.parsedQuery = true
|
|
|
|
if r.URL.RawQuery != "" {
|
|
|
|
var err error
|
|
|
|
r.queryMap, err = gstr.Parse(r.URL.RawQuery)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRaw parses the request raw data into r.rawMap.
|
|
|
|
func (r *Request) ParseBody() {
|
|
|
|
if r.parsedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.parsedBody = true
|
2019-11-30 09:42:07 +08:00
|
|
|
if body := r.GetBodyString(); len(body) > 0 {
|
|
|
|
r.bodyMap, _ = gstr.Parse(body)
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseForm parses the request form for HTTP method PUT, POST, PATCH.
|
|
|
|
// The form data is pared into r.formMap.
|
|
|
|
func (r *Request) ParseForm() {
|
|
|
|
if r.parsedForm {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.parsedForm = true
|
|
|
|
if contentType := r.Header.Get("Content-Type"); contentType != "" {
|
|
|
|
var err error
|
|
|
|
if gstr.Contains(contentType, "multipart/") {
|
|
|
|
// multipart/form-data, multipart/mixed
|
|
|
|
if err = r.ParseMultipartForm(r.Server.config.FormParsingMemory); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else if gstr.Contains(contentType, "form") {
|
|
|
|
// application/x-www-form-urlencoded
|
|
|
|
if err = r.Request.ParseForm(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(r.PostForm) > 0 {
|
|
|
|
// Re-parse the form data using united parsing way.
|
|
|
|
params := ""
|
|
|
|
for name, values := range r.PostForm {
|
|
|
|
if len(values) == 1 {
|
|
|
|
if len(params) > 0 {
|
|
|
|
params += "&"
|
|
|
|
}
|
|
|
|
params += name + "=" + gurl.Encode(values[0])
|
|
|
|
} else {
|
|
|
|
if len(name) > 2 && name[len(name)-2:] == "[]" {
|
|
|
|
name = name[:len(name)-2]
|
|
|
|
for _, v := range values {
|
|
|
|
if len(params) > 0 {
|
|
|
|
params += "&"
|
|
|
|
}
|
|
|
|
params += name + "[]=" + gurl.Encode(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(params) > 0 {
|
|
|
|
params += "&"
|
|
|
|
}
|
|
|
|
params += name + "=" + gurl.Encode(values[len(values)-1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.formMap, err = gstr.Parse(params); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-01 14:07:36 +08:00
|
|
|
} else {
|
|
|
|
r.ParseBody()
|
|
|
|
if len(r.bodyMap) > 0 {
|
|
|
|
r.formMap = r.bodyMap
|
|
|
|
}
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMultipartForm parses and returns the form as multipart form.
|
|
|
|
func (r *Request) GetMultipartForm() *multipart.Form {
|
|
|
|
r.ParseForm()
|
|
|
|
return r.MultipartForm
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMultipartFiles returns the post files array.
|
|
|
|
// Note that the request form should be type of multipart.
|
|
|
|
func (r *Request) GetMultipartFiles(name string) []*multipart.FileHeader {
|
|
|
|
form := r.GetMultipartForm()
|
|
|
|
if form == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if v := form.File[name]; len(v) > 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
// Support "name[]" as array parameter.
|
|
|
|
if v := form.File[name+"[]"]; len(v) > 0 {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|