diff --git a/server/upstream/upstream.go b/server/upstream/upstream.go index cacb636..d9c131e 100644 --- a/server/upstream/upstream.go +++ b/server/upstream/upstream.go @@ -46,8 +46,8 @@ type Options struct { TryIndexPages bool BranchTimestamp time.Time // internal - dontAppendTrailingSlash bool - redirectIfExists string + appendTrailingSlash bool + redirectIfExists string ServeRaw bool } @@ -95,19 +95,6 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi log.Trace().Msg("check response against last modified: outdated") } - if strings.HasSuffix(ctx.Path(), "/index.html") && !o.ServeRaw { - log.Trace().Msg("remove index.html from path and redirect") - ctx.Redirect(strings.TrimSuffix(ctx.Path(), "index.html"), http.StatusTemporaryRedirect) - return true - } - // Append trailing slash if missing (for index files), and redirect to fix filenames in general - // o.dontAppendTrailingSlash is only true when looking for index pages - if !o.dontAppendTrailingSlash && !strings.HasSuffix(ctx.Path(), "/") { - log.Trace().Msg("append trailing slash and redirect") - ctx.Redirect(ctx.Path()+"/", http.StatusTemporaryRedirect) - return true - } - log.Debug().Msg("Preparing") reader, header, statusCode, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) @@ -132,7 +119,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi // copy the o struct & try if an index page exists optionsForIndexPages := *o optionsForIndexPages.TryIndexPages = false - optionsForIndexPages.dontAppendTrailingSlash = false + optionsForIndexPages.appendTrailingSlash = true for _, indexPage := range upstreamIndexPages { optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { @@ -141,7 +128,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi } log.Trace().Msg("try html file with path name") // compatibility fix for GitHub Pages (/example → /example.html) - optionsForIndexPages.dontAppendTrailingSlash = true + optionsForIndexPages.appendTrailingSlash = false optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html" optionsForIndexPages.TargetPath = o.TargetPath + ".html" if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { @@ -157,7 +144,7 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi // copy the o struct & try if a not found page exists optionsForNotFoundPages := *o optionsForNotFoundPages.TryIndexPages = false - optionsForNotFoundPages.dontAppendTrailingSlash = true + optionsForNotFoundPages.appendTrailingSlash = false for _, notFoundPage := range upstreamNotFoundPages { optionsForNotFoundPages.TargetPath = "/" + notFoundPage if optionsForNotFoundPages.Upstream(ctx, giteaClient, redirectsCache) { @@ -193,6 +180,18 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi return true } + // Append trailing slash if missing (for index files), and redirect to fix filenames in general + // o.appendTrailingSlash is only true when looking for index pages + if o.appendTrailingSlash && !strings.HasSuffix(ctx.Path(), "/") { + log.Trace().Msg("append trailing slash and redirect") + ctx.Redirect(ctx.Path()+"/", http.StatusTemporaryRedirect) + return true + } + if strings.HasSuffix(ctx.Path(), "/index.html") && !o.ServeRaw { + log.Trace().Msg("remove index.html from path and redirect") + ctx.Redirect(strings.TrimSuffix(ctx.Path(), "index.html"), http.StatusTemporaryRedirect) + return true + } if o.redirectIfExists != "" { ctx.Redirect(o.redirectIfExists, http.StatusTemporaryRedirect) return true