Skip to content

Commit 5f938f5

Browse files
committed
feat(acceptHeaders): allow whitespace between value and qulity
1 parent 44ac225 commit 5f938f5

File tree

3 files changed

+36
-29
lines changed

3 files changed

+36
-29
lines changed

src/acceptHeaders/acceptItem.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package acceptHeaders
22

33
import (
4+
"mjpclab.dev/ghfs/src/util"
45
"strconv"
56
"strings"
67
)
78

8-
const qualitySign = ";q="
9+
const qualitySign = "q="
910
const defaultQuality = 1000
1011
const maxQualityDecimals = 3
1112

@@ -42,9 +43,21 @@ func (item acceptItem) match(value string) bool {
4243
}
4344

4445
func parseAcceptItem(input string) acceptItem {
45-
value := input
46-
if semiColonIndex := strings.IndexByte(value, ';'); semiColonIndex >= 0 {
47-
value = value[:semiColonIndex]
46+
entries := strings.Split(input, ";")
47+
if len(entries) == 0 {
48+
return acceptItem{}
49+
}
50+
util.InPlaceTrim(entries)
51+
52+
value := entries[0]
53+
entries = entries[1:]
54+
55+
quality := defaultQuality
56+
for _, entry := range entries {
57+
entry = strings.TrimSpace(entry)
58+
if strings.HasPrefix(entry, qualitySign) {
59+
quality = parseQuality(entry[len(qualitySign):])
60+
}
4861
}
4962

5063
wildcards := 0
@@ -54,43 +67,37 @@ func parseAcceptItem(input string) acceptItem {
5467
wildcards = 1
5568
}
5669

57-
rest := input[len(value):]
58-
qSignIndex := strings.Index(rest, qualitySign)
59-
if qSignIndex < 0 {
60-
return acceptItem{value, defaultQuality, wildcards}
61-
}
70+
return acceptItem{value, quality, wildcards}
71+
}
6272

63-
rest = rest[qSignIndex+len(qualitySign):]
64-
if semiColonIndex := strings.IndexByte(rest, ';'); semiColonIndex >= 0 {
65-
rest = rest[:semiColonIndex]
66-
}
67-
qLen := len(rest)
73+
func parseQuality(input string) (quality int) {
74+
qLen := len(input)
6875

6976
if qLen == 0 {
70-
return acceptItem{value, defaultQuality, wildcards}
77+
return defaultQuality
7178
}
72-
if qLen > 1 && rest[1] != '.' {
73-
return acceptItem{value, defaultQuality, wildcards}
79+
if qLen > 1 && input[1] != '.' {
80+
return defaultQuality
7481
}
7582

76-
// "q=1" or q is an invalid value
77-
if rest[0] != '0' {
78-
return acceptItem{value, defaultQuality, wildcards}
83+
// q is "1" or q is an invalid value
84+
if input[0] != '0' {
85+
return defaultQuality
7986
}
8087

81-
// "q=0."
88+
// "0."
8289
if qLen <= 2 {
83-
return acceptItem{value, 0, wildcards}
90+
return 0
8491
}
8592

86-
rest = rest[2:]
87-
qDecimalLen := len(rest)
93+
input = input[2:]
94+
qDecimalLen := len(input)
8895
if qDecimalLen > maxQualityDecimals {
8996
qDecimalLen = maxQualityDecimals
90-
rest = rest[:qDecimalLen]
97+
input = input[:qDecimalLen]
9198
}
9299

93-
quality, err := strconv.Atoi(rest)
100+
quality, err := strconv.Atoi(input)
94101
if err != nil {
95102
quality = defaultQuality
96103
} else {
@@ -99,5 +106,5 @@ func parseAcceptItem(input string) acceptItem {
99106
quality *= 10
100107
}
101108
}
102-
return acceptItem{value, quality, wildcards}
109+
return quality
103110
}

src/acceptHeaders/acceptItem_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestParseAcceptItem(t *testing.T) {
5151
t.Error(output.quality)
5252
}
5353

54-
input = "en-US;q=0.97"
54+
input = "en-US; q=0.97"
5555
output = parseAcceptItem(input)
5656
if output.value != "en-US" {
5757
t.Error(output.value)

src/acceptHeaders/accepts_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestParseAccept(t *testing.T) {
9-
accept := "text/html, */*;q=0.6, text/plain;q=0.9, application/json;q=0.7, image/*;q=0.8, image/png;q=0.8"
9+
accept := "text/html, */*;q=0.6, text/plain; q=0.9, application/json;q=0.7, image/*;q=0.8, image/png;q=0.8"
1010
accepts := ParseAccepts(accept)
1111
if len(accepts) != 6 {
1212
t.Error(len(accepts))

0 commit comments

Comments
 (0)