修复重定向问题

This commit is contained in:
ExplodingDragon
2025-11-25 20:36:36 +08:00
parent 60e3c8e90c
commit c195f18229
3 changed files with 25 additions and 20 deletions

View File

@@ -32,12 +32,9 @@ func FilterInstDirect(_ core.Params) (core.FilterInstance, error) {
http.Error(writer, "Method not allowed", http.StatusMethodNotAllowed)
return nil
}
var resp *http.Response
var path string
defaultPath := param.Prefix + strings.TrimSuffix(ctx.Path, "/")
for _, p := range []string{defaultPath, defaultPath + "/index.html"} {
zap.L().Debug("direct fetch", zap.String("path", p))
resp, err = ctx.NativeOpen(request.Context(), p, nil)
path := param.Prefix + strings.TrimSuffix(ctx.Path, "/")
zap.L().Debug("direct fetch", zap.String("path", path))
resp, err := ctx.NativeOpen(request.Context(), path, nil)
if err != nil {
if resp != nil {
resp.Body.Close()
@@ -46,10 +43,15 @@ func FilterInstDirect(_ core.Params) (core.FilterInstance, error) {
zap.L().Debug("error", zap.Any("error", err))
return err
}
continue
exists, e := ctx.Exists(ctx, path+"/index.html")
if e != nil {
return err
}
path = p
break
if exists {
http.Redirect(writer, request, strings.TrimSuffix(request.URL.Path, "/")+"/", http.StatusFound)
return nil
}
return err
}
if resp == nil {
return os.ErrNotExist

View File

@@ -23,9 +23,6 @@ func FilterInstTemplate(_ core.Params) (core.FilterInstance, error) {
if err != nil {
return err
}
if err != nil {
return err
}
out := &bytes.Buffer{}
parse, err := utils.NewTemplate().Funcs(map[string]any{
"load": func(path string) (any, error) {

View File

@@ -94,6 +94,12 @@ func Test_fail_back(t *testing.T) {
server.AddFile("org1/org1.example.com/gh-pages/index.html", "hello world 1")
server.AddFile("org1/org1.example.com/gh-pages/child/index.html", "hello world 2")
server.AddFile("org1/child/gh-pages/no.html", "hello world 3")
_, resp, err := server.OpenFile("https://org1.example.com/child")
assert.NoError(t, err)
assert.Equal(t, 302, resp.StatusCode)
assert.Equal(t, "/child/", resp.Header.Get("Location"))
data, _, err := server.OpenFile("https://org1.example.com/child/")
assert.NoError(t, err)
assert.Equal(t, "hello world 2", string(data))