Files
bewcloud/routes/carddav.tsx
Bruno Bernardino 47f443c300 Fix for Evolution CardDav/CalDav
They seem to make `GET` requests with `body`, which isn't allowed by the spec and causes Deno to fail. This prevents/ignores that.

It also makes the default `docker-compose.yml` "safer" by not exposing the database and container.

Finally, it removes a couple of unmaintained "one-click-deploy" buttons and simplifies documentation.
2025-08-28 14:57:51 +01:00

75 lines
2.1 KiB
TypeScript

import { Handler, RouteConfig } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { AppConfig } from '/lib/config.ts';
interface Data {}
export const config: RouteConfig = {
routeOverride: '/carddav/:path*',
};
export const handler: Handler<Data, FreshContextState> = async (request, context) => {
const contactsConfig = await AppConfig.getContactsConfig();
if (!contactsConfig.enableCardDavServer) {
return new Response('Not Found', { status: 404 });
}
if (!context.state.user) {
return new Response('Unauthorized', {
status: 401,
headers: { 'www-authenticate': 'Basic realm="bewCloud", charset="UTF-8"' },
});
}
const { path } = context.params;
const userId = context.state.user.id;
try {
const requestBodyText = await request.clone().text();
// Remove the `/carddav/` prefix from the hrefs in the request
let parsedRequestBodyText = requestBodyText.replaceAll('<href>/carddav/', `<href>/`).replaceAll(
':href>/carddav/',
`:href>/`,
);
// The spec doesn't allow a body for GET or HEAD requests (and Deno fails if you try)
if (request.method === 'GET' || request.method === 'HEAD') {
parsedRequestBodyText = '';
}
const response = await fetch(`${contactsConfig.cardDavUrl}/${path}`, {
headers: {
...Object.fromEntries(request.headers.entries()),
'X-Remote-User': `${userId}`,
},
method: request.method,
body: parsedRequestBodyText,
});
if (response.status === 204) {
return new Response(null, { status: 204 });
}
const responseBodyText = await response.clone().text();
// Add the `/carddav/` prefix to the hrefs in the response
const parsedBodyResponseText = responseBodyText.replaceAll('<href>/', `<href>/carddav/`).replaceAll(
':href>/',
`:href>/carddav/`,
);
return new Response(parsedBodyResponseText, {
status: response.status,
headers: response.headers,
});
} catch (error) {
console.error(error);
}
return new Response(null, { status: 405 });
};