2017-12-29 16:03:30 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/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://gitee.com/johng/gf.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io/ioutil"
|
2017-12-31 12:15:04 +08:00
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2017-12-31 11:09:16 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gconv"
|
2017-12-31 12:15:04 +08:00
|
|
|
|
"gitee.com/johng/gf/g/encoding/gjson"
|
2017-11-23 10:21:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2017-12-31 12:15:04 +08:00
|
|
|
|
// 请求对象
|
2018-01-02 15:52:32 +08:00
|
|
|
|
type Request struct {
|
2017-12-31 12:15:04 +08:00
|
|
|
|
http.Request
|
2018-01-11 16:06:42 +08:00
|
|
|
|
parsedPost bool // POST参数是否已经解析
|
|
|
|
|
getvals *url.Values // GET参数
|
|
|
|
|
Id int // 请求id(唯一)
|
|
|
|
|
Server *Server // 请求关联的服务器对象
|
|
|
|
|
Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全)
|
|
|
|
|
Session *Session // 与当前请求绑定的Session对象(并发安全)
|
|
|
|
|
Response *Response // 对应请求的返回数据操作对象
|
2017-12-30 23:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// 初始化GET请求参数
|
|
|
|
|
func (r *Request) initGet() {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if r.getvals == nil {
|
2018-01-03 10:23:37 +08:00
|
|
|
|
values := r.URL.Query()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
r.getvals = &values
|
|
|
|
|
}
|
2018-01-11 16:06:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化POST请求参数
|
|
|
|
|
func (r *Request) initPost() {
|
|
|
|
|
if !r.parsedPost {
|
|
|
|
|
// 快速保存,尽量避免并发问题
|
|
|
|
|
r.parsedPost = true
|
|
|
|
|
// MultiMedia表单请求解析允许最大使用内存:1GB
|
|
|
|
|
r.ParseMultipartForm(1024*1024*1024)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得指定名称的get参数列表
|
|
|
|
|
func (r *Request) GetQuery(k string) []string {
|
|
|
|
|
r.initGet()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if v, ok := (*r.getvals)[k]; ok {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryBool(k string) bool {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Bool(r.GetQueryString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryInt(k string) int {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Int(r.GetQueryString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryUint(k string) uint {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Uint(r.GetQueryString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryFloat32(k string) float32 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float32(r.GetQueryString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryFloat64(k string) float64 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float64(r.GetQueryString(k))
|
2017-12-08 09:50:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryString(k string) string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
v := r.GetQuery(k)
|
|
|
|
|
if v == nil {
|
|
|
|
|
return ""
|
|
|
|
|
} else {
|
|
|
|
|
return v[0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryArray(k string) []string {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return r.GetQuery(k)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
2018-01-11 16:06:42 +08:00
|
|
|
|
func (r *Request) GetQueryMap(defaultMap...map[string]string) map[string]string {
|
|
|
|
|
r.initGet()
|
2017-12-28 17:28:32 +08:00
|
|
|
|
m := make(map[string]string)
|
2018-01-11 16:06:42 +08:00
|
|
|
|
if len(defaultMap) == 0 {
|
|
|
|
|
for k, v := range *r.getvals {
|
|
|
|
|
m[k] = v[0]
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range defaultMap[0] {
|
|
|
|
|
v2 := r.GetQueryArray(k)
|
|
|
|
|
if v2 == nil {
|
|
|
|
|
m[k] = v
|
|
|
|
|
} else {
|
|
|
|
|
m[k] = v2[0]
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得post参数
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPost(k string) []string {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
r.initPost()
|
2017-11-23 10:21:28 +08:00
|
|
|
|
if v, ok := r.PostForm[k]; ok {
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostBool(k string) bool {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Bool(r.GetPostString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostInt(k string) int {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Int(r.GetPostString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostUint(k string) uint {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Uint(r.GetPostString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostFloat32(k string) float32 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float32(r.GetPostString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostFloat64(k string) float64 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float64(r.GetPostString(k))
|
2017-12-08 09:50:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostString(k string) string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
v := r.GetPost(k)
|
|
|
|
|
if v == nil {
|
|
|
|
|
return ""
|
|
|
|
|
} else {
|
|
|
|
|
return v[0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetPostArray(k string) []string {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return r.GetPost(k)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
2017-12-28 17:28:32 +08:00
|
|
|
|
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetPostArray获取特定字段内容
|
2018-01-11 16:06:42 +08:00
|
|
|
|
func (r *Request) GetPostMap(defaultMap...map[string]string) map[string]string {
|
|
|
|
|
r.initPost()
|
2017-12-28 17:28:32 +08:00
|
|
|
|
m := make(map[string]string)
|
2018-01-11 16:06:42 +08:00
|
|
|
|
if len(defaultMap) == 0 {
|
|
|
|
|
for k, v := range r.PostForm {
|
|
|
|
|
m[k] = v[0]
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range defaultMap[0] {
|
|
|
|
|
if v2, ok := r.PostForm[k]; ok {
|
|
|
|
|
m[k] = v2[0]
|
|
|
|
|
} else {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得post或者get提交的参数,如果有同名参数,那么按照get->post优先级进行覆盖
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequest(k string) []string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
v := r.GetQuery(k)
|
|
|
|
|
if v == nil {
|
|
|
|
|
return r.GetPost(k)
|
|
|
|
|
}
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestString(k string) string {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
v := r.GetRequest(k)
|
|
|
|
|
if v == nil {
|
|
|
|
|
return ""
|
|
|
|
|
} else {
|
|
|
|
|
return v[0]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestBool(k string) bool {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Bool(r.GetRequestString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestInt(k string) int {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Int(r.GetRequestString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestUint(k string) uint {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Uint(r.GetRequestString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestFloat32(k string) float32 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float32(r.GetRequestString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestFloat64(k string) float64 {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return gconv.Float64(r.GetRequestString(k))
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRequestArray(k string) []string {
|
2017-12-31 11:09:16 +08:00
|
|
|
|
return r.GetRequest(k)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取指定键名的关联数组,并且给定当指定键名不存在时的默认值
|
2017-12-28 17:28:32 +08:00
|
|
|
|
// 需要注意的是,如果其中一个字段为数组形式,那么只会返回第一个元素,如果需要获取全部的元素,请使用GetRequestArray获取特定字段内容
|
2018-01-11 16:06:42 +08:00
|
|
|
|
func (r *Request) GetRequestMap(defaultMap...map[string]string) map[string]string {
|
|
|
|
|
m := r.GetQueryMap()
|
|
|
|
|
if len(defaultMap) == 0 {
|
|
|
|
|
for k, v := range r.GetPostMap() {
|
|
|
|
|
if _, ok := m[k]; !ok {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range defaultMap[0] {
|
|
|
|
|
v2 := r.GetRequest(k)
|
|
|
|
|
if v2 != nil {
|
|
|
|
|
m[k] = v2[0]
|
|
|
|
|
} else {
|
|
|
|
|
m[k] = v
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// 获取原始请求输入字符串,注意:只能获取一次,读完就没了
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRaw() []byte {
|
2017-12-26 15:41:10 +08:00
|
|
|
|
result, _ := ioutil.ReadAll(r.Body)
|
|
|
|
|
return result
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 11:09:16 +08:00
|
|
|
|
// 获取原始json请求输入字符串,并解析为json对象
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetJson() *gjson.Json {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
data := r.GetRaw()
|
2017-12-26 15:41:10 +08:00
|
|
|
|
if data != nil {
|
|
|
|
|
if j, err := gjson.DecodeToJson(data); err == nil {
|
2017-12-14 17:32:51 +08:00
|
|
|
|
return j
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|