-
Notifications
You must be signed in to change notification settings - Fork 2
/
header.go
339 lines (299 loc) · 11.5 KB
/
header.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// go-aah/ahttp source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package ahttp
import (
"mime"
"net/http"
"path/filepath"
"sort"
"strconv"
"strings"
"aahframework.org/essentials.v0"
"aahframework.org/log.v0"
)
const vendorTreePrefix = "vnd."
// HTTP Header names
const (
HeaderAccept = "Accept"
HeaderAcceptEncoding = "Accept-Encoding"
HeaderAcceptLanguage = "Accept-Language"
HeaderAcceptRanges = "Accept-Ranges"
HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers"
HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods"
HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin"
HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers"
HeaderAccessControlMaxAge = "Access-Control-Max-Age"
HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers"
HeaderAccessControlRequestMethod = "Access-Control-Request-Method"
HeaderAge = "Age"
HeaderAllow = "Allow"
HeaderAuthorization = "Authorization"
HeaderCacheControl = "Cache-Control"
HeaderConnection = "Connection"
HeaderContentDisposition = "Content-Disposition"
HeaderContentEncoding = "Content-Encoding"
HeaderContentLength = "Content-Length"
HeaderContentType = "Content-Type"
HeaderContentSecurityPolicy = "Content-Security-Policy"
HeaderContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
HeaderCookie = "Cookie"
HeaderDate = "Date"
HeaderETag = "ETag"
HeaderExpires = "Expires"
HeaderHost = "Host"
HeaderIfMatch = "If-Match"
HeaderIfModifiedSince = "If-Modified-Since"
HeaderIfNoneMatch = "If-None-Match"
HeaderIfRange = "If-Range"
HeaderIfUnmodifiedSince = "If-Unmodified-Since"
HeaderKeepAlive = "Keep-Alive"
HeaderLastModified = "Last-Modified"
HeaderLocation = "Location"
HeaderOrigin = "Origin"
HeaderMethod = "Method"
HeaderPublicKeyPins = "Public-Key-Pins"
HeaderRange = "Range"
HeaderReferer = "Referer"
HeaderReferrerPolicy = "Referrer-Policy"
HeaderRetryAfter = "Retry-After"
HeaderServer = "Server"
HeaderSetCookie = "Set-Cookie"
HeaderStatus = "Status"
HeaderStrictTransportSecurity = "Strict-Transport-Security"
HeaderTransferEncoding = "Transfer-Encoding"
HeaderUpgrade = "Upgrade"
HeaderUserAgent = "User-Agent"
HeaderVary = "Vary"
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXContentTypeOptions = "X-Content-Type-Options"
HeaderXDNSPrefetchControl = "X-DNS-Prefetch-Control"
HeaderXCSRFToken = "X-CSRF-Token"
HeaderXForwardedFor = "X-Forwarded-For"
HeaderXForwardedHost = "X-Forwarded-Host"
HeaderXForwardedPort = "X-Forwarded-Port"
HeaderXForwardedProto = "X-Forwarded-Proto"
HeaderXForwardedProtocol = "X-Forwarded-Protocol"
HeaderXForwardedSsl = "X-Forwarded-Ssl"
HeaderXUrlScheme = "X-Url-Scheme"
HeaderXForwardedServer = "X-Forwarded-Server"
HeaderXFrameOptions = "X-Frame-Options"
HeaderXHTTPMethodOverride = "X-HTTP-Method-Override"
HeaderXPermittedCrossDomainPolicies = "X-Permitted-Cross-Domain-Policies"
HeaderXRealIP = "X-Real-Ip"
HeaderXRequestedWith = "X-Requested-With"
HeaderXRequestID = "X-Request-Id"
HeaderXXSSProtection = "X-XSS-Protection"
)
type (
// AcceptSpec used for HTTP Accept, Accept-Language, Accept-Encoding header
// value and it's quality. Implementation follows the specification of RFC7231
// https://tools.ietf.org/html/rfc7231#section-5.3
AcceptSpec struct {
Raw string
Value string
Q float32
Params map[string]string
}
// AcceptSpecs is list of values parsed from header and sorted by
// quality factor.
AcceptSpecs []AcceptSpec
)
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Global HTTP header methods
//___________________________________
// NegotiateContentType method negotiates the response `Content-Type` from the given HTTP
// `Accept` header. The resolve order is- 1) URL extension 2) Accept header
// Most quailfied one based quality factor otherwise default is HTML.
func NegotiateContentType(req *http.Request) *ContentType {
// 1) URL extension
ext := filepath.Ext(req.URL.Path)
switch ext {
case ".html", ".htm", ".json", ".js", ".xml", ".txt":
return parseMediaType(mime.TypeByExtension(ext))
}
// 2) From Accept header
spec := ParseAccept(req, HeaderAccept).MostQualified()
if spec == nil {
// if parsed spec is nil return content type as HTML.
return ContentTypeHTML
}
// 3) Accept Header Vendor Types
// RFC4288 https://tools.ietf.org/html/rfc4288#section-3.2
if parts, yes := isVendorType(spec.Value); yes {
subparts := strings.Split(parts[1], "+")
if strings.Contains(subparts[0], "-v") {
verparts := strings.Split(subparts[0], "-v")
spec.Params["vendor"] = strings.TrimPrefix(verparts[0], vendorTreePrefix)
spec.Params["version"] = verparts[1]
} else {
spec.Params["vendor"] = strings.TrimPrefix(subparts[0], vendorTreePrefix)
}
// Rewrite the Content-Type
spec.Value = parts[0] + "/" + subparts[1]
}
exts, _ := mime.ExtensionsByType(spec.Value)
return newContentType(spec.Value, exts, spec.Params)
}
// NegotiateLocale method negotiates the `Accept-Language` from the given HTTP
// request. Most quailfied one based on quality factor.
func NegotiateLocale(req *http.Request) *Locale {
return ToLocale(ParseAccept(req, HeaderAcceptLanguage).MostQualified())
}
// NegotiateEncoding negotiates the `Accept-Encoding` from the given HTTP
// request. Most quailfied one based on quality factor.
func NegotiateEncoding(req *http.Request) *AcceptSpec {
return ParseAcceptEncoding(req).MostQualified()
}
// ParseContentType method parses the request header `Content-Type` as per RFC1521.
func ParseContentType(req *http.Request) *ContentType {
contentType := req.Header.Get(HeaderContentType)
if contentType == "" {
return ContentTypeHTML
}
return parseMediaType(contentType)
}
// ParseAcceptEncoding method parses the request HTTP header `Accept-Encoding`
// as per RFC7231 https://tools.ietf.org/html/rfc7231#section-5.3.4. It returns
// `AcceptSpecs`.
func ParseAcceptEncoding(req *http.Request) AcceptSpecs {
return ParseAccept(req, HeaderAcceptEncoding)
}
// ParseAccept parses the HTTP Accept* headers from `http.Request`
// returns the specification with quality factor as per RFC7231
// https://tools.ietf.org/html/rfc7231#section-5.3. Level value is not honored.
//
// Good read - http://stackoverflow.com/a/5331486/1343356 and
// http://stackoverflow.com/questions/13890996/http-accept-level
//
// Known issues with WebKit and IE
// http://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers
func ParseAccept(req *http.Request, hdrKey string) AcceptSpecs {
hdrValue := req.Header.Get(hdrKey)
var specs AcceptSpecs
for _, hv := range strings.Split(hdrValue, ",") {
if ess.IsStrEmpty(hv) {
continue
}
hv = strings.TrimSpace(hv)
parts := strings.Split(hv, ";")
if len(parts) == 1 {
specs = append(specs, AcceptSpec{
Raw: hv,
Value: parts[0],
Q: float32(1.0),
Params: make(map[string]string),
})
continue
}
q := float32(1.0)
params := map[string]string{}
for _, pv := range parts[1:] {
paramParts := strings.Split(strings.TrimSpace(pv), "=")
if len(paramParts) == 1 {
params[paramParts[0]] = ""
continue
}
if paramParts[0] == "q" {
qv, err := strconv.ParseFloat(paramParts[1], 32)
if err != nil {
q = float32(0.0)
params[paramParts[0]] = "0.0"
continue
}
q = float32(qv)
}
params[paramParts[0]] = paramParts[1]
}
specs = append(specs, AcceptSpec{
Raw: hv,
Value: parts[0],
Q: q,
Params: params,
})
}
sort.Sort(specs)
return specs
}
// ToLocale method creates a locale instance from `AcceptSpec`
func ToLocale(a *AcceptSpec) *Locale {
if a == nil {
return nil
}
values := strings.SplitN(a.Value, "-", 2)
if len(values) == 2 {
return &Locale{
Raw: a.Raw,
Language: values[0],
Region: values[1],
}
}
return &Locale{
Raw: a.Raw,
Language: values[0],
}
}
// NewLocale method returns locale instance for given locale string.
func NewLocale(value string) *Locale {
return ToLocale(
&AcceptSpec{
Raw: value,
Value: value,
})
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Locale methods
//___________________________________
// String is stringer interface.
func (l Locale) String() string {
return l.Raw
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// AcceptSpecs methods
//___________________________________
// GetParam method returns the Accept* header param value otherwise returns default
// value.
// For e.g.:
// Accept: application/json; version=2
//
// Method returns `2` for key `version`
func (a AcceptSpec) GetParam(key string, defaultValue string) string {
if v, found := a.Params[key]; found {
return v
}
return defaultValue
}
// MostQualified method returns the most quailfied accept spec, since `AcceptSpec` is
// sorted by quaity factor. First position is the most quailfied otherwise `nil`.
func (specs AcceptSpecs) MostQualified() *AcceptSpec {
if len(specs) > 0 {
return &specs[0]
}
return nil
}
// sort.Interface methods for accept spec
func (specs AcceptSpecs) Len() int { return len(specs) }
func (specs AcceptSpecs) Swap(i, j int) { specs[i], specs[j] = specs[j], specs[i] }
func (specs AcceptSpecs) Less(i, j int) bool { return specs[i].Q > specs[j].Q }
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Unexported methods
//___________________________________
// isVendorType method check the mime type is vendor type as per
// RFC4288 https://tools.ietf.org/html/rfc4288#section-3.2 - Vendor Tree
// i.e. `vnd.` prefix.
func isVendorType(mime string) ([]string, bool) {
parts := strings.Split(mime, "/")
return parts, strings.HasPrefix(parts[1], vendorTreePrefix)
}
// parseMediaType method parses a media type value and any optional
// parameters, per RFC 1521. the values in Content-Type and
// Content-Disposition headers (RFC 2183).
func parseMediaType(value string) *ContentType {
ctype, params, err := mime.ParseMediaType(value)
if err != nil {
log.Errorf("%v for value: %v", err, value)
}
exts, _ := mime.ExtensionsByType(ctype)
return newContentType(ctype, exts, params)
}