修复在 proxy 时重定向的问题

This commit is contained in:
ExplodingDragon
2025-05-15 20:25:12 +08:00
parent 70706fe969
commit b8cf863f18
5 changed files with 52 additions and 7 deletions

View File

@@ -77,9 +77,6 @@ func (p *PageDomain) ReturnMeta(owner string, repo string, branch string, path [
rel.Owner = owner
rel.Repo = repo
rel.Path = strings.Join(path, "/")
if strings.HasSuffix(rel.Path, "/") || rel.Path == "" {
rel.Path = rel.Path + "index.html"
}
if err = p.alias.Bind(meta.Alias, rel.Owner, rel.Repo, branch); err != nil {
zap.L().Warn("别名绑定失败", zap.Error(err))
return nil, err

View File

@@ -144,6 +144,11 @@ func (s *Server) Serve(writer http.ResponseWriter, request *http.Request) error
}
}
}
// 在非反向代理时处理目录访问
if strings.HasSuffix(meta.Path, "/") || meta.Path == "" {
meta.Path = meta.Path + "index.html"
}
// 如果不是反向代理路由则跳过任何配置
if request.Method != "GET" {
return os.ErrNotExist

View File

@@ -23,6 +23,7 @@ func NewServer() *VServer {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
zap.L().Debug("ServeHTTP", zap.String("url", r.URL.String()))
http.Error(w, "", http.StatusNotFound)
})
go func() {
_ = http.Serve(listener, mux)

View File

@@ -33,7 +33,7 @@ alias:
data, resp, err = server.OpenFile("https://org1.example.com/repo1/")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://www.example.org/index.html")
assert.Equal(t, resp.Header.Get("Location"), "https://www.example.org/")
data, resp, err = server.OpenFile("https://www.example.org")
assert.NoError(t, err)
assert.Equal(t, "hello world", string(data))
@@ -44,14 +44,18 @@ alias:
`)
data, resp, err = server.OpenFile("https://www.example.org")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://zzz.example.top/index.html")
assert.Equal(t, resp.Header.Get("Location"), "https://zzz.example.top/")
data, resp, err = server.OpenFile("https://www.example.org")
assert.Equal(t, resp.StatusCode, 404)
data, resp, err = server.OpenFile("https://org1.example.com/repo1/")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://zzz.example.top/index.html")
assert.Equal(t, resp.Header.Get("Location"), "https://zzz.example.top/")
data, resp, err = server.OpenFile("https://org1.example.com/repo1/get/some")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://zzz.example.top/get/some")
}
func Test_fail_back(t *testing.T) {

View File

@@ -12,13 +12,15 @@ func Test_proxy(t *testing.T) {
hs := core.NewServer()
defer server.Close()
defer hs.Close()
hs.Add("/test/data", "hello data")
hs.Add("/test/", "hello proxy")
server.AddFile("org1/repo1/gh-pages/index.html", "hello world")
server.AddFile("org1/repo1/gh-pages/.pages.yaml", `
proxy:
/api: %s/test
`, hs.URL)
/abi: %s/
`, hs.URL, hs.URL)
data, _, err := server.OpenFile("https://org1.example.com/repo1/")
assert.NoError(t, err)
assert.Equal(t, "hello world", string(data))
@@ -26,4 +28,40 @@ proxy:
data, _, err = server.OpenFile("https://org1.example.com/repo1/api")
assert.NoError(t, err)
assert.Equal(t, "hello proxy", string(data))
data, _, err = server.OpenFile("https://org1.example.com/repo1/api/data")
assert.NoError(t, err)
assert.Equal(t, "hello data", string(data))
_, resp, err := server.OpenFile("https://org1.example.com/repo1/abi/data")
assert.Equal(t, resp.StatusCode, 404)
}
func Test_cname_proxy(t *testing.T) {
server := core.NewDefaultTestServer()
hs := core.NewServer()
defer server.Close()
defer hs.Close()
hs.Add("/test/", "hello proxy")
server.AddFile("org1/repo1/gh-pages/index.html", "hello world")
server.AddFile("org1/repo1/gh-pages/.pages.yaml", `
alias:
- www.example.org
proxy:
/api: %s/test
`, hs.URL)
_, resp, err := server.OpenFile("https://org1.example.com/repo1/")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://www.example.org/")
data, resp, err := server.OpenFile("https://www.example.org")
assert.NoError(t, err)
assert.Equal(t, "hello world", string(data))
_, resp, err = server.OpenFile("https://org1.example.com/repo1/api")
assert.Equal(t, resp.StatusCode, 302)
assert.Equal(t, resp.Header.Get("Location"), "https://www.example.org/api")
data, resp, err = server.OpenFile("https://www.example.org/api")
assert.NoError(t, err)
assert.Equal(t, "hello proxy", string(data))
}