Implement If-Match handling

This commit is contained in:
Conrad Hoffmann 2022-10-31 12:24:32 +01:00
parent 0719d5c32f
commit 7f0f9fd365

View File

@ -10,6 +10,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"net/http"
"os" "os"
"path" "path"
"path/filepath" "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 flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC
if opts.IfNoneMatch { if opts.IfNoneMatch {
flags |= os.O_EXCL 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) f, err := os.OpenFile(localPath, flags, 0666)
if os.IsExist(err) { if os.IsExist(err) {
return "", carddav.NewPreconditionError(carddav.PreconditionNoUIDConflict) 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 flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC
if opts.IfNoneMatch { if opts.IfNoneMatch {
flags |= os.O_EXCL 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) f, err := os.OpenFile(localPath, flags, 0666)
if os.IsExist(err) { if os.IsExist(err) {
return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict) return "", caldav.NewPreconditionError(caldav.PreconditionNoUIDConflict)