feat: Also send the content-length
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
PapaTutuWawa 2024-01-14 19:29:09 +01:00
parent 6028d3fe76
commit 0341ed8219

View File

@ -3,6 +3,7 @@ package pages
import ( import (
"mime" "mime"
"net/http" "net/http"
"strconv"
"strings" "strings"
"time" "time"
@ -27,10 +28,17 @@ func makePageContentCacheEntry(username, path string) string {
return username + ":" + path return username + ":" + path
} }
func addHeaders(csp, contentType string, w http.ResponseWriter) { func addHeaders(csp, contentType string, contentLength int, w http.ResponseWriter) {
w.Header().Set("Content-Type", contentType) // Always set a content type
if strings.Trim(contentType, " ") == "" {
w.Header().Set("Content-Type", "application/octet-stream")
} else {
w.Header().Set("Content-Type", contentType)
}
w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("Strict-Transport-Security", "max-age=31536000") w.Header().Set("Strict-Transport-Security", "max-age=31536000")
w.Header().Set("Content-Length", strconv.Itoa(contentLength))
if csp != "" { if csp != "" {
w.Header().Set("Content-Security-Policy", csp) w.Header().Set("Content-Security-Policy", csp)
@ -69,11 +77,11 @@ func ServeFile(username, reponame, path, defaultCsp string, giteaClient *repo.Gi
if err != nil { if err != nil {
if !found { if !found {
log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err) log.Errorf("Failed to get file %s/%s/%s (%s)", username, reponame, path, err)
addHeaders(csp, "text/html", w) addHeaders(csp, "text/html", 0, w)
w.WriteHeader(404) w.WriteHeader(404)
} else { } else {
log.Debugf("Request failed but page %s is cached in memory", path) log.Debugf("Request failed but page %s is cached in memory", path)
addHeaders(csp, mimeType, w) addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200) w.WriteHeader(200)
w.Write(content) w.Write(content)
} }
@ -83,7 +91,7 @@ func ServeFile(username, reponame, path, defaultCsp string, giteaClient *repo.Gi
if found && !changed { if found && !changed {
log.Debugf("Page %s is unchanged and cached in memory", path) log.Debugf("Page %s is unchanged and cached in memory", path)
addHeaders(csp, mimeType, w) addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200) w.WriteHeader(200)
w.Write(content) w.Write(content)
return return
@ -105,7 +113,7 @@ func ServeFile(username, reponame, path, defaultCsp string, giteaClient *repo.Gi
) )
log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now) log.Debugf("Page %s requested from Gitea and cached in memory at %v", path, now)
addHeaders(csp, mimeType, w) addHeaders(csp, mimeType, len(content), w)
w.WriteHeader(200) w.WriteHeader(200)
w.Write(content) w.Write(content)
} }