Go?Gin框架路由相關(guān)bug分析
引言
注:本文原文有錯誤,原文不改動,但在結(jié)尾進行了勘誤,注意讀到文章結(jié)尾。
Gin相關(guān)版本v1.9.1
當你按如下方法注冊兩個路由的時候,bug會發(fā)生。
r := gin.Default() r.GET("/static/", func(c *gin.Context) { c.String(200, "static") }) r.GET("/static/*file", func(c *gin.Context) { c.String(200, "static file") }) r.Run()
上面的代碼會報錯:
panic: runtime error: index out of range [0] with length 0
分析
雖然構(gòu)建路由樹的時候,Gin本身就會主動產(chǎn)生很多panic,但上面這個panic顯然是個意外。
這個bug由catchAll通配符的特異性導(dǎo)致。
catchAll通配符雖然寫作*paramname
,但其構(gòu)建路由樹的時候會向前匹配一位/
。
因為catchAll通配符通常是為了匹配路徑而存在的,catchAll通配符在gin中的經(jīng)典應(yīng)用就是配置靜態(tài)文件服務(wù)器。
參考gin項目的routergroup.go
文件:
func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes { if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") { panic("URL parameters can not be used when serving a static folder") } handler := group.createStaticHandler(relativePath, fs) urlPattern := path.Join(relativePath, "/*filepath") // Register GET and HEAD handlers group.GET(urlPattern, handler) group.HEAD(urlPattern, handler) return group.returnObj() }
一旦你使用Static
相關(guān)函數(shù)配置靜態(tài)文件服務(wù),最后都會調(diào)用到上面的方法。
其中用你傳入的relativePath
和/*filepath
組合為最終的url:relativePath/*filepath
。
假如你傳入的路徑是/static
,則最終url為/static/*filepath
。
這個路由會匹配所有以/static/
開頭的url,并將后面的所有內(nèi)容賦值到filepath
。
沒錯,是所有,包括后面的/
,比如html/group1/page1.html
,也就是說可以通過filepath
訪問到子目錄。
但上面描述的內(nèi)容實際上有一個錯誤,你以為filepath
保存的內(nèi)容是html/group1/page1.html
。
實際上是/html/group1/page1.html
。
catchAll通配符會嘗試向前多匹配一個/
,如果你的路由中沒有這個/
,會報錯。
這個特性的特異之處導(dǎo)致gin中有一個bug,就是當你已經(jīng)注冊了/static/
路由之后,再注冊/static/*file
的時候,我們會在/static/
節(jié)點上插入*filepath
而不是/*filepath
,這導(dǎo)致在程序判斷這是一個catchAll路由后,會去向前匹配一位/
,這時i--
后,變成了負數(shù),就導(dǎo)致了index out of range
的錯誤。
你可能會覺得報錯是對的啊,那么你需要注意分清報錯和bug產(chǎn)生的panic。
i-- if path[i] != '/' { panic("no / before catch-all in path '" + fullPath + "'") }
我說的報錯產(chǎn)生在panic("no / before catch-all in path '" + fullPath + "'")
。
而bug產(chǎn)生的panic產(chǎn)生在i--
變?yōu)樨摂?shù)后查詢if path[i] != '/'
時。
簡單處理的話,這里應(yīng)該對i
的值進行判斷,然后主動panic拋出可以讓人領(lǐng)悟的報錯。
當然,為了解決這個問題本身,可以將上面的代碼修改為:
r := gin.Default() r.GET("/static", func(c *gin.Context) { c.String(200, "static") }) r.GET("/static/*file", func(c *gin.Context) { c.String(200, "static file") }) r.Run()
第一個路由不要加末尾的/
,就可以規(guī)避這個bug。
勘誤
前文提到panic: runtime error: index out of range [0] with length 0
報錯,但這顯然不是index為負的報錯,是我之前預(yù)判i--
為負時一廂情愿了。
實際上這個錯誤發(fā)生在i--
的上面幾行:
if len(n.path) > 0 && n.path[len(n.path)-1] == '/' { pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0] panic("catch-all wildcard '" + path + "' in new path '" + fullPath + "' conflicts with existing path segment '" + pathSeg + "' in existing prefix '" + n.path + pathSeg + "'") } // currently fixed width 1 for '/' i-- if path[i] != '/' { panic("no / before catch-all in path '" + fullPath + "'") }
這一行:pathSeg := strings.SplitN(n.children[0].path, "/", 2)[0]
。
這里的children
長度實際為0,但這里卻默認children
有內(nèi)容。
看panic報錯的內(nèi)容:catch-all wildcard "path" in new path "fullPath" conflicts with existing path segment "pathSeg" in existing prefix "n.path" + "pathSeg"
。
大意上還是catchAll通配符和當前路徑?jīng)_突,但這里Gin默認此時n.children
不為空的邏輯我還是沒太想明白。
以上就是Go Gin框架路由相關(guān)bug分析的詳細內(nèi)容,更多關(guān)于Go Gin框架路由bug的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解go程序如何在windows服務(wù)中開啟和關(guān)閉
這篇文章主要介紹了一個go程序,如何在windows服務(wù)中優(yōu)雅開啟和關(guān)閉,文中通過代碼示例和圖文講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07淺析Golang中調(diào)度器的關(guān)鍵機制與性能
Golang的調(diào)度器是其并發(fā)模型的核心組件,負責(zé)管理Goroutine的調(diào)度和執(zhí)行,本文將從理論和代碼層面分析Golang調(diào)度器的關(guān)鍵機制,感興趣的可以了解下2025-03-03go語言之給定英語文章統(tǒng)計單詞數(shù)量(go語言小練習(xí))
這篇文章給大家分享go語言小練習(xí)給定英語文章統(tǒng)計單詞數(shù)量,實現(xiàn)思路大概是利用go語言的map類型,以每個單詞作為關(guān)鍵字存儲數(shù)量信息,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2020-01-01