Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
38938e884d | ||
|
57dce3b0c5 | ||
|
026a04e57e | ||
|
b6d7f5a6ee | ||
|
726d8321e8 | ||
|
989d00832f |
28
handler.go
28
handler.go
@@ -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 = targetPath + ".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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user