From c94fca6dbb645ab7683a15c601cc4de9651809d8 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 30 Jul 2018 13:50:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90ghttp=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E8=B7=AF=E7=94=B1=E6=94=B9=E8=BF=9B=E5=B7=A5=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/ghttp_server_router.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/g/net/ghttp/ghttp_server_router.go b/g/net/ghttp/ghttp_server_router.go index 02a325a35..d37da35a1 100644 --- a/g/net/ghttp/ghttp_server_router.go +++ b/g/net/ghttp/ghttp_server_router.go @@ -112,7 +112,12 @@ func (s *Server) setHandler(pattern string, handler *handlerItem, hook ... strin p := s.handlerTree[domain] // 当前节点的规则链表 lists := make([]*list.List, 0) - array := strings.Split(uri[1:], "/") + array := ([]string)(nil) + if strings.EqualFold("/", uri) { + array = []string{"/"} + } else { + array = strings.Split(uri[1:], "/") + } // 键名"*fuzz"代表模糊匹配节点,其下会有一个链表; // 键名"*list"代表链表,叶子节点和模糊匹配节点都有该属性; for k, v := range array { @@ -259,7 +264,12 @@ func (s *Server) searchHandler(method, path, domain string) *handlerParsedItem { if !strings.EqualFold(gDEFAULT_DOMAIN, domain) { domains = append(domains, domain) } - array := strings.Split(path[1:], "/") + array := ([]string)(nil) + if strings.EqualFold("/", path) { + array = []string{"/"} + } else { + array = strings.Split(path[1:], "/") + } for _, domain := range domains { p, ok := s.handlerTree[domain] if !ok { @@ -276,6 +286,7 @@ func (s *Server) searchHandler(method, path, domain string) *handlerParsedItem { if k == len(array) - 1 { if _, ok := p.(map[string]interface{})["*list"]; ok { lists = append(lists, p.(map[string]interface{})["*list"].(*list.List)) + break } } } else { @@ -300,7 +311,8 @@ func (s *Server) searchHandler(method, path, domain string) *handlerParsedItem { item := e.Value.(*handlerRegisterItem) // 动态匹配规则带有gDEFAULT_METHOD的情况,不会像静态规则那样直接解析为所有的HTTP METHOD存储 if strings.EqualFold(item.router.Method, gDEFAULT_METHOD) || strings.EqualFold(item.router.Method, method) { - if match, err := gregex.MatchString(item.router.RegRule, path); err == nil && len(match) > 1 { + // 注意当不带任何动态路由规则时,len(match) == 1 + if match, err := gregex.MatchString(item.router.RegRule, path); err == nil && len(match) > 0 { //gutil.Dump(match) //gutil.Dump(names) handlerItem := &handlerParsedItem{item, nil}