2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-03 15:22:31 +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.
|
2018-08-03 15:22:31 +08:00
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2019-08-03 17:14:54 +08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-06-19 09:06:52 +08:00
|
|
|
"strings"
|
2019-06-25 23:03:29 +08:00
|
|
|
|
2019-08-03 17:14:54 +08:00
|
|
|
"github.com/gogf/gf/container/glist"
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/text/gregex"
|
2018-08-03 15:22:31 +08:00
|
|
|
)
|
|
|
|
|
2020-03-05 16:08:55 +08:00
|
|
|
// handlerCacheItem is an item just for internal router searching cache.
|
2019-08-03 17:14:54 +08:00
|
|
|
type handlerCacheItem struct {
|
|
|
|
parsedItems []*handlerParsedItem
|
|
|
|
hasHook bool
|
2019-09-03 17:18:16 +08:00
|
|
|
hasServe bool
|
2019-08-03 17:14:54 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 16:08:55 +08:00
|
|
|
// serveHandlerKey creates and returns a handler key for router.
|
|
|
|
func (s *Server) serveHandlerKey(method, path, domain string) string {
|
|
|
|
if len(domain) > 0 {
|
|
|
|
domain = "@" + domain
|
|
|
|
}
|
|
|
|
if method == "" {
|
|
|
|
return path + strings.ToLower(domain)
|
|
|
|
}
|
|
|
|
return strings.ToUpper(method) + ":" + path + strings.ToLower(domain)
|
|
|
|
}
|
|
|
|
|
2020-03-04 17:29:23 +08:00
|
|
|
// getHandlersWithCache searches the router item with cache feature for given request.
|
2019-09-03 17:18:16 +08:00
|
|
|
func (s *Server) getHandlersWithCache(r *Request) (parsedItems []*handlerParsedItem, hasHook, hasServe bool) {
|
2020-03-04 22:52:56 +08:00
|
|
|
method := r.Method
|
|
|
|
// Special http method OPTIONS handling.
|
|
|
|
// It searches the handler with the request method instead of OPTIONS method.
|
|
|
|
if method == "OPTIONS" {
|
|
|
|
if v := r.Request.Header.Get("Access-Control-Request-Method"); v != "" {
|
|
|
|
method = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Search and cache the router handlers.
|
|
|
|
value := s.serveCache.GetOrSetFunc(s.serveHandlerKey(method, r.URL.Path, r.GetHost()), func() interface{} {
|
|
|
|
parsedItems, hasHook, hasServe = s.searchHandlers(method, r.URL.Path, r.GetHost())
|
2019-08-03 17:14:54 +08:00
|
|
|
if parsedItems != nil {
|
2019-09-03 17:18:16 +08:00
|
|
|
return &handlerCacheItem{parsedItems, hasHook, hasServe}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-09-03 17:18:16 +08:00
|
|
|
return nil
|
2019-12-01 14:24:56 +08:00
|
|
|
}, gROUTE_CACHE_DURATION)
|
2019-09-03 17:18:16 +08:00
|
|
|
if value != nil {
|
|
|
|
item := value.(*handlerCacheItem)
|
|
|
|
return item.parsedItems, item.hasHook, item.hasServe
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2019-08-03 17:14:54 +08:00
|
|
|
return
|
2018-08-03 15:22:31 +08:00
|
|
|
}
|
|
|
|
|
2020-03-04 17:29:23 +08:00
|
|
|
// searchHandlers retrieves and returns the routers with given parameters.
|
|
|
|
// Note that the returned routers contain serving handler, middleware handlers and hook handlers.
|
2019-09-03 17:18:16 +08:00
|
|
|
func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*handlerParsedItem, hasHook, hasServe bool) {
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(path) == 0 {
|
2019-09-03 17:18:16 +08:00
|
|
|
return nil, false, false
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-04 17:29:23 +08:00
|
|
|
// Split the URL.path to separate parts.
|
|
|
|
var array []string
|
2019-06-19 09:06:52 +08:00
|
|
|
if strings.EqualFold("/", path) {
|
|
|
|
array = []string{"/"}
|
|
|
|
} else {
|
|
|
|
array = strings.Split(path[1:], "/")
|
|
|
|
}
|
2019-08-03 18:08:10 +08:00
|
|
|
parsedItemList := glist.New()
|
2019-09-29 14:27:09 +08:00
|
|
|
lastMiddlewareElem := (*glist.Element)(nil)
|
2020-03-04 22:52:56 +08:00
|
|
|
repeatHandlerCheckMap := make(map[int]struct{}, 16)
|
|
|
|
// Default domain has the most priority when iteration.
|
|
|
|
for _, domain := range []string{gDEFAULT_DOMAIN, domain} {
|
2019-06-19 09:06:52 +08:00
|
|
|
p, ok := s.serveTree[domain]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
2020-03-04 22:52:56 +08:00
|
|
|
// Make a list array with capacity of 16.
|
2019-08-03 17:14:54 +08:00
|
|
|
lists := make([]*glist.List, 0, 16)
|
2020-03-04 17:29:23 +08:00
|
|
|
for i, part := range array {
|
2019-12-10 21:14:15 +08:00
|
|
|
// In case of double '/' URI, eg: /user//index
|
2020-03-04 17:29:23 +08:00
|
|
|
if part == "" {
|
2019-12-10 21:14:15 +08:00
|
|
|
continue
|
|
|
|
}
|
2020-03-05 16:08:55 +08:00
|
|
|
// Add all list of each node to the list array.
|
2020-03-04 17:29:23 +08:00
|
|
|
if v, ok := p.(map[string]interface{})["*list"]; ok {
|
|
|
|
lists = append(lists, v.(*glist.List))
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-04 22:52:56 +08:00
|
|
|
if v, ok := p.(map[string]interface{})[part]; ok {
|
2020-03-05 16:08:55 +08:00
|
|
|
// Loop to the next node by certain key name.
|
2020-03-04 22:52:56 +08:00
|
|
|
p = v
|
2020-03-04 17:29:23 +08:00
|
|
|
if i == len(array)-1 {
|
|
|
|
if v, ok := p.(map[string]interface{})["*list"]; ok {
|
|
|
|
lists = append(lists, v.(*glist.List))
|
2019-06-19 09:06:52 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 16:08:55 +08:00
|
|
|
} else if v, ok := p.(map[string]interface{})["*fuzz"]; ok {
|
|
|
|
// Loop to the next node by fuzzy node item.
|
|
|
|
p = v
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-04 17:29:23 +08:00
|
|
|
if i == len(array)-1 {
|
2020-03-05 16:08:55 +08:00
|
|
|
// It here also checks the fuzzy item,
|
|
|
|
// for rule case like: "/user/*action" matches to "/user".
|
2020-03-04 17:29:23 +08:00
|
|
|
if v, ok := p.(map[string]interface{})["*fuzz"]; ok {
|
|
|
|
p = v
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2020-03-05 16:08:55 +08:00
|
|
|
// The leaf must have a list item. It adds the list to the list array.
|
2020-03-04 17:29:23 +08:00
|
|
|
if v, ok := p.(map[string]interface{})["*list"]; ok {
|
|
|
|
lists = append(lists, v.(*glist.List))
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-03 15:22:31 +08:00
|
|
|
|
2020-03-05 16:08:55 +08:00
|
|
|
// OK, let's loop the result list array, adding the handler item to the result handler result array.
|
|
|
|
// As the tail of the list array has the most priority, it iterates the list array from its tail to head.
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := len(lists) - 1; i >= 0; i-- {
|
|
|
|
for e := lists[i].Front(); e != nil; e = e.Next() {
|
|
|
|
item := e.Value.(*handlerItem)
|
2020-03-05 16:08:55 +08:00
|
|
|
// Filter repeated handler item, especially the middleware and hook handlers.
|
|
|
|
// It is necessary, do not remove this checks logic unless you really know how it is necessary.
|
2020-03-04 22:52:56 +08:00
|
|
|
if _, ok := repeatHandlerCheckMap[item.itemId]; ok {
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
repeatHandlerCheckMap[item.itemId] = struct{}{}
|
|
|
|
}
|
2020-03-05 16:08:55 +08:00
|
|
|
// Serving handler can only be added to the handler array just once.
|
2019-09-03 17:18:16 +08:00
|
|
|
if hasServe {
|
2019-08-03 15:54:12 +08:00
|
|
|
switch item.itemType {
|
2019-12-03 17:16:52 +08:00
|
|
|
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
2019-08-03 15:54:12 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-03-04 17:29:23 +08:00
|
|
|
if item.router.Method == gDEFAULT_METHOD || item.router.Method == method {
|
2020-03-05 16:08:55 +08:00
|
|
|
// Note the rule having no fuzzy rules: len(match) == 1
|
2019-06-19 09:06:52 +08:00
|
|
|
if match, err := gregex.MatchString(item.router.RegRule, path); err == nil && len(match) > 0 {
|
|
|
|
parsedItem := &handlerParsedItem{item, nil}
|
2020-03-05 16:08:55 +08:00
|
|
|
// If the rule contains fuzzy names,
|
|
|
|
// it needs paring the URL to retrieve the values for the names.
|
2019-06-19 09:06:52 +08:00
|
|
|
if len(item.router.RegNames) > 0 {
|
|
|
|
if len(match) > len(item.router.RegNames) {
|
2019-09-18 23:20:45 +08:00
|
|
|
parsedItem.values = make(map[string]string)
|
2020-03-05 16:08:55 +08:00
|
|
|
// It there repeated names, it just overwrites the same one.
|
2019-06-19 09:06:52 +08:00
|
|
|
for i, name := range item.router.RegNames {
|
2019-09-18 23:20:45 +08:00
|
|
|
parsedItem.values[name] = match[i+1]
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 18:08:10 +08:00
|
|
|
switch item.itemType {
|
2020-03-05 16:08:55 +08:00
|
|
|
// The serving handler can be only added just once.
|
2019-08-03 18:08:10 +08:00
|
|
|
case gHANDLER_TYPE_HANDLER, gHANDLER_TYPE_OBJECT, gHANDLER_TYPE_CONTROLLER:
|
2019-09-03 17:18:16 +08:00
|
|
|
hasServe = true
|
2019-08-03 18:08:10 +08:00
|
|
|
parsedItemList.PushBack(parsedItem)
|
|
|
|
|
2020-03-05 16:08:55 +08:00
|
|
|
// The middleware is inserted before the serving handler.
|
|
|
|
// If there're multiple middlewares, they're inserted into the result list by their registering order.
|
|
|
|
// The middlewares are also executed by their registering order.
|
2019-08-03 18:08:10 +08:00
|
|
|
case gHANDLER_TYPE_MIDDLEWARE:
|
2019-09-29 14:27:09 +08:00
|
|
|
if lastMiddlewareElem == nil {
|
|
|
|
lastMiddlewareElem = parsedItemList.PushFront(parsedItem)
|
2019-08-03 18:08:10 +08:00
|
|
|
} else {
|
2019-10-01 16:35:44 +08:00
|
|
|
lastMiddlewareElem = parsedItemList.InsertAfter(lastMiddlewareElem, parsedItem)
|
2019-08-03 15:54:12 +08:00
|
|
|
}
|
2019-08-03 18:08:10 +08:00
|
|
|
|
2020-03-05 16:08:55 +08:00
|
|
|
// HOOK handler, just push it back to the list.
|
2019-08-03 18:08:10 +08:00
|
|
|
case gHANDLER_TYPE_HOOK:
|
2019-08-03 17:14:54 +08:00
|
|
|
hasHook = true
|
2019-08-03 18:08:10 +08:00
|
|
|
parsedItemList.PushBack(parsedItem)
|
|
|
|
|
|
|
|
default:
|
2019-09-29 14:27:09 +08:00
|
|
|
panic(fmt.Sprintf(`invalid handler type %d`, item.itemType))
|
2019-08-03 17:14:54 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 18:08:10 +08:00
|
|
|
if parsedItemList.Len() > 0 {
|
|
|
|
index := 0
|
|
|
|
parsedItems = make([]*handlerParsedItem, parsedItemList.Len())
|
|
|
|
for e := parsedItemList.Front(); e != nil; e = e.Next() {
|
|
|
|
parsedItems[index] = e.Value.(*handlerParsedItem)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
2019-08-03 17:14:54 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
|
|
|
func (item *handlerItem) MarshalJSON() ([]byte, error) {
|
2019-08-03 23:57:20 +08:00
|
|
|
switch item.itemType {
|
|
|
|
case gHANDLER_TYPE_HOOK:
|
2019-08-03 17:14:54 +08:00
|
|
|
return json.Marshal(
|
|
|
|
fmt.Sprintf(
|
|
|
|
`%s %s:%s (%s)`,
|
|
|
|
item.router.Uri,
|
|
|
|
item.router.Domain,
|
|
|
|
item.router.Method,
|
|
|
|
item.hookName,
|
|
|
|
),
|
|
|
|
)
|
2019-08-03 23:57:20 +08:00
|
|
|
case gHANDLER_TYPE_MIDDLEWARE:
|
|
|
|
return json.Marshal(
|
|
|
|
fmt.Sprintf(
|
|
|
|
`%s %s:%s (MIDDLEWARE)`,
|
|
|
|
item.router.Uri,
|
|
|
|
item.router.Domain,
|
|
|
|
item.router.Method,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return json.Marshal(
|
|
|
|
fmt.Sprintf(
|
|
|
|
`%s %s:%s`,
|
|
|
|
item.router.Uri,
|
|
|
|
item.router.Domain,
|
|
|
|
item.router.Method,
|
|
|
|
),
|
|
|
|
)
|
2019-08-03 17:14:54 +08:00
|
|
|
}
|
2019-08-03 23:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
|
|
|
func (item *handlerParsedItem) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(item.handler)
|
2018-08-03 15:22:31 +08:00
|
|
|
}
|