cardav working (tm)

This commit is contained in:
Lennart J. Kurzweg (Nx2)
2026-03-31 02:08:39 +02:00
parent ce78c6e07f
commit e0796a071b
7 changed files with 503 additions and 52 deletions

102
main.go
View File

@@ -1,17 +1,20 @@
package main
import (
"context"
"fmt"
"log"
"os"
"context"
"fmt"
"log"
"os"
"net/http"
"net/url"
"strings"
"time"
"github.com/emersion/go-webdav/caldav"
"github.com/emersion/go-webdav/carddav"
"nxcaldav/internal/backend"
"nxcaldav/internal/extra"
"nxcaldav/internal/config"
)
@@ -36,8 +39,8 @@ func main() {
log.Fatalf("failed to initialize database backend: %v", err)
}
handler := &caldav.Handler{Backend: be}
caldavHandler := &caldav.Handler{Backend: be}
carddavHandler := &carddav.Handler{Backend: be}
publicURL, _ := url.Parse(cfg.Server.PublicURL)
http.HandleFunc("/respond", func(w http.ResponseWriter, r *http.Request) {
@@ -114,35 +117,72 @@ func main() {
return
}
log.Printf("[user: %s] %s %s ", user, r.Method, r.URL.Path)
principalPath := prefix + fmt.Sprintf("/%s/", user)
ctx := context.WithValue(r.Context(), "principal", principalPath)
log.Printf("[user: %s] %s %s", user, r.Method, r.URL.Path)
if r.URL.Path == "/.well-known/caldav" || r.URL.Path == prefix+"/.well-known/caldav" {
// If normalized request, use normalized host/scheme for redirect
if publicURL != nil && publicURL.Host != "" {
scheme := r.URL.Scheme
if scheme == "" {
scheme = "http"
}
target := fmt.Sprintf("%s://%s%s", scheme, r.Host, principalPath)
http.Redirect(w, r, target, http.StatusMovedPermanently)
} else {
http.Redirect(w, r, principalPath, http.StatusMovedPermanently)
}
return
}
principalPath := prefix + fmt.Sprintf("/%s/", user)
// Add addressbook to DAV header for discovery/access paths
// This includes principal path and .well-known
isCardDAVPath := strings.Contains(r.URL.Path, "/addressbooks/") ||
r.URL.Path == "/.well-known/carddav" ||
r.URL.Path == prefix+"/.well-known/carddav" ||
r.URL.Path == principalPath ||
r.URL.Path == strings.TrimSuffix(principalPath, "/")
if isCardDAVPath {
w.Header().Add("DAV", "addressbook")
}
// needed because color info is not RFC, so regex hack
if r.Method == "PROPFIND" {
extra.AddColorToCalendarPropfind(r, ctx, handler, w, be)
} else {
handler.ServeHTTP(w, r.WithContext(ctx))
}
})
ctx := context.WithValue(r.Context(), "principal", principalPath)
fmt.Printf("Starting CalDAV server on %s...\n", cfg.Server.BindAddress)
if r.URL.Path == "/.well-known/caldav" || r.URL.Path == prefix+"/.well-known/caldav" ||
r.URL.Path == "/.well-known/carddav" || r.URL.Path == prefix+"/.well-known/carddav" {
// If normalized request, use normalized host/scheme for redirect
if publicURL != nil && publicURL.Host != "" {
scheme := r.URL.Scheme
if scheme == "" {
scheme = "http"
}
target := fmt.Sprintf("%s://%s%s", scheme, r.Host, principalPath)
http.Redirect(w, r, target, http.StatusMovedPermanently)
} else {
http.Redirect(w, r, principalPath, http.StatusMovedPermanently)
}
return
}
// needed because color info is not RFC, so regex hack
if strings.Contains(r.URL.Path, "/calendars/") {
if r.Method == "PROPFIND" {
extra.AddColorToCalendarPropfind(r, ctx, caldavHandler, w, be)
} else {
caldavHandler.ServeHTTP(w, r.WithContext(ctx))
}
} else if strings.Contains(r.URL.Path, "/addressbooks/") {
carddavHandler.ServeHTTP(w, r.WithContext(ctx))
} else {
// Fallback: try both or default to caldav for principal path etc.
if strings.HasSuffix(r.URL.Path, user+"/") || strings.HasSuffix(r.URL.Path, user) {
// For principal path, use merged discovery handler
if r.Method == "PROPFIND" {
extra.HandleDiscoveryPropfind(r, ctx, caldavHandler, w, be)
} else {
caldavHandler.ServeHTTP(w, r.WithContext(ctx))
// Ensure DAV header includes addressbook for OPTIONS
if r.Method == "OPTIONS" {
dav := w.Header().Get("DAV")
if dav != "" && !strings.Contains(dav, "addressbook") {
w.Header().Set("DAV", dav+", addressbook")
}
}
}
} else {
http.NotFound(w, r)
}
}
})
fmt.Printf("Starting CalDAV/CardDAV server on %s...\n", cfg.Server.BindAddress)
server := &http.Server{
Addr: cfg.Server.BindAddress,
ReadTimeout: 30 * time.Second,