-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathcopy.go
641 lines (562 loc) · 18.5 KB
/
copy.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// Copyright 2018-2024 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"
"strconv"
"strings"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
"github.com/cs3org/reva/internal/http/services/datagateway"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/rhttp/router"
"github.com/cs3org/reva/pkg/utils"
"github.com/rs/zerolog"
)
type copy struct {
sourceInfo *provider.ResourceInfo
destination *provider.Reference
depth string
successCode int
}
type intermediateDirRefFunc func() (*provider.Reference, *rpc.Status, error)
func (s *svc) handlePathCopy(w http.ResponseWriter, r *http.Request, ns string) {
ctx := r.Context()
if s.c.EnableHTTPTpc {
if r.Header.Get("Source") != "" {
// HTTP Third-Party Copy Pull mode
s.handleTPCPull(ctx, w, r, ns)
return
} else if r.Header.Get("Destination") != "" {
// HTTP Third-Party Copy Push mode
s.handleTPCPush(ctx, w, r, ns)
return
}
}
// Local copy: in this case Destination is mandatory
src := path.Join(ns, r.URL.Path)
dst, err := extractDestination(r)
if err != nil {
appctx.GetLogger(ctx).Warn().Msg("HTTP COPY: failed to extract destination")
w.WriteHeader(http.StatusBadRequest)
return
}
for _, r := range nameRules {
if !r.Test(dst) {
appctx.GetLogger(ctx).Warn().Msgf("HTTP COPY: destination %s failed validation", dst)
w.WriteHeader(http.StatusBadRequest)
return
}
}
dst = path.Join(ns, dst)
sublog := appctx.GetLogger(ctx).With().Str("src", src).Str("dst", dst).Logger()
srcRef := &provider.Reference{Path: src}
// check dst exists
dstRef := &provider.Reference{Path: dst}
intermediateDirRefFunc := func() (*provider.Reference, *rpc.Status, error) {
intermediateDir := path.Dir(dst)
ref := &provider.Reference{Path: intermediateDir}
return ref, &rpc.Status{Code: rpc.Code_CODE_OK}, nil
}
cp := s.prepareCopy(ctx, w, r, srcRef, dstRef, intermediateDirRefFunc, &sublog)
if cp == nil {
return
}
client, err := s.getClient()
if err != nil {
sublog.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := s.executePathCopy(ctx, client, w, r, cp); err != nil {
sublog.Error().Err(err).Str("depth", cp.depth).Msg("error executing path copy")
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(cp.successCode)
}
func (s *svc) executePathCopy(ctx context.Context, client gateway.GatewayAPIClient, w http.ResponseWriter, r *http.Request, cp *copy) error {
log := appctx.GetLogger(ctx)
log.Debug().Str("src", cp.sourceInfo.Path).Str("dst", cp.destination.Path).Msg("descending")
if cp.sourceInfo.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
// create dir
createReq := &provider.CreateContainerRequest{
Ref: cp.destination,
}
createRes, err := client.CreateContainer(ctx, createReq)
if err != nil {
log.Error().Err(err).Msg("error performing create container grpc request")
return err
}
if createRes.Status.Code != rpc.Code_CODE_OK {
if createRes.Status.Code == rpc.Code_CODE_PERMISSION_DENIED {
w.WriteHeader(http.StatusForbidden)
m := fmt.Sprintf("Permission denied to create %v", createReq.Ref.Path)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: m,
})
HandleWebdavError(log, w, b, err)
}
return nil
}
// TODO: also copy properties: https://tools.ietf.org/html/rfc4918#section-9.8.2
if cp.depth != "infinity" {
return nil
}
// descend for children
listReq := &provider.ListContainerRequest{
Ref: &provider.Reference{Path: cp.sourceInfo.Path},
}
res, err := client.ListContainer(ctx, listReq)
if err != nil {
return err
}
if res.Status.Code != rpc.Code_CODE_OK {
w.WriteHeader(http.StatusInternalServerError)
return nil
}
for i := range res.Infos {
childDst := &provider.Reference{Path: path.Join(cp.destination.Path, path.Base(res.Infos[i].Path))}
err := s.executePathCopy(ctx, client, w, r, ©{sourceInfo: res.Infos[i], destination: childDst, depth: cp.depth, successCode: cp.successCode})
if err != nil {
return err
}
}
} else {
// copy file
// 1. get download url
dReq := &provider.InitiateFileDownloadRequest{
Ref: &provider.Reference{Path: cp.sourceInfo.Path},
}
dRes, err := client.InitiateFileDownload(ctx, dReq)
if err != nil {
return err
}
if dRes.Status.Code != rpc.Code_CODE_OK {
return fmt.Errorf("status code %d", dRes.Status.Code)
}
var downloadEP, downloadToken string
for _, p := range dRes.Protocols {
if p.Protocol == "simple" {
downloadEP, downloadToken = p.DownloadEndpoint, p.Token
}
}
// 2. get upload url
uReq := &provider.InitiateFileUploadRequest{
Ref: cp.destination,
Opaque: &typespb.Opaque{
Map: map[string]*typespb.OpaqueEntry{
"Upload-Length": {
Decoder: "plain",
// TODO: handle case where size is not known in advance
Value: []byte(strconv.FormatUint(cp.sourceInfo.GetSize(), 10)),
},
},
},
}
uRes, err := client.InitiateFileUpload(ctx, uReq)
if err != nil {
return err
}
if uRes.Status.Code != rpc.Code_CODE_OK {
if uRes.Status.Code == rpc.Code_CODE_PERMISSION_DENIED {
w.WriteHeader(http.StatusForbidden)
m := fmt.Sprintf("Permissions denied to create %v", uReq.Ref.Path)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: m,
})
HandleWebdavError(log, w, b, err)
return nil
}
HandleErrorStatus(log, w, uRes.Status)
return nil
}
var uploadEP, uploadToken string
for _, p := range uRes.Protocols {
if p.Protocol == "simple" {
uploadEP, uploadToken = p.UploadEndpoint, p.Token
}
}
// 3. do download
httpDownloadReq, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadEP, nil)
if err != nil {
return err
}
httpDownloadReq.Header.Set(datagateway.TokenTransportHeader, downloadToken)
httpDownloadRes, err := s.client.Do(httpDownloadReq)
if err != nil {
return err
}
defer httpDownloadRes.Body.Close()
if httpDownloadRes.StatusCode != http.StatusOK {
return fmt.Errorf("status code %d", httpDownloadRes.StatusCode)
}
// 4. do upload
httpUploadReq, err := http.NewRequestWithContext(ctx, http.MethodPut, uploadEP, httpDownloadRes.Body)
if err != nil {
return err
}
httpUploadReq.Header.Set(datagateway.TokenTransportHeader, uploadToken)
httpUploadRes, err := s.client.Do(httpUploadReq)
if err != nil {
return err
}
defer httpUploadRes.Body.Close()
if httpUploadRes.StatusCode != http.StatusOK {
return err
}
}
return nil
}
func (s *svc) handleSpacesCopy(w http.ResponseWriter, r *http.Request, spaceID string) {
ctx := r.Context()
dst, err := extractDestination(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
sublog := appctx.GetLogger(ctx).With().Str("spaceid", spaceID).Str("path", r.URL.Path).Str("destination", dst).Logger()
// retrieve a specific storage space
srcRef, status, err := s.lookUpStorageSpaceReference(ctx, spaceID, r.URL.Path)
if err != nil {
sublog.Error().Err(err).Msg("error sending a grpc request")
w.WriteHeader(http.StatusInternalServerError)
return
}
if status.Code != rpc.Code_CODE_OK {
HandleErrorStatus(&sublog, w, status)
return
}
dstSpaceID, dstRelPath := router.ShiftPath(dst)
// retrieve a specific storage space
dstRef, status, err := s.lookUpStorageSpaceReference(ctx, dstSpaceID, dstRelPath)
if err != nil {
sublog.Error().Err(err).Msg("error sending a grpc request")
w.WriteHeader(http.StatusInternalServerError)
return
}
if status.Code != rpc.Code_CODE_OK {
HandleErrorStatus(&sublog, w, status)
return
}
intermediateDirRefFunc := func() (*provider.Reference, *rpc.Status, error) {
intermediateDir := path.Dir(dstRelPath)
return s.lookUpStorageSpaceReference(ctx, dstSpaceID, intermediateDir)
}
cp := s.prepareCopy(ctx, w, r, srcRef, dstRef, intermediateDirRefFunc, &sublog)
if cp == nil {
return
}
client, err := s.getClient()
if err != nil {
sublog.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return
}
err = s.executeSpacesCopy(ctx, w, client, cp)
if err != nil {
sublog.Error().Err(err).Str("depth", cp.depth).Msg("error descending directory")
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(cp.successCode)
}
func (s *svc) executeSpacesCopy(ctx context.Context, w http.ResponseWriter, client gateway.GatewayAPIClient, cp *copy) error {
log := appctx.GetLogger(ctx)
log.Debug().Interface("src", cp.sourceInfo).Interface("dst", cp.destination).Msg("descending")
if cp.sourceInfo.Type == provider.ResourceType_RESOURCE_TYPE_CONTAINER {
// create dir
createReq := &provider.CreateContainerRequest{
Ref: cp.destination,
}
createRes, err := client.CreateContainer(ctx, createReq)
if err != nil {
log.Error().Err(err).Msg("error performing create container grpc request")
return err
}
if createRes.Status.Code != rpc.Code_CODE_OK {
if createRes.Status.Code == rpc.Code_CODE_PERMISSION_DENIED {
w.WriteHeader(http.StatusForbidden)
// TODO path could be empty or relative...
m := fmt.Sprintf("Permission denied to create %v", createReq.Ref.Path)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: m,
})
HandleWebdavError(log, w, b, err)
}
return nil
}
// TODO: also copy properties: https://tools.ietf.org/html/rfc4918#section-9.8.2
if cp.depth != "infinity" {
return nil
}
// descend for children
listReq := &provider.ListContainerRequest{Ref: &provider.Reference{ResourceId: cp.sourceInfo.Id, Path: "."}}
res, err := client.ListContainer(ctx, listReq)
if err != nil {
return err
}
if res.Status.Code != rpc.Code_CODE_OK {
w.WriteHeader(http.StatusInternalServerError)
return nil
}
for i := range res.Infos {
childRef := &provider.Reference{
ResourceId: cp.destination.ResourceId,
Path: utils.MakeRelativePath(path.Join(cp.destination.Path, res.Infos[i].Path)),
}
err := s.executeSpacesCopy(ctx, w, client, ©{sourceInfo: res.Infos[i], destination: childRef, depth: cp.depth, successCode: cp.successCode})
if err != nil {
return err
}
}
} else {
// copy file
// 1. get download url
dReq := &provider.InitiateFileDownloadRequest{Ref: &provider.Reference{ResourceId: cp.sourceInfo.Id, Path: "."}}
dRes, err := client.InitiateFileDownload(ctx, dReq)
if err != nil {
return err
}
if dRes.Status.Code != rpc.Code_CODE_OK {
return fmt.Errorf("status code %d", dRes.Status.Code)
}
var downloadEP, downloadToken string
for _, p := range dRes.Protocols {
if p.Protocol == "spaces" {
downloadEP, downloadToken = p.DownloadEndpoint, p.Token
}
}
// 2. get upload url
uReq := &provider.InitiateFileUploadRequest{
Ref: cp.destination,
Opaque: &typespb.Opaque{
Map: map[string]*typespb.OpaqueEntry{
HeaderUploadLength: {
Decoder: "plain",
// TODO: handle case where size is not known in advance
Value: []byte(strconv.FormatUint(cp.sourceInfo.GetSize(), 10)),
},
},
},
}
uRes, err := client.InitiateFileUpload(ctx, uReq)
if err != nil {
return err
}
if uRes.Status.Code != rpc.Code_CODE_OK {
if uRes.Status.Code == rpc.Code_CODE_PERMISSION_DENIED {
w.WriteHeader(http.StatusForbidden)
// TODO path can be empty or relative
m := fmt.Sprintf("Permissions denied to create %v", uReq.Ref.Path)
b, err := Marshal(exception{
code: SabredavPermissionDenied,
message: m,
})
HandleWebdavError(log, w, b, err)
return nil
}
HandleErrorStatus(log, w, uRes.Status)
return nil
}
var uploadEP, uploadToken string
for _, p := range uRes.Protocols {
if p.Protocol == "simple" {
uploadEP, uploadToken = p.UploadEndpoint, p.Token
}
}
// 3. do download
httpDownloadReq, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadEP, nil)
if err != nil {
return err
}
if downloadToken != "" {
httpDownloadReq.Header.Set(datagateway.TokenTransportHeader, downloadToken)
}
httpDownloadRes, err := s.client.Do(httpDownloadReq)
if err != nil {
return err
}
defer httpDownloadRes.Body.Close()
if httpDownloadRes.StatusCode != http.StatusOK {
return fmt.Errorf("status code %d", httpDownloadRes.StatusCode)
}
// 4. do upload
httpUploadReq, err := http.NewRequestWithContext(ctx, http.MethodPut, uploadEP, httpDownloadRes.Body)
if err != nil {
return err
}
httpUploadReq.Header.Set(datagateway.TokenTransportHeader, uploadToken)
httpUploadRes, err := s.client.Do(httpUploadReq)
if err != nil {
return err
}
defer httpUploadRes.Body.Close()
if httpUploadRes.StatusCode != http.StatusOK {
return err
}
}
return nil
}
func (s *svc) prepareCopy(ctx context.Context, w http.ResponseWriter, r *http.Request, srcRef, dstRef *provider.Reference, intermediateDirRef intermediateDirRefFunc, log *zerolog.Logger) *copy {
overwrite, err := extractOverwrite(w, r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
m := fmt.Sprintf("Overwrite header is set to incorrect value %v", overwrite)
b, err := Marshal(exception{
code: SabredavBadRequest,
message: m,
})
HandleWebdavError(log, w, b, err)
return nil
}
depth, err := extractDepth(w, r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
m := fmt.Sprintf("Depth header is set to incorrect value %v", depth)
b, err := Marshal(exception{
code: SabredavBadRequest,
message: m,
})
HandleWebdavError(log, w, b, err)
return nil
}
log.Debug().Str("overwrite", overwrite).Str("depth", depth).Msg("copy")
client, err := s.getClient()
if err != nil {
log.Error().Err(err).Msg("error getting grpc client")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
srcStatReq := &provider.StatRequest{Ref: srcRef}
srcStatRes, err := client.Stat(ctx, srcStatReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if srcStatRes.Status.Code != rpc.Code_CODE_OK {
if srcStatRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
w.WriteHeader(http.StatusNotFound)
m := fmt.Sprintf("Resource %v not found", srcStatReq.Ref.Path)
b, err := Marshal(exception{
code: SabredavNotFound,
message: m,
})
HandleWebdavError(log, w, b, err)
}
HandleErrorStatus(log, w, srcStatRes.Status)
return nil
}
dstStatReq := &provider.StatRequest{Ref: dstRef}
dstStatRes, err := client.Stat(ctx, dstStatReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if dstStatRes.Status.Code != rpc.Code_CODE_OK && dstStatRes.Status.Code != rpc.Code_CODE_NOT_FOUND {
HandleErrorStatus(log, w, srcStatRes.Status)
return nil
}
successCode := http.StatusCreated // 201 if new resource was created, see https://tools.ietf.org/html/rfc4918#section-9.8.5
if dstStatRes.Status.Code == rpc.Code_CODE_OK {
successCode = http.StatusNoContent // 204 if target already existed, see https://tools.ietf.org/html/rfc4918#section-9.8.5
if overwrite == "F" {
log.Warn().Str("overwrite", overwrite).Msg("dst already exists")
w.WriteHeader(http.StatusPreconditionFailed)
m := fmt.Sprintf("Could not overwrite Resource %v", dstRef.Path)
b, err := Marshal(exception{
code: SabredavPreconditionFailed,
message: m,
})
HandleWebdavError(log, w, b, err) // 412, see https://tools.ietf.org/html/rfc4918#section-9.8.5
return nil
}
// delete existing tree
delReq := &provider.DeleteRequest{Ref: dstRef}
delRes, err := client.Delete(ctx, delReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc delete request")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if delRes.Status.Code != rpc.Code_CODE_OK && delRes.Status.Code != rpc.Code_CODE_NOT_FOUND {
HandleErrorStatus(log, w, delRes.Status)
return nil
}
} else {
// check if an intermediate path / the parent exists
intermediateRef, status, err := intermediateDirRef()
if err != nil {
log.Error().Err(err).Msg("error sending a grpc request")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if status.Code != rpc.Code_CODE_OK {
HandleErrorStatus(log, w, status)
return nil
}
intStatReq := &provider.StatRequest{Ref: intermediateRef}
intStatRes, err := client.Stat(ctx, intStatReq)
if err != nil {
log.Error().Err(err).Msg("error sending grpc stat request")
w.WriteHeader(http.StatusInternalServerError)
return nil
}
if intStatRes.Status.Code != rpc.Code_CODE_OK {
if intStatRes.Status.Code == rpc.Code_CODE_NOT_FOUND {
// 409 if intermediate dir is missing, see https://tools.ietf.org/html/rfc4918#section-9.8.5
log.Debug().Interface("parent", intermediateRef).Interface("status", intStatRes.Status).Msg("conflict")
w.WriteHeader(http.StatusConflict)
} else {
HandleErrorStatus(log, w, srcStatRes.Status)
}
return nil
}
// TODO what if intermediate is a file?
}
return ©{sourceInfo: srcStatRes.Info, depth: depth, successCode: successCode, destination: dstRef}
}
func extractOverwrite(w http.ResponseWriter, r *http.Request) (string, error) {
overwrite := r.Header.Get(HeaderOverwrite)
overwrite = strings.ToUpper(overwrite)
if overwrite == "" {
overwrite = "T"
}
if overwrite != "T" && overwrite != "F" {
return "", errInvalidValue
}
return overwrite, nil
}
func extractDepth(w http.ResponseWriter, r *http.Request) (string, error) {
depth := r.Header.Get(HeaderDepth)
if depth == "" {
depth = "infinity"
}
if depth != "infinity" && depth != "0" {
return "", errInvalidValue
}
return depth, nil
}