This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfc7234.go
291 lines (273 loc) · 7.69 KB
/
rfc7234.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package httpheader
import (
"net/http"
"strconv"
"strings"
"time"
)
// A WarningElem represents one element of the Warning header
// (RFC 7234 Section 5.5).
type WarningElem struct {
Code int
Agent string // defaults to "-" on output
Text string
Date time.Time // zero if missing
}
// Warning parses the Warning header from h (RFC 7234 Section 5.5).
func Warning(h http.Header) []WarningElem {
values := h["Warning"]
if values == nil {
return nil
}
elems := make([]WarningElem, 0, estimateElems(values))
for v, vs := iterElems("", values); v != ""; v, vs = iterElems(v, vs) {
var elem WarningElem
var codeStr string
codeStr, v = consumeTo(v, ' ', false)
elem.Code, _ = strconv.Atoi(codeStr)
elem.Agent, v = consumeTo(v, ' ', false)
elem.Text, v = consumeQuoted(v)
v = skipWS(v)
if peek(v) == '"' {
var dateStr string
dateStr, v = consumeTo(v[1:], '"', false)
elem.Date, _ = http.ParseTime(dateStr)
}
elems = append(elems, elem)
}
return elems
}
// SetWarning replaces the Warning header in h (RFC 7234 Section 5.5).
// See also AddWarning.
func SetWarning(h http.Header, elems []WarningElem) {
if len(elems) == 0 {
h.Del("Warning")
return
}
h.Set("Warning", buildWarning(elems))
}
// AddWarning is like SetWarning but appends instead of replacing.
func AddWarning(h http.Header, elems ...WarningElem) {
if len(elems) == 0 {
return
}
h.Add("Warning", buildWarning(elems))
}
func buildWarning(elems []WarningElem) string {
b := &strings.Builder{}
for i, elem := range elems {
if i > 0 {
write(b, ", ")
}
if elem.Agent == "" {
elem.Agent = "-"
}
write(b, strconv.Itoa(elem.Code), " ", elem.Agent, " ")
writeQuoted(b, elem.Text)
if !elem.Date.IsZero() {
write(b, ` "`, elem.Date.Format(http.TimeFormat), `"`)
}
}
return b.String()
}
// CacheDirectives represents directives of the Cache-Control header
// (RFC 7234 Section 5.2). Standard directives are stored in the corresponding
// fields; any unknown extensions are stored in Ext.
type CacheDirectives struct {
NoStore bool
NoTransform bool
OnlyIfCached bool
MustRevalidate bool
Public bool
ProxyRevalidate bool
Immutable bool // RFC 8246
// NoCache is true if the no-cache directive is present without an argument.
// If it has an argument -- a list of header names -- these are
// stored in NoCacheHeaders, canonicalized with http.CanonicalHeaderKey;
// while NoCache remains false. Similarly for the private directive.
NoCache bool
Private bool
NoCacheHeaders []string
PrivateHeaders []string
MaxAge Delta
SMaxage Delta
MinFresh Delta
StaleWhileRevalidate Delta // RFC 5861 Section 3
StaleIfError Delta // RFC 5861 Section 4
// A max-stale directive without an argument (meaning "any age")
// is represented as the special very large value Eternity.
MaxStale Delta
// Any unknown extension directives.
// A key mapping to an empty string is serialized to a directive
// without an argument.
Ext map[string]string
}
// A Delta represents a numeric cache directive which may be either absent
// or a number of seconds. The zero value of Delta is the absent value,
// not 0 seconds.
type Delta struct {
seconds int
ok bool
}
// Value returns the duration of d if it is present; otherwise 0, false.
func (d Delta) Value() (dur time.Duration, ok bool) {
return time.Duration(d.seconds) * time.Second, d.ok
}
// DeltaSeconds returns a Delta of the given number of seconds.
func DeltaSeconds(s int) Delta {
return Delta{s, true}
}
// Eternity represents unlimited age for the max-stale cache directive.
var Eternity = Delta{1<<31 - 1, true}
// CacheControl parses the Cache-Control header from h (RFC 7234 Section 5.2).
func CacheControl(h http.Header) CacheDirectives {
var cc CacheDirectives
for v, vs := iterElems("", h["Cache-Control"]); v != ""; v, vs = iterElems(v, vs) {
var name, value string
name, value, v = consumeParam(v)
switch name {
case "private":
if value == "" {
cc.Private = true
} else {
cc.PrivateHeaders = headerNames(value)
}
case "public":
cc.Public = true
case "max-age":
if seconds, err := strconv.Atoi(value); err == nil {
cc.MaxAge = DeltaSeconds(seconds)
}
case "s-maxage":
if seconds, err := strconv.Atoi(value); err == nil {
cc.SMaxage = DeltaSeconds(seconds)
}
case "no-cache":
if value == "" {
cc.NoCache = true
} else {
cc.NoCacheHeaders = headerNames(value)
}
case "must-revalidate":
cc.MustRevalidate = true
case "no-store":
cc.NoStore = true
case "stale-while-revalidate":
if seconds, err := strconv.Atoi(value); err == nil {
cc.StaleWhileRevalidate = DeltaSeconds(seconds)
}
case "stale-if-error":
if seconds, err := strconv.Atoi(value); err == nil {
cc.StaleIfError = DeltaSeconds(seconds)
}
case "no-transform":
cc.NoTransform = true
case "immutable":
cc.Immutable = true
case "only-if-cached":
cc.OnlyIfCached = true
case "proxy-revalidate":
cc.ProxyRevalidate = true
case "max-stale":
if value == "" {
cc.MaxStale = Eternity
} else if seconds, err := strconv.Atoi(value); err == nil {
cc.MaxStale = DeltaSeconds(seconds)
}
case "min-fresh":
if seconds, err := strconv.Atoi(value); err == nil {
cc.MinFresh = DeltaSeconds(seconds)
}
default:
if cc.Ext == nil {
cc.Ext = make(map[string]string)
}
cc.Ext[name] = value
}
}
return cc
}
// SetCacheControl replaces the Cache-Control header in h.
func SetCacheControl(h http.Header, cc CacheDirectives) {
b := &strings.Builder{}
var wrote bool
if cc.NoStore {
wrote = writeDirective(b, wrote, "no-store", "")
}
if cc.NoTransform {
wrote = writeDirective(b, wrote, "no-transform", "")
}
if cc.OnlyIfCached {
wrote = writeDirective(b, wrote, "only-if-cached", "")
}
if cc.MustRevalidate {
wrote = writeDirective(b, wrote, "must-revalidate", "")
}
if cc.Public {
wrote = writeDirective(b, wrote, "public", "")
}
if cc.ProxyRevalidate {
wrote = writeDirective(b, wrote, "proxy-revalidate", "")
}
if cc.Immutable {
wrote = writeDirective(b, wrote, "immutable", "")
}
if cc.Private || len(cc.PrivateHeaders) > 0 {
// "A sender SHOULD NOT generate the token form"
wrote = writeDirective(b, wrote, "private", "")
if !cc.Private {
write(b, `="`, strings.Join(cc.PrivateHeaders, ","), `"`)
}
}
if cc.NoCache || len(cc.NoCacheHeaders) > 0 {
// "A sender SHOULD NOT generate the token form"
wrote = writeDirective(b, wrote, "no-cache", "")
if !cc.NoCache {
write(b, `="`, strings.Join(cc.NoCacheHeaders, ","), `"`)
}
}
if cc.MaxAge.ok {
wrote = writeDirective(b, wrote, "max-age",
strconv.Itoa(cc.MaxAge.seconds))
}
if cc.SMaxage.ok {
wrote = writeDirective(b, wrote, "s-maxage",
strconv.Itoa(cc.SMaxage.seconds))
}
if cc.MaxStale.ok {
var value string
if cc.MaxStale != Eternity {
value = strconv.Itoa(cc.MaxStale.seconds)
}
wrote = writeDirective(b, wrote, "max-stale", value)
}
if cc.MinFresh.ok {
wrote = writeDirective(b, wrote, "min-fresh",
strconv.Itoa(cc.MinFresh.seconds))
}
if cc.StaleWhileRevalidate.ok {
wrote = writeDirective(b, wrote, "stale-while-revalidate",
strconv.Itoa(cc.StaleWhileRevalidate.seconds))
}
if cc.StaleIfError.ok {
wrote = writeDirective(b, wrote, "stale-if-error",
strconv.Itoa(cc.StaleIfError.seconds))
}
for name, value := range cc.Ext {
wrote = writeDirective(b, wrote, name, value)
}
if !wrote {
h.Del("Cache-Control")
return
}
h.Set("Cache-Control", b.String())
}
func headerNames(v string) []string {
names := strings.FieldsFunc(v, func(r rune) bool {
return r == ' ' || r == '\t' || r == ','
})
for i := range names {
names[i] = http.CanonicalHeaderKey(names[i])
}
return names
}