CHANGE redirect and traling slash appending back

This commit is contained in:
Hoernschen 2024-02-26 09:48:22 +01:00
parent 5a844330d9
commit aeaf084ea1
No known key found for this signature in database
GPG Key ID: 37591FAF4E6D3462

View File

@ -46,7 +46,7 @@ type Options struct {
TryIndexPages bool TryIndexPages bool
BranchTimestamp time.Time BranchTimestamp time.Time
// internal // internal
dontAppendTrailingSlash bool appendTrailingSlash bool
redirectIfExists string redirectIfExists string
ServeRaw bool 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") 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") log.Debug().Msg("Preparing")
reader, header, statusCode, err := giteaClient.ServeRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, o.TargetPath) 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 // copy the o struct & try if an index page exists
optionsForIndexPages := *o optionsForIndexPages := *o
optionsForIndexPages.TryIndexPages = false optionsForIndexPages.TryIndexPages = false
optionsForIndexPages.dontAppendTrailingSlash = false optionsForIndexPages.appendTrailingSlash = true
for _, indexPage := range upstreamIndexPages { for _, indexPage := range upstreamIndexPages {
optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage optionsForIndexPages.TargetPath = strings.TrimSuffix(o.TargetPath, "/") + "/" + indexPage
if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { 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") log.Trace().Msg("try html file with path name")
// compatibility fix for GitHub Pages (/example → /example.html) // compatibility fix for GitHub Pages (/example → /example.html)
optionsForIndexPages.dontAppendTrailingSlash = true optionsForIndexPages.appendTrailingSlash = false
optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html" optionsForIndexPages.redirectIfExists = strings.TrimSuffix(ctx.Path(), "/") + ".html"
optionsForIndexPages.TargetPath = o.TargetPath + ".html" optionsForIndexPages.TargetPath = o.TargetPath + ".html"
if optionsForIndexPages.Upstream(ctx, giteaClient, redirectsCache) { 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 // copy the o struct & try if a not found page exists
optionsForNotFoundPages := *o optionsForNotFoundPages := *o
optionsForNotFoundPages.TryIndexPages = false optionsForNotFoundPages.TryIndexPages = false
optionsForNotFoundPages.dontAppendTrailingSlash = true optionsForNotFoundPages.appendTrailingSlash = false
for _, notFoundPage := range upstreamNotFoundPages { for _, notFoundPage := range upstreamNotFoundPages {
optionsForNotFoundPages.TargetPath = "/" + notFoundPage optionsForNotFoundPages.TargetPath = "/" + notFoundPage
if optionsForNotFoundPages.Upstream(ctx, giteaClient, redirectsCache) { if optionsForNotFoundPages.Upstream(ctx, giteaClient, redirectsCache) {
@ -193,6 +180,18 @@ func (o *Options) Upstream(ctx *context.Context, giteaClient *gitea.Client, redi
return true 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 != "" { if o.redirectIfExists != "" {
ctx.Redirect(o.redirectIfExists, http.StatusTemporaryRedirect) ctx.Redirect(o.redirectIfExists, http.StatusTemporaryRedirect)
return true return true