2024-01-01 13:19:19 +00:00
|
|
|
package pages
|
2023-12-31 12:26:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
2024-01-14 18:29:09 +00:00
|
|
|
"strconv"
|
2023-12-31 12:26:56 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
"git.polynom.me/rio/internal/constants"
|
2024-02-03 11:05:10 +00:00
|
|
|
"git.polynom.me/rio/internal/context"
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2023-12-31 12:26:56 +00:00
|
|
|
"github.com/patrickmn/go-cache"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-31 23:38:39 +00:00
|
|
|
pageCache = cache.New(6*time.Hour, 1*time.Hour)
|
2023-12-31 12:26:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type PageContentCache struct {
|
2023-12-31 23:38:39 +00:00
|
|
|
Content []byte
|
|
|
|
mimeType string
|
2023-12-31 23:38:01 +00:00
|
|
|
RequestedAt time.Time
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func makePageContentCacheEntry(username, path string) string {
|
|
|
|
return username + ":" + path
|
|
|
|
}
|
|
|
|
|
2024-02-03 14:18:37 +00:00
|
|
|
func addHeaders(repoInfo *context.RepositoryInformation, contentType string, contentLength int, w http.ResponseWriter) {
|
2024-01-14 18:29:09 +00:00
|
|
|
// Always set a content type
|
|
|
|
if strings.Trim(contentType, " ") == "" {
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
}
|
|
|
|
|
2024-01-06 16:42:08 +00:00
|
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
w.Header().Set("Strict-Transport-Security", "max-age=31536000")
|
2024-01-14 18:29:09 +00:00
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(contentLength))
|
2024-01-06 16:42:08 +00:00
|
|
|
|
2024-02-03 14:18:37 +00:00
|
|
|
if repoInfo != nil {
|
|
|
|
for key, value := range repoInfo.Headers {
|
|
|
|
w.Header().Set(key, value)
|
|
|
|
}
|
2024-01-06 16:42:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:05:10 +00:00
|
|
|
func ServeFile(context *context.Context) {
|
2023-12-31 12:26:56 +00:00
|
|
|
// Strip away a starting / as it messes with Gitea
|
2024-02-03 11:05:10 +00:00
|
|
|
path := context.Path
|
2023-12-31 12:26:56 +00:00
|
|
|
if path[:1] == "/" {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
|
2024-02-03 11:05:10 +00:00
|
|
|
key := makePageContentCacheEntry(context.Username, path)
|
2023-12-31 12:26:56 +00:00
|
|
|
entry, found := pageCache.Get(key)
|
|
|
|
var content []byte
|
|
|
|
var mimeType string
|
2023-12-31 13:08:23 +00:00
|
|
|
var err error
|
2024-01-06 13:47:47 +00:00
|
|
|
var since *time.Time = nil
|
2023-12-31 12:26:56 +00:00
|
|
|
if found {
|
|
|
|
log.Debugf("Returning %s from cache", path)
|
|
|
|
content = entry.(PageContentCache).Content
|
|
|
|
mimeType = entry.(PageContentCache).mimeType
|
2024-01-06 13:47:47 +00:00
|
|
|
sinceRaw := entry.(PageContentCache).RequestedAt
|
|
|
|
since = &sinceRaw
|
2023-12-31 23:38:01 +00:00
|
|
|
}
|
|
|
|
|
2024-02-03 11:05:10 +00:00
|
|
|
content, changed, err := context.Global.Gitea.GetFile(
|
|
|
|
context.Username,
|
|
|
|
context.Reponame,
|
2024-01-06 13:47:47 +00:00
|
|
|
constants.PagesBranch,
|
2023-12-31 23:38:01 +00:00
|
|
|
path,
|
2024-01-06 13:47:47 +00:00
|
|
|
since,
|
2023-12-31 23:38:01 +00:00
|
|
|
)
|
2024-02-03 14:18:37 +00:00
|
|
|
repoInfo := context.Global.Cache.GetRepositoryInformation(
|
|
|
|
context.Username,
|
|
|
|
context.Reponame,
|
|
|
|
)
|
2024-01-06 13:47:47 +00:00
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
if err != nil {
|
|
|
|
if !found {
|
2024-02-03 11:05:10 +00:00
|
|
|
log.Errorf("Failed to get file %s/%s/%s (%s)", context.Username, context.Reponame, path, err)
|
2024-02-03 14:18:37 +00:00
|
|
|
addHeaders(repoInfo, "text/html", 0, context.Writer)
|
2024-02-03 11:05:10 +00:00
|
|
|
context.Writer.WriteHeader(404)
|
2023-12-31 23:38:01 +00:00
|
|
|
} else {
|
|
|
|
log.Debugf("Request failed but page %s is cached in memory", path)
|
2024-02-03 14:18:37 +00:00
|
|
|
addHeaders(repoInfo, mimeType, len(content), context.Writer)
|
2024-02-03 11:05:10 +00:00
|
|
|
context.Writer.WriteHeader(200)
|
|
|
|
context.Writer.Write(content)
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-31 12:26:56 +00:00
|
|
|
|
2024-01-06 13:47:47 +00:00
|
|
|
if found && !changed {
|
2023-12-31 23:38:01 +00:00
|
|
|
log.Debugf("Page %s is unchanged and cached in memory", path)
|
2024-02-03 14:18:37 +00:00
|
|
|
addHeaders(repoInfo, mimeType, len(content), context.Writer)
|
2024-02-03 11:05:10 +00:00
|
|
|
context.Writer.WriteHeader(200)
|
|
|
|
context.Writer.Write(content)
|
2023-12-31 23:38:01 +00:00
|
|
|
return
|
|
|
|
}
|
2023-12-31 13:08:23 +00:00
|
|
|
|
2023-12-31 23:38:01 +00:00
|
|
|
pathParts := strings.Split(path, ".")
|
2023-12-31 23:38:39 +00:00
|
|
|
ext := pathParts[len(pathParts)-1]
|
2023-12-31 23:38:01 +00:00
|
|
|
mimeType = mime.TypeByExtension("." + ext)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
pageCache.Set(
|
|
|
|
key,
|
|
|
|
PageContentCache{
|
|
|
|
content,
|
|
|
|
mimeType,
|
|
|
|
now,
|
|
|
|
},
|
|
|
|
cache.DefaultExpiration,
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now)
|
2024-02-03 14:18:37 +00:00
|
|
|
addHeaders(repoInfo, mimeType, len(content), context.Writer)
|
2024-02-03 11:05:10 +00:00
|
|
|
context.Writer.WriteHeader(200)
|
|
|
|
context.Writer.Write(content)
|
2024-02-02 20:07:29 +00:00
|
|
|
|
|
|
|
// Tell Loki about if, if desired
|
2024-02-03 11:05:10 +00:00
|
|
|
if context.Global.MetricConfig.ShouldSendMetrics(path, context.UserAgent) {
|
|
|
|
context.Global.MetricConfig.SendMetricPing(context.Domain, path, context.Referrer)
|
2024-02-02 20:07:29 +00:00
|
|
|
}
|
2023-12-31 12:26:56 +00:00
|
|
|
}
|