-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
177 lines (138 loc) · 3.08 KB
/
parse.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
/*
Copyright (C) 2017 Kagucho <kagucho.net@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package unchunked
import (
"sort"
"strings"
)
func isWhitespace(character byte) bool {
return character == ' ' || character == '\t'
}
type codingIndex uint
const (
codingAny codingIndex = iota
codingGzip
codingIdentity
codingUnknown
)
var codings = []string{
codingAny: `*`, codingGzip: `GZIP`, codingIdentity: `IDENTITY`,
}
func parseCodings(field string, begin int) (codingIndex, int) {
end := begin
for end < len(field) && field[end] != ',' && field[end] != ';' && !isWhitespace(field[end]) {
end++
}
coding := strings.ToUpper(field[begin:end])
index := codingIndex(sort.SearchStrings(codings, coding))
if index < codingIndex(len(codings)) && codings[index] != coding {
index = codingUnknown
}
return index, end
}
const qDigitWidth = 8
const qInitIndex = 3 * qDigitWidth
const qMask = 0x0F0F0F0F
func parseQvalue(field string, cursor int) (uint, int) {
index := uint(qInitIndex)
q := uint(field[cursor]) << index
cursor++
if cursor >= len(field) {
goto end
}
if field[cursor] != '.' {
goto skip
}
for index > 0 {
cursor++
if cursor >= len(field) {
goto end
}
if field[cursor] < '0' {
goto skip
}
index -= qDigitWidth
q |= uint(field[cursor]) << index
}
skip:
for cursor < len(field) && field[cursor] != ',' {
cursor++
}
end:
return q & qMask, cursor
}
func parseWeight(field string, cursor int) (uint, int) {
for {
if cursor >= len(field) || field[cursor] == ',' {
return uint(1 << qInitIndex), cursor
}
if field[cursor] == ';' {
break
}
cursor++
}
for {
cursor++
if field[cursor] == 'q' {
break
}
}
return parseQvalue(field, cursor+2)
}
func parseField(field string, callback func(codingIndex, uint)) {
cursor := 0
parseCodingWeight := func() {
var coding codingIndex
coding, cursor = parseCodings(field, cursor)
var q uint
q, cursor = parseWeight(field, cursor)
callback(coding, q)
}
if len(field) == 0 {
return
}
// ","
if field[0] == ',' {
// OWS
for {
cursor++
if cursor >= len(field) {
return
}
if field[cursor] == ',' {
break
}
}
} else {
parseCodingWeight()
}
for {
if cursor >= len(field) {
return
}
// Ignore comma
for {
cursor++
if !isWhitespace(field[cursor]) {
break
}
}
parseCodingWeight()
}
}
func parse(fields []string, callback func(codingIndex, uint)) {
for _, field := range fields {
parseField(field, callback)
}
}