9 Commits
v2.2 ... v2.4

Author SHA1 Message Date
Moritz Marquardt
67a190f68a Hotfix for #27: avoid slash before .html in GitHub compatibility redirects 2021-12-05 22:12:48 +01:00
Moritz Marquardt
2e970dbcda Merge pull request 'Fix github-style non-.html URLs repeating the path twice' (#23) from bugfix/github-style-nohtml-paths into main
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/23
2021-12-05 13:56:01 +01:00
Moritz Marquardt
51c79f512d Fix github-style non-.html URLs repeating the path twice
Issue was reported in https://codeberg.org/Codeberg/Community/issues/547#issuecomment-285075
2021-12-04 13:54:18 +01:00
Moritz Marquardt
38938e884d Merge pull request 'Add redirect for GitHub-style non-".html" paths & force remove index.html suffix' (#13) from feature/github-style-nohtml-paths into main
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/13
2021-12-02 20:35:43 +01:00
Moritz Marquardt
57dce3b0c5 Add redirect for GitHub-style non-".html" paths & force remove index.html suffix
See https://codeberg.org/Codeberg/Community/issues/547 for more info
2021-12-02 20:35:43 +01:00
Moritz Marquardt
026a04e57e Merge pull request 'Change browser cache to 10 minutes to make bigger pages more performant' (#14) from feature/browser-side-caching into main
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/14
2021-12-02 20:35:33 +01:00
Moritz Marquardt
b6d7f5a6ee Change browser cache to 10 minutes to make bigger pages more performant 2021-12-02 20:35:33 +01:00
Moritz Marquardt
726d8321e8 Merge pull request 'Fix (half) empty cache issue' (#17) from bugfix/large-files-are-empty into main
Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/17
2021-12-02 20:35:22 +01:00
Moritz Marquardt
989d00832f Fix (half) empty cache issue 2021-12-02 19:11:13 +01:00

View File

@@ -25,8 +25,8 @@ func handler(ctx *fasthttp.RequestCtx) {
// Force new default from specification (since November 2020) - see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy#strict-origin-when-cross-origin // Force new default from specification (since November 2020) - see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy#strict-origin-when-cross-origin
ctx.Response.Header.Set("Referrer-Policy", "strict-origin-when-cross-origin") ctx.Response.Header.Set("Referrer-Policy", "strict-origin-when-cross-origin")
// Enable caching, but require revalidation to reduce confusion // Enable browser caching for up to 10 minutes
ctx.Response.Header.Set("Cache-Control", "must-revalidate") ctx.Response.Header.Set("Cache-Control", "public, max-age=600")
trimmedHost := TrimHostPort(ctx.Request.Host()) trimmedHost := TrimHostPort(ctx.Request.Host())
@@ -418,7 +418,7 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
var res *fasthttp.Response var res *fasthttp.Response
var cachedResponse fileResponse var cachedResponse fileResponse
var err error var err error
if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + strconv.FormatInt(options.BranchTimestamp.Unix(), 10)); ok { if cachedValue, ok := fileResponseCache.Get(uri + "?timestamp=" + strconv.FormatInt(options.BranchTimestamp.Unix(), 10)); ok && len(cachedValue.(fileResponse).body) > 0 {
cachedResponse = cachedValue.(fileResponse) cachedResponse = cachedValue.(fileResponse)
} else { } else {
req = fasthttp.AcquireRequest() req = fasthttp.AcquireRequest()
@@ -444,6 +444,15 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
return true return true
} }
} }
// compatibility fix for GitHub Pages (/example → /example.html)
optionsForIndexPages.AppendTrailingSlash = false
optionsForIndexPages.RedirectIfExists = strings.TrimSuffix(string(ctx.Request.URI().Path()), "/") + ".html"
if upstream(ctx, targetOwner, targetRepo, targetBranch, targetPath + ".html", &optionsForIndexPages) {
_ = fileResponseCache.Set(uri+"?timestamp="+strconv.FormatInt(options.BranchTimestamp.Unix(), 10), fileResponse{
exists: false,
}, FileCacheTimeout)
return true
}
} }
ctx.Response.SetStatusCode(fasthttp.StatusNotFound) ctx.Response.SetStatusCode(fasthttp.StatusNotFound)
if res != nil { if res != nil {
@@ -460,12 +469,20 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
return true return true
} }
// Append trailing slash if missing (for index files) // Append trailing slash if missing (for index files), and redirect to fix filenames in general
// options.AppendTrailingSlash is only true when looking for index pages // options.AppendTrailingSlash is only true when looking for index pages
if options.AppendTrailingSlash && !bytes.HasSuffix(ctx.Request.URI().Path(), []byte{'/'}) { if options.AppendTrailingSlash && !bytes.HasSuffix(ctx.Request.URI().Path(), []byte{'/'}) {
ctx.Redirect(string(ctx.Request.URI().Path())+"/", fasthttp.StatusTemporaryRedirect) ctx.Redirect(string(ctx.Request.URI().Path())+"/", fasthttp.StatusTemporaryRedirect)
return true return true
} }
if bytes.HasSuffix(ctx.Request.URI().Path(), []byte("/index.html")) {
ctx.Redirect(strings.TrimSuffix(string(ctx.Request.URI().Path()), "index.html"), fasthttp.StatusTemporaryRedirect)
return true
}
if options.RedirectIfExists != "" {
ctx.Redirect(options.RedirectIfExists, fasthttp.StatusTemporaryRedirect)
return true
}
s.Step("error handling") s.Step("error handling")
// Set the MIME type // Set the MIME type
@@ -504,7 +521,7 @@ func upstream(ctx *fasthttp.RequestCtx, targetOwner string, targetRepo string, t
} }
s.Step("response") s.Step("response")
if res != nil { if res != nil && ctx.Err() == nil {
cachedResponse.exists = true cachedResponse.exists = true
cachedResponse.mimeType = mimeType cachedResponse.mimeType = mimeType
cachedResponse.body = cacheBodyWriter.Bytes() cachedResponse.body = cacheBodyWriter.Bytes()
@@ -520,5 +537,6 @@ type upstreamOptions struct {
ForbiddenMimeTypes map[string]struct{} ForbiddenMimeTypes map[string]struct{}
TryIndexPages bool TryIndexPages bool
AppendTrailingSlash bool AppendTrailingSlash bool
RedirectIfExists string
BranchTimestamp time.Time BranchTimestamp time.Time
} }