Skip to content
File

Blob: src/worker/dav/conditional.ts

typescript34 lines
1import { ifNoneMatchBlocks, strongEtagMatches } from "@/worker/dav/etag";
2import { ifHeaderMatchesStateAndEtags, type ParsedIfHeader } from "@/worker/dav/if-header";
3 
4export interface DavConditionalInput {
5 ifMatch: string | null;
6 ifNoneMatch: string | null;
7 ifHeader: ParsedIfHeader;
8 targetHref: string;
9 targetEtag: string | null;
10 getEtag: (href: string) => string | null;
11 getStateToken: (href: string) => string | null;
12}
13 
14export function davConditionalFailure(input: DavConditionalInput): string | null {
15 if (input.ifMatch && !strongEtagMatches(input.ifMatch, input.targetEtag)) {
16 return "If-Match precondition failed";
17 }
18 if (input.ifNoneMatch && input.targetEtag !== null && ifNoneMatchBlocks(input.ifNoneMatch, input.targetEtag)) {
19 return "If-None-Match precondition failed";
20 }
21 if (
22 !ifHeaderMatchesStateAndEtags({
23 header: input.ifHeader,
24 targetHref: input.targetHref,
25 targetEtag: input.targetEtag,
26 getEtag: input.getEtag,
27 getStateToken: input.getStateToken,
28 })
29 ) {
30 return "If precondition failed";
31 }
32 return null;
33}