Skip to content

Commit b3355e1

Browse files
committed
fix(gateway): honor Accept parameter ordering
1 parent c095255 commit b3355e1

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

‎src/gateway/http-media-range.ts‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ function parseMediaType(value: string, allowQuality: boolean): ParsedMediaType |
117117
if (!HTTP_TOKEN_PATTERN.test(name)) {
118118
return null;
119119
}
120-
// RFC 7231 used q as the boundary between media parameters and Accept extensions.
121-
// Keep accepting that deployed syntax, but extensions never affect representation matching.
122-
if (qualitySeen) {
123-
if (name === "q") {
124-
return null;
125-
}
126-
if (separator < 0) {
127-
continue;
128-
}
129-
const extensionValue = parameter.slice(separator + 1);
130-
if (!extensionValue || parseParameterValue(extensionValue) === null) {
131-
return null;
132-
}
133-
continue;
134-
}
135120
if (separator <= 0) {
136121
return null;
137122
}
@@ -142,6 +127,8 @@ function parseMediaType(value: string, allowQuality: boolean): ParsedMediaType |
142127
return null;
143128
}
144129
if (name === "q") {
130+
// RFC 9110 recognizes q as the weight regardless of parameter order;
131+
// every other parameter still participates in representation matching.
145132
if (!allowQuality || qualitySeen || !HTTP_QVALUE_PATTERN.test(rawValue)) {
146133
return null;
147134
}

‎src/gateway/sessions-history-http.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,16 @@ describe("session history Accept parsing", () => {
515515
expected: true,
516516
name: "more-specific matching parameter acceptance",
517517
},
518+
{
519+
accept: "text/event-stream;q=0.5;charset=utf-8",
520+
expected: true,
521+
name: "matching media parameter after q",
522+
},
523+
{
524+
accept: "text/event-stream;q=1;charset=utf-16",
525+
expected: false,
526+
name: "mismatched media parameter after q",
527+
},
518528
{
519529
accept: "text/event-stream; charset=utf-16",
520530
expected: false,
@@ -551,8 +561,8 @@ describe("session history Accept parsing", () => {
551561
{ accept: "text/event-stream;q=0.5;q=1", expected: false, name: "duplicate q parameter" },
552562
{
553563
accept: 'text/event-stream;q=0.5;legacy;note="quoted,comma;semicolon"',
554-
expected: true,
555-
name: "legacy Accept extensions after q",
564+
expected: false,
565+
name: "obsolete bare Accept extension after q",
556566
},
557567
{
558568
accept: 'text/event-stream; note="unterminated',
@@ -593,6 +603,8 @@ describe("session history HTTP endpoints", () => {
593603
accept: "text/event-stream;q=0, text/event-stream;charset=utf-8;q=0.5",
594604
expected: "sse",
595605
},
606+
{ accept: "text/event-stream;q=0.5;charset=utf-8", expected: "sse" },
607+
{ accept: "text/event-stream;q=1;charset=utf-16", expected: "json" },
596608
{ accept: "text/event-stream;charset=utf-16", expected: "json" },
597609
{ accept: "text/event-streaming", expected: "json" },
598610
{ accept: "text/event-streamx", expected: "json" },
@@ -607,7 +619,7 @@ describe("session history HTTP endpoints", () => {
607619
{ accept: "text/event-stream;\u00a0q=0.5", expected: "json" },
608620
{
609621
accept: 'text/event-stream;q=0.5;legacy;note="quoted,comma;semicolon"',
610-
expected: "sse",
622+
expected: "json",
611623
},
612624
] as const;
613625

0 commit comments

Comments
 (0)