Fix:middleware matching error 修复中间件匹配错误#77
Open
AimTao wants to merge 1 commit intogeektutu:masterfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
gee-web/day5-middleware
背景
比如,我有三个路由地址:
其中,/user 是一个 RouterGroup,上面配置了一个中间件 logger。
问题
当我访问 /username 时,它并不属于 /user group,但也会匹配到这个中间件。
原因
ServeHTTP 中的这个判断,会判断 /username 包含 /user。
修正
不直接使用 group.prefix,而使用 group.prefix + "/" 进行匹配。
因为我们路径是按照 "/" 来分隔的,这样处理不会先出现误匹配的情况。
PS:看了一下 gin 的实现,对于中间件的匹配,是在 addRoute 时处理的,并将中间件保存在 Router 的 map 中。
这样既可以避免查找,也有利于提升处理请求的性能,大家可以参考。