ghttp_server_router.go 12 KB

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