gitea
Зеркало из https://github.com/go-gitea/gitea
1// Copyright 2020 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package httpcache
5
6import (
7"io"
8"net/http"
9"strconv"
10"strings"
11"time"
12
13"code.gitea.io/gitea/modules/setting"
14)
15
16// SetCacheControlInHeader sets suitable cache-control headers in the response
17func SetCacheControlInHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
18directives := make([]string, 0, 2+len(additionalDirectives))
19
20// "max-age=0 + must-revalidate" (aka "no-cache") is preferred instead of "no-store"
21// because browsers may restore some input fields after navigate-back / reload a page.
22if setting.IsProd {
23if maxAge == 0 {
24directives = append(directives, "max-age=0", "private", "must-revalidate")
25} else {
26directives = append(directives, "private", "max-age="+strconv.Itoa(int(maxAge.Seconds())))
27}
28} else {
29directives = append(directives, "max-age=0", "private", "must-revalidate")
30
31// to remind users they are using non-prod setting.
32h.Set("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
33}
34
35h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
36}
37
38func ServeContentWithCacheControl(w http.ResponseWriter, req *http.Request, name string, modTime time.Time, content io.ReadSeeker) {
39SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
40http.ServeContent(w, req, name, modTime, content)
41}
42
43// HandleGenericETagCache handles ETag-based caching for a HTTP request.
44// It returns true if the request was handled.
45func HandleGenericETagCache(req *http.Request, w http.ResponseWriter, etag string) (handled bool) {
46if len(etag) > 0 {
47w.Header().Set("Etag", etag)
48if checkIfNoneMatchIsValid(req, etag) {
49w.WriteHeader(http.StatusNotModified)
50return true
51}
52}
53SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
54return false
55}
56
57// checkIfNoneMatchIsValid tests if the header If-None-Match matches the ETag
58func checkIfNoneMatchIsValid(req *http.Request, etag string) bool {
59ifNoneMatch := req.Header.Get("If-None-Match")
60if len(ifNoneMatch) > 0 {
61for _, item := range strings.Split(ifNoneMatch, ",") {
62item = strings.TrimPrefix(strings.TrimSpace(item), "W/") // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#directives
63if item == etag {
64return true
65}
66}
67}
68return false
69}
70
71// HandleGenericETagTimeCache handles ETag-based caching with Last-Modified caching for a HTTP request.
72// It returns true if the request was handled.
73func HandleGenericETagTimeCache(req *http.Request, w http.ResponseWriter, etag string, lastModified *time.Time) (handled bool) {
74if len(etag) > 0 {
75w.Header().Set("Etag", etag)
76}
77if lastModified != nil && !lastModified.IsZero() {
78// http.TimeFormat required a UTC time, refer to https://pkg.go.dev/net/http#TimeFormat
79w.Header().Set("Last-Modified", lastModified.UTC().Format(http.TimeFormat))
80}
81
82if len(etag) > 0 {
83if checkIfNoneMatchIsValid(req, etag) {
84w.WriteHeader(http.StatusNotModified)
85return true
86}
87}
88if lastModified != nil && !lastModified.IsZero() {
89ifModifiedSince := req.Header.Get("If-Modified-Since")
90if ifModifiedSince != "" {
91t, err := time.Parse(http.TimeFormat, ifModifiedSince)
92if err == nil && lastModified.Unix() <= t.Unix() {
93w.WriteHeader(http.StatusNotModified)
94return true
95}
96}
97}
98SetCacheControlInHeader(w.Header(), setting.StaticCacheTime)
99return false
100}
101