mirror of
https://github.com/WJQSERVER-STUDIO/ghproxy.git
synced 2026-02-03 08:11:11 +08:00
update matcher for gist usercontent
This commit is contained in:
parent
bbb108689a
commit
8ab622d149
2 changed files with 34 additions and 8 deletions
|
|
@ -10,14 +10,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
githubPrefix = "https://github.com/"
|
githubPrefix = "https://github.com/"
|
||||||
rawPrefix = "https://raw.githubusercontent.com/"
|
rawPrefix = "https://raw.githubusercontent.com/"
|
||||||
gistPrefix = "https://gist.github.com/"
|
gistPrefix = "https://gist.github.com/"
|
||||||
apiPrefix = "https://api.github.com/"
|
gistContentPrefix = "https://gist.githubusercontent.com/"
|
||||||
githubPrefixLen int
|
apiPrefix = "https://api.github.com/"
|
||||||
rawPrefixLen int
|
githubPrefixLen int
|
||||||
gistPrefixLen int
|
rawPrefixLen int
|
||||||
apiPrefixLen int
|
gistPrefixLen int
|
||||||
|
gistContentPrefixLen int
|
||||||
|
apiPrefixLen int
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|
@ -25,6 +27,7 @@ func init() {
|
||||||
rawPrefixLen = len(rawPrefix)
|
rawPrefixLen = len(rawPrefix)
|
||||||
gistPrefixLen = len(gistPrefix)
|
gistPrefixLen = len(gistPrefix)
|
||||||
apiPrefixLen = len(apiPrefix)
|
apiPrefixLen = len(apiPrefix)
|
||||||
|
gistContentPrefixLen = len(gistContentPrefix)
|
||||||
//log.Printf("githubPrefixLen: %d, rawPrefixLen: %d, gistPrefixLen: %d, apiPrefixLen: %d", githubPrefixLen, rawPrefixLen, gistPrefixLen, apiPrefixLen)
|
//log.Printf("githubPrefixLen: %d, rawPrefixLen: %d, gistPrefixLen: %d, apiPrefixLen: %d", githubPrefixLen, rawPrefixLen, gistPrefixLen, apiPrefixLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,6 +117,23 @@ func Matcher(rawPath string, cfg *config.Config) (string, string, string, *GHPro
|
||||||
return user, "", "gist", nil
|
return user, "", "gist", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 匹配 "https://gist.githubusercontent.com/"
|
||||||
|
if strings.HasPrefix(rawPath, gistContentPrefix) {
|
||||||
|
remaining := rawPath[gistContentPrefixLen:]
|
||||||
|
i := strings.IndexByte(remaining, '/')
|
||||||
|
if i <= 0 {
|
||||||
|
// case: https://gist.githubusercontent.com/user
|
||||||
|
// 这种情况下, gist_id 缺失, 但我们仍然可以认为 user 是有效的
|
||||||
|
if len(remaining) > 0 {
|
||||||
|
return remaining, "", "gist", nil
|
||||||
|
}
|
||||||
|
return "", "", "", NewErrorWithStatusLookup(400, "malformed gist url: missing user")
|
||||||
|
}
|
||||||
|
// case: https://gist.githubusercontent.com/user/gist_id...
|
||||||
|
user := remaining[:i]
|
||||||
|
return user, "", "gist", nil
|
||||||
|
}
|
||||||
|
|
||||||
// 匹配 "https://api.github.com/"
|
// 匹配 "https://api.github.com/"
|
||||||
if strings.HasPrefix(rawPath, apiPrefix) {
|
if strings.HasPrefix(rawPath, apiPrefix) {
|
||||||
if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) {
|
if !cfg.Auth.ForceAllowApi && (cfg.Auth.Method != "header" || !cfg.Auth.Enabled) {
|
||||||
|
|
|
||||||
|
|
@ -87,6 +87,12 @@ func TestMatcher_Compatibility(t *testing.T) {
|
||||||
config: cfgWithAuth,
|
config: cfgWithAuth,
|
||||||
expectedUser: "user", expectedRepo: "", expectedMatcher: "gist",
|
expectedUser: "user", expectedRepo: "", expectedMatcher: "gist",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Gist UserContent Path",
|
||||||
|
rawPath: "https://gist.githubusercontent.com/user/abcdef1234567890",
|
||||||
|
config: cfgWithAuth,
|
||||||
|
expectedUser: "user", expectedRepo: "", expectedMatcher: "gist",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "API Repos Path (with Auth)",
|
name: "API Repos Path (with Auth)",
|
||||||
rawPath: "https://api.github.com/repos/owner/repo/pulls",
|
rawPath: "https://api.github.com/repos/owner/repo/pulls",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue