11// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
22// This module is browser compatible.
33
4- import {
5- ETag ,
6- isString ,
7- isValidDate ,
8- parseETag ,
9- parseHttpDate ,
10- RepresentationHeader ,
11- } from "../deps.ts" ;
4+ import { isString , isValidDate , parseETag , parseHttpDate } from "../deps.ts" ;
125import { parse } from "../if_match.ts" ;
6+ import { matchStrong , matchWeak } from "../etag.ts" ;
7+
8+ const enum Msg {
9+ InvalidField = "field value is invalid <HTTP-date> format." ,
10+ InvalidLastModified = "last-modified is invalid <HTTP-date> format." ,
11+ }
1312
1413/** Match `If-Match` field and `ETag` field.
1514 * @throws {SyntaxError } If the input is invalid syntax.
@@ -38,37 +37,23 @@ export function ifNoneMatch(fieldValue: string, etag: string): boolean {
3837 return ifNoneMatch . every ( ( tag ) => ! matchWeak ( tag , etagObj ) ) ;
3938}
4039
41- export function matchWeak ( left : ETag , right : ETag ) : boolean {
42- return left . tag === right . tag ;
43- }
44-
45- export function matchStrong ( left : ETag , right : ETag ) : boolean {
46- return isStrong ( left ) && isStrong ( right ) && left . tag === right . tag ;
47- }
48-
49- export function isStrong ( etag : ETag ) : boolean {
50- return ! etag . weak ;
51- }
52-
5340/**
5441 * @throws {SyntaxError } If the input is invalid.
5542 */
5643export function ifModifiedSince (
5744 fieldValue : string ,
5845 lastModified : string ,
5946) : boolean {
60- // A recipient MUST ignore the If-Modified-Since header field if the
61- // received field-value is not a valid HTTP-date
6247 const date = parseHttpDate ( fieldValue . trim ( ) ) ;
6348
6449 if ( ! isValidDate ( date ) ) {
65- throw TypeError ( "field value is invalid <HTTP-date> format." ) ;
50+ throw TypeError ( Msg . InvalidField ) ;
6651 }
6752
6853 const lastMod = parseHttpDate ( lastModified . trim ( ) ) ;
6954
7055 if ( ! isValidDate ( lastMod ) ) {
71- throw TypeError ( "last-modified is invalid <HTTP-date> format." ) ;
56+ throw TypeError ( Msg . InvalidLastModified ) ;
7257 }
7358
7459 // The origin server SHOULD NOT perform the requested
@@ -84,18 +69,16 @@ export function ifUnmodifiedSince(
8469 fieldValue : string ,
8570 lastModified : string ,
8671) : boolean {
87- // A recipient MUST ignore the If-Modified-Since header field if the
88- // received field-value is not a valid HTTP-date
8972 const date = parseHttpDate ( fieldValue . trim ( ) ) ;
9073
9174 if ( ! isValidDate ( date ) ) {
92- throw TypeError ( "field value is invalid <HTTP-date> format." ) ;
75+ throw TypeError ( Msg . InvalidField ) ;
9376 }
9477
9578 const lastMod = parseHttpDate ( lastModified . trim ( ) ) ;
9679
9780 if ( ! isValidDate ( lastMod ) ) {
98- throw TypeError ( "last-modified is invalid <HTTP-date> format." ) ;
81+ throw TypeError ( Msg . InvalidLastModified ) ;
9982 }
10083
10184 // The origin server MUST NOT perform the requested method
@@ -128,13 +111,13 @@ export function ifRange(fieldValue: string, headers: IfRangeHeaders): boolean {
128111 const left = parseHttpDate ( fieldValue ) ;
129112
130113 if ( ! isValidDate ( left ) ) {
131- throw TypeError ( "field value is invalid <HTTP-date> format." ) ;
114+ throw TypeError ( Msg . InvalidField ) ;
132115 }
133116
134117 const right = parseHttpDate ( lastModified ) ;
135118
136119 if ( ! isValidDate ( right ) ) {
137- throw TypeError ( "last-modified is invalid <HTTP-date> format." ) ;
120+ throw TypeError ( Msg . InvalidLastModified ) ;
138121 }
139122
140123 return left . getTime ( ) === right . getTime ( ) ;
@@ -149,13 +132,4 @@ function isStar(input: unknown): input is Star {
149132 return input === "*" ;
150133}
151134
152- export function isBannedHeader ( fieldName : string ) : boolean {
153- return ( [
154- RepresentationHeader . ContentEncoding ,
155- RepresentationHeader . ContentLanguage ,
156- RepresentationHeader . ContentLength ,
157- RepresentationHeader . ContentType ,
158- ] as string [ ] ) . includes ( fieldName ) ;
159- }
160-
161135export type Star = "*" ;
0 commit comments