Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions controller/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,42 +176,40 @@ func GetStatus(c *gin.Context) {

func GetNotice(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
notice := common.OptionMap["Notice"]
common.OptionMapRWMutex.RUnlock()
serveRevalidatedJSON(c, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["Notice"],
"data": notice,
})
return
}

func GetAbout(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
about := common.OptionMap["About"]
common.OptionMapRWMutex.RUnlock()
serveRevalidatedJSON(c, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["About"],
"data": about,
})
return
}

func GetUserAgreement(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
serveRevalidatedJSON(c, gin.H{
"success": true,
"message": "",
"data": system_setting.GetLegalSettings().UserAgreement,
})
return
}

func GetPrivacyPolicy(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
serveRevalidatedJSON(c, gin.H{
"success": true,
"message": "",
"data": system_setting.GetLegalSettings().PrivacyPolicy,
})
return
}

func GetMidjourney(c *gin.Context) {
Expand All @@ -227,13 +225,13 @@ func GetMidjourney(c *gin.Context) {

func GetHomePageContent(c *gin.Context) {
common.OptionMapRWMutex.RLock()
defer common.OptionMapRWMutex.RUnlock()
c.JSON(http.StatusOK, gin.H{
homePageContent := common.OptionMap["HomePageContent"]
common.OptionMapRWMutex.RUnlock()
serveRevalidatedJSON(c, gin.H{
"success": true,
"message": "",
"data": common.OptionMap["HomePageContent"],
"data": homePageContent,
})
return
}

func SendEmailVerification(c *gin.Context) {
Expand Down
87 changes: 87 additions & 0 deletions controller/revalidated_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package controller

import (
"crypto/sha256"
"encoding/hex"
"net/http"
"strings"

"github.com/QuantumNous/new-api/common"
"github.com/gin-gonic/gin"
)

// serveRevalidatedJSON writes payload as JSON with a weak content-derived ETag
// and answers conditional requests with 304 Not Modified.
//
// Intended for small, public, admin-editable payloads (notice, home page
// content) that every anonymous visitor fetches on page load. The goal is to
// make those fetches cheap without ever serving stale content:
//
// - The ETag is a hash of the response body, so it is identical across
// replicas. Deriving it from a timestamp would not be, and the Option table
// has no updated_at column to derive one from anyway.
// - The validator is weak (W/ prefixed) because /api is gzip-compressed by
// middleware that runs after this handler returns. The hash is computed over
// the uncompressed body, so the compressed and identity forms of one payload
// share a validator, and a strong ETag asserts byte-for-byte equality that
// does not hold across encodings (RFC 9110 §8.8.1). Weakening it costs
// nothing here: conditional GET compares weakly anyway, and these payloads
// are a few hundred bytes of JSON that no client Range-requests.
// - Cache-Control is "no-cache", which means "may be stored, but must be
// revalidated before reuse" (RFC 9111 §5.2.2.4). Browsers and CDNs both
// revalidate on every request, so an admin edit takes effect immediately.
// max-age/s-maxage are deliberately not set: upstream cannot assume how
// long any given deployment tolerates a stale notice.
// - Vary: Accept-Encoding is still required. Weakening the validator makes
// revalidation correct, but it does not separate the two encodings in a
// shared cache. Without Vary, a cache holding the gzip copy would hand those
// bytes to a client that never sent Accept-Encoding: gzip.
func serveRevalidatedJSON(c *gin.Context, payload any) {
body, err := common.Marshal(payload)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": err.Error(),
})
return
}

digest := sha256.Sum256(body)
etag := `W/"` + hex.EncodeToString(digest[:]) + `"`

c.Header("ETag", etag)
c.Header("Cache-Control", "no-cache")
c.Header("Vary", "Accept-Encoding")

if etagMatches(c.GetHeader("If-None-Match"), etag) {
c.Status(http.StatusNotModified)
return
}

c.Data(http.StatusOK, "application/json; charset=utf-8", body)
}

// etagMatches reports whether an If-None-Match header field matches etag,
// using the weak comparison required for conditional GET (RFC 9110 §13.1.2).
// The field is a comma-separated list of entity-tags or the wildcard "*".
//
// Weak comparison ignores the W/ prefix on both operands, so it must be
// stripped from the served etag as well as from each candidate. Stripping only
// the candidate would make a weak served validator match nothing, silently
// disabling 304 responses.
func etagMatches(ifNoneMatch string, etag string) bool {
ifNoneMatch = strings.TrimSpace(ifNoneMatch)
if ifNoneMatch == "" {
return false
}
if ifNoneMatch == "*" {
return true
}
etag = strings.TrimPrefix(etag, "W/")
for _, candidate := range strings.Split(ifNoneMatch, ",") {
if strings.TrimPrefix(strings.TrimSpace(candidate), "W/") == etag {
return true
}
}
return false
}
Loading