This repository has been archived by the owner on Feb 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
WebDavSession.cs
1582 lines (1374 loc) · 89.8 KB
/
WebDavSession.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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using DecaTec.WebDav.Exceptions;
using DecaTec.WebDav.Headers;
using DecaTec.WebDav.Tools;
using DecaTec.WebDav.WebDavArtifacts;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace DecaTec.WebDav
{
/// <summary>
/// Class for WebDAV sessions.
/// </summary>
/// <remarks>
/// <para>This class acts as an abstraction layer between the application and the <see cref="DecaTec.WebDav.WebDavClient"/>, which is used to communicate with the WebDAV server.</para>
/// <para>If you want to communicate with the WebDAV server directly, you should use the <see cref="DecaTec.WebDav.WebDavClient"/>.</para>
/// <para>The WebDavSession can be used with a base URL/<see cref="System.Uri"/>. If such a base URL/<see cref="System.Uri"/> is specified, all subsequent operations involving an
/// URL/<see cref="System.Uri"/> will be relative on this base URL/<see cref="System.Uri"/>.
/// If no base URL/<see cref="System.Uri"/> is specified, all operations has the be called with an absolute URL/<see cref="System.Uri"/>.</para>
/// <para>The WebDavSession uses HTTP/2 by default. To use other HTTP versions, specify the HTTP version to use in an overloaded constructor of WebDavSession or set the HTTP version with the property <see cref="HttpVersion"/>.</para>
/// <para>The methods of WebDavSession often return a boolean value in order to indicate if the operation was completed successfully and to avoid throwing of exceptions.
/// If you prefer that exceptions are thrown with more information about what went wrong, set the property <see cref="WebDavSession.ThrowExceptions"/> to true (defaults to false).</para>
/// </remarks>
/// <example>See the following code to list the content of a directory with the WebDavSession:
/// <code>
/// // You have to add a reference to DecaTec.WebDav.dll.
/// //
/// // Specify the user credentials and use it to create a WebDavSession instance.
/// var credentials = new NetworkCredential("MyUserName", "MyPassword");
/// var webDavSession = new WebDavSession(@"http://www.myserver.com/webdav/", credentials);
/// var items = await webDavSession.ListAsync(@"MyFolder/");
///
/// foreach (var item in items)
/// {
/// Console.WriteLine(item.Name);
/// }
///
/// // Dispose the WebDavSession when it is not longer needed.
/// webDavSession.Dispose();
/// </code>
/// <para></para>
/// See the following code which uses locking with a WebDavSession:
/// <code>
/// // Specify the user credentials and use it to create a WebDavSession instance.
/// var credentials = new NetworkDavCredential("MyUserName", "MyPassword");
/// var webDavSession = new WebDavSession(@"http://www.myserver.com/webdav/", credentials);
/// await webDavSession.LockAsync(@"Test/");
///
/// // Create new folder and delete it.
/// // You DO NOT have to care about that the folder is locked (i.e. you do not have to submit a lock token).
/// // This is all handled by the WebDavSession itself.
/// await webDavSession.CreateDirectoryAsync("MyFolder/NewFolder");
/// await webDavSession.DeleteAsync("MyFolder/NewFolder");
///
/// // Unlock the folder again.
/// await webDavSession.UnlockAsync(@"MyFolder/");
///
/// // You should always call Dispose on the WebDavSession when it is not longer needed.
/// // During Dispose, all locks held by the WebDavSession will be automatically unlocked.
/// webDavSession.Dispose();
/// </code>
/// </example>
/// <seealso cref="DecaTec.WebDav.WebDavClient"/>
public class WebDavSession : IDisposable
{
/// <summary>
/// The <see cref="WebDavClient"/> to use for this WebDavSession.
/// </summary>
protected readonly WebDavClient webDavClient;
private readonly ConcurrentDictionary<Uri, PermanentLock> permanentLocks;
#region Constructor
/// <summary>
/// Creates a new instance of WebDavSession.
/// </summary>
protected WebDavSession()
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with a default <see cref="HttpClientHandler"/>.
/// </summary>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(ICredentials credentials, bool throwExceptions = false)
: this(new HttpClientHandler() { PreAuthenticate = true, Credentials = credentials }, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with a default <see cref="HttpClientHandler"/> and the HTTP version specified.
/// </summary>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(ICredentials credentials, Version httpVersion, bool throwExceptions = false)
: this(credentials, throwExceptions)
{
this.webDavClient.HttpVersion = httpVersion;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and a default <see cref="HttpClientHandler"/>.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(string baseUrl, ICredentials credentials, bool throwExceptions = false)
: this(new Uri(baseUrl), new HttpClientHandler() { PreAuthenticate = true, Credentials = credentials }, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and a default <see cref="HttpClientHandler"/> and the HTTP version specified.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(string baseUrl, ICredentials credentials, Version httpVersion, bool throwExceptions = false)
: this(baseUrl, credentials, throwExceptions)
{
this.webDavClient.HttpVersion = httpVersion;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and a default <see cref="HttpClientHandler"/>.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(Uri baseUri, ICredentials credentials, bool throwExceptions = false)
: this(baseUri, new HttpClientHandler() { PreAuthenticate = true, Credentials = credentials }, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and a default <see cref="HttpClientHandler"/> and the HTTP version specified.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="credentials">The <see cref="ICredentials"/> to use.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
public WebDavSession(Uri baseUri, ICredentials credentials, Version httpVersion, bool throwExceptions = false)
: this(baseUri, credentials, throwExceptions)
{
this.webDavClient.HttpVersion = httpVersion;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the <see cref="HttpMessageHandler"/> specified.
/// </summary>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(HttpMessageHandler httpMessageHandler, bool throwExceptions = false)
: this(string.Empty, httpMessageHandler, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the <see cref="HttpMessageHandler"/> specified and the HTTP version specified.
/// </summary>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(HttpMessageHandler httpMessageHandler, Version httpVersion, bool throwExceptions = false)
: this(httpMessageHandler, throwExceptions)
{
this.webDavClient.HttpVersion = httpVersion;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and the <see cref="HttpMessageHandler"/> specified.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(string baseUrl, HttpMessageHandler httpMessageHandler, bool throwExceptions = false)
: this(string.IsNullOrEmpty(baseUrl) ? null : new Uri(baseUrl), httpMessageHandler, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and the <see cref="HttpMessageHandler"/> specified and the HTTP version specified.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(string baseUrl, HttpMessageHandler httpMessageHandler, Version httpVersion, bool throwExceptions = false)
: this(baseUrl, httpMessageHandler, throwExceptions)
{
this.webDavClient.HttpVersion = httpVersion;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and the <see cref="HttpMessageHandler"/> specified.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(Uri baseUri, HttpMessageHandler httpMessageHandler, bool throwExceptions = false)
: this(baseUri, httpMessageHandler, WebDavClient.DefaultHttpVersion, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and the <see cref="HttpMessageHandler"/> specified and the HTTP version specified.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="httpMessageHandler">The <see cref="HttpMessageHandler"/> to use with this WebDavSession.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
public WebDavSession(Uri baseUri, HttpMessageHandler httpMessageHandler, Version httpVersion, bool throwExceptions = false)
{
this.ThrowExceptions = throwExceptions;
this.permanentLocks = new ConcurrentDictionary<Uri, PermanentLock>();
this.webDavClient = CreateWebDavClient(httpMessageHandler);
this.webDavClient.HttpVersion = httpVersion;
this.BaseUri = baseUri;
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and <see cref="WebDavClient"/> specified.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="webDavClient">The base <see cref="WebDavClient"/> to use for this WebDavSession.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
protected WebDavSession(string baseUrl, WebDavClient webDavClient, bool throwExceptions = false)
: this(string.IsNullOrEmpty(baseUrl) ? null : new Uri(baseUrl), webDavClient, WebDavClient.DefaultHttpVersion, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base URL and <see cref="WebDavClient"/> specified and the HTTP version specified.
/// </summary>
/// <param name="baseUrl">The base URL to use for this WebDavSession.</param>
/// <param name="webDavClient">The base <see cref="WebDavClient"/> to use for this WebDavSession.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
protected WebDavSession(string baseUrl, WebDavClient webDavClient, Version httpVersion, bool throwExceptions = false)
: this(string.IsNullOrEmpty(baseUrl) ? null : new Uri(baseUrl), webDavClient, httpVersion, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and <see cref="WebDavClient"/> specified.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="webDavClient">The base <see cref="WebDavClient"/> to use for this WebDavSession.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
protected WebDavSession(Uri baseUri, WebDavClient webDavClient, bool throwExceptions = false)
: this(baseUri, webDavClient, WebDavClient.DefaultHttpVersion, throwExceptions)
{
}
/// <summary>
/// Initializes a new instance of WebDavSession with the given base <see cref="Uri"/> and <see cref="WebDavClient"/> specified and the HTTP version specified.
/// </summary>
/// <param name="baseUri">The base <see cref="Uri"/> to use for this WebDavSession.</param>
/// <param name="webDavClient">The base <see cref="WebDavClient"/> to use for this WebDavSession.</param>
/// <param name="httpVersion">The HTTP <see cref="Version"/> to use for requests.</param>
/// <param name="throwExceptions">Indicates if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.</param>
/// <remarks>If credentials are needed, these are part of the <see cref="HttpMessageHandler"/> and are specified with it.</remarks>
protected WebDavSession(Uri baseUri, WebDavClient webDavClient, Version httpVersion, bool throwExceptions = false)
{
this.ThrowExceptions = throwExceptions;
this.webDavClient = webDavClient;
this.webDavClient.HttpVersion = httpVersion;
}
#endregion Constructor
#region Properties
/// <summary>
/// Gets or sets the base <see cref="Uri"/> of this WebDavSession.
/// </summary>
public Uri BaseUri
{
get;
set;
}
/// <summary>
/// Gets or sets the BaseUri of this WebDavSession by a URL string.
/// </summary>
public string BaseUrl
{
get
{
return this.BaseUri == null ? string.Empty : this.BaseUri.ToString();
}
set
{
this.BaseUri = new Uri(value);
}
}
/// <summary>
/// Gets or sets the timespan to wait before the request times out.
/// </summary>
/// <remarks>The default value is 100,000 milliseconds (100 seconds).
/// To set an infinite timeout, set the property value to <see cref="Timeout.InfiniteTimeSpan"/>.</remarks>
public TimeSpan Timeout
{
get
{
return this.webDavClient.Timeout;
}
set
{
this.webDavClient.Timeout = value;
}
}
/// <summary>
/// Gets or sets the HTTP version the WebDavClient should use for its requests. Defaults to HTTP/2.
/// </summary>
/// <remarks>Even when HTTP/2 is specified as HTTP version, there will be a fall back to HTTP/1.1 when the server does not support HTTP/2.</remarks>
public Version HttpVersion
{
get
{
return this.webDavClient.HttpVersion;
}
set
{
this.webDavClient.HttpVersion = value;
}
}
/// <summary>
/// Gets the headers which should be sent with every request of this WebDavSession.
/// </summary>
public HttpRequestHeaders DefaultRequestHeaders
{
get
{
return this.webDavClient.DefaultRequestHeaders;
}
}
/// <summary>
/// Gets or sets a value indicating if <see cref="WebDavSession"/> should process error responses from server. If value is <b>true</b> will throw <see cref="WebDavException"/>;
/// otherwise returns result of operation and caller must check it. Default value is <b>false</b>.
/// </summary>
public bool ThrowExceptions
{
get;
set;
}
#endregion Properties
#region Public methods
#region Copy
/// <summary>
/// Copies a resource from the source URL to the destination URL (without overwriting).
/// </summary>
/// <param name="sourceUrl">The source URL.</param>
/// <param name="destinationUrl">The destination URL.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(string sourceUrl, string destinationUrl)
{
return CopyAsync(UriHelper.CreateUriFromUrl(sourceUrl), UriHelper.CreateUriFromUrl(destinationUrl), false);
}
/// <summary>
/// Copies a resource specified as <see cref="WebDavSessionItem"/> to the destination URL (without overwriting).
/// </summary>
/// <param name="itemToCopy">The <see cref="WebDavSessionItem"/> to copy.</param>
/// <param name="destinationUrl">The destination URL.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(WebDavSessionItem itemToCopy, string destinationUrl)
{
return CopyAsync(itemToCopy, UriHelper.CreateUriFromUrl(destinationUrl), false);
}
/// <summary>
/// Copies a resource from the source <see cref="Uri"/> to the destination <see cref="Uri"/> (without overwriting).
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destinationUri">The destination <see cref="Uri"/>.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(Uri sourceUri, Uri destinationUri)
{
return CopyAsync(sourceUri, destinationUri, false);
}
/// <summary>
/// Copies a resource specified as <see cref="WebDavSessionItem"/> to the destination URL (without overwriting).
/// </summary>
/// <param name="itemToCopy">The <see cref="WebDavSessionItem"/> to copy.</param>
/// <param name="destinationUri">The destination <see cref="Uri"/>.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(WebDavSessionItem itemToCopy, Uri destinationUri)
{
return CopyAsync(itemToCopy, destinationUri, false);
}
/// <summary>
/// Copies a resource from the source URL to the destination URL.
/// </summary>
/// <param name="sourceUrl">The source URL.</param>
/// <param name="destinationUrl">The destination URL.</param>
/// <param name="overwrite">True, if an already existing resource should be overwritten, otherwise false.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(string sourceUrl, string destinationUrl, bool overwrite)
{
return CopyAsync(UriHelper.CreateUriFromUrl(sourceUrl), UriHelper.CreateUriFromUrl(destinationUrl), overwrite);
}
/// <summary>
/// Copies a resource specified as <see cref="WebDavSessionItem"/> to the destination URL.
/// </summary>
/// <param name="itemToCopy">The <see cref="WebDavSessionItem"/> to copy.</param>
/// <param name="destinationUrl">The destination URL.</param>
/// <param name="overwrite">True, if an already existing resource should be overwritten, otherwise false.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(WebDavSessionItem itemToCopy, string destinationUrl, bool overwrite)
{
return CopyAsync(itemToCopy, UriHelper.CreateUriFromUrl(destinationUrl), overwrite);
}
/// <summary>
/// Copies a resource specified as <see cref="WebDavSessionItem"/> to the destination <see cref="Uri"/>.
/// </summary>
/// <param name="itemToCopy">The <see cref="WebDavSessionItem"/> to copy.</param>
/// <param name="destinationUri">The destination <see cref="Uri"/>.</param>
/// <param name="overwrite">True, if an already existing resource should be overwritten, otherwise false.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CopyAsync(WebDavSessionItem itemToCopy, Uri destinationUri, bool overwrite)
{
return CopyAsync(UriHelper.CombineUri(this.BaseUri, itemToCopy.Uri, true), destinationUri, overwrite);
}
/// <summary>
/// Copies a resource from the source <see cref="Uri"/> to the destination <see cref="Uri"/>.
/// </summary>
/// <param name="sourceUri">The source <see cref="Uri"/>.</param>
/// <param name="destinationUri">The destination <see cref="Uri"/>.</param>
/// <param name="overwrite">True, if an already existing resource should be overwritten, otherwise false.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> CopyAsync(Uri sourceUri, Uri destinationUri, bool overwrite)
{
sourceUri = UriHelper.CombineUri(this.BaseUri, sourceUri, true);
destinationUri = UriHelper.CombineUri(this.BaseUri, destinationUri, true);
var lockToken = GetAffectedLockToken(destinationUri);
var response = await webDavClient.CopyAsync(sourceUri, destinationUri, overwrite, WebDavDepthHeaderValue.Infinity, lockToken);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error copying file from '{sourceUri}' to '{destinationUri}'. Response: {response.ReasonPhrase}");
return response.IsSuccessStatusCode;
}
#endregion Copy
#region Create directory
/// <summary>
/// Creates a directory at the URL specified.
/// </summary>
/// <param name="url">The URL of the directory to create.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> CreateDirectoryAsync(string url)
{
return CreateDirectoryAsync(UriHelper.CreateUriFromUrl(url));
}
/// <summary>
/// Creates a directory at the <see cref="Uri"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the directory to create.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> CreateDirectoryAsync(Uri uri)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var lockToken = GetAffectedLockToken(uri);
var response = await this.webDavClient.MkcolAsync(uri, lockToken);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error creating directory '{uri}'. Response: {response.ReasonPhrase}");
return response.IsSuccessStatusCode;
}
#endregion Create directory
#region Delete
/// <summary>
/// Deletes a directory or file at the URL specified.
/// </summary>
/// <param name="url">The URL of the directory or file to delete.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DeleteAsync(string url)
{
return DeleteAsync(UriHelper.CreateUriFromUrl(url));
}
/// <summary>
/// Deletes a directory or file specified by a <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="itemToDelete">The <see cref="WebDavSessionItem"/> to delete.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DeleteAsync(WebDavSessionItem itemToDelete)
{
return DeleteAsync(UriHelper.CombineUri(this.BaseUri, itemToDelete.Uri, true));
}
/// <summary>
/// Deletes a directory or file at the <see cref="Uri"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the directory or file to delete.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> DeleteAsync(Uri uri)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var lockToken = GetAffectedLockToken(uri);
var response = await this.webDavClient.DeleteAsync(uri, lockToken);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error deleting '{uri}'. Response: {response.ReasonPhrase}");
return response.IsSuccessStatusCode;
}
#endregion Delete
#region Download file
/// <summary>
/// Downloads a file from the URL specified.
/// </summary>
/// <param name="url">The URL of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileAsync(string url, Stream localStream)
{
return DownloadFileAsync(UriHelper.CreateUriFromUrl(url), localStream);
}
/// <summary>
/// Downloads a file from the <see cref="Uri"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileAsync(Uri uri, Stream localStream)
{
return DownloadFileAsync(uri, localStream, CancellationToken.None);
}
/// <summary>
/// Downloads a file specified by a <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="itemToDownload">The <see cref="WebDavSessionItem"/> to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileAsync(WebDavSessionItem itemToDownload, Stream localStream)
{
return DownloadFileAsync(itemToDownload, localStream, CancellationToken.None);
}
/// <summary>
/// Downloads a file from the URL specified.
/// </summary>
/// <param name="url">The URL of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <param name="ct">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileAsync(string url, Stream localStream, CancellationToken ct)
{
return DownloadFileAsync(UriHelper.CreateUriFromUrl(url), localStream, ct);
}
/// <summary>
/// Downloads a file specified by a <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="itemToDownload">The <see cref="WebDavSessionItem"/> to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <param name="ct">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileAsync(WebDavSessionItem itemToDownload, Stream localStream, CancellationToken ct)
{
return DownloadFileAsync(UriHelper.CombineUri(this.BaseUri, itemToDownload.Uri, true), localStream, ct);
}
/// <summary>
/// Downloads a file from the <see cref="Uri"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the file to.</param>
/// <param name="ct">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> DownloadFileAsync(Uri uri, Stream localStream, CancellationToken ct)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var response = await this.webDavClient.GetAsync(uri, ct);
if ((!response.IsSuccessStatusCode || response.Content == null) && ThrowExceptions)
throw new WebDavException($"Error downloading file '{uri}'. Response: {response.ReasonPhrase}");
if (response.Content != null)
{
try
{
var contentStream = await response.Content.ReadAsStreamAsync();
await contentStream.CopyToAsync(localStream);
return true;
}
catch (IOException exception)
{
if (ThrowExceptions)
throw new WebDavException($"Error downloading file '{uri}'. Message: {exception.Message}", exception);
return false;
}
}
else
return false;
}
/// <summary>
/// Downloads a file (with progress) from the given URL.
/// </summary>
/// <param name="url">Te URL of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileWithProgressAsync(string url, Stream localStream, IProgress<WebDavProgress> progress)
{
return DownloadFileWithProgressAsync(url, localStream, progress, CancellationToken.None);
}
/// <summary>
/// Downloads a file (with progress) specified by a <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="itemToDownload">The <see cref="WebDavSessionItem"/> to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileWithProgressAsync(WebDavSessionItem itemToDownload, Stream localStream, IProgress<WebDavProgress> progress)
{
return DownloadFileWithProgressAsync(UriHelper.CombineUri(this.BaseUri, itemToDownload.Uri, true), localStream, progress, CancellationToken.None);
}
/// <summary>
/// Downloads a file (with progress) from the given URL.
/// </summary>
/// <param name="url">Te URL of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileWithProgressAsync(string url, Stream localStream, IProgress<WebDavProgress> progress, CancellationToken cancellationToken)
{
return DownloadFileWithProgressAsync(UriHelper.CreateUriFromUrl(url), localStream, progress, cancellationToken);
}
/// <summary>
/// Downloads a file (with progress) specified by a <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="itemToDownload">The <see cref="WebDavSessionItem"/> to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileWithProgressAsync(WebDavSessionItem itemToDownload, Stream localStream, IProgress<WebDavProgress> progress, CancellationToken cancellationToken)
{
return DownloadFileWithProgressAsync(UriHelper.CombineUri(this.BaseUri, itemToDownload.Uri, true), localStream, progress, cancellationToken);
}
/// <summary>
/// Downloads a file (with progress) from the given <see cref="Uri"/>.
/// </summary>
/// <param name="uri">Te <see cref="Uri"/> of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> DownloadFileWithProgressAsync(Uri uri, Stream localStream, IProgress<WebDavProgress> progress)
{
return DownloadFileWithProgressAsync(uri, localStream, progress, CancellationToken.None);
}
/// <summary>
/// Downloads a file (with progress) from the given <see cref="Uri"/>.
/// </summary>
/// <param name="uri">Te <see cref="Uri"/> of the file to download.</param>
/// <param name="localStream">The <see cref="Stream"/> to save the downloaded file to.</param>
/// <param name="progress">An object representing the progress of the operation.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> DownloadFileWithProgressAsync(Uri uri, Stream localStream, IProgress<WebDavProgress> progress, CancellationToken cancellationToken)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var response = await this.webDavClient.DownloadFileWithProgressAsync(uri, localStream, cancellationToken, progress);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error downloading file '{uri}'. Response: {response.ReasonPhrase}");
return response.IsSuccessStatusCode;
}
#endregion Download file
#region Exists
/// <summary>
/// Checks if a file or directory exists at the URL specified.
/// </summary>
/// <param name="url">The URL to check.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<bool> ExistsAsync(string url)
{
return ExistsAsync(UriHelper.CreateUriFromUrl(url));
}
/// <summary>
/// Checks if a file or directory exists at the <see cref="Uri"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> to check.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<bool> ExistsAsync(Uri uri)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var response = await this.webDavClient.HeadAsync(uri);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error check exists '{uri}'. Response: {response.ReasonPhrase}");
return response.IsSuccessStatusCode;
}
#endregion Exists
#region GetSupportedPropNames
/// <summary>
/// Gets the WebDAV property names which are supported by the WebDAV server for the given <see cref="WebDavSessionItem"/>.
/// </summary>
/// <param name="item">The <see cref="WebDavSessionItem"/> to get the supported property names for.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>Not all WebDAV servers return all known property names upon such a request.</remarks>
public async Task<string[]> GetSupportedPropertyNamesAsync(WebDavSessionItem item)
{
return await GetSupportedPropertyNamesAsync(UriHelper.CombineUri(this.BaseUri, item.Uri, true));
}
/// <summary>
/// Gets the WebDAV property names which are supported by the WebDAV server for the element at the given URL.
/// </summary>
/// <param name="url">The URL to get the supported property names for.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>Not all WebDAV servers return all known property names upon such a request.</remarks>
public async Task<string[]> GetSupportedPropertyNamesAsync(string url)
{
return await GetSupportedPropertyNamesAsync(UriHelper.CreateUriFromUrl(url));
}
/// <summary>
/// Gets the WebDAV property names which are supported by the WebDAV server for the element at the given <see cref="Uri"/>.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> to get the supported property names for.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>Not all WebDAV servers return all known property names upon such a request.</remarks>
public async Task<string[]> GetSupportedPropertyNamesAsync(Uri uri)
{
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var propFind = PropFind.CreatePropFindWithPropName();
var response = await this.webDavClient.PropFindAsync(uri, WebDavDepthHeaderValue.Zero, propFind);
if (!response.IsSuccessStatusCode && ThrowExceptions)
throw new WebDavException($"Error while getting supported property names {uri}. Response {response.ReasonPhrase}.");
var propertyNames = await WebDavHelper.GetPropertyNamesKnownAndUnknownFromMultiStatusContentAsync(response.Content);
return propertyNames;
}
#endregion GetSupportedPropNames
#region List
/// <summary>
/// Retrieves a list of files and directories of the directory at the <see cref="Uri"/> specified (using 'allprop').
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the directory which content should be listed. Has to be an absolute URI (including the base URI) or a relative URI (relative to base URI).</param>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>This method uses a so called 'allprop'. A server should return all known properties to the server.
/// If not all of the expected properties are return by the server, use an overload of this method specifying a <see cref="PropFind"/> explicitly.</remarks>
public Task<IList<WebDavSessionItem>> ListAsync(Uri uri, UriKind uriKind = UriKind.Absolute)
{
return ListAsync(uri, PropFind.CreatePropFindAllProp(), uriKind);
}
/// <summary>
/// Retrieves a list of files and directories of the directory at the URL specified.
/// </summary>
/// <param name="url">The URL of the directory which content should be listed. Has to be an absolute URL (including the base URL) or a relative URL (relative to base URL).</param>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <remarks>This method uses a so called 'allprop'. A server should return all known properties to the server.
/// If not all of the expected properties are return by the server, use an overload of this method specifying a <see cref="PropFind"/> explicitly.</remarks>
public Task<IList<WebDavSessionItem>> ListAsync(string url, UriKind uriKind = UriKind.Absolute)
{
return ListAsync(UriHelper.CreateUriFromUrl(url), uriKind);
}
/// <summary>
/// Retrieves a list of files and directories of the directory from the <see cref="WebDavSessionItem"/> specified
/// </summary>
/// <param name="item">The <see cref="WebDavSessionItem"/> which content should be listed.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <remarks>This method uses a so called 'allprop'. A server should return all known properties to the server.
/// If not all of the expected properties are return by the server, use an overload of this method specifying a <see cref="PropFind"/> explicitly.</remarks>
public Task<IList<WebDavSessionItem>> ListAsync(WebDavSessionItem item, UriKind uriKind = UriKind.Absolute)
{
return ListAsync(UriHelper.CombineUri(this.BaseUri, item.Uri, true), uriKind);
}
/// <summary>
/// Retrieves a list of files and directories of the directory at the URL specified.
/// </summary>
/// <param name="url">The URL of the directory which content should be listed. Has to be an absolute URL (including the base URL) or a relative URL (relative to base URL).</param>
/// <param name="propFind">The <see cref="PropFind"/> to use. Different PropFind types can be created using the static methods of the class <see cref="PropFind"/>.</param>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<IList<WebDavSessionItem>> ListAsync(string url, PropFind propFind, UriKind uriKind = UriKind.Absolute)
{
return ListAsync(UriHelper.CreateUriFromUrl(url), propFind, uriKind);
}
/// <summary>
/// Retrieves a list of files and directories of the directory from the <see cref="WebDavSessionItem"/> specified using the <see cref="PropFind"/> specified.
/// </summary>
/// <param name="item">The <see cref="WebDavSessionItem"/> which content should be listed. Has to be an absolute URI (including the base URI) or a relative URI (relative to base URI).</param>
/// <param name="propFind">The <see cref="PropFind"/> to use. Different PropFind types can be created using the static methods of the class <see cref="PropFind"/>.</param>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public Task<IList<WebDavSessionItem>> ListAsync(WebDavSessionItem item, PropFind propFind, UriKind uriKind = UriKind.Absolute)
{
return ListAsync(UriHelper.CombineUri(this.BaseUri, item.Uri, true), propFind, uriKind);
}
/// <summary>
/// Retrieves a list of files and directories of the directory at the <see cref="Uri"/> specified using the <see cref="PropFind"/> specified.
/// </summary>
/// <param name="uri">The <see cref="Uri"/> of the directory which content should be listed. Has to be an absolute URI (including the base URI) or a relative URI (relative to base URI).</param>
/// <param name="propFind">The <see cref="PropFind"/> to use. Different PropFind types can be created using the static methods of the class <see cref="PropFind"/>.</param>
/// <param name="uriKind">The <see cref="UriKind"/> of the resulting <see cref="Uri"/>s in the <see cref="WebDavSessionItem"/>s. Default is <see cref="UriKind.Absolute"/>. If <see cref="UriKind.RelativeOrAbsolute"/> is specified, the resulting <see cref="Uri"/>s are absolute Uris.</param>
/// <returns>The <see cref="Task"/> representing the asynchronous operation.</returns>
public async Task<IList<WebDavSessionItem>> ListAsync(Uri uri, PropFind propFind, UriKind uriKind = UriKind.Absolute)
{
if (propFind == null)
throw new ArgumentException("Argument propFind must not be null.");
uri = UriHelper.CombineUri(this.BaseUri, uri, true);
var response = await this.webDavClient.PropFindAsync(uri, WebDavDepthHeaderValue.One, propFind);
// Remember the original port to include it in the hrefs later.
var port = UriHelper.GetPort(uri);
if (response.StatusCode != WebDavStatusCode.MultiStatus)
throw new WebDavException($"Error while executing ListAsync (wrong response status code). Expected status code: 207 (MultiStatus); actual status code: {(int)response.StatusCode} ({response.StatusCode})");
var multistatus = await WebDavResponseContentParser.ParseMultistatusResponseContentAsync(response.Content);
var itemList = new List<WebDavSessionItem>();
foreach (var responseItem in multistatus.Response)
{
Uri webDavSessionItemUri = null;
string webDavSessionItemName = string.Empty;
string webDavSessionItemDisplayName = string.Empty;
DateTime? webDavSessionItemCreationDate = null;
string webDavSessionItemContentLanguage = string.Empty;
long? webDavSessionItemContentLength = null;
string webDavSessionItemContentType = string.Empty;
string webDavSessionItemETag = string.Empty;
DateTime? webDavSessionItemLastModified = null;
long? webDavSessionItemQuotaAvailableBytes = null;
long? webDavSessionItemQuotaUsedBytes = null;
long? webDavSessionItemChildCount = null;
string webDavSessionItemDefaultDocument = string.Empty;
string webDavSessionItemId = string.Empty;
bool? webDavSessionItemIsStructuredDocument = null;
bool? webDavSessionItemHasSubDirectories = null;
bool? webDavSessionItemNoSubDirectoriesAllowed = null;
long? webDavSessionItemFileCount = null;
bool? webDavSessionItemIsReserved = null;
long? webDavSessionItemVisibleFiles = null;
string webDavSessionItemContentClass = string.Empty;
bool? webDavSessionItemIsReadonly = null;
bool? webDavSessionItemIsRoot = null;
DateTime? webDavSessionItemLastAccessed = null;
string webDavSessionItemParentName = string.Empty;
bool? webDavSessionItemIsFolder = null;
XElement[] webDavSessionItemAdditionalProperties = null;
Uri href = null;
if (!string.IsNullOrEmpty(responseItem.Href))
{
if (uriKind == UriKind.Relative)
{
var hrefTmp = responseItem.Href;
if (!string.IsNullOrEmpty(this.BaseUrl) && hrefTmp.StartsWith(this.BaseUrl))
hrefTmp = hrefTmp.Remove(0, this.BaseUrl.Length);
if (UriHelper.TryCreateUriFromUrl(hrefTmp, out href))
webDavSessionItemUri = href;
}
else
{
if (UriHelper.TryCreateUriFromUrl(responseItem.Href, out href))
{
var fullQualifiedUri = UriHelper.CombineUri(uri, href, true);
fullQualifiedUri = UriHelper.SetPort(fullQualifiedUri, port);
webDavSessionItemUri = fullQualifiedUri;
}
}
}
// Skip the folder which contents were requested, only add children.
if (href != null && WebUtility.UrlDecode(UriHelper.RemovePort(uri).ToString().Trim('/')).EndsWith(WebUtility.UrlDecode(UriHelper.RemovePort(href).ToString().Trim('/')), StringComparison.OrdinalIgnoreCase))
continue;
foreach (var item in responseItem.Items)
{
var propStat = item as Propstat;
// Do not items where no properties could be found.
if (propStat == null || !PropstatHelper.IsSuccessStatusCode(propStat.Status))
continue;
var prop = propStat.Prop;
// Do not add hidden items.
if (prop.IsHidden ?? false)
continue;
webDavSessionItemDisplayName = prop.DisplayName;
webDavSessionItemCreationDate = prop.CreationDate;
webDavSessionItemContentLanguage = prop.GetContentLanguage;
webDavSessionItemContentLength = prop.GetContentLength;
webDavSessionItemContentType = prop.GetContentType;
webDavSessionItemETag = prop.GetEtag;
webDavSessionItemLastModified = prop.GetLastModified;
// RFC 4331
webDavSessionItemQuotaAvailableBytes = prop.QuotaAvailableBytes;
webDavSessionItemQuotaUsedBytes = prop.QuotaUsedBytes;
// Additional WebDAV Collection Properties
webDavSessionItemChildCount = prop.ChildCount;
webDavSessionItemDefaultDocument = prop.DefaultDocument;