修复重定向问题
This commit is contained in:
@@ -32,12 +32,9 @@ func FilterInstDirect(_ core.Params) (core.FilterInstance, error) {
|
|||||||
http.Error(writer, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(writer, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var resp *http.Response
|
path := param.Prefix + strings.TrimSuffix(ctx.Path, "/")
|
||||||
var path string
|
zap.L().Debug("direct fetch", zap.String("path", path))
|
||||||
defaultPath := param.Prefix + strings.TrimSuffix(ctx.Path, "/")
|
resp, err := ctx.NativeOpen(request.Context(), path, nil)
|
||||||
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)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
@@ -46,10 +43,15 @@ func FilterInstDirect(_ core.Params) (core.FilterInstance, error) {
|
|||||||
zap.L().Debug("error", zap.Any("error", err))
|
zap.L().Debug("error", zap.Any("error", err))
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
continue
|
exists, e := ctx.Exists(ctx, path+"/index.html")
|
||||||
|
if e != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
path = p
|
if exists {
|
||||||
break
|
http.Redirect(writer, request, strings.TrimSuffix(request.URL.Path, "/")+"/", http.StatusFound)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if resp == nil {
|
if resp == nil {
|
||||||
return os.ErrNotExist
|
return os.ErrNotExist
|
||||||
|
|||||||
@@ -23,9 +23,6 @@ func FilterInstTemplate(_ core.Params) (core.FilterInstance, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
parse, err := utils.NewTemplate().Funcs(map[string]any{
|
parse, err := utils.NewTemplate().Funcs(map[string]any{
|
||||||
"load": func(path string) (any, error) {
|
"load": func(path string) (any, error) {
|
||||||
|
|||||||
@@ -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/index.html", "hello world 1")
|
||||||
server.AddFile("org1/org1.example.com/gh-pages/child/index.html", "hello world 2")
|
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")
|
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/")
|
data, _, err := server.OpenFile("https://org1.example.com/child/")
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "hello world 2", string(data))
|
assert.Equal(t, "hello world 2", string(data))
|
||||||
|
|||||||
Reference in New Issue
Block a user