Files
Atay-Makhzan/modules/httpcache/httpcache.go
T

116 lines
3.7 KiB
Go
Raw Normal View History

// Copyright 2020 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package httpcache
import (
2025-03-13 07:04:50 +08:00
"fmt"
"net/http"
"strconv"
2021-04-12 16:49:26 +02:00
"strings"
"time"
"code.gitea.io/gitea/modules/setting"
2025-03-13 07:04:50 +08:00
"code.gitea.io/gitea/modules/util"
)
2025-03-13 07:04:50 +08:00
type CacheControlOptions struct {
IsPublic bool
MaxAge time.Duration
NoTransform bool
}
2023-03-08 22:40:04 +02:00
// SetCacheControlInHeader sets suitable cache-control headers in the response
2025-03-13 07:04:50 +08:00
func SetCacheControlInHeader(h http.Header, opts *CacheControlOptions) {
directives := make([]string, 0, 4)
2023-02-03 01:39:38 +08:00
// "max-age=0 + must-revalidate" (aka "no-cache") is preferred instead of "no-store"
// because browsers may restore some input fields after navigate-back / reload a page.
2025-03-13 07:04:50 +08:00
publicPrivate := util.Iif(opts.IsPublic, "public", "private")
if setting.IsProd {
2025-03-13 07:04:50 +08:00
if opts.MaxAge == 0 {
directives = append(directives, "max-age=0", "private", "must-revalidate")
} else {
2025-03-13 07:04:50 +08:00
directives = append(directives, publicPrivate, "max-age="+strconv.Itoa(int(opts.MaxAge.Seconds())))
}
} else {
2025-03-13 07:04:50 +08:00
// use dev-related controls, and remind users they are using non-prod setting.
directives = append(directives, "max-age=0", publicPrivate, "must-revalidate")
h.Set("X-Gitea-Debug", fmt.Sprintf("RUN_MODE=%v, MaxAge=%s", setting.RunMode, opts.MaxAge))
}
2025-03-13 07:04:50 +08:00
if opts.NoTransform {
directives = append(directives, "no-transform")
}
2025-03-13 07:04:50 +08:00
h.Set("Cache-Control", strings.Join(directives, ", "))
}
2025-03-13 07:04:50 +08:00
func CacheControlForPublicStatic() *CacheControlOptions {
return &CacheControlOptions{
IsPublic: true,
MaxAge: setting.StaticCacheTime,
NoTransform: true,
}
}
2025-03-13 07:04:50 +08:00
func CacheControlForPrivateStatic() *CacheControlOptions {
return &CacheControlOptions{
MaxAge: setting.StaticCacheTime,
NoTransform: true,
}
2021-04-12 16:49:26 +02:00
}
2021-04-12 16:49:26 +02:00
// 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 {
2025-06-18 03:48:09 +02:00
for item := range strings.SplitSeq(ifNoneMatch, ",") {
2024-01-30 00:18:40 +08:00
item = strings.TrimPrefix(strings.TrimSpace(item), "W/") // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#directives
2021-04-12 16:49:26 +02:00
if item == etag {
return true
}
}
}
return false
}
func HandleGenericETagPublicCache(req *http.Request, w http.ResponseWriter, etag string, lastModified *time.Time) bool {
return handleGenericETagTimeCache(req, w, etag, lastModified, CacheControlForPublicStatic())
}
func HandleGenericETagPrivateCache(req *http.Request, w http.ResponseWriter, etag string, lastModified *time.Time) bool {
return handleGenericETagTimeCache(req, w, etag, lastModified, CacheControlForPrivateStatic())
}
// handleGenericETagTimeCache handles ETag-based caching with Last-Modified caching for the HTTP request.
// It returns true if the request was handled.
func handleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag string, lastModified *time.Time, cacheControlOpts *CacheControlOptions) (handled bool) {
if etag != "" {
w.Header().Set("Etag", etag)
}
2023-07-07 07:31:56 +02:00
if lastModified != nil && !lastModified.IsZero() {
2024-09-22 05:56:25 +08:00
// http.TimeFormat required a UTC time, refer to https://pkg.go.dev/net/http#TimeFormat
w.Header().Set("Last-Modified", lastModified.UTC().Format(http.TimeFormat))
}
if etag != "" {
if checkIfNoneMatchIsValid(req, etag) {
w.WriteHeader(http.StatusNotModified)
return true
}
}
2023-07-07 07:31:56 +02:00
if lastModified != nil && !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
}
}
}
2025-03-13 07:04:50 +08:00
SetCacheControlInHeader(w.Header(), cacheControlOpts)
return false
}