Skip to content
File

Blob: src/worker/dav/destination.ts

typescript31 lines
1import { normalizeFilesPath, type NormalizedDavPath } from "@/worker/dav/paths";
2 
3export type DestinationResult = { ok: true; path: NormalizedDavPath } | { ok: false; status: number; message: string };
4 
5export function parseDestination(headerValue: string | null, requestUrl: URL): DestinationResult {
6 if (!headerValue) return { ok: false, status: 400, message: "Missing Destination header" };
7 
8 let destinationUrl: URL;
9 try {
10 destinationUrl = headerValue.startsWith("/") ? new URL(headerValue, requestUrl) : new URL(headerValue);
11 } catch {
12 return { ok: false, status: 400, message: "Invalid Destination header" };
13 }
14 
15 if (destinationUrl.host.toLowerCase() !== requestUrl.host.toLowerCase()) {
16 return { ok: false, status: 502, message: "Cross-host COPY and MOVE are not supported" };
17 }
18 
19 const normalized = normalizeFilesPath(destinationUrl.pathname);
20 if (!normalized.ok) return normalized;
21 return { ok: true, path: normalized.path };
22}
23 
24export function overwriteAllowed(headerValue: string | null): boolean | null {
25 if (headerValue === null || headerValue === "") return true;
26 const value = headerValue.toUpperCase();
27 if (value === "T") return true;
28 if (value === "F") return false;
29 return null;
30}