2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-31 18:19:58 +08:00
|
|
|
|
|
2017-11-23 10:21:28 +08:00
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
|
|
import (
|
2019-05-06 13:52:34 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gogf/gf/g/container/gvar"
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/encoding/gjson"
|
|
|
|
|
"github.com/gogf/gf/g/os/gtime"
|
|
|
|
|
"github.com/gogf/gf/g/text/gregex"
|
|
|
|
|
"github.com/gogf/gf/third/github.com/fatih/structs"
|
2018-11-16 18:20:09 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
2018-08-12 10:50:03 +08:00
|
|
|
|
"strings"
|
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 {
|
2019-01-24 13:40:48 +08:00
|
|
|
|
*http.Request
|
2018-11-19 21:11:43 +08:00
|
|
|
|
parsedGet bool // GET参数是否已经解析
|
|
|
|
|
parsedPost bool // POST参数是否已经解析
|
|
|
|
|
queryVars map[string][]string // GET参数
|
|
|
|
|
routerVars map[string][]string // 路由解析参数
|
|
|
|
|
exit bool // 是否退出当前请求流程执行
|
|
|
|
|
Id int // 请求id(唯一)
|
|
|
|
|
Server *Server // 请求关联的服务器对象
|
|
|
|
|
Cookie *Cookie // 与当前请求绑定的Cookie对象(并发安全)
|
|
|
|
|
Session *Session // 与当前请求绑定的Session对象(并发安全)
|
|
|
|
|
Response *Response // 对应请求的返回数据操作对象
|
|
|
|
|
Router *Router // 匹配到的路由对象
|
|
|
|
|
EnterTime int64 // 请求进入时间(微秒)
|
|
|
|
|
LeaveTime int64 // 请求完成时间(微秒)
|
2018-11-23 09:20:45 +08:00
|
|
|
|
params map[string]interface{} // 开发者自定义参数(请求流程中有效)
|
2018-11-19 21:11:43 +08:00
|
|
|
|
parsedHost string // 解析过后不带端口号的服务器域名名称
|
|
|
|
|
clientIp string // 解析过后的客户端IP地址
|
2018-12-28 21:44:36 +08:00
|
|
|
|
rawContent []byte // 客户端提交的原始参数
|
2018-11-19 21:11:43 +08:00
|
|
|
|
isFileRequest bool // 是否为静态文件请求(非服务请求,当静态文件存在时,优先级会被服务请求高,被识别为文件请求)
|
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-11-16 18:20:09 +08:00
|
|
|
|
request := &Request {
|
2018-07-29 22:01:29 +08:00
|
|
|
|
routerVars : make(map[string][]string),
|
2018-04-11 16:06:45 +08:00
|
|
|
|
Id : s.servedCount.Add(1),
|
|
|
|
|
Server : s,
|
2019-01-24 13:40:48 +08:00
|
|
|
|
Request : r,
|
2018-08-12 12:22:02 +08:00
|
|
|
|
Response : newResponse(s, w),
|
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
|
|
|
|
}
|
|
|
|
|
|
2019-03-21 18:20:20 +08:00
|
|
|
|
// 获取Web Socket连接对象(如果是非WS请求会失败,注意检查返回的error结果)
|
2018-06-02 23:47:16 +08:00
|
|
|
|
func (r *Request) WebSocket() (*WebSocket, error) {
|
2019-01-24 13:40:48 +08:00
|
|
|
|
if conn, err := wsUpgrader.Upgrade(r.Response.ResponseWriter.ResponseWriter, r.Request, nil); err == nil {
|
2018-06-02 23:47:16 +08:00
|
|
|
|
return &WebSocket {
|
|
|
|
|
conn,
|
|
|
|
|
}, nil
|
|
|
|
|
} else {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 22:01:29 +08:00
|
|
|
|
// 获得指定名称的参数字符串(Router/GET/POST),同 GetRequestString
|
2018-04-14 12:34:11 +08:00
|
|
|
|
// 这是常用方法的简化别名
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) Get(key string, def...interface{}) string {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestString(key, def...)
|
2018-04-14 12:34:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:20:45 +08:00
|
|
|
|
// 建议都用该参数替代参数获取
|
2019-05-10 13:38:06 +08:00
|
|
|
|
func (r *Request) GetVar(key string, def...interface{}) *gvar.Var {
|
2018-10-09 13:33:00 +08:00
|
|
|
|
return r.GetRequestVar(key, def...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 21:17:56 +08:00
|
|
|
|
// 获取原始请求输入二进制。
|
2018-01-02 15:52:32 +08:00
|
|
|
|
func (r *Request) GetRaw() []byte {
|
2019-03-22 14:31:02 +08:00
|
|
|
|
err := error(nil)
|
2018-12-28 21:44:36 +08:00
|
|
|
|
if r.rawContent == nil {
|
2019-03-22 14:31:02 +08:00
|
|
|
|
r.rawContent, err = ioutil.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
r.Error("error reading request body: ", err)
|
|
|
|
|
}
|
2018-12-28 21:44:36 +08:00
|
|
|
|
}
|
|
|
|
|
return r.rawContent
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-27 21:17:56 +08:00
|
|
|
|
// 获取原始请求输入字符串。
|
|
|
|
|
func (r *Request) GetRawString() string {
|
2019-03-22 14:31:02 +08:00
|
|
|
|
return string(r.GetRaw())
|
2019-02-27 21:17:56 +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()
|
2019-03-22 14:31:02 +08:00
|
|
|
|
if len(data) > 0 {
|
2017-12-26 15:41:10 +08:00
|
|
|
|
if j, err := gjson.DecodeToJson(data); err == nil {
|
2017-12-14 17:32:51 +08:00
|
|
|
|
return j
|
2019-03-21 18:20:20 +08:00
|
|
|
|
} else {
|
2019-03-22 14:31:02 +08:00
|
|
|
|
r.Error(err, ": ", string(data))
|
2017-12-14 17:32:51 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetString(key string, def...interface{}) string {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestString(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetInt(key string, def...interface{}) int {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestInt(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetInts(key string, def...interface{}) []int {
|
2018-10-09 10:05:55 +08:00
|
|
|
|
return r.GetRequestInts(key, def...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetUint(key string, def...interface{}) uint {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestUint(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetFloat32(key string, def...interface{}) float32 {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestFloat32(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetFloat64(key string, def...interface{}) float64 {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestFloat64(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetFloats(key string, def...interface{}) []float64 {
|
2018-10-09 10:05:55 +08:00
|
|
|
|
return r.GetRequestFloats(key, def...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetArray(key string, def...interface{}) []string {
|
2018-08-19 22:45:23 +08:00
|
|
|
|
return r.GetRequestArray(key, def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetStrings(key string, def...interface{}) []string {
|
2018-10-09 10:05:55 +08:00
|
|
|
|
return r.GetRequestStrings(key, def...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) GetInterfaces(key string, def...interface{}) []interface{} {
|
2018-10-09 10:05:55 +08:00
|
|
|
|
return r.GetRequestInterfaces(key, def...)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-19 22:45:23 +08:00
|
|
|
|
func (r *Request) GetMap(def...map[string]string) map[string]string {
|
|
|
|
|
return r.GetRequestMap(def...)
|
2018-08-17 18:21:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 14:19:27 +08:00
|
|
|
|
// 将所有的request参数映射到struct属性上,参数pointer应当为一个struct对象的指针,
|
|
|
|
|
// mapping为非必需参数,自定义参数与属性的映射关系
|
|
|
|
|
func (r *Request) GetToStruct(pointer interface{}, mapping...map[string]string) {
|
|
|
|
|
r.GetRequestToStruct(pointer, mapping...)
|
2018-08-12 10:50:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 00:43:36 +08:00
|
|
|
|
// 仅退出当前逻辑执行函数, 如:服务函数、HOOK函数
|
2018-04-16 16:50:24 +08:00
|
|
|
|
func (r *Request) Exit() {
|
2019-01-13 00:43:36 +08:00
|
|
|
|
panic(gEXCEPTION_EXIT)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 退出当前请求执行,后续所有的服务逻辑流程(包括其他的HOOK)将不会执行
|
|
|
|
|
func (r *Request) ExitAll() {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
r.exit = true
|
2019-01-16 20:27:58 +08:00
|
|
|
|
panic(gEXCEPTION_EXIT_ALL)
|
2018-04-16 16:50:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-13 00:43:36 +08:00
|
|
|
|
// 仅针对HOOK执行,默认情况下HOOK会按照优先级进行调用,当使用ExitHook后当前类型的后续HOOK将不会被调用
|
|
|
|
|
func (r *Request) ExitHook() {
|
|
|
|
|
panic(gEXCEPTION_EXIT_HOOK)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 16:50:24 +08:00
|
|
|
|
// 判断当前请求是否停止执行
|
2018-04-16 16:50:26 +08:00
|
|
|
|
func (r *Request) IsExited() bool {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
return r.exit
|
2018-04-16 16:50:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 17:11:34 +08:00
|
|
|
|
// 获取请求的服务端IP/域名
|
|
|
|
|
func (r *Request) GetHost() string {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if len(r.parsedHost) == 0 {
|
2018-07-11 17:06:47 +08:00
|
|
|
|
array, _ := gregex.MatchString(`(.+):(\d+)`, r.Host)
|
2018-05-28 17:02:53 +08:00
|
|
|
|
if len(array) > 1 {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
r.parsedHost = array[1]
|
2018-05-28 17:02:53 +08:00
|
|
|
|
} else {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
r.parsedHost = r.Host
|
2018-05-28 17:02:53 +08:00
|
|
|
|
}
|
2018-04-25 17:11:34 +08:00
|
|
|
|
}
|
2018-11-16 18:20:09 +08:00
|
|
|
|
return r.parsedHost
|
2018-04-25 17:11:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-23 00:31:14 +08:00
|
|
|
|
// 判断是否为静态文件请求
|
|
|
|
|
func (r *Request) IsFileRequest() bool {
|
|
|
|
|
return r.isFileRequest
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 19:56:01 +08:00
|
|
|
|
// 判断是否为AJAX请求
|
2018-08-23 00:31:14 +08:00
|
|
|
|
func (r *Request) IsAjaxRequest() bool {
|
2018-08-22 19:56:01 +08:00
|
|
|
|
return strings.EqualFold(r.Header.Get("X-Requested-With"), "XMLHttpRequest")
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-25 17:11:34 +08:00
|
|
|
|
// 获取请求的客户端IP地址
|
2018-04-20 15:43:02 +08:00
|
|
|
|
func (r *Request) GetClientIp() string {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
if len(r.clientIp) == 0 {
|
|
|
|
|
if r.clientIp = r.Header.Get("X-Real-IP"); r.clientIp == "" {
|
2018-08-31 18:51:45 +08:00
|
|
|
|
array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr)
|
|
|
|
|
if len(array) > 1 {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
r.clientIp = array[1]
|
2018-08-31 18:51:45 +08:00
|
|
|
|
} else {
|
2018-11-16 18:20:09 +08:00
|
|
|
|
r.clientIp = r.RemoteAddr
|
2018-08-31 18:51:45 +08:00
|
|
|
|
}
|
2018-05-28 17:02:53 +08:00
|
|
|
|
}
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
2018-11-16 18:20:09 +08:00
|
|
|
|
return r.clientIp
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|
2019-05-06 13:52:34 +08:00
|
|
|
|
// 获得当前请求URL地址
|
|
|
|
|
func (r *Request) GetUrl() string {
|
|
|
|
|
scheme := "http"
|
|
|
|
|
if r.TLS != nil {
|
|
|
|
|
scheme = "https"
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf(`%s://%s%s`, scheme, r.Host, r.URL.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获得请求来源URL地址
|
2018-08-24 14:57:49 +08:00
|
|
|
|
func (r *Request) GetReferer() string {
|
|
|
|
|
return r.Header.Get("Referer")
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-23 09:20:45 +08:00
|
|
|
|
// 获得结构体对象的参数名称标签,构成map返回
|
2019-05-09 14:19:27 +08:00
|
|
|
|
func (r *Request) getStructParamsTagMap(pointer interface{}) map[string]string {
|
2019-03-21 18:20:20 +08:00
|
|
|
|
tagMap := make(map[string]string)
|
2019-05-09 14:19:27 +08:00
|
|
|
|
fields := structs.Fields(pointer)
|
2018-08-12 10:50:03 +08:00
|
|
|
|
for _, field := range fields {
|
|
|
|
|
if tag := field.Tag("params"); tag != "" {
|
|
|
|
|
for _, v := range strings.Split(tag, ",") {
|
2019-03-21 18:20:20 +08:00
|
|
|
|
tagMap[strings.TrimSpace(v)] = field.Name()
|
2018-08-12 10:50:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-21 18:20:20 +08:00
|
|
|
|
return tagMap
|
2018-08-12 10:50:03 +08:00
|
|
|
|
}
|