From 03633121d978d6a3476e6d46ccb45c508c65334d Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Tue, 31 May 2022 12:44:03 +0200 Subject: [PATCH] client: support redirects in PropfindFlat() One common method for CalDAV or CardDAV clients to find the current user principal URL is to request the `/.well-known` URL (see [RFC 6764, section 6][1]), expecting a redirect. Such URL is for example a valid result of the discovery phase described in that RFC. The expectation is that a client, given such URL, is able to find the principal URL by following a redirect when sending a PROPFIND request. This change makes `PropfindFlat()` (and, by extension, `FindCurrentUserPrincipal()`) handle such a redirect and correctly return the requested properties, even if their HREF is different from the original request path. [1]: https://datatracker.ietf.org/doc/html/rfc6764#section-6 --- internal/client.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/client.go b/internal/client.go index 3b48ecf..7c7a2e2 100644 --- a/internal/client.go +++ b/internal/client.go @@ -149,7 +149,11 @@ func (c *Client) PropfindFlat(path string, propfind *Propfind) (*Response, error return nil, err } - return ms.Get(c.ResolveHref(path).Path) + // If the client followed a redirect, the Href might be different from the request path + if len(ms.Responses) != 1 { + return nil, fmt.Errorf("PROPFIND with Depth: 0 returned %d responses", len(ms.Responses)) + } + return &ms.Responses[0], nil } func parseCommaSeparatedSet(values []string, upper bool) map[string]bool {