caldav, carddav: support If-Match/If-None-Match on client PUT
PutCalendarObject and PutAddressObject sent no conditional request headers, so a client could not perform an optimistic-concurrency write (overwrite only if the resource still carries an expected ETag). Add an options parameter to both, reusing the existing server-side PutCalendarObjectOptions / PutAddressObjectOptions types so the client and Backend interface mirror each other. A nil opts preserves the prior unconditional behaviour; a non-nil opts sets If-Match / If-None-Match. Signed-off-by: Henry Stern <henry@stern.ca> Assisted-by: Claude Opus 4.8 <noreply@anthropic.com> Closes: https://github.com/emersion/go-webdav/issues/130
This commit is contained in:
parent
bcd0dee048
commit
4ce863ae71
@ -432,8 +432,11 @@ func (c *Client) GetCalendarObject(ctx context.Context, path string) (*CalendarO
|
|||||||
return co, nil
|
return co, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.Calendar) (*CalendarObject, error) {
|
// PutCalendarObject stores cal as the calendar object resource at path.
|
||||||
// TODO: add support for If-None-Match and If-Match
|
func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.Calendar, opts *PutCalendarObjectOptions) (*CalendarObject, error) {
|
||||||
|
if opts == nil {
|
||||||
|
opts = new(PutCalendarObjectOptions)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: some servers want a Content-Length header, so we can't stream the
|
// TODO: some servers want a Content-Length header, so we can't stream the
|
||||||
// request body here. See the Radicale issue:
|
// request body here. See the Radicale issue:
|
||||||
@ -449,6 +452,12 @@ func (c *Client) PutCalendarObject(ctx context.Context, path string, cal *ical.C
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", ical.MIMEType)
|
req.Header.Set("Content-Type", ical.MIMEType)
|
||||||
|
if opts.IfMatch.IsSet() {
|
||||||
|
req.Header.Set("If-Match", string(opts.IfMatch))
|
||||||
|
}
|
||||||
|
if opts.IfNoneMatch.IsSet() {
|
||||||
|
req.Header.Set("If-None-Match", string(opts.IfNoneMatch))
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.ic.Do(req.WithContext(ctx))
|
resp, err := c.ic.Do(req.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
88
caldav/client_ifmatch_test.go
Normal file
88
caldav/client_ifmatch_test.go
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
package caldav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/emersion/go-ical"
|
||||||
|
|
||||||
|
"github.com/emersion/go-webdav"
|
||||||
|
"github.com/emersion/go-webdav/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
// minimalCalendar builds a single-VEVENT VCALENDAR valid enough to encode.
|
||||||
|
func minimalCalendar() *ical.Calendar {
|
||||||
|
cal := ical.NewCalendar()
|
||||||
|
cal.Props.SetText(ical.PropVersion, "2.0")
|
||||||
|
cal.Props.SetText(ical.PropProductID, "-//go-webdav//test//EN")
|
||||||
|
ev := ical.NewEvent()
|
||||||
|
ev.Props.SetText(ical.PropUID, "test-uid")
|
||||||
|
ev.Props.SetDateTime(ical.PropDateTimeStamp, time.Date(2026, 6, 24, 0, 0, 0, 0, time.UTC))
|
||||||
|
cal.Children = append(cal.Children, ev.Component)
|
||||||
|
return cal
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPutCalendarObjectSendsIfMatch verifies the client forwards the
|
||||||
|
// PutCalendarObjectOptions.IfMatch ETag as an If-Match request header and
|
||||||
|
// returns the server's new ETag.
|
||||||
|
func TestPutCalendarObjectSendsIfMatch(t *testing.T) {
|
||||||
|
var gotIfMatch string
|
||||||
|
var gotMethod string
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
gotMethod = r.Method
|
||||||
|
gotIfMatch = r.Header.Get("If-Match")
|
||||||
|
w.Header().Set("ETag", `"new-etag"`)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := &PutCalendarObjectOptions{IfMatch: webdav.ConditionalMatch(`"old-etag"`)}
|
||||||
|
co, err := c.PutCalendarObject(context.Background(), "/cal/test-uid.ics", minimalCalendar(), opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("PutCalendarObject: %v", err)
|
||||||
|
}
|
||||||
|
if gotMethod != http.MethodPut {
|
||||||
|
t.Errorf("method = %q, want PUT", gotMethod)
|
||||||
|
}
|
||||||
|
if gotIfMatch != `"old-etag"` {
|
||||||
|
t.Errorf("If-Match header = %q, want %q", gotIfMatch, `"old-etag"`)
|
||||||
|
}
|
||||||
|
if co.ETag != "new-etag" {
|
||||||
|
t.Errorf("returned ETag = %q, want %q", co.ETag, "new-etag")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPutCalendarObjectPreconditionFailed verifies a 412 response surfaces as
|
||||||
|
// an error whose status code consumers can read via webdav.HTTPErrorCode.
|
||||||
|
func TestPutCalendarObjectPreconditionFailed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusPreconditionFailed)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
opts := &PutCalendarObjectOptions{IfMatch: webdav.ConditionalMatch(`"stale"`)}
|
||||||
|
_, err = c.PutCalendarObject(context.Background(), "/cal/test-uid.ics", minimalCalendar(), opts)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("PutCalendarObject: want error on 412, got nil")
|
||||||
|
}
|
||||||
|
var httpErr *internal.HTTPError
|
||||||
|
if !errors.As(err, &httpErr) {
|
||||||
|
t.Fatalf("not an HTTP error: %v", err)
|
||||||
|
}
|
||||||
|
if httpErr.Code != http.StatusPreconditionFailed {
|
||||||
|
t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusPreconditionFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -384,8 +384,11 @@ func (c *Client) GetAddressObject(ctx context.Context, path string) (*AddressObj
|
|||||||
return ao, nil
|
return ao, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.Card) (*AddressObject, error) {
|
// PutAddressObject stores card as the address object resource at path.
|
||||||
// TODO: add support for If-None-Match and If-Match
|
func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.Card, opts *PutAddressObjectOptions) (*AddressObject, error) {
|
||||||
|
if opts == nil {
|
||||||
|
opts = new(PutAddressObjectOptions)
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: some servers want a Content-Length header, so we can't stream the
|
// TODO: some servers want a Content-Length header, so we can't stream the
|
||||||
// request body here. See the Radicale issue:
|
// request body here. See the Radicale issue:
|
||||||
@ -408,6 +411,12 @@ func (c *Client) PutAddressObject(ctx context.Context, path string, card vcard.C
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", vcard.MIMEType)
|
req.Header.Set("Content-Type", vcard.MIMEType)
|
||||||
|
if opts.IfMatch.IsSet() {
|
||||||
|
req.Header.Set("If-Match", string(opts.IfMatch))
|
||||||
|
}
|
||||||
|
if opts.IfNoneMatch.IsSet() {
|
||||||
|
req.Header.Set("If-None-Match", string(opts.IfNoneMatch))
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.ic.Do(req.WithContext(ctx))
|
resp, err := c.ic.Do(req.WithContext(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
81
carddav/client_ifmatch_test.go
Normal file
81
carddav/client_ifmatch_test.go
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
package carddav
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/emersion/go-vcard"
|
||||||
|
|
||||||
|
"github.com/emersion/go-webdav"
|
||||||
|
"github.com/emersion/go-webdav/internal"
|
||||||
|
)
|
||||||
|
|
||||||
|
func minimalCard() vcard.Card {
|
||||||
|
card := make(vcard.Card)
|
||||||
|
card.SetValue(vcard.FieldFormattedName, "Alice Gopher")
|
||||||
|
card.SetValue(vcard.FieldUID, "test-uid")
|
||||||
|
vcard.ToV4(card)
|
||||||
|
return card
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPutAddressObjectSendsIfMatch verifies the client forwards
|
||||||
|
// PutAddressObjectOptions.IfMatch as an If-Match request header and returns the
|
||||||
|
// server's new ETag.
|
||||||
|
func TestPutAddressObjectSendsIfMatch(t *testing.T) {
|
||||||
|
var gotIfMatch, gotMethod string
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
gotMethod = r.Method
|
||||||
|
gotIfMatch = r.Header.Get("If-Match")
|
||||||
|
w.Header().Set("ETag", `"new-etag"`)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
opts := &PutAddressObjectOptions{IfMatch: webdav.ConditionalMatch(`"old-etag"`)}
|
||||||
|
ao, err := c.PutAddressObject(context.Background(), "/contacts/test-uid.vcf", minimalCard(), opts)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("PutAddressObject: %v", err)
|
||||||
|
}
|
||||||
|
if gotMethod != http.MethodPut {
|
||||||
|
t.Errorf("method = %q, want PUT", gotMethod)
|
||||||
|
}
|
||||||
|
if gotIfMatch != `"old-etag"` {
|
||||||
|
t.Errorf("If-Match header = %q, want %q", gotIfMatch, `"old-etag"`)
|
||||||
|
}
|
||||||
|
if ao.ETag != "new-etag" {
|
||||||
|
t.Errorf("returned ETag = %q, want %q", ao.ETag, "new-etag")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPutAddressObjectPreconditionFailed verifies a 412 response surfaces as an
|
||||||
|
// error readable via webdav.HTTPErrorCode.
|
||||||
|
func TestPutAddressObjectPreconditionFailed(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusPreconditionFailed)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c, err := NewClient(http.DefaultClient, srv.URL)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewClient: %v", err)
|
||||||
|
}
|
||||||
|
opts := &PutAddressObjectOptions{IfMatch: webdav.ConditionalMatch(`"stale"`)}
|
||||||
|
_, err = c.PutAddressObject(context.Background(), "/contacts/test-uid.vcf", minimalCard(), opts)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("PutAddressObject: want error on 412, got nil")
|
||||||
|
}
|
||||||
|
var httpErr *internal.HTTPError
|
||||||
|
if !errors.As(err, &httpErr) {
|
||||||
|
t.Fatalf("not an HTTP error: %v", err)
|
||||||
|
}
|
||||||
|
if httpErr.Code != http.StatusPreconditionFailed {
|
||||||
|
t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusPreconditionFailed)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user