-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser_accept.go
More file actions
45 lines (34 loc) · 751 Bytes
/
parser_accept.go
File metadata and controls
45 lines (34 loc) · 751 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
42
43
44
45
package mimeheader
import (
"strconv"
"strings"
)
const DefaultQuality float32 = 1.0
func ParseAcceptHeader(header string) AcceptHeader {
accepts := strings.Split(header, ",")
if len(accepts) == 0 {
return AcceptHeader{}
}
mheaders := make([]MimeHeader, 0, len(accepts))
for _, accept := range accepts {
mtype, err := ParseMediaType(accept)
if err != nil {
continue
}
header := MimeHeader{
MimeType: mtype,
Quality: DefaultQuality,
}
if qs, ok := header.Params["q"]; ok {
const floatSize = 32
quality, err := strconv.ParseFloat(qs, floatSize)
if err == nil {
header.Quality = float32(quality)
}
}
mheaders = append(mheaders, header)
}
ah := AcceptHeader{}
ah.Set(mheaders)
return ah
}