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"
|
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"
|
2018-04-09 18:00:39 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gtype"
|
2018-04-20 15:43:02 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gregx"
|
2018-04-29 23:19:02 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gtime"
|
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-04-11 16:06:45 +08:00
|
|
|
|
parsedGet *gtype.Bool // GET参数是否已经解析
|
|
|
|
|
parsedPost *gtype.Bool // POST参数是否已经解析
|
|
|
|
|
values map[string][]string // GET参数
|
2018-04-16 16:50:24 +08:00
|
|
|
|
exit *gtype.Bool // 是否退出当前请求流程执行
|
2018-04-11 16:06:45 +08:00
|
|
|
|
Id int // 请求id(唯一)
|
|
|
|
|
Server *Server // 请求关联的服务器对象
|
|
|
|
|
Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全)
|
|
|
|
|
Session *Session // 与当前请求绑定的Session对象(并发安全)
|
|
|
|
|
Response *Response // 对应请求的返回数据操作对象
|
2018-04-22 22:17:20 +08:00
|
|
|
|
Router *Router // 匹配到的路由对象
|
2018-04-29 23:19:02 +08:00
|
|
|
|
EnterTime int64 // 请求进入时间(微秒)
|
|
|
|
|
LeaveTime int64 // 请求完成时间(微秒)
|
2017-12-30 23:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-11 16:06:45 +08:00
|
|
|
|
// 创建一个Request对象
|
|
|
|
|
func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
2018-04-14 01:05:46 +08:00
|
|
|
|
request := &Request{
|
2018-04-11 16:06:45 +08:00
|
|
|
|
parsedGet : gtype.NewBool(),
|
|
|
|
|
parsedPost : gtype.NewBool(),
|
|
|
|
|
values : make(map[string][]string),
|
2018-04-16 16:50:24 +08:00
|
|
|
|
exit : gtype.NewBool(),
|
2018-04-11 16:06:45 +08:00
|
|
|
|
Id : s.servedCount.Add(1),
|
|
|
|
|
Server : s,
|
|
|
|
|
Request : *r,
|
|
|
|
|
Response : &Response {
|
2018-04-20 15:43:02 +08:00
|
|
|
|
ResponseWriter : ResponseWriter{w, http.StatusOK, 0},
|
2018-04-11 16:06:45 +08:00
|
|
|
|
},
|
2018-04-29 23:19:02 +08:00
|
|
|
|
EnterTime : gtime.Microsecond(),
|
2018-04-11 16:06:45 +08:00
|
|
|
|
}
|
2018-04-14 01:05:46 +08:00
|
|
|
|
// 会话处理
|
2018-04-16 14:33:25 +08:00
|
|
|
|
request.Cookie = GetCookie(request)
|
|
|
|
|
request.Session = GetSession(request)
|
|
|
|
|
request.Response.request = request
|
2018-04-14 01:05:46 +08:00
|
|
|
|
return request
|
2018-04-11 16:06:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// 初始化GET请求参数
|
|
|
|
|
func (r *Request) initGet() {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if !r.parsedGet.Val() {
|
|
|
|
|
if len(r.values) == 0 {
|
|
|
|
|
r.values = r.URL.Query()
|
|
|
|
|
} else {
|
|
|
|
|
for k, v := range r.URL.Query() {
|
|
|
|
|
r.values[k] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
2018-01-11 16:06:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化POST请求参数
|
|
|
|
|
func (r *Request) initPost() {
|
2018-04-09 18:00:39 +08:00
|
|
|
|
if !r.parsedPost.Val() {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// 快速保存,尽量避免并发问题
|
2018-04-09 18:00:39 +08:00
|
|
|
|
r.parsedPost.Set(true)
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// MultiMedia表单请求解析允许最大使用内存:1GB
|
|
|
|
|
r.ParseMultipartForm(1024*1024*1024)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-14 12:34:11 +08:00
|
|
|
|
// 获得指定名称的参数字符串(GET/POST),同 GetRequestString
|
|
|
|
|
// 这是常用方法的简化别名
|
|
|
|
|
func (r *Request) Get(k string) string {
|
|
|
|
|
return r.GetRequestString(k)
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 16:06:42 +08:00
|
|
|
|
// 获得指定名称的get参数列表
|
|
|
|
|
func (r *Request) GetQuery(k string) []string {
|
|
|
|
|
r.initGet()
|
2018-04-11 16:06:45 +08:00
|
|
|
|
if v, ok := r.values[k]; ok {
|
2017-11-23 10:21:28 +08:00
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryBool(k string) bool {
|
2018-04-14 12:34:11 +08:00
|
|
|
|
return gconv.Bool(r.Get(k))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryInt(k string) int {
|
2018-04-14 12:34:11 +08:00
|
|
|
|
return gconv.Int(r.Get(k))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryUint(k string) uint {
|
2018-04-14 12:34:11 +08:00
|
|
|
|
return gconv.Uint(r.Get(k))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryFloat32(k string) float32 {
|
2018-04-14 12:34:11 +08:00
|
|
|
|
return gconv.Float32(r.Get(k))
|
2017-12-31 11:09:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetQueryFloat64(k string) float64 {
|
2018-04-14 12:34:11 +08:00
|
|
|
|
return gconv.Float64(r.Get(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 {
|
2018-04-11 16:06:45 +08:00
|
|
|
|
for k, v := range r.values {
|
2018-01-11 16:06:42 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 16:50:24 +08:00
|
|
|
|
// 退出当前请求执行,原理是在Request.exit做标记,由服务逻辑流程做判断,自行停止
|
|
|
|
|
func (r *Request) Exit() {
|
|
|
|
|
r.exit.Set(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断当前请求是否停止执行
|
2018-04-16 16:50:26 +08:00
|
|
|
|
func (r *Request) IsExited() bool {
|
2018-04-16 16:50:24 +08:00
|
|
|
|
return r.exit.Val()
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 17:11:34 +08:00
|
|
|
|
// 获取请求的服务端IP/域名
|
|
|
|
|
func (r *Request) GetHost() string {
|
|
|
|
|
array, _ := gregx.MatchString(`(.+):(\d+)`, r.Host)
|
|
|
|
|
if len(array) > 1 {
|
|
|
|
|
return array[1]
|
|
|
|
|
}
|
|
|
|
|
return r.Host
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取请求的客户端IP地址
|
2018-04-20 15:43:02 +08:00
|
|
|
|
func (r *Request) GetClientIp() string {
|
|
|
|
|
array, _ := gregx.MatchString(`(.+):(\d+)`, r.RemoteAddr)
|
|
|
|
|
if len(array) > 1 {
|
|
|
|
|
return array[1]
|
|
|
|
|
}
|
|
|
|
|
return r.RemoteAddr
|
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|