Skip to content

Commit e45ff96

Browse files
committed
#6 Ignore q param in sorting in accept header
1 parent 1b34d61 commit e45ff96

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

accept_header.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,56 @@ func (ah AcceptHeader) Less(i, j int) bool {
3434
return ah.mheaders[i].Quality < ah.mheaders[j].Quality
3535
}
3636

37+
less, done := ah.lessWildcard(i, j)
38+
if done {
39+
return less
40+
}
41+
42+
return ah.lessParams(i, j)
43+
}
44+
45+
func (ah AcceptHeader) lessParams(i, j int) bool {
46+
li := len(ah.mheaders[i].Params)
47+
_, ok := ah.mheaders[i].Params["q"]
48+
49+
if ok {
50+
li--
51+
}
52+
53+
lj := len(ah.mheaders[j].Params)
54+
_, ok = ah.mheaders[j].Params["q"]
55+
56+
if ok {
57+
lj--
58+
}
59+
60+
return li < lj
61+
}
62+
63+
func (ah AcceptHeader) lessWildcard(i, j int) (less, done bool) {
3764
// '*' value has less priority than a specific type
3865
// If i contains '*' and j has specific type, then i less than j
3966
if ah.mheaders[i].Type == MimeAny && ah.mheaders[j].Type != MimeAny {
40-
return true
67+
return true, true
4168
}
4269

4370
// If i contains a specific type and j contains '*' then i greater than j
4471
if ah.mheaders[i].Type != MimeAny && ah.mheaders[j].Type == MimeAny {
45-
return false
72+
return false, true
4673
}
4774

4875
// '*' value has less priority than a specific type
4976
// If i contains '*' and j has specific type, then i less than j
5077
if ah.mheaders[i].Subtype == MimeAny && ah.mheaders[j].Subtype != MimeAny {
51-
return true
78+
return true, true
5279
}
5380

5481
// If i contains a specific type and j contains '*' then i greater than j
5582
if ah.mheaders[i].Subtype != MimeAny && ah.mheaders[j].Subtype == MimeAny {
56-
return false
83+
return false, true
5784
}
5885

59-
// More specific params has greater priority
60-
return len(ah.mheaders[i].Params) < len(ah.mheaders[j].Params)
86+
return false, false
6187
}
6288

6389
// Swap function for sort.Interface interface.

accept_header_set_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,44 @@ func provideAcceptHeaderSet() []acceptHeaderSet {
455455
headers: []mimeheader.MimeHeader{},
456456
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{}),
457457
},
458+
{
459+
name: "Issue #6 sort by params",
460+
headers: []mimeheader.MimeHeader{
461+
{
462+
MimeType: mimeheader.MimeType{
463+
Type: "application",
464+
Subtype: "xml",
465+
Params: map[string]string{"q": "1.0", "test": "t"},
466+
},
467+
Quality: 1,
468+
},
469+
{
470+
MimeType: mimeheader.MimeType{
471+
Type: "application",
472+
Subtype: "json",
473+
Params: map[string]string{"charset": "utf-8", "test": "t"},
474+
},
475+
Quality: 1,
476+
},
477+
},
478+
exp: mimeheader.NewAcceptHeaderPlain([]mimeheader.MimeHeader{
479+
{
480+
MimeType: mimeheader.MimeType{
481+
Type: "application",
482+
Subtype: "json",
483+
Params: map[string]string{"charset": "utf-8", "test": "t"},
484+
},
485+
Quality: 1,
486+
},
487+
{
488+
MimeType: mimeheader.MimeType{
489+
Type: "application",
490+
Subtype: "xml",
491+
Params: map[string]string{"q": "1.0", "test": "t"},
492+
},
493+
Quality: 1,
494+
},
495+
}),
496+
},
458497
}
459498
}

0 commit comments

Comments
 (0)