From 7f0f9fd3657b566616a661ce9c0eb9c4485b247f Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Mon, 31 Oct 2022 12:24:32 +0100 Subject: [PATCH] Implement If-Match handling --- storage/filesystem.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/storage/filesystem.go b/storage/filesystem.go index ee46863..7da8ea7 100644 --- a/storage/filesystem.go +++ b/storage/filesystem.go @@ -10,6 +10,7 @@ import ( "io" "io/fs" "io/ioutil" + "net/http" "os" "path" "path/filepath" @@ -428,9 +429,19 @@ func (b *filesystemBackend) PutAddressObject(ctx context.Context, objPath string flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC if opts.IfNoneMatch { flags |= os.O_EXCL + } else if opts.IfMatch == "*" { + flags &= ^os.O_CREATE + } else if opts.IfMatch != "" { + etag, err := etagForFile(localPath) + if err != nil { + return "", webdav.NewHTTPError(http.StatusPreconditionFailed, err) + } + if opts.IfMatch != etag { + err = fmt.Errorf("If-Match does not match current ETag (%s/%s)", opts.IfMatch, etag) + return "", webdav.NewHTTPError(http.StatusPreconditionFailed, err) + } } - // TODO handle IfMatch f, err := os.OpenFile(localPath, flags, 0666) if os.IsExist(err) { return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict) @@ -608,9 +619,19 @@ func (b *filesystemBackend) PutCalendarObject(ctx context.Context, objPath strin flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC if opts.IfNoneMatch { flags |= os.O_EXCL + } else if opts.IfMatch == "*" { + flags &= ^os.O_CREATE + } else if opts.IfMatch != "" { + etag, err := etagForFile(localPath) + if err != nil { + return "", webdav.NewHTTPError(http.StatusPreconditionFailed, err) + } + if opts.IfMatch != etag { + err = fmt.Errorf("If-Match does not match current ETag (%s/%s)", opts.IfMatch, etag) + return "", webdav.NewHTTPError(http.StatusPreconditionFailed, err) + } } - // TODO handle IfMatch f, err := os.OpenFile(localPath, flags, 0666) if os.IsExist(err) { return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict)