mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git), so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form used by the handful of files that had ours. Shape is now: // Copyright 2026 Hanzo AI, Inc. All rights reserved. // Copyright 2015 The Gogs Authors. All rights reserved. // Copyright 2019 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT Ours on top, theirs retained verbatim. Retained because MIT requires it, and separately because they are true — those authors did write that code. Not renamed to "The Git Authors": Git is a different project under a different license, and putting its name here would attribute this code to people who have nothing to do with it. Adding our line is the fix; overwriting theirs would have been both a licence violation and a false statement. gofmt still reports import-order drift in some of these files. It predates this change (verified against a stash) and is left alone rather than burying a one-line-per-file legal change under 3000 files of unrelated reordering. Build clean. Co-authored-by: Hanzo Dev <[email protected]>
110 lines
3.6 KiB
Go
110 lines
3.6 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package httpcache
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzoai/git/modules/setting"
|
|
"github.com/hanzoai/git/modules/util"
|
|
)
|
|
|
|
type CacheControlOptions struct {
|
|
IsPublic bool
|
|
MaxAge time.Duration
|
|
}
|
|
|
|
// SetCacheControlInHeader sets suitable cache-control headers in the response
|
|
func SetCacheControlInHeader(h http.Header, opts *CacheControlOptions) {
|
|
directives := make([]string, 0, 4)
|
|
|
|
// "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.
|
|
publicPrivate := util.Iif(opts.IsPublic, "public", "private")
|
|
if setting.IsProd {
|
|
if opts.MaxAge == 0 {
|
|
directives = append(directives, "max-age=0", "private", "must-revalidate")
|
|
} else {
|
|
directives = append(directives, publicPrivate, "max-age="+strconv.Itoa(int(opts.MaxAge.Seconds())))
|
|
}
|
|
} else {
|
|
// 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-Cache-Debug", fmt.Sprintf("RUN_MODE=%v, MaxAge=%s", setting.RunMode, opts.MaxAge))
|
|
}
|
|
h.Set("Cache-Control", strings.Join(directives, ", "))
|
|
}
|
|
|
|
func CacheControlForPublicStatic() *CacheControlOptions {
|
|
return &CacheControlOptions{
|
|
IsPublic: true,
|
|
MaxAge: setting.StaticCacheTime,
|
|
}
|
|
}
|
|
|
|
func CacheControlForPrivateStatic() *CacheControlOptions {
|
|
return &CacheControlOptions{
|
|
MaxAge: setting.StaticCacheTime,
|
|
}
|
|
}
|
|
|
|
// 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.SplitSeq(ifNoneMatch, ",") {
|
|
item = strings.TrimPrefix(strings.TrimSpace(item), "W/") // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag#directives
|
|
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)
|
|
}
|
|
if lastModified != nil && !lastModified.IsZero() {
|
|
// 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
|
|
}
|
|
}
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
SetCacheControlInHeader(w.Header(), cacheControlOpts)
|
|
return false
|
|
}
|