-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmkcol.go
113 lines (101 loc) · 3.31 KB
/
mkcol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package ocdav
import (
"context"
"fmt"
"net/http"
"path"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/rs/zerolog"
"go.opencensus.io/trace"
)
func (s *svc) handlePathMkcol(w http.ResponseWriter, r *http.Request, ns string) {
ctx := r.Context()
ctx, span := trace.StartSpan(ctx, "mkcol")
defer span.End()
fn := path.Join(ns, r.URL.Path)
for _, r := range nameRules {
if !r.Test(fn) {
w.WriteHeader(http.StatusBadRequest)
return
}
}
sublog := appctx.GetLogger(ctx).With().Str("path", fn).Logger()
ref := &provider.Reference{Path: fn}
s.handleMkcol(ctx, w, r, ref, sublog)
}
func (s *svc) handleMkcol(ctx context.Context, w http.ResponseWriter, r *http.Request, ref *provider.Reference, log zerolog.Logger) {
if r.Body != http.NoBody {
w.WriteHeader(http.StatusUnsupportedMediaType)
return
}
client, err := s.getClient()
if err != nil {
log.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
}
// check fn exists
statReq := &provider.StatRequest{Ref: ref}
statRes, err := client.Stat(ctx, statReq)
if err != nil {
log.Error().Err(err).Msg("error sending a grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return
}
if statRes.Status.Code != rpc.Code_CODE_NOT_FOUND {
if statRes.Status.Code == rpc.Code_CODE_OK {
w.WriteHeader(http.StatusMethodNotAllowed) // 405 if it already exists
b, err := Marshal(exception{
code: SabredavMethodNotAllowed,
message: "The resource you tried to create already exists",
})
HandleWebdavError(&log, w, b, err)
} else {
HandleErrorStatus(&log, w, statRes.Status)
}
return
}
req := &provider.CreateContainerRequest{Ref: ref}
res, err := client.CreateContainer(ctx, req)
if err != nil {
log.Error().Err(err).Msg("error sending create container grpc request")
w.WriteHeader(http.StatusInternalServerError)
return
}
switch res.Status.Code {
case rpc.Code_CODE_OK:
w.WriteHeader(http.StatusCreated)
case rpc.Code_CODE_NOT_FOUND:
log.Debug().Str("path", ref.Path).Interface("status", statRes.Status).Msg("conflict")
w.WriteHeader(http.StatusConflict)
case rpc.Code_CODE_PERMISSION_DENIED:
w.WriteHeader(http.StatusForbidden)
m := fmt.Sprintf("Permission denied to create %v", ref.Path)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: m,
})
HandleWebdavError(&log, w, b, err)
default:
HandleErrorStatus(&log, w, res.Status)
}
}