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 (
|
2020-03-10 20:45:22 +08:00
|
|
|
"context"
|
2019-05-06 13:52:34 +08:00
|
|
|
"fmt"
|
2020-01-17 21:12:52 +08:00
|
|
|
"github.com/gogf/gf/os/gres"
|
2019-11-20 18:45:09 +08:00
|
|
|
"github.com/gogf/gf/os/gview"
|
2019-07-03 22:09:35 +08:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2019-09-11 21:19:45 +08:00
|
|
|
"github.com/gogf/gf/os/gsession"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
|
"github.com/gogf/gf/text/gregex"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// Request is the context object for a request.
|
2018-01-02 15:52:32 +08:00
|
|
|
type Request struct {
|
2019-06-19 09:06:52 +08:00
|
|
|
*http.Request
|
2020-03-14 17:12:44 +08:00
|
|
|
Server *Server // Server.
|
2019-11-20 18:45:09 +08:00
|
|
|
Cookie *Cookie // Cookie.
|
|
|
|
Session *gsession.Session // Session.
|
|
|
|
Response *Response // Corresponding Response of this request.
|
2020-03-14 17:12:44 +08:00
|
|
|
Router *Router // Matched Router for this request. Note that it's not available in HOOK handler.
|
2019-11-20 18:45:09 +08:00
|
|
|
EnterTime int64 // Request starting time in microseconds.
|
|
|
|
LeaveTime int64 // Request ending time in microseconds.
|
2020-03-14 17:12:44 +08:00
|
|
|
Middleware *Middleware // Middleware manager.
|
|
|
|
StaticFile *StaticFile // Static file object for static file serving.
|
|
|
|
Context context.Context // Custom context for internal usage purpose.
|
|
|
|
handlers []*handlerParsedItem // All matched handlers containing handler, hook and middleware for this request.
|
2019-11-20 18:45:09 +08:00
|
|
|
hasHookHandler bool // A bool marking whether there's hook handler in the handlers for performance purpose.
|
|
|
|
hasServeHandler bool // A bool marking whether there's serving handler in the handlers for performance purpose.
|
2019-11-29 22:02:19 +08:00
|
|
|
parsedQuery bool // A bool marking whether the GET parameters parsed.
|
|
|
|
parsedBody bool // A bool marking whether the request body parsed.
|
2019-11-28 11:18:09 +08:00
|
|
|
parsedForm bool // A bool marking whether request Form parsed for HTTP method PUT, POST, PATCH.
|
2020-03-14 17:12:44 +08:00
|
|
|
paramsMap map[string]interface{} // Custom parameters map.
|
2019-12-03 17:16:52 +08:00
|
|
|
routerMap map[string]string // Router parameters map, which might be nil if there're no router parameters.
|
2019-11-29 22:02:19 +08:00
|
|
|
queryMap map[string]interface{} // Query parameters map, which is nil if there's no query string.
|
2019-12-01 14:07:36 +08:00
|
|
|
formMap map[string]interface{} // Form parameters map, which is nil if there's no form data from client.
|
2019-11-29 22:02:19 +08:00
|
|
|
bodyMap map[string]interface{} // Body parameters map, which might be nil if there're no body content.
|
2019-11-20 18:45:09 +08:00
|
|
|
error error // Current executing error of the request.
|
|
|
|
exit bool // A bool marking whether current request is exited.
|
|
|
|
parsedHost string // The parsed host name for current host used by GetHost function.
|
|
|
|
clientIp string // The parsed client ip for current host used by GetClientIp function.
|
2019-11-30 09:42:07 +08:00
|
|
|
bodyContent []byte // Request body content.
|
2019-11-20 18:45:09 +08:00
|
|
|
isFileRequest bool // A bool marking whether current request is file serving.
|
2019-12-10 21:14:15 +08:00
|
|
|
viewObject *gview.View // Custom template view engine object for this response.
|
2019-11-20 18:45:09 +08:00
|
|
|
viewParams gview.Params // Custom template view variables for this response.
|
|
|
|
}
|
|
|
|
|
2020-01-17 21:12:52 +08:00
|
|
|
// StaticFile is the file struct for static file service.
|
|
|
|
type StaticFile struct {
|
|
|
|
File *gres.File // Resource file object.
|
|
|
|
Path string // File path.
|
|
|
|
IsDir bool // Is directory.
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// newRequest creates and returns a new request object.
|
2018-04-11 16:06:45 +08:00
|
|
|
func newRequest(s *Server, r *http.Request, w http.ResponseWriter) *Request {
|
2019-06-19 09:06:52 +08:00
|
|
|
request := &Request{
|
2019-09-18 23:20:45 +08:00
|
|
|
Server: s,
|
|
|
|
Request: r,
|
|
|
|
Response: newResponse(s, w),
|
2020-02-07 16:29:14 +08:00
|
|
|
EnterTime: gtime.TimestampMilli(),
|
2020-03-10 20:45:22 +08:00
|
|
|
Context: r.Context(),
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
request.Cookie = GetCookie(request)
|
2019-09-11 21:19:45 +08:00
|
|
|
request.Session = s.sessionManager.New(request.GetSessionId())
|
2019-09-24 20:19:18 +08:00
|
|
|
request.Response.Request = request
|
2019-08-06 20:40:04 +08:00
|
|
|
request.Middleware = &Middleware{
|
2019-08-03 15:54:12 +08:00
|
|
|
request: request,
|
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
return request
|
2018-04-11 16:06:45 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// WebSocket upgrades current request as a websocket request.
|
|
|
|
// It returns a new WebSocket object if success, or the error if failure.
|
|
|
|
// Note that the request should be a websocket request, or it will surely fail upgrading.
|
2018-06-02 23:47:16 +08:00
|
|
|
func (r *Request) WebSocket() (*WebSocket, error) {
|
2019-09-23 19:25:03 +08:00
|
|
|
if conn, err := wsUpgrader.Upgrade(r.Response.Writer, r.Request, nil); err == nil {
|
2019-06-19 09:06:52 +08:00
|
|
|
return &WebSocket{
|
|
|
|
conn,
|
|
|
|
}, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-02 23:47:16 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// Exit exits executing of current HTTP handler.
|
2018-04-16 16:50:24 +08:00
|
|
|
func (r *Request) Exit() {
|
2019-06-19 09:06:52 +08:00
|
|
|
panic(gEXCEPTION_EXIT)
|
2019-01-13 00:43:36 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// ExitAll exits executing of current and following HTTP handlers.
|
2019-01-13 00:43:36 +08:00
|
|
|
func (r *Request) ExitAll() {
|
2019-06-19 09:06:52 +08:00
|
|
|
r.exit = true
|
|
|
|
panic(gEXCEPTION_EXIT_ALL)
|
2018-04-16 16:50:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// ExitHook exits executing of current and following HTTP HOOK handlers.
|
2019-01-13 00:43:36 +08:00
|
|
|
func (r *Request) ExitHook() {
|
2019-06-19 09:06:52 +08:00
|
|
|
panic(gEXCEPTION_EXIT_HOOK)
|
2019-01-13 00:43:36 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// IsExited checks and returns whether current request is exited.
|
2018-04-16 16:50:26 +08:00
|
|
|
func (r *Request) IsExited() bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return r.exit
|
2018-04-16 16:50:24 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// GetHost returns current request host name, which might be a domain or an IP without port.
|
2018-04-25 17:11:34 +08:00
|
|
|
func (r *Request) GetHost() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(r.parsedHost) == 0 {
|
|
|
|
array, _ := gregex.MatchString(`(.+):(\d+)`, r.Host)
|
|
|
|
if len(array) > 1 {
|
|
|
|
r.parsedHost = array[1]
|
|
|
|
} else {
|
|
|
|
r.parsedHost = r.Host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.parsedHost
|
2018-04-25 17:11:34 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// IsFileRequest checks and returns whether current request is serving file.
|
2018-08-23 00:31:14 +08:00
|
|
|
func (r *Request) IsFileRequest() bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return r.isFileRequest
|
2018-08-23 00:31:14 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// IsAjaxRequest checks and returns whether current request is an AJAX request.
|
2018-08-23 00:31:14 +08:00
|
|
|
func (r *Request) IsAjaxRequest() bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return strings.EqualFold(r.Header.Get("X-Requested-With"), "XMLHttpRequest")
|
2018-08-22 19:56:01 +08:00
|
|
|
}
|
|
|
|
|
2019-12-04 10:03:03 +08:00
|
|
|
// GetClientIp returns the client ip of this request without port.
|
2018-04-20 15:43:02 +08:00
|
|
|
func (r *Request) GetClientIp() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(r.clientIp) == 0 {
|
|
|
|
if r.clientIp = r.Header.Get("X-Real-IP"); r.clientIp == "" {
|
|
|
|
array, _ := gregex.MatchString(`(.+):(\d+)`, r.RemoteAddr)
|
|
|
|
if len(array) > 1 {
|
|
|
|
r.clientIp = array[1]
|
|
|
|
} else {
|
|
|
|
r.clientIp = r.RemoteAddr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.clientIp
|
2018-04-20 15:43:02 +08:00
|
|
|
}
|
2017-11-23 10:21:28 +08:00
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// GetUrl returns current URL of this request.
|
2019-05-06 13:52:34 +08:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// GetSessionId retrieves and returns session id from cookie or header.
|
2019-08-10 16:39:46 +08:00
|
|
|
func (r *Request) GetSessionId() string {
|
|
|
|
id := r.Cookie.GetSessionId()
|
|
|
|
if id == "" {
|
|
|
|
id = r.Header.Get(r.Server.GetSessionIdName())
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2019-11-20 18:45:09 +08:00
|
|
|
// GetReferer returns referer of this request.
|
2018-08-24 14:57:49 +08:00
|
|
|
func (r *Request) GetReferer() string {
|
2019-06-19 09:06:52 +08:00
|
|
|
return r.Header.Get("Referer")
|
2018-08-24 14:57:49 +08:00
|
|
|
}
|
2019-12-04 19:50:49 +08:00
|
|
|
|
|
|
|
// GetError returns the error occurs in the procedure of the request.
|
|
|
|
// It returns nil if there's no error.
|
|
|
|
func (r *Request) GetError() error {
|
|
|
|
return r.error
|
|
|
|
}
|