generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 2
/
etag.go
105 lines (95 loc) · 2.55 KB
/
etag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2023 CloudWeGo Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package etag
import (
"bytes"
"context"
"hash/crc32"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/common/bytebufferpool"
"github.com/cloudwego/hertz/pkg/protocol/consts"
)
const HeaderIfNoneMatch = "If-None-Match"
// New will create an etag middleware
func New(opts ...Option) app.HandlerFunc {
options := newOptions(opts...)
var (
headerETag = []byte("Etag")
weakPrefix = []byte("W/")
)
return func(ctx context.Context, c *app.RequestContext) {
c.Next(ctx)
// skip etag if next returns true
if options.next != nil && options.next(ctx, c) {
c.Next(ctx)
return
}
if c.Response.StatusCode() != consts.StatusOK {
return
}
respBody := c.Response.Body()
if len(respBody) == 0 {
return
}
if c.Response.Header.Peek(b2s(headerETag)) != nil {
return
}
// build etag
var etag []byte
bb := bytebufferpool.Get()
defer bytebufferpool.Put(bb)
if options.generator != nil {
// custom generation
// e.g. W/your-custom-etag
if options.weak {
_, _ = bb.Write(weakPrefix)
}
_, _ = bb.Write(options.generator(ctx, c))
etag = bb.Bytes()
} else {
// default generation
// e.g. W/"11-222957957"
if options.weak {
_, _ = bb.Write(weakPrefix)
}
_ = bb.WriteByte('"')
bb.B = appendUint(bb.Bytes(), uint32(len(respBody)))
_ = bb.WriteByte('-')
bb.B = appendUint(bb.Bytes(), crc32.ChecksumIEEE(respBody))
_ = bb.WriteByte('"')
etag = bb.Bytes()
}
// verify etag
clientETag := c.Request.Header.Peek(HeaderIfNoneMatch)
if bytes.HasPrefix(clientETag, weakPrefix) {
// client - server
// W/0 == 0 || W/0 == W/0
if bytes.Equal(clientETag[2:], etag) || bytes.Equal(clientETag[2:], etag[2:]) {
c.NotModified()
return
}
// W/0 != W/1 || W/0 != 1
c.Response.Header.SetCanonical(headerETag, etag)
return
}
if bytes.Contains(clientETag, etag) {
// 0 == 0
c.NotModified()
return
}
// 0 != 1
c.Response.Header.SetCanonical(headerETag, etag)
}
}