This commit is contained in:
Lennart J. Kurzweg (Nx2)
2026-03-23 22:06:12 +01:00
parent d55fff9f4a
commit b5a3a63dde
6 changed files with 80 additions and 112 deletions

57
main.go
View File

@@ -4,7 +4,9 @@ import (
"context"
"fmt"
"log"
"os"
"net/http"
"net/url"
"strings"
"time"
@@ -14,7 +16,13 @@ import (
)
func main() {
cfg, err := config.Load("config.yaml")
path := "config.yaml";
if len(os.Args) != 0 {
if os.Args[1] == "-c" {
path = os.Args[2]
}
}
cfg, err := config.Load()
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
@@ -27,8 +35,34 @@ func main() {
handler := &caldav.Handler{Backend: be}
publicURL, _ := url.Parse(cfg.Server.PublicURL)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/public/") {
// Proxy-aware normalization:
if publicURL != nil && publicURL.Host != "" {
r.Host = publicURL.Host
r.URL.Host = publicURL.Host
// Detect scheme: prioritize X-Forwarded-Proto, then PublicURL
scheme := publicURL.Scheme
if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" {
scheme = proto
}
r.URL.Scheme = scheme
// Also rewrite WebDAV Destination header (used for MOVE/COPY)
if dest := r.Header.Get("Destination"); dest != "" {
destURL, err := url.Parse(dest)
if err == nil {
destURL.Host = publicURL.Host
destURL.Scheme = scheme
r.Header.Set("Destination", destURL.String())
}
}
}
prefix := cfg.Server.BasePath()
if strings.HasPrefix(r.URL.Path, prefix+"/public/") {
be.ServePublicICS(w, r)
return
}
@@ -55,15 +89,26 @@ func main() {
}
log.Printf("%s %s (user: %s)", r.Method, r.URL.Path, user)
principalPath := fmt.Sprintf("/%s/", user)
prefix = cfg.Server.BasePath()
principalPath := prefix + fmt.Sprintf("/%s/", user)
ctx := context.WithValue(r.Context(), "principal", principalPath)
if r.URL.Path == "/.well-known/caldav" {
http.Redirect(w, r, principalPath, http.StatusMovedPermanently)
if r.URL.Path == "/.well-known/caldav" || r.URL.Path == prefix+"/.well-known/caldav" {
// If we normalized the request, use the normalized host/scheme for the 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
}
handler.ServeHTTP(w, r.WithContext(ctx))
})