summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2020-11-17 23:44:52 +0100
committerGitHub <noreply@github.com>2020-11-17 17:44:52 -0500
commit0615b668dcbdeb8819662f2532cd5843f427dbcc (patch)
tree240a16a14d8ab503c83b75b25617c8275bc450d0 /modules
parent9ec5e6c40b0c0e4a8009acbdd5dfea8c0e60cfcd (diff)
downloadgitea-0615b668dcbdeb8819662f2532cd5843f427dbcc.tar.gz
gitea-0615b668dcbdeb8819662f2532cd5843f427dbcc.zip
HTTP cache rework and enable caching for storage assets (#13569)
This enabled HTTP time-based cache for storage assets, primarily avatars. I have not observed If-Modified-Since from browsers during tests but I guess it's good to support regardless. It introduces a new generic httpcache module that can handle both time-based and etag-based caching. Additionally, manifest.json and robots.txt are now also cachable.
Diffstat (limited to 'modules')
-rw-r--r--modules/httpcache/httpcache.go59
-rw-r--r--modules/public/public.go28
-rw-r--r--modules/setting/setting.go3
3 files changed, 67 insertions, 23 deletions
diff --git a/modules/httpcache/httpcache.go b/modules/httpcache/httpcache.go
new file mode 100644
index 0000000000..c4134f8e17
--- /dev/null
+++ b/modules/httpcache/httpcache.go
@@ -0,0 +1,59 @@
+// Copyright 2020 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package httpcache
+
+import (
+ "encoding/base64"
+ "fmt"
+ "net/http"
+ "os"
+ "strconv"
+ "time"
+
+ "code.gitea.io/gitea/modules/setting"
+)
+
+// GetCacheControl returns a suitable "Cache-Control" header value
+func GetCacheControl() string {
+ if setting.RunMode == "dev" {
+ return "no-store"
+ }
+ return "private, max-age=" + strconv.FormatInt(int64(setting.StaticCacheTime.Seconds()), 10)
+}
+
+// generateETag generates an ETag based on size, filename and file modification time
+func generateETag(fi os.FileInfo) string {
+ etag := fmt.Sprint(fi.Size()) + fi.Name() + fi.ModTime().UTC().Format(http.TimeFormat)
+ return base64.StdEncoding.EncodeToString([]byte(etag))
+}
+
+// HandleTimeCache handles time-based caching for a HTTP request
+func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
+ ifModifiedSince := req.Header.Get("If-Modified-Since")
+ if ifModifiedSince != "" {
+ t, err := time.Parse(http.TimeFormat, ifModifiedSince)
+ if err == nil && fi.ModTime().Unix() <= t.Unix() {
+ w.WriteHeader(http.StatusNotModified)
+ return true
+ }
+ }
+
+ w.Header().Set("Cache-Control", GetCacheControl())
+ w.Header().Set("Last-Modified", fi.ModTime().Format(http.TimeFormat))
+ return false
+}
+
+// HandleEtagCache handles ETag-based caching for a HTTP request
+func HandleEtagCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
+ etag := generateETag(fi)
+ if req.Header.Get("If-None-Match") == etag {
+ w.WriteHeader(http.StatusNotModified)
+ return true
+ }
+
+ w.Header().Set("Cache-Control", GetCacheControl())
+ w.Header().Set("ETag", etag)
+ return false
+}
diff --git a/modules/public/public.go b/modules/public/public.go
index 3a2fa4c57c..fc933637d8 100644
--- a/modules/public/public.go
+++ b/modules/public/public.go
@@ -5,15 +5,13 @@
package public
import (
- "encoding/base64"
- "fmt"
"log"
"net/http"
"path"
"path/filepath"
"strings"
- "time"
+ "code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/setting"
)
@@ -22,11 +20,8 @@ type Options struct {
Directory string
IndexFile string
SkipLogging bool
- // if set to true, will enable caching. Expires header will also be set to
- // expire after the defined time.
- ExpiresAfter time.Duration
- FileSystem http.FileSystem
- Prefix string
+ FileSystem http.FileSystem
+ Prefix string
}
// KnownPublicEntries list all direct children in the `public` directory
@@ -158,23 +153,10 @@ func (opts *Options) handle(w http.ResponseWriter, req *http.Request, opt *Optio
log.Println("[Static] Serving " + file)
}
- // Add an Expires header to the static content
- if opt.ExpiresAfter > 0 {
- w.Header().Set("Expires", time.Now().Add(opt.ExpiresAfter).UTC().Format(http.TimeFormat))
- tag := GenerateETag(fmt.Sprint(fi.Size()), fi.Name(), fi.ModTime().UTC().Format(http.TimeFormat))
- w.Header().Set("ETag", tag)
- if req.Header.Get("If-None-Match") == tag {
- w.WriteHeader(304)
- return true
- }
+ if httpcache.HandleEtagCache(req, w, fi) {
+ return true
}
http.ServeContent(w, req, file, fi.ModTime(), f)
return true
}
-
-// GenerateETag generates an ETag based on size, filename and file modification time
-func GenerateETag(fileSize, fileName, modTime string) string {
- etag := fileSize + fileName + modTime
- return base64.StdEncoding.EncodeToString([]byte(etag))
-}
diff --git a/modules/setting/setting.go b/modules/setting/setting.go
index 7ae8bb352d..708dc28233 100644
--- a/modules/setting/setting.go
+++ b/modules/setting/setting.go
@@ -67,6 +67,7 @@ var (
// AppVer settings
AppVer string
AppBuiltWith string
+ AppStartTime time.Time
AppName string
AppURL string
AppSubURL string
@@ -362,6 +363,7 @@ var (
PIDFile = "/run/gitea.pid"
WritePIDFile bool
ProdMode bool
+ RunMode string
RunUser string
IsWindows bool
HasRobotsTxt bool
@@ -837,6 +839,7 @@ func NewContext() {
}
RunUser = Cfg.Section("").Key("RUN_USER").MustString(user.CurrentUsername())
+ RunMode = Cfg.Section("").Key("RUN_MODE").MustString("dev")
// Does not check run user when the install lock is off.
if InstallLock {
currentUser, match := IsRunUserMatchCurrentUser(RunUser)