2020-11-17 15:44:52 -07:00
|
|
|
// 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"
|
2021-04-12 07:49:26 -07:00
|
|
|
"strings"
|
2020-11-17 15:44:52 -07:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 16:25:46 -07:00
|
|
|
// AddCacheControlToHeader adds suitable cache-control headers to response
|
2022-07-22 23:38:03 -07:00
|
|
|
func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
|
|
|
|
directives := make([]string, 0, 2+len(additionalDirectives))
|
|
|
|
|
2021-10-20 07:37:19 -07:00
|
|
|
if setting.IsProd {
|
2022-07-22 23:38:03 -07:00
|
|
|
if maxAge == 0 {
|
|
|
|
directives = append(directives, "no-store")
|
|
|
|
} else {
|
|
|
|
directives = append(directives, "private", "max-age="+strconv.Itoa(int(maxAge.Seconds())))
|
|
|
|
}
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 16:25:46 -07:00
|
|
|
} else {
|
2022-07-22 23:38:03 -07:00
|
|
|
directives = append(directives, "no-store")
|
|
|
|
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 16:25:46 -07:00
|
|
|
// to remind users they are using non-prod setting.
|
|
|
|
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
|
2020-11-17 15:44:52 -07:00
|
|
|
}
|
2022-07-22 23:38:03 -07:00
|
|
|
|
|
|
|
h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
|
2020-11-17 15:44:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2021-04-12 07:49:26 -07:00
|
|
|
return `"` + base64.StdEncoding.EncodeToString([]byte(etag)) + `"`
|
2020-11-17 15:44:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// HandleTimeCache handles time-based caching for a HTTP request
|
|
|
|
func HandleTimeCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
|
2022-05-09 08:54:51 -07:00
|
|
|
return HandleGenericTimeCache(req, w, fi.ModTime())
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleGenericTimeCache handles time-based caching for a HTTP request
|
|
|
|
func HandleGenericTimeCache(req *http.Request, w http.ResponseWriter, lastModified time.Time) (handled bool) {
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 16:25:46 -07:00
|
|
|
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
|
2021-04-12 07:49:26 -07:00
|
|
|
|
2020-11-17 15:44:52 -07:00
|
|
|
ifModifiedSince := req.Header.Get("If-Modified-Since")
|
|
|
|
if ifModifiedSince != "" {
|
|
|
|
t, err := time.Parse(http.TimeFormat, ifModifiedSince)
|
2022-05-09 08:54:51 -07:00
|
|
|
if err == nil && lastModified.Unix() <= t.Unix() {
|
2020-11-17 15:44:52 -07:00
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-09 08:54:51 -07:00
|
|
|
w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
|
2020-11-17 15:44:52 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-12 07:49:26 -07:00
|
|
|
// HandleFileETagCache handles ETag-based caching for a HTTP request
|
|
|
|
func HandleFileETagCache(req *http.Request, w http.ResponseWriter, fi os.FileInfo) (handled bool) {
|
2020-11-17 15:44:52 -07:00
|
|
|
etag := generateETag(fi)
|
2021-04-12 07:49:26 -07:00
|
|
|
return HandleGenericETagCache(req, w, etag)
|
|
|
|
}
|
2020-11-17 15:44:52 -07:00
|
|
|
|
2021-04-12 07:49:26 -07:00
|
|
|
// HandleGenericETagCache handles ETag-based caching for a HTTP request.
|
|
|
|
// It returns true if the request was handled.
|
|
|
|
func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag string) (handled bool) {
|
|
|
|
if len(etag) > 0 {
|
|
|
|
w.Header().Set("Etag", etag)
|
|
|
|
if checkIfNoneMatchIsValid(req, etag) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
Avatar refactor, move avatar code from `models` to `models.avatars`, remove duplicated code (#17123)
Why this refactor
The goal is to move most files from `models` package to `models.xxx` package. Many models depend on avatar model, so just move this first.
And the existing logic is not clear, there are too many function like `AvatarLink`, `RelAvatarLink`, `SizedRelAvatarLink`, `SizedAvatarLink`, `MakeFinalAvatarURL`, `HashedAvatarLink`, etc. This refactor make everything clear:
* user.AvatarLink()
* user.AvatarLinkWithSize(size)
* avatars.GenerateEmailAvatarFastLink(email, size)
* avatars.GenerateEmailAvatarFinalLink(email, size)
And many duplicated code are deleted in route handler, the handler and the model share the same avatar logic now.
2021-10-05 16:25:46 -07:00
|
|
|
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
|
2021-04-12 07:49:26 -07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkIfNoneMatchIsValid tests if the header If-None-Match matches the ETag
|
|
|
|
func checkIfNoneMatchIsValid(req *http.Request, etag string) bool {
|
|
|
|
ifNoneMatch := req.Header.Get("If-None-Match")
|
|
|
|
if len(ifNoneMatch) > 0 {
|
|
|
|
for _, item := range strings.Split(ifNoneMatch, ",") {
|
|
|
|
item = strings.TrimSpace(item)
|
|
|
|
if item == etag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-17 15:44:52 -07:00
|
|
|
return false
|
|
|
|
}
|
2022-05-09 08:54:51 -07:00
|
|
|
|
|
|
|
// HandleGenericETagTimeCache handles ETag-based caching with Last-Modified caching for a HTTP request.
|
|
|
|
// It returns true if the request was handled.
|
|
|
|
func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag string, lastModified time.Time) (handled bool) {
|
|
|
|
if len(etag) > 0 {
|
|
|
|
w.Header().Set("Etag", etag)
|
|
|
|
}
|
|
|
|
if !lastModified.IsZero() {
|
|
|
|
w.Header().Set("Last-Modified", lastModified.Format(http.TimeFormat))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(etag) > 0 {
|
|
|
|
if checkIfNoneMatchIsValid(req, etag) {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lastModified.IsZero() {
|
|
|
|
ifModifiedSince := req.Header.Get("If-Modified-Since")
|
|
|
|
if ifModifiedSince != "" {
|
|
|
|
t, err := time.Parse(http.TimeFormat, ifModifiedSince)
|
|
|
|
if err == nil && lastModified.Unix() <= t.Unix() {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AddCacheControlToHeader(w.Header(), setting.StaticCacheTime)
|
|
|
|
return false
|
|
|
|
}
|