/
WebDavClient.cs
927 lines (816 loc) · 44.9 KB
/
WebDavClient.cs
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace WebDav
{
/// <summary>
/// Represents a WebDAV client that can perform WebDAV operations.
/// </summary>
public class WebDavClient : IWebDavClient
{
private IWebDavDispatcher _dispatcher;
private IResponseParser<PropfindResponse> _propfindResponseParser;
private IResponseParser<ProppatchResponse> _proppatchResponseParser;
private IResponseParser<LockResponse> _lockResponseParser;
/// <summary>
/// Initializes a new instance of the <see cref="WebDavClient"/> class.
/// </summary>
public WebDavClient()
: this(new WebDavClientParams())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WebDavClient"/> class.
/// </summary>
/// <param name="params">The parameters of the WebDAV client.</param>
public WebDavClient(WebDavClientParams @params)
{
Initialize(ConfigureHttpClient(@params));
}
/// <summary>
/// Initializes a new instance of the <see cref="WebDavClient"/> class.
/// </summary>
/// <param name="httpClient">The pre-configured instance of <see cref="HttpClient"/>.</param>
public WebDavClient(HttpClient httpClient)
{
Initialize(httpClient);
}
private void Initialize(HttpClient httpClient)
{
SetWebDavDispatcher(new WebDavDispatcher(httpClient));
var lockResponseParser = new LockResponseParser();
SetPropfindResponseParser(new PropfindResponseParser(lockResponseParser));
SetProppatchResponseParser(new ProppatchResponseParser());
SetLockResponseParser(lockResponseParser);
}
/// <summary>
/// Retrieves properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public Task<PropfindResponse> Propfind(string requestUri)
{
return Propfind(CreateUri(requestUri), new PropfindParameters());
}
/// <summary>
/// Retrieves properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public Task<PropfindResponse> Propfind(Uri requestUri)
{
return Propfind(requestUri, new PropfindParameters());
}
/// <summary>
/// Retrieves properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the PROPFIND operation.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public Task<PropfindResponse> Propfind(string requestUri, PropfindParameters parameters)
{
return Propfind(CreateUri(requestUri), parameters);
}
/// <summary>
/// Retrieves properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the PROPFIND operation.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public async Task<PropfindResponse> Propfind(Uri requestUri, PropfindParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var applyTo = parameters.ApplyTo ?? ApplyTo.Propfind.ResourceAndChildren;
var headers = new HeaderBuilder()
.Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForPropfind(applyTo))
.AddWithOverwrite(parameters.Headers)
.Build();
HttpContent requestBody = null;
if (parameters.RequestType != PropfindRequestType.AllPropertiesImplied)
{
var content = PropfindRequestBuilder.BuildRequest(parameters.RequestType, parameters.CustomProperties, parameters.Namespaces);
requestBody = new StringContent(content);
}
var requestParams = new RequestParameters { Headers = headers, Content = requestBody, ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Propfind, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Sets and/or removes properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the PROPPATCH operation.</param>
/// <returns>An instance of <see cref="ProppatchResponse" />.</returns>
public Task<ProppatchResponse> Proppatch(string requestUri, ProppatchParameters parameters)
{
return Proppatch(CreateUri(requestUri), parameters);
}
/// <summary>
/// Sets and/or removes properties defined on the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the PROPPATCH operation.</param>
/// <returns>An instance of <see cref="ProppatchResponse" />.</returns>
public async Task<ProppatchResponse> Proppatch(Uri requestUri, ProppatchParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headerBuilder = new HeaderBuilder();
if (!string.IsNullOrEmpty(parameters.LockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestBody = ProppatchRequestBuilder.BuildRequestBody(
parameters.PropertiesToSet,
parameters.PropertiesToRemove,
parameters.Namespaces);
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Proppatch, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _proppatchResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Creates a new collection resource at the location specified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Mkcol(string requestUri)
{
return Mkcol(CreateUri(requestUri), new MkColParameters());
}
/// <summary>
/// Creates a new collection resource at the location specified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Mkcol(Uri requestUri)
{
return Mkcol(requestUri, new MkColParameters());
}
/// <summary>
/// Creates a new collection resource at the location specified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the MKCOL operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Mkcol(string requestUri, MkColParameters parameters)
{
return Mkcol(CreateUri(requestUri), parameters);
}
/// <summary>
/// Creates a new collection resource at the location specified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the MKCOL operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public async Task<WebDavResponse> Mkcol(Uri requestUri, MkColParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headerBuilder = new HeaderBuilder();
if (!string.IsNullOrEmpty(parameters.LockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Mkcol, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return it without processing.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetRawFile(string requestUri)
{
return GetFile(CreateUri(requestUri), false, new GetFileParameters());
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return it without processing.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetRawFile(Uri requestUri)
{
return GetFile(requestUri, false, new GetFileParameters());
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return it without processing.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the GET operation.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetRawFile(string requestUri, GetFileParameters parameters)
{
return GetFile(CreateUri(requestUri), false, parameters);
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return it without processing.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the GET operation.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetRawFile(Uri requestUri, GetFileParameters parameters)
{
return GetFile(requestUri, false, parameters);
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return a processed response, if possible.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetProcessedFile(string requestUri)
{
return GetFile(CreateUri(requestUri), true, new GetFileParameters());
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return a processed response, if possible.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetProcessedFile(Uri requestUri)
{
return GetFile(requestUri, true, new GetFileParameters());
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return a processed response, if possible.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the GET operation.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetProcessedFile(string requestUri, GetFileParameters parameters)
{
return GetFile(CreateUri(requestUri), true, parameters);
}
/// <summary>
/// Retrieves the file identified by the request URI telling the server to return a processed response, if possible.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the GET operation.</param>
/// <returns>An instance of <see cref="WebDavStreamResponse" />.</returns>
public Task<WebDavStreamResponse> GetProcessedFile(Uri requestUri, GetFileParameters parameters)
{
return GetFile(requestUri, true, parameters);
}
internal virtual async Task<WebDavStreamResponse> GetFile(Uri requestUri, bool translate, GetFileParameters parameters)
{
var response = await GetFileResponse(requestUri, translate, parameters).ConfigureAwait(false);
var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return new WebDavStreamResponse(response, stream);
}
/// <summary>
/// Retrieves the raw http response of a file identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="translate">A parameter indicating if the response can be processed by the web server.</param>
/// <param name="parameters">Parameters of the GET operation.</param>
/// <returns>An instance of <see cref="HttpResponseMessage" />.</returns>
public async Task<HttpResponseMessage> GetFileResponse(Uri requestUri, bool translate, GetFileParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headers = new HeaderBuilder()
.Add(WebDavHeaders.Translate, translate ? "t" : "f")
.AddWithOverwrite(parameters.Headers)
.Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(
requestUri,
HttpMethod.Get,
requestParams,
parameters.CancellationToken,
HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
return response;
}
/// <summary>
/// Deletes the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Delete(string requestUri)
{
return Delete(CreateUri(requestUri), new DeleteParameters());
}
/// <summary>
/// Deletes the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Delete(Uri requestUri)
{
return Delete(requestUri, new DeleteParameters());
}
/// <summary>
/// Deletes the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the DELETE operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Delete(string requestUri, DeleteParameters parameters)
{
return Delete(CreateUri(requestUri), parameters);
}
/// <summary>
/// Deletes the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the DELETE operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public async Task<WebDavResponse> Delete(Uri requestUri, DeleteParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headerBuilder = new HeaderBuilder();
if (!string.IsNullOrEmpty(parameters.LockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(requestUri, HttpMethod.Delete, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, Stream stream)
{
return PutFile(CreateUri(requestUri), new StreamContent(stream), new PutFileParameters());
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(Uri requestUri, Stream stream)
{
return PutFile(requestUri, new StreamContent(stream), new PutFileParameters());
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <param name="contentType">The content type of the request body.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, Stream stream, string contentType)
{
var @params = new PutFileParameters { ContentType = new MediaTypeHeaderValue(contentType) };
return PutFile(CreateUri(requestUri), new StreamContent(stream), @params);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <param name="contentType">The content type of the request body.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(Uri requestUri, Stream stream, string contentType)
{
var @params = new PutFileParameters { ContentType = new MediaTypeHeaderValue(contentType) };
return PutFile(requestUri, new StreamContent(stream), @params);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <param name="parameters">Parameters of the PUT operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, Stream stream, PutFileParameters parameters)
{
return PutFile(CreateUri(requestUri), new StreamContent(stream), parameters);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="stream">The stream of content of the resource.</param>
/// <param name="parameters">Parameters of the PUT operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(Uri requestUri, Stream stream, PutFileParameters parameters)
{
return PutFile(requestUri, new StreamContent(stream), parameters);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="content">The content to pass to the request.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, HttpContent content)
{
return PutFile(CreateUri(requestUri), content, new PutFileParameters());
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="content">The content to pass to the request.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(Uri requestUri, HttpContent content)
{
return PutFile(requestUri, content, new PutFileParameters());
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="content">The content to pass to the request.</param>
/// <param name="parameters">Parameters of the PUT operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> PutFile(string requestUri, HttpContent content, PutFileParameters parameters)
{
return PutFile(CreateUri(requestUri), content, parameters);
}
/// <summary>
/// Requests the resource to be stored under the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="content">The content to pass to the request.</param>
/// <param name="parameters">Parameters of the PUT operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public async Task<WebDavResponse> PutFile(Uri requestUri, HttpContent content, PutFileParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
Guard.NotNull(content, "content");
var headerBuilder = new HeaderBuilder();
if (!string.IsNullOrEmpty(parameters.LockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.LockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestParams = new RequestParameters { Headers = headers, Content = content, ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, HttpMethod.Put, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Creates a duplicate of the source resource identified by the source URI in the destination resource identified by the destination URI.
/// </summary>
/// <param name="sourceUri">A string that represents the source URI.</param>
/// <param name="destUri">A string that represents the destination URI.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Copy(string sourceUri, string destUri)
{
return Copy(CreateUri(sourceUri), CreateUri(destUri), new CopyParameters());
}
/// <summary>
/// Creates a duplicate of the source resource identified by the source URI in the destination resource identified by the destination URI.
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destUri">The destination <see cref="Uri"/>.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Copy(Uri sourceUri, Uri destUri)
{
return Copy(sourceUri, destUri, new CopyParameters());
}
/// <summary>
/// Creates a duplicate of the source resource identified by the source URI in the destination resource identified by the destination URI.
/// </summary>
/// <param name="sourceUri">A string that represents the source URI.</param>
/// <param name="destUri">A string that represents the destination URI.</param>
/// <param name="parameters">Parameters of the COPY operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Copy(string sourceUri, string destUri, CopyParameters parameters)
{
return Copy(CreateUri(sourceUri), CreateUri(destUri), parameters);
}
/// <summary>
/// Creates a duplicate of the source resource identified by the source URI in the destination resource identified by the destination URI.
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destUri">The destination <see cref="Uri"/>.</param>
/// <param name="parameters">Parameters of the COPY operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" /></returns>
public async Task<WebDavResponse> Copy(Uri sourceUri, Uri destUri, CopyParameters parameters)
{
Guard.NotNull(sourceUri, "sourceUri");
Guard.NotNull(destUri, "destUri");
var applyTo = parameters.ApplyTo ?? ApplyTo.Copy.ResourceAndAncestors;
var headerBuilder = new HeaderBuilder()
.Add(WebDavHeaders.Destination, GetAbsoluteUri(destUri).AbsoluteUri)
.Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForCopy(applyTo))
.Add(WebDavHeaders.Overwrite, parameters.Overwrite ? "T" : "F");
if (!string.IsNullOrEmpty(parameters.DestLockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.DestLockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(sourceUri, WebDavMethod.Copy, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Moves the resource identified by the source URI to the destination identified by the destination URI.
/// </summary>
/// <param name="sourceUri">A string that represents the source URI.</param>
/// <param name="destUri">A string that represents the destination URI.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Move(string sourceUri, string destUri)
{
return Move(CreateUri(sourceUri), CreateUri(destUri), new MoveParameters());
}
/// <summary>
/// Moves the resource identified by the source URI to the destination identified by the destination URI.
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destUri">The destination <see cref="Uri"/>.</param>
/// <returns>An instance of <see cref="WebDavResponse" /></returns>
public Task<WebDavResponse> Move(Uri sourceUri, Uri destUri)
{
return Move(sourceUri, destUri, new MoveParameters());
}
/// <summary>
/// Moves the resource identified by the source URI to the destination identified by the destination URI.
/// </summary>
/// <param name="sourceUri">A string that represents the source URI.</param>
/// <param name="destUri">A string that represents the destination URI.</param>
/// <param name="parameters">Parameters of the MOVE operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Move(string sourceUri, string destUri, MoveParameters parameters)
{
return Move(CreateUri(sourceUri), CreateUri(destUri), parameters);
}
/// <summary>
/// Moves the resource identified by the source URI to the destination identified by the destination URI.
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destUri">The destination <see cref="Uri"/>.</param>
/// <param name="parameters">Parameters of the MOVE operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public async Task<WebDavResponse> Move(Uri sourceUri, Uri destUri, MoveParameters parameters)
{
Guard.NotNull(sourceUri, "sourceUri");
Guard.NotNull(destUri, "destUri");
var headerBuilder = new HeaderBuilder()
.Add(WebDavHeaders.Destination, GetAbsoluteUri(destUri).AbsoluteUri)
.Add(WebDavHeaders.Overwrite, parameters.Overwrite ? "T" : "F");
if (!string.IsNullOrEmpty(parameters.SourceLockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.SourceLockToken));
if (!string.IsNullOrEmpty(parameters.DestLockToken))
headerBuilder.Add(WebDavHeaders.If, IfHeaderHelper.GetHeaderValue(parameters.DestLockToken));
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(sourceUri, WebDavMethod.Move, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Takes out a shared lock or refreshes an existing lock of the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <returns>An instance of <see cref="LockResponse" />.</returns>
public Task<LockResponse> Lock(string requestUri)
{
return Lock(CreateUri(requestUri), new LockParameters());
}
/// <summary>
/// Takes out a shared lock or refreshes an existing lock of the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <returns>An instance of <see cref="LockResponse" />.</returns>
public Task<LockResponse> Lock(Uri requestUri)
{
return Lock(requestUri, new LockParameters());
}
/// <summary>
/// Takes out a lock of any type or refreshes an existing lock of the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the LOCK operation.</param>
/// <returns>An instance of <see cref="LockResponse" />.</returns>
public Task<LockResponse> Lock(string requestUri, LockParameters parameters)
{
return Lock(CreateUri(requestUri), parameters);
}
/// <summary>
/// Takes out a lock of any type or refreshes an existing lock of the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the LOCK operation.</param>
/// <returns>An instance of <see cref="LockResponse" />.</returns>
public async Task<LockResponse> Lock(Uri requestUri, LockParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headerBuilder = new HeaderBuilder();
if (parameters.ApplyTo.HasValue)
headerBuilder.Add(WebDavHeaders.Depth, DepthHeaderHelper.GetValueForLock(parameters.ApplyTo.Value));
if (parameters.Timeout.HasValue)
headerBuilder.Add(WebDavHeaders.Timeout, $"Second-{parameters.Timeout.Value.TotalSeconds}");
var headers = headerBuilder.AddWithOverwrite(parameters.Headers).Build();
var requestBody = LockRequestBuilder.BuildRequestBody(parameters);
var requestParams = new RequestParameters { Headers = headers, Content = new StringContent(requestBody), ContentType = parameters.ContentType };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Lock, requestParams, parameters.CancellationToken).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
return new LockResponse((int)response.StatusCode, response.ReasonPhrase);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _lockResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Removes the lock identified by the lock token from the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="lockToken">The resource lock token.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Unlock(string requestUri, string lockToken)
{
return Unlock(CreateUri(requestUri), new UnlockParameters(lockToken));
}
/// <summary>
/// Removes the lock identified by the lock token from the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="lockToken">The resource lock token.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Unlock(Uri requestUri, string lockToken)
{
return Unlock(requestUri, new UnlockParameters(lockToken));
}
/// <summary>
/// Removes the lock identified by the lock token from the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the UNLOCK operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public Task<WebDavResponse> Unlock(string requestUri, UnlockParameters parameters)
{
return Unlock(CreateUri(requestUri), parameters);
}
/// <summary>
/// Removes the lock identified by the lock token from the resource identified by the request URI.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the UNLOCK operation.</param>
/// <returns>An instance of <see cref="WebDavResponse" />.</returns>
public async Task<WebDavResponse> Unlock(Uri requestUri, UnlockParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
var headers = new HeaderBuilder()
.Add(WebDavHeaders.LockToken, $"<{parameters.LockToken}>")
.AddWithOverwrite(parameters.Headers)
.Build();
var requestParams = new RequestParameters { Headers = headers };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Unlock, requestParams, parameters.CancellationToken).ConfigureAwait(false);
return new WebDavResponse((int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Executes a SEARCH operation.
/// </summary>
/// <param name="requestUri">A string that represents the request URI.</param>
/// <param name="parameters">Parameters of the SEARCH operation.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public Task<PropfindResponse> Search(string requestUri, SearchParameters parameters)
{
return Search(CreateUri(requestUri), parameters);
}
/// <summary>
/// Executes a SEARCH operation.
/// </summary>
/// <param name="requestUri">The <see cref="Uri"/> to request.</param>
/// <param name="parameters">Parameters of the SEARCH operation.</param>
/// <returns>An instance of <see cref="PropfindResponse" />.</returns>
public async Task<PropfindResponse> Search(Uri requestUri, SearchParameters parameters)
{
Guard.NotNull(requestUri, "requestUri");
parameters.AssertParametersAreValid();
var headers = new HeaderBuilder()
.AddWithOverwrite(parameters.Headers)
.Build();
var requestBody = new StringContent(SearchRequestBuilder.BuildRequestBody(parameters));
var requestParams = new RequestParameters { Headers = headers, Content = requestBody, ContentType = new MediaTypeHeaderValue("text/xml") };
var response = await _dispatcher.Send(requestUri, WebDavMethod.Search, requestParams, parameters.CancellationToken).ConfigureAwait(false);
var responseContent = await ReadContentAsString(response.Content).ConfigureAwait(false);
return _propfindResponseParser.Parse(responseContent, (int)response.StatusCode, response.ReasonPhrase);
}
/// <summary>
/// Sets the dispatcher of WebDAV requests.
/// </summary>
/// <param name="dispatcher">The dispatcher of WebDAV http requests.</param>
/// <returns>This instance of <see cref="WebDavClient" /> to support chain calls.</returns>
internal WebDavClient SetWebDavDispatcher(IWebDavDispatcher dispatcher)
{
Guard.NotNull(dispatcher, "dispather");
_dispatcher = dispatcher;
return this;
}
/// <summary>
/// Sets the parser of PROPFIND responses.
/// </summary>
/// <param name="responseParser">The parser of WebDAV PROPFIND responses.</param>
/// <returns>This instance of <see cref="WebDavClient" /> to support chain calls.</returns>
internal WebDavClient SetPropfindResponseParser(IResponseParser<PropfindResponse> responseParser)
{
Guard.NotNull(responseParser, "responseParser");
_propfindResponseParser = responseParser;
return this;
}
/// <summary>
/// Sets the parser of PROPPATCH responses.
/// </summary>
/// <param name="responseParser">The parser of WebDAV PROPPATCH responses.</param>
/// <returns>This instance of <see cref="WebDavClient" /> to support chain calls.</returns>
internal WebDavClient SetProppatchResponseParser(IResponseParser<ProppatchResponse> responseParser)
{
Guard.NotNull(responseParser, "responseParser");
_proppatchResponseParser = responseParser;
return this;
}
/// <summary>
/// Sets the parser of LOCK responses.
/// </summary>
/// <param name="responseParser">The parser of WebDAV LOCK responses.</param>
/// <returns>This instance of <see cref="WebDavClient" /> to support chain calls.</returns>
internal WebDavClient SetLockResponseParser(IResponseParser<LockResponse> responseParser)
{
Guard.NotNull(responseParser, "responseParser");
_lockResponseParser = responseParser;
return this;
}
private static HttpClient ConfigureHttpClient(WebDavClientParams @params)
{
var httpHandler = new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
PreAuthenticate = @params.PreAuthenticate,
UseDefaultCredentials = @params.UseDefaultCredentials,
UseProxy = @params.UseProxy
};
if (@params.Credentials != null)
{
httpHandler.Credentials = @params.Credentials;
httpHandler.UseDefaultCredentials = false;
}
if (@params.Proxy != null)
{
httpHandler.Proxy = @params.Proxy;
}
var httpClient = new HttpClient(httpHandler, true)
{
BaseAddress = @params.BaseAddress
};
if (@params.Timeout.HasValue)
{
httpClient.Timeout = @params.Timeout.Value;
}
foreach (var header in @params.DefaultRequestHeaders)
{
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
}
return httpClient;
}
private static Uri CreateUri(string requestUri)
{
return requestUri != null ? new Uri(requestUri, UriKind.RelativeOrAbsolute) : null;
}
private static Exception CreateInvalidUriException()
{
return
new InvalidOperationException(
"An invalid request URI was provided. The request URI must either be an absolute URI or BaseAddress must be set.");
}
private static Encoding GetResponseEncoding(HttpContent content, Encoding fallbackEncoding)
{
if (content.Headers.ContentType?.CharSet == null)
return fallbackEncoding;
try
{
return Encoding.GetEncoding(content.Headers.ContentType.CharSet);
}
catch (ArgumentException)
{
return fallbackEncoding;
}
}
private static async Task<string> ReadContentAsString(HttpContent content)
{
var data = await content.ReadAsByteArrayAsync().ConfigureAwait(false);
return GetResponseEncoding(content, Encoding.UTF8).GetString(data, 0, data.Length);
}
private Uri GetAbsoluteUri(Uri uri)
{
if (uri == null && _dispatcher.BaseAddress == null)
throw CreateInvalidUriException();
if (uri == null)
return _dispatcher.BaseAddress;
if (uri.IsAbsoluteUri)
return uri;
if (_dispatcher.BaseAddress == null)
throw CreateInvalidUriException();
return new Uri(_dispatcher.BaseAddress, uri);
}
#region IDisposable
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting managed/unmanaged resources.
/// Disposes the underlying HttpClient.
/// </summary>
public void Dispose()
{
DisposeManagedResources();
}
/// <summary>
/// Disposes the managed resources.
/// </summary>
protected virtual void DisposeManagedResources()
{
var disposableDispatcher = _dispatcher as IDisposable;
disposableDispatcher?.Dispose();
}
#endregion
}
}