Skip to content

Commit

Permalink
proppatch
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Jun 3, 2022
1 parent 723c49c commit 0f8b70e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 51 deletions.
71 changes: 22 additions & 49 deletions internal/http/services/owncloud/ocdav/proppatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import (
"github.com/cs3org/reva/v2/internal/http/services/owncloud/ocdav/spacelookup"
"github.com/cs3org/reva/v2/pkg/appctx"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
rstatus "github.com/cs3org/reva/v2/pkg/rgrpc/status"
rtrace "github.com/cs3org/reva/v2/pkg/trace"
"github.com/rs/zerolog"
)

func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns string) {
func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns string) (status int, err error) {
ctx, span := rtrace.Provider.Tracer("ocdav").Start(r.Context(), "proppatch")
defer span.End()

Expand All @@ -51,55 +53,35 @@ func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns str

pp, status, err := readProppatch(r.Body)
if err != nil {
sublog.Debug().Err(err).Msg("error reading proppatch")
w.WriteHeader(status)
m := fmt.Sprintf("Error reading proppatch: %v", err)
b, err := errors.Marshal(status, m, "")
errors.HandleWebdavError(&sublog, w, b, err)
return
return status, err
}

c, err := s.getClient()
if err != nil {
sublog.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}

space, rpcStatus, err := spacelookup.LookUpStorageSpaceForPath(ctx, c, fn)
if err != nil {
sublog.Error().Err(err).Str("path", fn).Msg("failed to look up storage space")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}
if rpcStatus.Code != rpc.Code_CODE_OK {
errors.HandleErrorStatus(&sublog, w, rpcStatus)
return
return rstatus.HTTPStatusFromCode(rpcStatus.Code), errtypes.NewErrtypeFromStatus(rpcStatus)
}
// check if resource exists
statReq := &provider.StatRequest{Ref: spacelookup.MakeRelativeReference(space, fn, false)}
statRes, err := c.Stat(ctx, statReq)
if err != nil {
sublog.Error().Err(err).Msg("error sending a grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}

if statRes.Status.Code != rpc.Code_CODE_OK {
if statRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
w.WriteHeader(http.StatusNotFound)
m := fmt.Sprintf("Resource %v not found", fn)
b, err := errors.Marshal(http.StatusNotFound, m, "")
errors.HandleWebdavError(&sublog, w, b, err)
}
errors.HandleErrorStatus(&sublog, w, statRes.Status)
return
return rstatus.HTTPStatusFromCode(rpcStatus.Code), errtypes.NewErrtypeFromStatus(rpcStatus)
}

acceptedProps, removedProps, ok := s.handleProppatch(ctx, w, r, spacelookup.MakeRelativeReference(space, fn, false), pp, sublog)
if !ok {
// handleProppatch handles responses in error cases so we can just return
return
// handleProppatch handles responses in error cases so return 0
return 0, nil
}

nRef := strings.TrimPrefix(fn, ns)
Expand All @@ -109,39 +91,32 @@ func (s *svc) handlePathProppatch(w http.ResponseWriter, r *http.Request, ns str
}

s.handleProppatchResponse(ctx, w, r, acceptedProps, removedProps, nRef, sublog)
return 0, nil
}

func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spaceID string) {
func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spaceID string) (status int, err error) {
ctx, span := rtrace.Provider.Tracer("ocdav").Start(r.Context(), "spaces_proppatch")
defer span.End()

sublog := appctx.GetLogger(ctx).With().Str("path", r.URL.Path).Str("spaceid", spaceID).Logger()

c, err := s.getClient()
if err != nil {
sublog.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}

pp, status, err := readProppatch(r.Body)
if err != nil {
sublog.Debug().Err(err).Msg("error reading proppatch")
w.WriteHeader(status)
return
return status, err
}

// retrieve a specific storage space
ref, rpcStatus, err := spacelookup.LookUpStorageSpaceReference(ctx, c, spaceID, r.URL.Path, true)
if err != nil {
sublog.Error().Err(err).Msg("error sending a grpc request")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}

if rpcStatus.Code != rpc.Code_CODE_OK {
errors.HandleErrorStatus(&sublog, w, rpcStatus)
return
return rstatus.HTTPStatusFromCode(rpcStatus.Code), errtypes.NewErrtypeFromStatus(rpcStatus)
}

// check if resource exists
Expand All @@ -150,20 +125,17 @@ func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spac
}
statRes, err := c.Stat(ctx, statReq)
if err != nil {
sublog.Error().Err(err).Msg("error sending a grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return
return http.StatusInternalServerError, err
}

if statRes.Status.Code != rpc.Code_CODE_OK {
errors.HandleErrorStatus(&sublog, w, statRes.Status)
return
return rstatus.HTTPStatusFromCode(rpcStatus.Code), errtypes.NewErrtypeFromStatus(rpcStatus)
}

acceptedProps, removedProps, ok := s.handleProppatch(ctx, w, r, ref, pp, sublog)
if !ok {
// handleProppatch handles responses in error cases so we can just return
return
// handleProppatch handles responses in error cases so return 0
return 0, nil
}

nRef := path.Join(spaceID, statRes.Info.Path)
Expand All @@ -173,6 +145,7 @@ func (s *svc) handleSpacesProppatch(w http.ResponseWriter, r *http.Request, spac
}

s.handleProppatchResponse(ctx, w, r, acceptedProps, removedProps, nRef, sublog)
return 0, nil
}

func (s *svc) handleProppatch(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, patches []Proppatch, log zerolog.Logger) ([]xml.Name, []xml.Name, bool) {
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (h *SpacesHandler) Handler(s *svc, trashbinHandler *TrashbinHandler) http.H
})
p.HandleSpacesPropfind(w, r, spaceID)
case MethodProppatch:
s.handleSpacesProppatch(w, r, spaceID)
status, err = s.handleSpacesProppatch(w, r, spaceID)
case MethodLock:
status, err = s.handleSpacesLock(w, r, spaceID)
case MethodUnlock:
Expand Down
2 changes: 1 addition & 1 deletion internal/http/services/owncloud/ocdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (h *WebDavHandler) Handler(s *svc) http.Handler {
case MethodUnlock:
status, err = s.handleUnlock(w, r, ns)
case MethodProppatch:
s.handlePathProppatch(w, r, ns)
status, err = s.handlePathProppatch(w, r, ns)
case MethodMkcol:
s.handlePathMkcol(w, r, ns)
case MethodMove:
Expand Down

0 comments on commit 0f8b70e

Please sign in to comment.