Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webdav: add support for If-Match/If-None-Match in DELETE #175

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions fs_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,33 +114,43 @@ func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bo
return l, errFromOS(err)
}

func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fi *FileInfo, created bool, err error) {
p, err := fs.localPath(name)
if err != nil {
return nil, false, err
}
fi, _ = fs.Stat(ctx, name)
created = fi == nil
func checkConditionalMatches(fi *FileInfo, ifMatch, ifNoneMatch ConditionalMatch) error {
etag := ""
if fi != nil {
etag = fi.ETag
}

if opts.IfMatch.IsSet() {
if ok, err := opts.IfMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)
if ifMatch.IsSet() {
if ok, err := ifMatch.MatchETag(etag); err != nil {
return NewHTTPError(http.StatusBadRequest, err)
} else if !ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-Match condition failed"))
return NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-Match condition failed"))
}
}
if opts.IfNoneMatch.IsSet() {
if ok, err := opts.IfNoneMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)

if ifNoneMatch.IsSet() {
if ok, err := ifNoneMatch.MatchETag(etag); err != nil {
return NewHTTPError(http.StatusBadRequest, err)
} else if ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-None-Match condition failed"))
return NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-None-Match condition failed"))
}
}

return nil
}

func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fi *FileInfo, created bool, err error) {
p, err := fs.localPath(name)
if err != nil {
return nil, false, err
}
fi, _ = fs.Stat(ctx, name)
created = fi == nil

if err := checkConditionalMatches(fi, opts.IfMatch, opts.IfNoneMatch); err != nil {
return nil, false, err
}

wc, err := os.Create(p)
if err != nil {
return nil, false, errFromOS(err)
Expand All @@ -164,18 +174,23 @@ func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadC
return fi, created, err
}

func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error {
func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string, opts *RemoveAllOptions) error {
p, err := fs.localPath(name)
if err != nil {
return err
}

// WebDAV semantics are that it should return a "404 Not Found" error in
// case the resource doesn't exist. We need to Stat before RemoveAll.
if _, err = os.Stat(p); err != nil {
fi, err := fs.Stat(ctx, name)
if err != nil {
return errFromOS(err)
}

if err := checkConditionalMatches(fi, opts.IfMatch, opts.IfNoneMatch); err != nil {
return err
}

return errFromOS(os.RemoveAll(p))
}

Expand Down
11 changes: 9 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type FileSystem interface {
Stat(ctx context.Context, name string) (*FileInfo, error)
ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error)
Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fileInfo *FileInfo, created bool, err error)
RemoveAll(ctx context.Context, name string) error
RemoveAll(ctx context.Context, name string, opts *RemoveAllOptions) error
Mkdir(ctx context.Context, name string) error
Copy(ctx context.Context, name, dest string, options *CopyOptions) (created bool, err error)
Move(ctx context.Context, name, dest string, options *MoveOptions) (created bool, err error)
Expand Down Expand Up @@ -226,7 +226,14 @@ func (b *backend) Put(w http.ResponseWriter, r *http.Request) error {
}

func (b *backend) Delete(r *http.Request) error {
return b.FileSystem.RemoveAll(r.Context(), r.URL.Path)
ifNoneMatch := ConditionalMatch(r.Header.Get("If-None-Match"))
ifMatch := ConditionalMatch(r.Header.Get("If-Match"))

opts := RemoveAllOptions{
IfNoneMatch: ifNoneMatch,
IfMatch: ifMatch,
}
return b.FileSystem.RemoveAll(r.Context(), r.URL.Path, &opts)
}

func (b *backend) Mkcol(r *http.Request) error {
Expand Down
5 changes: 5 additions & 0 deletions webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ type CreateOptions struct {
IfNoneMatch ConditionalMatch
}

type RemoveAllOptions struct {
IfMatch ConditionalMatch
IfNoneMatch ConditionalMatch
}

type CopyOptions struct {
NoRecursive bool
NoOverwrite bool
Expand Down