Skip to content

Commit 90a7a37

Browse files
committed
fix(if_match): change if-match header parser logic
ignore empty element
1 parent 7d1d34b commit 90a7a37

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

deps.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export { isString } from "https://deno.land/x/isx@1.1.1/is_string.ts";
1313
export { isNumber } from "https://deno.land/x/isx@1.1.1/is_number.ts";
1414
export { isNegativeNumber } from "https://deno.land/x/isx@1.1.1/number/is_negative_number.ts";
1515
export { isValidDate } from "https://deno.land/x/isx@1.1.1/date/is_valid_date.ts";
16+
export { trim } from "https://deno.land/x/prelude_js@1.0.0/trim.ts";
1617
export {
1718
type Handler,
1819
type Middleware,

if_match.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// TODO(miyauci): External Packaging
55

6-
import { type ETag, parseETag } from "./deps.ts";
6+
import { type ETag, parseETag, trim } from "./deps.ts";
77

88
export type IfMatch = Star | ETag[];
99
export type IfNoneMatch = IfMatch;
@@ -13,12 +13,11 @@ export type Star = "*";
1313
* @throws {SyntaxError} If the input is invalid.
1414
*/
1515
export function parse(input: string): IfMatch | IfNoneMatch {
16-
input = input.trim();
17-
1816
if (input === "*") return input;
1917

2018
return input
2119
.split(",")
20+
.map(trim)
2221
.filter(Boolean)
2322
.map(parseETag);
2423
}

if_match_test.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ describe("parse", () => {
66
it("should return star", () => {
77
const table: string[] = [
88
"*",
9-
" *",
10-
"* ",
11-
" * ",
129
];
1310

1411
table.forEach((input) => {
@@ -20,6 +17,8 @@ describe("parse", () => {
2017
const table: string[] = [
2118
"",
2219
" ",
20+
" , ",
21+
", , ,",
2322
];
2423

2524
table.forEach((input) => {
@@ -62,6 +61,10 @@ describe("parse", () => {
6261
`* *`,
6362
`**`,
6463
`"", *`,
64+
" *",
65+
"* ",
66+
" * ",
67+
"a,",
6568
];
6669

6770
table.forEach((input) => {

preconditions/utils.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ export function ifModifiedSince(
5050
fieldValue: string,
5151
lastModified: string,
5252
): boolean {
53-
const date = parseHttpDate(fieldValue.trim());
53+
const date = parseHttpDate(fieldValue);
5454

5555
if (!isValidDate(date)) {
5656
throw SyntaxError(Msg.InvalidField);
5757
}
5858

59-
const lastMod = parseHttpDate(lastModified.trim());
59+
const lastMod = parseHttpDate(lastModified);
6060

6161
if (!isValidDate(lastMod)) {
6262
throw SyntaxError(Msg.InvalidLastModified);
@@ -75,13 +75,13 @@ export function ifUnmodifiedSince(
7575
fieldValue: string,
7676
lastModified: string,
7777
): boolean {
78-
const date = parseHttpDate(fieldValue.trim());
78+
const date = parseHttpDate(fieldValue);
7979

8080
if (!isValidDate(date)) {
8181
throw SyntaxError(Msg.InvalidField);
8282
}
8383

84-
const lastMod = parseHttpDate(lastModified.trim());
84+
const lastMod = parseHttpDate(lastModified);
8585

8686
if (!isValidDate(lastMod)) {
8787
throw SyntaxError(Msg.InvalidLastModified);

0 commit comments

Comments
 (0)