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"
|
|
|
|
|
"gitee.com/johng/gf/g/encoding/gjson"
|
2018-04-09 18:00:39 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gtype"
|
2018-07-11 17:06:47 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gregex"
|
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参数是否已经解析
|
2018-07-29 22:01:29 +08:00
|
|
|
|
queryVars map[string][]string // GET参数
|
|
|
|
|
routerVars map[string][]string // 路由解析参数
|
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 // 请求完成时间(微秒)
|
2018-04-30 08:37:20 +08:00
|
|
|
|
Param interface{} // 开发者自定义参数
|
2018-05-28 17:02:53 +08:00
|
|
|
|
parsedHost *gtype.String // 解析过后不带端口号的服务器域名名称
|
|
|
|
|
clientIp *gtype.String // 解析过后的客户端IP地址
|
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(),
|
2018-07-29 22:01:29 +08:00
|
|
|
|
queryVars : make(map[string][]string),
|
|
|
|
|
routerVars : 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,
|
2018-04-30 22:14:14 +08:00
|
|
|
|
Response : newResponse(w),
|
2018-04-29 23:19:02 +08:00
|
|
|
|
EnterTime : gtime.Microsecond(),
|
2018-05-28 17:02:53 +08:00
|
|
|
|
parsedHost : gtype.NewString(),
|
|
|
|
|
clientIp : gtype.NewString(),
|
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-06-02 23:47:16 +08:00
|
|
|
|
// 获取Web Socket连接对象
|
|
|
|
|
func (r *Request) WebSocket() (*WebSocket, error) {
|
|
|
|
|
if conn, err := wsUpgrader.Upgrade(r.Response.ResponseWriter.ResponseWriter, &r.Request, nil); err == nil {
|
|
|
|
|
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
|
|
|
|
// 这是常用方法的简化别名
|
|
|
|
|
func (r *Request) Get(k string) string {
|
|
|
|
|
return r.GetRequestString(k)
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2018-05-28 17:02:53 +08:00
|
|
|
|
host := r.parsedHost.Val()
|
|
|
|
|
if len(host) == 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 {
|
|
|
|
|
host = array[1]
|
|
|
|
|
} else {
|
|
|
|
|
host = r.Host
|
|
|
|
|
}
|
|
|
|
|
r.parsedHost.Set(host)
|
2018-04-25 17:11:34 +08:00
|
|
|
|
}
|
2018-05-28 17:02:53 +08:00
|
|
|
|
return host
|
2018-04-25 17:11:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取请求的客户端IP地址
|
2018-04-20 15:43:02 +08:00
|
|
|
|
func (r *Request) GetClientIp() string {
|
2018-05-28 17:02:53 +08:00
|
|
|
|
ip := r.clientIp.Val()
|
|
|
|
|
if len(ip) == 0 {
|
2018-07-11 17:06:47 +08:00
|
|
|
|
array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr)
|
2018-05-28 17:02:53 +08:00
|
|
|
|
if len(array) > 1 {
|
|
|
|
|
ip = array[1]
|
|
|
|
|
} else {
|
|
|
|
|
ip = r.RemoteAddr
|
|
|
|
|
}
|
|
|
|
|
r.clientIp.Set(ip)
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
2018-05-28 17:02:53 +08:00
|
|
|
|
return ip
|
2018-04-20 15:43:02 +08:00
|
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
|