import { normalizeFilesPath, type NormalizedDavPath } from "@/worker/dav/paths"; export type DestinationResult = { ok: true; path: NormalizedDavPath } | { ok: false; status: number; message: string }; export function parseDestination(headerValue: string | null, requestUrl: URL): DestinationResult { if (!headerValue) return { ok: false, status: 400, message: "Missing Destination header" }; let destinationUrl: URL; try { destinationUrl = headerValue.startsWith("/") ? new URL(headerValue, requestUrl) : new URL(headerValue); } catch { return { ok: false, status: 400, message: "Invalid Destination header" }; } if (destinationUrl.host.toLowerCase() !== requestUrl.host.toLowerCase()) { return { ok: false, status: 502, message: "Cross-host COPY and MOVE are not supported" }; } const normalized = normalizeFilesPath(destinationUrl.pathname); if (!normalized.ok) return normalized; return { ok: true, path: normalized.path }; } export function overwriteAllowed(headerValue: string | null): boolean | null { if (headerValue === null || headerValue === "") return true; const value = headerValue.toUpperCase(); if (value === "T") return true; if (value === "F") return false; return null; }