From 91cd34b26a60c0ea203af14d963659ec73b9abc4 Mon Sep 17 00:00:00 2001 From: john Date: Wed, 29 Jul 2020 00:32:54 +0800 Subject: [PATCH] improve multiple seperator chars handling for router of ghttp.Server --- net/ghttp/ghttp_server_router_serve.go | 29 +++++++++++++++++------ net/ghttp/ghttp_unit_router_basic_test.go | 3 +++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/net/ghttp/ghttp_server_router_serve.go b/net/ghttp/ghttp_server_router_serve.go index 720fe8cfe..135f55e43 100644 --- a/net/ghttp/ghttp_server_router_serve.go +++ b/net/ghttp/ghttp_server_router_serve.go @@ -64,6 +64,22 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han if len(path) == 0 { return nil, false, false } + // In case of double '/' URI, for example: + // /user//index, //user/index, //user//index// + var previousIsSep = false + for i := 0; i < len(path); { + if path[i] == '/' { + if previousIsSep { + path = path[:i] + path[i+1:] + continue + } else { + previousIsSep = true + } + } else { + previousIsSep = false + } + i++ + } // Split the URL.path to separate parts. var array []string if strings.EqualFold("/", path) { @@ -71,9 +87,12 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han } else { array = strings.Split(path[1:], "/") } - parsedItemList := glist.New() - lastMiddlewareElem := (*glist.Element)(nil) - repeatHandlerCheckMap := make(map[int]struct{}, 16) + var ( + lastMiddlewareElem *glist.Element + parsedItemList = glist.New() + repeatHandlerCheckMap = make(map[int]struct{}, 16) + ) + // Default domain has the most priority when iteration. for _, domain := range []string{gDEFAULT_DOMAIN, domain} { p, ok := s.serveTree[domain] @@ -83,10 +102,6 @@ func (s *Server) searchHandlers(method, path, domain string) (parsedItems []*han // Make a list array with capacity of 16. lists := make([]*glist.List, 0, 16) for i, part := range array { - // In case of double '/' URI, eg: /user//index - if part == "" { - continue - } // Add all list of each node to the list array. if v, ok := p.(map[string]interface{})["*list"]; ok { lists = append(lists, v.(*glist.List)) diff --git a/net/ghttp/ghttp_unit_router_basic_test.go b/net/ghttp/ghttp_unit_router_basic_test.go index 2b43dd775..48d5ed0de 100644 --- a/net/ghttp/ghttp_unit_router_basic_test.go +++ b/net/ghttp/ghttp_unit_router_basic_test.go @@ -137,6 +137,9 @@ func Test_Router_ExtraChar(t *testing.T) { t.Assert(client.GetContent("/api/test"), "test") t.Assert(client.GetContent("/api/test/"), "test") t.Assert(client.GetContent("/api/test//"), "test") + t.Assert(client.GetContent("//api/test//"), "test") + t.Assert(client.GetContent("//api//test//"), "test") + t.Assert(client.GetContent("///api///test///"), "test") }) }