-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.go
More file actions
41 lines (37 loc) · 803 Bytes
/
header.go
File metadata and controls
41 lines (37 loc) · 803 Bytes
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
package main
import (
"mime"
"net/http"
"strings"
)
const (
mediaTypeNDJson = "application/x-ndjson"
mediaTypeJson = "application/json"
mediaTypeAny = "*/*"
)
type accepts struct {
any bool
ndjson bool
json bool
acceptHeaderFound bool
}
func getAccepts(r *http.Request) (accepts, error) {
var a accepts
values := r.Header.Values("Accept")
a.acceptHeaderFound = len(values) > 0
for _, accept := range values {
amts := strings.SplitSeq(accept, ",")
for amt := range amts {
if mt, _, err := mime.ParseMediaType(amt); err != nil {
return a, err
} else if mt == mediaTypeNDJson {
a.ndjson = true
} else if mt == mediaTypeJson {
a.json = true
} else if mt == mediaTypeAny {
a.any = true
}
}
}
return a, nil
}