-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
136 lines (108 loc) · 2.51 KB
/
parser.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
package negotiator
import (
"net/http"
"sort"
"strconv"
"strings"
)
type headerParser struct {
header http.Header
hasSlashVal bool
defaultQ float64
wildCard string
}
func newHeaderParser(header http.Header, hasSlashVal bool) *headerParser {
hp := &headerParser{header: header, hasSlashVal: hasSlashVal, defaultQ: 1.0}
if hp.hasSlashVal {
hp.wildCard = "*/*"
} else {
hp.wildCard = "*"
}
return hp
}
func (p headerParser) parse(headerName string) (specs specs) {
headerVal := formatHeaderVal(p.header.Get(headerName))
if headerVal == "" {
specs = []spec{spec{val: p.wildCard, q: p.defaultQ}}
return
}
for _, accept := range strings.Split(headerVal, ",") {
pair := strings.Split(strings.TrimSpace(accept), ";")
if len(pair) < 1 || len(pair) > 2 {
if p.hasSlashVal {
if strings.Index(pair[0], "/") == -1 {
continue
}
} else {
continue
}
}
spec := spec{val: pair[0], q: p.defaultQ}
if len(pair) == 2 {
var i int
if strings.HasPrefix(pair[1], "q=") {
i = 2
} else if strings.HasPrefix(pair[1], "level=") {
i = 6
} else {
continue
}
if q, err := strconv.ParseFloat(pair[1][i:], 64); err == nil && q != 0.0 {
if q > p.defaultQ {
q = p.defaultQ
}
spec.q = q
} else {
continue
}
}
specs = append(specs, spec)
}
sort.Sort(specs)
return
}
func (p headerParser) selectOffer(offers []string, specs specs) (bestOffer string) {
bestQ := 0.0
var bestWild, totalWild int
if p.hasSlashVal {
bestWild, totalWild = 3, 3
} else {
bestWild, totalWild = 2, 2
}
if len(specs) == 0 {
return
}
if len(offers) == 0 {
bestOffer = specs[0].val
return
}
for _, offer := range offers {
lowerCaseOffer := strings.ToLower(offer)
for _, spec := range specs {
switch {
case spec.q <= bestQ:
continue
case spec.val == p.wildCard && !specs.hasVal(lowerCaseOffer):
if spec.q > bestQ || bestWild > totalWild-1 {
bestOffer = offer
bestQ, bestWild = spec.q, totalWild-1
}
case p.hasSlashVal && strings.HasSuffix(spec.val, "/*"):
if strings.HasPrefix(lowerCaseOffer, spec.val[:len(spec.val)-1]) &&
(spec.q > bestQ || bestWild > totalWild-2) {
bestOffer = offer
bestQ, bestWild = spec.q, totalWild-2
}
case spec.val == lowerCaseOffer:
if spec.q > bestQ || bestWild > 0 {
bestOffer = offer
bestQ, bestWild = spec.q, 0
}
}
}
}
return
}
func formatHeaderVal(val string) string {
return strings.ToLower(strings.Replace(val, " ", "", -1))
}