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 (
|
2020-01-01 14:18:00 +08:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-11-29 22:02:19 +08:00
|
|
|
"github.com/gogf/gf/container/gvar"
|
|
|
|
"github.com/gogf/gf/encoding/gjson"
|
|
|
|
"github.com/gogf/gf/encoding/gurl"
|
2020-01-01 14:18:00 +08:00
|
|
|
"github.com/gogf/gf/encoding/gxml"
|
2020-01-04 15:35:21 +08:00
|
|
|
"github.com/gogf/gf/text/gregex"
|
2019-11-29 22:02:19 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
2020-01-01 14:18:00 +08:00
|
|
|
"github.com/gogf/gf/util/gvalid"
|
2019-11-29 22:02:19 +08:00
|
|
|
"io/ioutil"
|
|
|
|
"mime/multipart"
|
2020-01-04 15:35:21 +08:00
|
|
|
"strings"
|
2019-11-29 22:02:19 +08:00
|
|
|
)
|
|
|
|
|
2020-01-01 14:18:00 +08:00
|
|
|
var (
|
|
|
|
// xmlHeaderBytes is the most common XML format header.
|
|
|
|
xmlHeaderBytes = []byte("<?xml")
|
|
|
|
)
|
|
|
|
|
|
|
|
// Parse calls r.GetStruct to convert the parameters, which are sent from client,
|
|
|
|
// to given struct, and then calls gvalid.CheckStruct validating the struct according
|
|
|
|
// to the validation tag of the struct.
|
|
|
|
//
|
|
|
|
// See GetStruct, gvalid.CheckStruct.
|
|
|
|
func (r *Request) Parse(pointer interface{}) error {
|
|
|
|
if err := r.GetStruct(pointer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gvalid.CheckStruct(pointer, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-01 14:18:00 +08:00
|
|
|
return r.GetRequestStruct(pointer, mapping...)
|
2019-12-19 15:14:05 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-01 14:18:00 +08:00
|
|
|
return r.GetRequestStruct(pointer, mapping...)
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
|
2020-01-01 14:18:00 +08:00
|
|
|
// parseQuery parses query string into r.queryMap.
|
|
|
|
func (r *Request) parseQuery() {
|
2019-11-29 22:02:19 +08:00
|
|
|
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.
|
2020-01-01 14:18:00 +08:00
|
|
|
// Note that it also supports JSON data from client request.
|
|
|
|
func (r *Request) parseBody() {
|
2019-11-29 22:02:19 +08:00
|
|
|
if r.parsedBody {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.parsedBody = true
|
2020-01-01 14:18:00 +08:00
|
|
|
if body := r.GetBody(); len(body) > 0 {
|
|
|
|
// Trim space/new line characters.
|
|
|
|
body = bytes.TrimSpace(body)
|
|
|
|
// JSON format checks.
|
|
|
|
if body[0] == '{' && body[len(body)-1] == '}' {
|
|
|
|
_ = json.Unmarshal(body, &r.bodyMap)
|
|
|
|
}
|
|
|
|
// XML format checks.
|
|
|
|
if len(body) > 5 && bytes.EqualFold(body[:5], xmlHeaderBytes) {
|
|
|
|
r.bodyMap, _ = gxml.DecodeWithoutRoot(body)
|
|
|
|
}
|
|
|
|
if body[0] == '<' && body[len(body)-1] == '>' {
|
|
|
|
r.bodyMap, _ = gxml.DecodeWithoutRoot(body)
|
|
|
|
}
|
|
|
|
// Default parameters decoding.
|
|
|
|
if r.bodyMap == nil {
|
|
|
|
r.bodyMap, _ = gstr.Parse(r.GetBodyString())
|
|
|
|
}
|
2019-11-29 22:02:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 14:18:00 +08:00
|
|
|
// parseForm parses the request form for HTTP method PUT, POST, PATCH.
|
2019-11-29 22:02:19 +08:00
|
|
|
// The form data is pared into r.formMap.
|
2020-01-04 15:35:21 +08:00
|
|
|
//
|
|
|
|
// Note that if the form was parsed firstly, the request body would be cleared and empty.
|
2020-01-01 14:18:00 +08:00
|
|
|
func (r *Request) parseForm() {
|
2019-11-29 22:02:19 +08:00
|
|
|
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 {
|
2020-01-04 15:35:21 +08:00
|
|
|
// Invalid parameter name.
|
2020-01-22 20:28:42 +08:00
|
|
|
// Only allow chars of: '\w', '[', ']', '-'.
|
|
|
|
if !gregex.IsMatchString(`^[\w\-\[\]]+$`, name) {
|
2020-01-04 15:35:21 +08:00
|
|
|
if len(r.PostForm) == 1 {
|
|
|
|
// It might be JSON/XML content.
|
|
|
|
r.bodyContent = gconv.UnsafeStrToBytes(name + strings.Join(values, " "))
|
|
|
|
}
|
|
|
|
params = ""
|
|
|
|
break
|
|
|
|
}
|
2019-11-29 22:02:19 +08:00
|
|
|
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)
|
|
|
|
}
|
2020-01-04 15:35:21 +08:00
|
|
|
}
|
|
|
|
if r.formMap == nil {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseBody()
|
2019-12-01 14:07:36 +08:00
|
|
|
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 {
|
2020-01-01 14:18:00 +08:00
|
|
|
r.parseForm()
|
2019-11-29 22:02:19 +08:00
|
|
|
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
|
|
|
|
}
|