Skip to content

Commit 6afba95

Browse files
ksw2000gaby
andauthored
🩹 Fix: sorting error in sortAcceptedTypes (#3331)
* 🩹 Fix: correct sorting error in sortAcceptedTypes. * ♻️ Refactor: remove redundant branch --------- Co-authored-by: Juan Calderon-Perez <[email protected]>
1 parent 86cf806 commit 6afba95

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

helpers.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head
483483

484484
if len(acceptedTypes) > 1 {
485485
// Sort accepted types by quality and specificity, preserving order of equal elements
486-
sortAcceptedTypes(&acceptedTypes)
486+
sortAcceptedTypes(acceptedTypes)
487487
}
488488

489489
// Find the first offer that matches the accepted types
@@ -511,19 +511,14 @@ func getOffer(header []byte, isAccepted func(spec, offer string, specParams head
511511
// A type with parameters has higher priority than an equivalent one without parameters.
512512
// e.g., text/html;a=1;b=2 comes before text/html;a=1
513513
// See: https://www.rfc-editor.org/rfc/rfc9110#name-content-negotiation-fields
514-
func sortAcceptedTypes(acceptedTypes *[]acceptedType) {
515-
if acceptedTypes == nil || len(*acceptedTypes) < 2 {
516-
return
517-
}
518-
at := *acceptedTypes
519-
514+
func sortAcceptedTypes(at []acceptedType) {
520515
for i := 1; i < len(at); i++ {
521516
lo, hi := 0, i-1
522517
for lo <= hi {
523518
mid := (lo + hi) / 2
524519
if at[i].quality < at[mid].quality ||
525520
(at[i].quality == at[mid].quality && at[i].specificity < at[mid].specificity) ||
526-
(at[i].quality == at[mid].quality && at[i].specificity < at[mid].specificity && len(at[i].params) < len(at[mid].params)) ||
521+
(at[i].quality == at[mid].quality && at[i].specificity == at[mid].specificity && len(at[i].params) < len(at[mid].params)) ||
527522
(at[i].quality == at[mid].quality && at[i].specificity == at[mid].specificity && len(at[i].params) == len(at[mid].params) && at[i].order > at[mid].order) {
528523
lo = mid + 1
529524
} else {

helpers_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,6 @@ func Test_Utils_SortAcceptedTypes(t *testing.T) {
354354
{spec: "text/html", quality: 1, specificity: 3, order: 0},
355355
{spec: "text/*", quality: 0.5, specificity: 2, order: 1},
356356
{spec: "*/*", quality: 0.1, specificity: 1, order: 2},
357-
{spec: "application/json", quality: 0.999, specificity: 3, order: 3},
358357
{spec: "application/xml", quality: 1, specificity: 3, order: 4},
359358
{spec: "application/pdf", quality: 1, specificity: 3, order: 5},
360359
{spec: "image/png", quality: 1, specificity: 3, order: 6},
@@ -363,8 +362,9 @@ func Test_Utils_SortAcceptedTypes(t *testing.T) {
363362
{spec: "image/gif", quality: 1, specificity: 3, order: 9},
364363
{spec: "text/plain", quality: 1, specificity: 3, order: 10},
365364
{spec: "application/json", quality: 0.999, specificity: 3, params: headerParams{"a": []byte("1")}, order: 11},
365+
{spec: "application/json", quality: 0.999, specificity: 3, order: 3},
366366
}
367-
sortAcceptedTypes(&acceptedTypes)
367+
sortAcceptedTypes(acceptedTypes)
368368
require.Equal(t, []acceptedType{
369369
{spec: "text/html", quality: 1, specificity: 3, order: 0},
370370
{spec: "application/xml", quality: 1, specificity: 3, order: 4},
@@ -390,7 +390,7 @@ func Benchmark_Utils_SortAcceptedTypes_Sorted(b *testing.B) {
390390
acceptedTypes[0] = acceptedType{spec: "text/html", quality: 1, specificity: 1, order: 0}
391391
acceptedTypes[1] = acceptedType{spec: "text/*", quality: 0.5, specificity: 1, order: 1}
392392
acceptedTypes[2] = acceptedType{spec: "*/*", quality: 0.1, specificity: 1, order: 2}
393-
sortAcceptedTypes(&acceptedTypes)
393+
sortAcceptedTypes(acceptedTypes)
394394
}
395395
require.Equal(b, "text/html", acceptedTypes[0].spec)
396396
require.Equal(b, "text/*", acceptedTypes[1].spec)
@@ -414,7 +414,7 @@ func Benchmark_Utils_SortAcceptedTypes_Unsorted(b *testing.B) {
414414
acceptedTypes[8] = acceptedType{spec: "image/*", quality: 1, specificity: 2, order: 8}
415415
acceptedTypes[9] = acceptedType{spec: "image/gif", quality: 1, specificity: 3, order: 9}
416416
acceptedTypes[10] = acceptedType{spec: "text/plain", quality: 1, specificity: 3, order: 10}
417-
sortAcceptedTypes(&acceptedTypes)
417+
sortAcceptedTypes(acceptedTypes)
418418
}
419419
require.Equal(b, []acceptedType{
420420
{spec: "text/html", quality: 1, specificity: 3, order: 0},

0 commit comments

Comments
 (0)