Skip to content

Commit 2dfd335

Browse files
committed
fix: 修正 Vary 报头的内容缺失和错误
1 parent e0c4ca2 commit 2dfd335

3 files changed

Lines changed: 13 additions & 8 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ module github.com/issue9/web
33
go 1.24.0
44

55
require (
6-
github.com/BurntSushi/toml v1.5.0
6+
github.com/BurntSushi/toml v1.6.0
77
github.com/andybalholm/brotli v1.2.0
88
github.com/fxamacker/cbor/v2 v2.9.0
9-
github.com/goccy/go-yaml v1.19.0
9+
github.com/goccy/go-yaml v1.19.1
1010
github.com/issue9/assert/v4 v4.3.1
1111
github.com/issue9/cache v0.19.5
1212
github.com/issue9/config v0.9.4

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
2-
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
1+
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
2+
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
33
github.com/andybalholm/brotli v1.2.0 h1:ukwgCxwYrmACq68yiUqwIWnGY0cTPox/M94sVwToPjQ=
44
github.com/andybalholm/brotli v1.2.0/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY=
55
github.com/bradfitz/gomemcache v0.0.0-20250403215159-8d39553ac7cf h1:TqhNAT4zKbTdLa62d2HDBFdvgSbIGB3eJE8HqhgiL9I=
@@ -16,8 +16,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
1616
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
1717
github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM=
1818
github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ=
19-
github.com/goccy/go-yaml v1.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE=
20-
github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
19+
github.com/goccy/go-yaml v1.19.1 h1:3rG3+v8pkhRqoQ/88NYNMHYVGYztCOCIZ7UQhu7H+NE=
20+
github.com/goccy/go-yaml v1.19.1/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
2121
github.com/issue9/assert/v4 v4.3.1 h1:dHYODk1yV7j/1baIB6K6UggI4r1Hfuljqic7PaDbwLg=
2222
github.com/issue9/assert/v4 v4.3.1/go.mod h1:v7qDRXi7AsaZZNh8eAK2rkLJg5/clztqQGA1DRv9Lv4=
2323
github.com/issue9/cache v0.19.5 h1:ICVIDu9zE0V970fh1h4eMd3jZORK5AQyTY3aR0w9af4=

output.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func (ctx *Context) Render(status int, body any) {
7272
ctx.Header().Set(header.ContentType, qheader.BuildContentType(ctx.Mimetype(false), ctx.Charset()))
7373
if id := ctx.LanguageTag().String(); id != "" {
7474
ctx.Header().Set(header.ContentLanguage, id)
75+
ctx.Header().Add(header.Vary, header.AcceptLanguage)
7576
}
7677

7778
data, err := ctx.Marshal(body)
@@ -93,7 +94,10 @@ func (ctx *Context) Render(status int, body any) {
9394
}
9495

9596
// Marshal 将对象 v 按用户要求编码并返回
96-
func (ctx *Context) Marshal(v any) ([]byte, error) { return ctx.outputMimetype.Marshal(ctx, v) }
97+
func (ctx *Context) Marshal(v any) ([]byte, error) {
98+
ctx.Header().Add(header.Vary, header.Accept)
99+
return ctx.outputMimetype.Marshal(ctx, v)
100+
}
97101

98102
// Wrote 是否已经有内容输出
99103
func (ctx *Context) Wrote() bool { return ctx.wrote }
@@ -116,6 +120,7 @@ func (ctx *Context) Write(bs []byte) (n int, err error) {
116120
closes := make([]io.Closer, 0, 2)
117121

118122
if ctx.outputCompressor != nil {
123+
ctx.Header().Add(header.Vary, header.AcceptEncoding)
119124
w, err := ctx.outputCompressor.NewEncoder(ctx.writer)
120125
if err != nil {
121126
return 0, err
@@ -125,7 +130,7 @@ func (ctx *Context) Write(bs []byte) (n int, err error) {
125130
}
126131

127132
if !qheader.CharsetIsNop(ctx.outputCharset) {
128-
ctx.Header().Add(header.Vary, header.ContentEncoding) // 只有在确定需要输出内容时才输出 Vary 报头
133+
ctx.Header().Add(header.Vary, header.AcceptCharset)
129134
w := transform.NewWriter(ctx.writer, ctx.outputCharset.NewEncoder())
130135
ctx.writer = w
131136
closes = append(closes, w)

0 commit comments

Comments
 (0)