ghttp_server_router.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package ghttp
  7. import (
  8. "fmt"
  9. "strings"
  10. "github.com/gogf/gf/container/gtype"
  11. "github.com/gogf/gf/errors/gcode"
  12. "github.com/gogf/gf/errors/gerror"
  13. "github.com/gogf/gf/debug/gdebug"
  14. "github.com/gogf/gf/container/glist"
  15. "github.com/gogf/gf/text/gregex"
  16. "github.com/gogf/gf/text/gstr"
  17. )
  18. const (
  19. stackFilterKey = "/net/ghttp/ghttp"
  20. )
  21. var (
  22. // handlerIdGenerator is handler item id generator.
  23. handlerIdGenerator = gtype.NewInt()
  24. )
  25. // routerMapKey creates and returns an unique router key for given parameters.
  26. // This key is used for Server.routerMap attribute, which is mainly for checks for
  27. // repeated router registering.
  28. func (s *Server) routerMapKey(hook, method, path, domain string) string {
  29. return hook + "%" + s.serveHandlerKey(method, path, domain)
  30. }
  31. // parsePattern parses the given pattern to domain, method and path variable.
  32. func (s *Server) parsePattern(pattern string) (domain, method, path string, err error) {
  33. path = strings.TrimSpace(pattern)
  34. domain = defaultDomainName
  35. method = defaultMethod
  36. if array, err := gregex.MatchString(`([a-zA-Z]+):(.+)`, pattern); len(array) > 1 && err == nil {
  37. path = strings.TrimSpace(array[2])
  38. if v := strings.TrimSpace(array[1]); v != "" {
  39. method = v
  40. }
  41. }
  42. if array, err := gregex.MatchString(`(.+)@([\w\.\-]+)`, path); len(array) > 1 && err == nil {
  43. path = strings.TrimSpace(array[1])
  44. if v := strings.TrimSpace(array[2]); v != "" {
  45. domain = v
  46. }
  47. }
  48. if path == "" {
  49. err = gerror.NewCode(gcode.CodeInvalidParameter, "invalid pattern: URI should not be empty")
  50. }
  51. if path != "/" {
  52. path = strings.TrimRight(path, "/")
  53. }
  54. return
  55. }
  56. // setHandler creates router item with given handler and pattern and registers the handler to the router tree.
  57. // The router tree can be treated as a multilayer hash table, please refer to the comment in following codes.
  58. // This function is called during server starts up, which cares little about the performance. What really cares
  59. // is the well designed router storage structure for router searching when the request is under serving.
  60. func (s *Server) setHandler(pattern string, handler *handlerItem) {
  61. handler.Id = handlerIdGenerator.Add(1)
  62. if handler.Source == "" {
  63. _, file, line := gdebug.CallerWithFilter([]string{stackFilterKey})
  64. handler.Source = fmt.Sprintf(`%s:%d`, file, line)
  65. }
  66. domain, method, uri, err := s.parsePattern(pattern)
  67. if err != nil {
  68. s.Logger().Fatal("invalid pattern:", pattern, err)
  69. return
  70. }
  71. if len(uri) == 0 || uri[0] != '/' {
  72. s.Logger().Fatal("invalid pattern:", pattern, "URI should lead with '/'")
  73. return
  74. }
  75. // Repeated router checks, this feature can be disabled by server configuration.
  76. routerKey := s.routerMapKey(handler.HookName, method, uri, domain)
  77. if !s.config.RouteOverWrite {
  78. switch handler.Type {
  79. case handlerTypeHandler, handlerTypeObject, handlerTypeController:
  80. if item, ok := s.routesMap[routerKey]; ok {
  81. s.Logger().Fatalf(
  82. `duplicated route registry "%s" at %s , already registered at %s`,
  83. pattern, handler.Source, item[0].Source,
  84. )
  85. return
  86. }
  87. }
  88. }
  89. // Create a new router by given parameter.
  90. handler.Router = &Router{
  91. Uri: uri,
  92. Domain: domain,
  93. Method: strings.ToUpper(method),
  94. Priority: strings.Count(uri[1:], "/"),
  95. }
  96. handler.Router.RegRule, handler.Router.RegNames = s.patternToRegular(uri)
  97. if _, ok := s.serveTree[domain]; !ok {
  98. s.serveTree[domain] = make(map[string]interface{})
  99. }
  100. // List array, very important for router registering.
  101. // There may be multiple lists adding into this array when searching from root to leaf.
  102. lists := make([]*glist.List, 0)
  103. array := ([]string)(nil)
  104. if strings.EqualFold("/", uri) {
  105. array = []string{"/"}
  106. } else {
  107. array = strings.Split(uri[1:], "/")
  108. }
  109. // Multilayer hash table:
  110. // 1. Each node of the table is separated by URI path which is split by char '/'.
  111. // 2. The key "*fuzz" specifies this node is a fuzzy node, which has no certain name.
  112. // 3. The key "*list" is the list item of the node, MOST OF THE NODES HAVE THIS ITEM,
  113. // especially the fuzzy node. NOTE THAT the fuzzy node must have the "*list" item,
  114. // and the leaf node also has "*list" item. If the node is not a fuzzy node either
  115. // a leaf, it neither has "*list" item.
  116. // 2. The "*list" item is a list containing registered router items ordered by their
  117. // priorities from high to low.
  118. // 3. There may be repeated router items in the router lists. The lists' priorities
  119. // from root to leaf are from low to high.
  120. p := s.serveTree[domain]
  121. for i, part := range array {
  122. // Ignore empty URI part, like: /user//index
  123. if part == "" {
  124. continue
  125. }
  126. // Check if it's a fuzzy node.
  127. if gregex.IsMatchString(`^[:\*]|\{[\w\.\-]+\}|\*`, part) {
  128. part = "*fuzz"
  129. // If it's a fuzzy node, it creates a "*list" item - which is a list - in the hash map.
  130. // All the sub router items from this fuzzy node will also be added to its "*list" item.
  131. if v, ok := p.(map[string]interface{})["*list"]; !ok {
  132. newListForFuzzy := glist.New()
  133. p.(map[string]interface{})["*list"] = newListForFuzzy
  134. lists = append(lists, newListForFuzzy)
  135. } else {
  136. lists = append(lists, v.(*glist.List))
  137. }
  138. }
  139. // Make a new bucket for current node.
  140. if _, ok := p.(map[string]interface{})[part]; !ok {
  141. p.(map[string]interface{})[part] = make(map[string]interface{})
  142. }
  143. // Loop to next bucket.
  144. p = p.(map[string]interface{})[part]
  145. // The leaf is a hash map and must have an item named "*list", which contains the router item.
  146. // The leaf can be furthermore extended by adding more ket-value pairs into its map.
  147. // Note that the `v != "*fuzz"` comparison is required as the list might be added in the former
  148. // fuzzy checks.
  149. if i == len(array)-1 && part != "*fuzz" {
  150. if v, ok := p.(map[string]interface{})["*list"]; !ok {
  151. leafList := glist.New()
  152. p.(map[string]interface{})["*list"] = leafList
  153. lists = append(lists, leafList)
  154. } else {
  155. lists = append(lists, v.(*glist.List))
  156. }
  157. }
  158. }
  159. // It iterates the list array of <lists>, compares priorities and inserts the new router item in
  160. // the proper position of each list. The priority of the list is ordered from high to low.
  161. item := (*handlerItem)(nil)
  162. for _, l := range lists {
  163. pushed := false
  164. for e := l.Front(); e != nil; e = e.Next() {
  165. item = e.Value.(*handlerItem)
  166. // Checks the priority whether inserting the route item before current item,
  167. // which means it has more higher priority.
  168. if s.compareRouterPriority(handler, item) {
  169. l.InsertBefore(e, handler)
  170. pushed = true
  171. goto end
  172. }
  173. }
  174. end:
  175. // Just push back in default.
  176. if !pushed {
  177. l.PushBack(handler)
  178. }
  179. }
  180. // Initialize the route map item.
  181. if _, ok := s.routesMap[routerKey]; !ok {
  182. s.routesMap[routerKey] = make([]registeredRouteItem, 0)
  183. }
  184. routeItem := registeredRouteItem{
  185. Source: handler.Source,
  186. Handler: handler,
  187. }
  188. switch handler.Type {
  189. case handlerTypeHandler, handlerTypeObject, handlerTypeController:
  190. // Overwrite the route.
  191. s.routesMap[routerKey] = []registeredRouteItem{routeItem}
  192. default:
  193. // Append the route.
  194. s.routesMap[routerKey] = append(s.routesMap[routerKey], routeItem)
  195. }
  196. }
  197. // compareRouterPriority compares the priority between <newItem> and <oldItem>. It returns true
  198. // if <newItem>'s priority is higher than <oldItem>, else it returns false. The higher priority
  199. // item will be insert into the router list before the other one.
  200. //
  201. // Comparison rules:
  202. // 1. The middleware has the most high priority.
  203. // 2. URI: The deeper the higher (simply check the count of char '/' in the URI).
  204. // 3. Route type: {xxx} > :xxx > *xxx.
  205. func (s *Server) compareRouterPriority(newItem *handlerItem, oldItem *handlerItem) bool {
  206. // If they're all type of middleware, the priority is according their registered sequence.
  207. if newItem.Type == handlerTypeMiddleware && oldItem.Type == handlerTypeMiddleware {
  208. return false
  209. }
  210. // The middleware has the most high priority.
  211. if newItem.Type == handlerTypeMiddleware && oldItem.Type != handlerTypeMiddleware {
  212. return true
  213. }
  214. // URI: The deeper the higher (simply check the count of char '/' in the URI).
  215. if newItem.Router.Priority > oldItem.Router.Priority {
  216. return true
  217. }
  218. if newItem.Router.Priority < oldItem.Router.Priority {
  219. return false
  220. }
  221. // Compare the length of their URI,
  222. // but the fuzzy and named parts of the URI are not calculated to the result.
  223. // Example:
  224. // /admin-goods-{page} > /admin-{page}
  225. // /{hash}.{type} > /{hash}
  226. var uriNew, uriOld string
  227. uriNew, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", newItem.Router.Uri)
  228. uriOld, _ = gregex.ReplaceString(`\{[^/]+?\}`, "", oldItem.Router.Uri)
  229. uriNew, _ = gregex.ReplaceString(`:[^/]+?`, "", uriNew)
  230. uriOld, _ = gregex.ReplaceString(`:[^/]+?`, "", uriOld)
  231. uriNew, _ = gregex.ReplaceString(`\*[^/]*`, "", uriNew) // Replace "/*" and "/*any".
  232. uriOld, _ = gregex.ReplaceString(`\*[^/]*`, "", uriOld) // Replace "/*" and "/*any".
  233. if len(uriNew) > len(uriOld) {
  234. return true
  235. }
  236. if len(uriNew) < len(uriOld) {
  237. return false
  238. }
  239. // Route type checks: {xxx} > :xxx > *xxx.
  240. // Example:
  241. // /name/act > /{name}/:act
  242. var (
  243. fuzzyCountFieldNew int
  244. fuzzyCountFieldOld int
  245. fuzzyCountNameNew int
  246. fuzzyCountNameOld int
  247. fuzzyCountAnyNew int
  248. fuzzyCountAnyOld int
  249. fuzzyCountTotalNew int
  250. fuzzyCountTotalOld int
  251. )
  252. for _, v := range newItem.Router.Uri {
  253. switch v {
  254. case '{':
  255. fuzzyCountFieldNew++
  256. case ':':
  257. fuzzyCountNameNew++
  258. case '*':
  259. fuzzyCountAnyNew++
  260. }
  261. }
  262. for _, v := range oldItem.Router.Uri {
  263. switch v {
  264. case '{':
  265. fuzzyCountFieldOld++
  266. case ':':
  267. fuzzyCountNameOld++
  268. case '*':
  269. fuzzyCountAnyOld++
  270. }
  271. }
  272. fuzzyCountTotalNew = fuzzyCountFieldNew + fuzzyCountNameNew + fuzzyCountAnyNew
  273. fuzzyCountTotalOld = fuzzyCountFieldOld + fuzzyCountNameOld + fuzzyCountAnyOld
  274. if fuzzyCountTotalNew < fuzzyCountTotalOld {
  275. return true
  276. }
  277. if fuzzyCountTotalNew > fuzzyCountTotalOld {
  278. return false
  279. }
  280. // If the counts of their fuzzy rules equal.
  281. // Eg: /name/{act} > /name/:act
  282. if fuzzyCountFieldNew > fuzzyCountFieldOld {
  283. return true
  284. }
  285. if fuzzyCountFieldNew < fuzzyCountFieldOld {
  286. return false
  287. }
  288. // Eg: /name/:act > /name/*act
  289. if fuzzyCountNameNew > fuzzyCountNameOld {
  290. return true
  291. }
  292. if fuzzyCountNameNew < fuzzyCountNameOld {
  293. return false
  294. }
  295. // It then compares the accuracy of their http method,
  296. // the more accurate the more priority.
  297. if newItem.Router.Method != defaultMethod {
  298. return true
  299. }
  300. if oldItem.Router.Method != defaultMethod {
  301. return true
  302. }
  303. // If they have different router type,
  304. // the new router item has more priority than the other one.
  305. if newItem.Type == handlerTypeHandler || newItem.Type == handlerTypeObject || newItem.Type == handlerTypeController {
  306. return true
  307. }
  308. // Other situations, like HOOK items,
  309. // the old router item has more priority than the other one.
  310. return false
  311. }
  312. // patternToRegular converts route rule to according regular expression.
  313. func (s *Server) patternToRegular(rule string) (regular string, names []string) {
  314. if len(rule) < 2 {
  315. return rule, nil
  316. }
  317. regular = "^"
  318. array := strings.Split(rule[1:], "/")
  319. for _, v := range array {
  320. if len(v) == 0 {
  321. continue
  322. }
  323. switch v[0] {
  324. case ':':
  325. if len(v) > 1 {
  326. regular += `/([^/]+)`
  327. names = append(names, v[1:])
  328. } else {
  329. regular += `/[^/]+`
  330. }
  331. case '*':
  332. if len(v) > 1 {
  333. regular += `/{0,1}(.*)`
  334. names = append(names, v[1:])
  335. } else {
  336. regular += `/{0,1}.*`
  337. }
  338. default:
  339. // Special chars replacement.
  340. v = gstr.ReplaceByMap(v, map[string]string{
  341. `.`: `\.`,
  342. `+`: `\+`,
  343. `*`: `.*`,
  344. })
  345. s, _ := gregex.ReplaceStringFunc(`\{[\w\.\-]+\}`, v, func(s string) string {
  346. names = append(names, s[1:len(s)-1])
  347. return `([^/]+)`
  348. })
  349. if strings.EqualFold(s, v) {
  350. regular += "/" + v
  351. } else {
  352. regular += "/" + s
  353. }
  354. }
  355. }
  356. regular += `$`
  357. return
  358. }