Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MicrosoftGraphBackend.cs « OneDrive « Backend « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6bf0bf02fe2d955d06bef3e3ee94c719b12a71a6 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
using Duplicati.Library.Backend.MicrosoftGraph;
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using Duplicati.Library.Logging;
using Duplicati.Library.Utility;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend
{
    /// <summary>
    /// Base class for all backends based on the Microsoft Graph API:
    /// https://developer.microsoft.com/en-us/graph/
    /// </summary>
    /// <remarks>
    /// HttpClient is used instead of OAuthHelper because OAuthHelper internally converts URLs to System.Uri, which throws UriFormatException when the URL contains ':' characters.
    /// https://stackoverflow.com/questions/2143856/why-does-colon-in-uri-passed-to-uri-makerelativeuri-cause-an-exception
    /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/bf11fc74-975a-4c4d-8335-8e0579d17fdf/uri-containing-colons-incorrectly-throws-uriformatexception?forum=netfxbcl
    /// 
    /// Note that instead of using Task.Result to wait for the results of asynchronous operations,
    /// this class uses the Utility.Await() extension method, since it doesn't wrap exceptions in AggregateExceptions.
    /// </remarks>
    public abstract class MicrosoftGraphBackend : IBackend, IStreamingBackend, IQuotaEnabledBackend, IRenameEnabledBackend
    {
        private static readonly string LOGTAG = Log.LogTagFromType<MicrosoftGraphBackend>();

        private const string SERVICES_AGREEMENT = "https://www.microsoft.com/en-us/servicesagreement";
        private const string PRIVACY_STATEMENT = "https://privacy.microsoft.com/en-us/privacystatement";

        private const string BASE_ADDRESS = "https://graph.microsoft.com";

        private const string AUTHID_OPTION = "authid";
        private const string UPLOAD_SESSION_FRAGMENT_SIZE_OPTION = "fragment-size";
        private const string UPLOAD_SESSION_FRAGMENT_RETRY_COUNT_OPTION = "fragment-retry-count";
        private const string UPLOAD_SESSION_FRAGMENT_RETRY_DELAY_OPTION = "fragment-retry-delay";
        private const string USE_HTTP_CLIENT = "use-http-client";

        private const int UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_COUNT = 5;
        private const int UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_DELAY = 1000;

        /// <summary>
        /// Max size of file that can be uploaded in a single PUT request is 4 MB:
        /// https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/driveitem_put_content
        /// </summary>
        private const int PUT_MAX_SIZE = 4 * 1000 * 1000;

        /// <summary>
        /// Max size of each individual upload in an upload session is 60 MiB:
        /// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
        /// </summary>
        private const int UPLOAD_SESSION_FRAGMENT_MAX_SIZE = 60 * 1024 * 1024;

        /// <summary>
        /// Default fragment size of 10 MiB, as the documentation recommends something in the range of 5-10 MiB,
        /// and it still complies with the 320 KiB multiple requirement.
        /// </summary>
        private const int UPLOAD_SESSION_FRAGMENT_DEFAULT_SIZE = 10 * 1024 * 1024;

        /// <summary>
        /// Each fragment in an upload session must be a size that is multiple of this size.
        /// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
        /// There is some confusion in the docs as to whether this is actually required, however...
        /// </summary>
        private const int UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE = 320 * 1024;

        /// <summary>
        /// Whether to use the HttpClient class for HTTP requests.
        /// Default is false when running under Mono (as it seems it might be causing a memory leak in some environments / versions)
        /// but true in other cases (where these memory leaks haven't been reproduced).
        /// </summary>
        private static readonly bool USE_HTTP_CLIENT_DEFAULT = Utility.Utility.IsMono ? false : true;

        private static readonly HttpMethod PatchMethod = new HttpMethod("PATCH");

        /// <summary>
        /// Dummy UploadSession given as an empty body to createUploadSession requests when using the OAuthHelper instead of the OAuthHttpClient.
        /// The API expects a ContentLength to be specified, but the body content is optional.
        /// Passing an empty object (or specifying the ContentLength explicitly) bypasses this error.
        /// </summary>
        private static readonly UploadSession dummyUploadSession = new UploadSession();

        protected delegate string DescriptionTemplateDelegate(string mssadescription, string mssalink, string msopdescription, string msoplink);

        private readonly JsonSerializer m_serializer = new JsonSerializer();
        private readonly OAuthHttpClient m_client;
        private readonly OAuthHelper m_oAuthHelper;
        private readonly int fragmentSize;
        private readonly int fragmentRetryCount;
        private readonly int fragmentRetryDelay; // In milliseconds

        // Whenever a response includes a Retry-After header, we'll update this timestamp with when we can next
        // send a request. And before sending any requests, we'll make sure to wait until at least this time.
        // Since this may be read and written by multiple threads, it is stored as a long and updated using Interlocked.Exchange.
        private readonly RetryAfterHelper m_retryAfter;

        private string[] dnsNames = null;

        private readonly Lazy<string> rootPathFromURL;
        private string RootPath => this.rootPathFromURL.Value;

        protected MicrosoftGraphBackend() { } // Constructor needed for dynamic loading to find it

        protected MicrosoftGraphBackend(string url, string protocolKey, Dictionary<string, string> options)
        {
            string authid;
            options.TryGetValue(AUTHID_OPTION, out authid);
            if (string.IsNullOrEmpty(authid))
                throw new UserInformationException(Strings.MicrosoftGraph.MissingAuthId(OAuthHelper.OAUTH_LOGIN_URL(protocolKey)), "MicrosoftGraphBackendMissingAuthId");

            string fragmentSizeStr;
            if (options.TryGetValue(UPLOAD_SESSION_FRAGMENT_SIZE_OPTION, out fragmentSizeStr) && int.TryParse(fragmentSizeStr, out this.fragmentSize))
            {
                // Make sure the fragment size is a multiple of the desired multiple size.
                // If it isn't, we round down to the nearest multiple below it.
                this.fragmentSize = (this.fragmentSize / UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE) * UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE;

                // Make sure the fragment size isn't larger than the maximum, or smaller than the minimum
                this.fragmentSize = Math.Max(Math.Min(this.fragmentSize, UPLOAD_SESSION_FRAGMENT_MAX_SIZE), UPLOAD_SESSION_FRAGMENT_MULTIPLE_SIZE);
            }
            else
            {
                this.fragmentSize = UPLOAD_SESSION_FRAGMENT_DEFAULT_SIZE;
            }

            string fragmentRetryCountStr;
            if (!(options.TryGetValue(UPLOAD_SESSION_FRAGMENT_RETRY_COUNT_OPTION, out fragmentRetryCountStr) && int.TryParse(fragmentRetryCountStr, out this.fragmentRetryCount)))
            {
                this.fragmentRetryCount = UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_COUNT;
            }

            string fragmentRetryDelayStr;
            if (!(options.TryGetValue(UPLOAD_SESSION_FRAGMENT_RETRY_DELAY_OPTION, out fragmentRetryDelayStr) && int.TryParse(fragmentRetryDelayStr, out this.fragmentRetryDelay)))
            {
                this.fragmentRetryDelay = UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_DELAY;
            }

            bool useHttpClient;
            string useHttpClientStr;
            if (options.TryGetValue(USE_HTTP_CLIENT, out useHttpClientStr))
            {
                useHttpClient = Utility.Utility.ParseBool(useHttpClientStr, USE_HTTP_CLIENT_DEFAULT);
            }
            else
            {
                useHttpClient = USE_HTTP_CLIENT_DEFAULT;
            }

            if (useHttpClient)
            {
                this.m_client = new OAuthHttpClient(authid, protocolKey);
                this.m_client.BaseAddress = new System.Uri(BASE_ADDRESS);
            }
            else
            {
                this.m_oAuthHelper = new OAuthHelper(authid, protocolKey);
                this.m_oAuthHelper.AutoAuthHeader = true;
            }

            this.m_retryAfter = new RetryAfterHelper();

            // Extract out the path to the backup root folder from the given URI.  Since this can be an expensive operation, 
            // we will cache the value using a lazy initializer.
            this.rootPathFromURL = new Lazy<string>(() => MicrosoftGraphBackend.NormalizeSlashes(this.GetRootPathFromUrl(url)));
        }

        public abstract string ProtocolKey { get; }

        public abstract string DisplayName { get; }

        public string Description
        {
            get
            {
                return this.DescriptionTemplate(
                    "Microsoft Service Agreement",
                    SERVICES_AGREEMENT,
                    "Microsoft Online Privacy Statement", 
                    PRIVACY_STATEMENT); 
            }
        }

        public IList<ICommandLineArgument> SupportedCommands
        {
            get
            {
                return new[]
                {
                    new CommandLineArgument(AUTHID_OPTION, CommandLineArgument.ArgumentType.Password, Strings.MicrosoftGraph.AuthIdShort, Strings.MicrosoftGraph.AuthIdLong(OAuthHelper.OAUTH_LOGIN_URL(this.ProtocolKey))),
                    new CommandLineArgument(UPLOAD_SESSION_FRAGMENT_SIZE_OPTION, CommandLineArgument.ArgumentType.Integer, Strings.MicrosoftGraph.FragmentSizeShort, Strings.MicrosoftGraph.FragmentSizeLong, Library.Utility.Utility.FormatSizeString(UPLOAD_SESSION_FRAGMENT_DEFAULT_SIZE)),
                    new CommandLineArgument(UPLOAD_SESSION_FRAGMENT_RETRY_COUNT_OPTION, CommandLineArgument.ArgumentType.Integer, Strings.MicrosoftGraph.FragmentRetryCountShort, Strings.MicrosoftGraph.FragmentRetryCountLong, UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_COUNT.ToString()),
                    new CommandLineArgument(UPLOAD_SESSION_FRAGMENT_RETRY_DELAY_OPTION, CommandLineArgument.ArgumentType.Integer, Strings.MicrosoftGraph.FragmentRetryDelayShort, Strings.MicrosoftGraph.FragmentRetryDelayLong, UPLOAD_SESSION_FRAGMENT_DEFAULT_RETRY_DELAY.ToString()),
                    new CommandLineArgument(USE_HTTP_CLIENT, CommandLineArgument.ArgumentType.Boolean, Strings.MicrosoftGraph.UseHttpClientShort, Strings.MicrosoftGraph.UseHttpClientLong, USE_HTTP_CLIENT_DEFAULT.ToString()),
                }
                .Concat(this.AdditionalSupportedCommands).ToList();
            }
        }

        public string[] DNSName
        {
            get
            {
                if (this.dnsNames == null)
                {
                    // The DNS names that this instance may need to access include:
                    // - Core graph API endpoint
                    // - Upload session endpoint (which seems to be different depending on the drive being accessed - not sure if it can vary for a single drive)
                    // To get the upload session endpoint, we can start an upload session and then immediately cancel it.
                    // We pick a random file name (using a guid) to make sure we don't conflict with an existing file
                    string dnsTestFile = string.Format("DNSNameTest-{0}", Guid.NewGuid());
                    UploadSession uploadSession = this.Post<UploadSession>(string.Format("{0}/root:{1}{2}:/createUploadSession", this.DrivePrefix, this.RootPath, NormalizeSlashes(dnsTestFile)), MicrosoftGraphBackend.dummyUploadSession);

                    // Canceling an upload session is done by sending a DELETE to the upload URL
                    m_retryAfter.WaitForRetryAfter();
                    if (this.m_client != null)
                    {
                        using (var request = new HttpRequestMessage(HttpMethod.Delete, uploadSession.UploadUrl))
                        using (var response = this.m_client.SendAsync(request).Await())
                        {
                            this.CheckResponse(response);
                        }
                    }
                    else
                    {
                        using (var response = this.m_oAuthHelper.GetResponseWithoutException(uploadSession.UploadUrl, MicrosoftGraphBackend.dummyUploadSession, HttpMethod.Delete.ToString()))
                        {
                            this.CheckResponse(response);
                        }
                    }

                    this.dnsNames = new[]
                        {
                            new System.Uri(BASE_ADDRESS).Host,
                            new System.Uri(uploadSession.UploadUrl).Host,
                        }
                        .Distinct(StringComparer.OrdinalIgnoreCase)
                        .ToArray();
                }

                return this.dnsNames;
            }
        }

        public IQuotaInfo Quota
        {
            get
            {
                Drive driveInfo = this.Get<Drive>(this.DrivePrefix);
                if (driveInfo.Quota != null)
                {
                    // Some sources (SharePoint for example) seem to return 0 for these values even when the quota isn't exceeded..
                    // As a special test, if all the returned values are 0, we pretend that no quota was reported.
                    // This way we don't send spurious warnings because the quota looks like it is exceeded.
                    if (driveInfo.Quota.Total != 0 || driveInfo.Quota.Remaining != 0 || driveInfo.Quota.Used != 0)
                    {
                        return new QuotaInfo(driveInfo.Quota.Total, driveInfo.Quota.Remaining);
                    }
                }

                return null;
            }
        }

        /// <summary>
        /// Override-able fragment indicating the API version to use each query
        /// </summary>
        protected virtual string ApiVersion
        {
            get { return "/v1.0"; }
        }

        /// <summary>
        /// Normalized fragment (starting with a slash and ending without one) for the path to the drive to be used.
        /// For example: "/me/drive" for the default drive for a user.
        /// </summary>
        protected abstract string DrivePath { get; }

        protected abstract DescriptionTemplateDelegate DescriptionTemplate { get; }

        protected virtual IList<ICommandLineArgument> AdditionalSupportedCommands
        {
            get
            {
                return new ICommandLineArgument[0];
            }
        }

        private string DrivePrefix
        {
            get
            {
                if (this.m_client != null)
                {
                    return this.ApiVersion + this.DrivePath;
                }
                else
                {
                    // When not using the HttpClient for requests, the base address needs to be included in this prefix
                    return BASE_ADDRESS + this.ApiVersion + this.DrivePath;
                }
            }
        }

        public void CreateFolder()
        {
            string parentFolder = "root";
            string parentFolderPath = string.Empty;
            foreach (string folder in this.RootPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries))
            {
                string nextPath = parentFolderPath + "/" + folder;
                DriveItem folderItem;
                try
                {
                    folderItem = this.Get<DriveItem>(string.Format("{0}/root:{1}", this.DrivePrefix, NormalizeSlashes(nextPath)));
                }
                catch (DriveItemNotFoundException)
                {
                    DriveItem newFolder = new DriveItem()
                    {
                        Name = folder,
                        Folder = new FolderFacet(),
                    };

                    folderItem = this.Post(string.Format("{0}/items/{1}/children", this.DrivePrefix, parentFolder), newFolder);
                }

                parentFolder = folderItem.Id;
                parentFolderPath = nextPath;
            }
        }

        public IEnumerable<IFileEntry> List()
        {
            foreach (DriveItem item in this.Enumerate<DriveItem>(string.Format("{0}/root:{1}:/children", this.DrivePrefix, this.RootPath)))
            {
                // Exclude non-files and deleted items (not sure if they show up in this listing, but make sure anyway)
                if (item.IsFile && !item.IsDeleted)
                {
                    yield return new FileEntry(
                        item.Name,
                        item.Size ?? 0, // Files should always have a size, but folders don't need it
                        item.FileSystemInfo?.LastAccessedDateTime?.UtcDateTime ?? new DateTime(),
                        item.FileSystemInfo?.LastModifiedDateTime?.UtcDateTime ?? item.LastModifiedDateTime?.UtcDateTime ?? new DateTime());
                }
            }
        }

        public void Get(string remotename, string filename)
        {
            using (FileStream fileStream = File.OpenWrite(filename))
            {
                this.Get(remotename, fileStream);
            }
        }

        public void Get(string remotename, Stream stream)
        {
            try
            {
                m_retryAfter.WaitForRetryAfter();
                string getUrl = string.Format("{0}/root:{1}{2}:/content", this.DrivePrefix, this.RootPath, NormalizeSlashes(remotename));
                if (this.m_client != null)
                {
                    using (var response = this.m_client.GetAsync(getUrl).Await())
                    {
                        this.CheckResponse(response);
                        using (Stream responseStream = response.Content.ReadAsStreamAsync().Await())
                        {
                            responseStream.CopyTo(stream);
                        }
                    }
                }
                else
                {
                    using (var response = this.m_oAuthHelper.GetResponseWithoutException(getUrl))
                    {
                        this.CheckResponse(response);
                        using (Stream responseStream = response.GetResponseStream())
                        {
                            responseStream.CopyTo(stream);
                        }
                    }
                }
            }
            catch (DriveItemNotFoundException ex)
            {
                // If the item wasn't found, wrap the exception so normal handling can occur.
                throw new FileMissingException(ex);
            }
        }

        public void Rename(string oldname, string newname)
        {
            try
            {
                this.Patch(string.Format("{0}/root:{1}{2}", this.DrivePrefix, this.RootPath, NormalizeSlashes(oldname)), new DriveItem() { Name = newname });
            }
            catch (DriveItemNotFoundException ex)
            {
                // If the item wasn't found, wrap the exception so normal handling can occur.
                throw new FileMissingException(ex);
            }
        }

        public async Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
        {
            using (FileStream fileStream = File.OpenRead(filename))
            {
                await PutAsync(remotename, fileStream, cancelToken).ConfigureAwait(false);
            }
        }

        public async Task PutAsync(string remotename, Stream stream, CancellationToken cancelToken)
        {
            // PUT only supports up to 4 MB file uploads. There's a separate process for larger files.
            if (stream.Length < PUT_MAX_SIZE)
            {
                await m_retryAfter.WaitForRetryAfterAsync(cancelToken).ConfigureAwait(false);
                string putUrl = string.Format("{0}/root:{1}{2}:/content", this.DrivePrefix, this.RootPath, NormalizeSlashes(remotename));
                if (this.m_client != null)
                {
                    using (StreamContent streamContent = new StreamContent(stream))
                    {
                        streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                        using (var response = await this.m_client.PutAsync(putUrl, streamContent, cancelToken).ConfigureAwait(false))
                        {
                            // Make sure this response is a valid drive item, though we don't actually use it for anything currently.
                            this.ParseResponse<DriveItem>(response);
                        }
                    }
                }
                else
                {
                    using (var response = await this.m_oAuthHelper.GetResponseWithoutExceptionAsync(putUrl, cancelToken, stream, HttpMethod.Put.ToString()).ConfigureAwait(false))
                    {
                        // Make sure this response is a valid drive item, though we don't actually use it for anything currently.
                        this.ParseResponse<DriveItem>(response);
                    }
                }
            }
            else
            {
                // This file is too large to be sent in a single request, so we need to send it in pieces in an upload session:
                // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession
                // The documentation seems somewhat contradictory - it states that uploads must be done sequentially,
                // but also states that the nextExpectedRanges value returned may indicate multiple ranges...
                // For now, this plays it safe and does a sequential upload.
                string createSessionUrl = string.Format("{0}/root:{1}{2}:/createUploadSession", this.DrivePrefix, this.RootPath, NormalizeSlashes(remotename));
                if (this.m_client != null)
                {
                    await m_retryAfter.WaitForRetryAfterAsync(cancelToken).ConfigureAwait(false);
                    using (HttpRequestMessage createSessionRequest = new HttpRequestMessage(HttpMethod.Post, createSessionUrl))
                    using (HttpResponseMessage createSessionResponse = await this.m_client.SendAsync(createSessionRequest, cancelToken).ConfigureAwait(false))
                    {
                        UploadSession uploadSession = this.ParseResponse<UploadSession>(createSessionResponse);

                        // If the stream's total length is less than the chosen fragment size, then we should make the buffer only as large as the stream.
                        int bufferSize = (int)Math.Min(this.fragmentSize, stream.Length);

                        long read = 0;
                        for (long offset = 0; offset < stream.Length; offset += read)
                        {
                            // If the stream isn't long enough for this to be a full buffer, then limit the length
                            long currentBufferSize = bufferSize;
                            if (stream.Length < offset + bufferSize)
                            {
                                currentBufferSize = stream.Length - offset;
                            }

                            using (Stream subStream = new ReadLimitLengthStream(stream, offset, currentBufferSize))
                            {
                                read = subStream.Length;

                                int fragmentCount = (int)Math.Ceiling((double)stream.Length / bufferSize);
                                int retryCount = this.fragmentRetryCount;
                                for (int attempt = 0; attempt < retryCount; attempt++)
                                {
                                    await m_retryAfter.WaitForRetryAfterAsync(cancelToken).ConfigureAwait(false);

                                    int fragmentNumber = (int)(offset / bufferSize);
                                    Log.WriteVerboseMessage(
                                        LOGTAG,
                                        "MicrosoftGraphFragmentUpload",
                                        "Uploading fragment {0}/{1} of remote file {2}",
                                        fragmentNumber,
                                        fragmentCount,
                                        remotename);

                                    using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, uploadSession.UploadUrl))
                                    using (StreamContent fragmentContent = new StreamContent(subStream))
                                    {
                                        fragmentContent.Headers.ContentLength = read;
                                        fragmentContent.Headers.ContentRange = new ContentRangeHeaderValue(offset, offset + read - 1, stream.Length);

                                        request.Content = fragmentContent;

                                        try
                                        {
                                            // The uploaded put requests will error if they are authenticated
                                            using (HttpResponseMessage response = await this.m_client.SendAsync(request, false, cancelToken).ConfigureAwait(false))
                                            {
                                                // Note: On the last request, the json result includes the default properties of the item that was uploaded
                                                this.ParseResponse<UploadSession>(response);
                                            }
                                        }
                                        catch (MicrosoftGraphException ex)
                                        {
                                            if (subStream.Position != 0)
                                            {
                                                if (subStream.CanSeek)
                                                {
                                                    // Make sure to reset the substream to its start in case this is a retry
                                                    subStream.Seek(0, SeekOrigin.Begin);
                                                }
                                                else
                                                {
                                                    // If any of the source stream was read and the substream can't be seeked back to the beginning,
                                                    // then the internal retry mechanism can't be used and the caller will have to retry this whole file.
                                                    // Should we consider signaling to the graph API that we're abandoning this upload session?
                                                    await this.ThrowUploadSessionException(
                                                        uploadSession,
                                                        createSessionResponse,
                                                        fragmentNumber,
                                                        fragmentCount,
                                                        ex,
                                                        cancelToken).ConfigureAwait(false);
                                                }
                                            }
                                            
                                            // Error handling based on recommendations here:
                                            // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession#best-practices
                                            if (attempt >= retryCount - 1)
                                            {
                                                // We've used up all our retry attempts
                                                await this.ThrowUploadSessionException(
                                                    uploadSession,
                                                    createSessionResponse,
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    ex,
                                                    cancelToken).ConfigureAwait(false);
                                            }
                                            else if ((int)ex.StatusCode >= 500 && (int)ex.StatusCode < 600)
                                            {
                                                // If a 5xx error code is hit, we should use an exponential backoff strategy before retrying.
                                                // To make things simpler, we just use the current attempt number as the exponential factor.
                                                // If there was a Retry-After header, we'll wait for that right before sending the next request as well.
                                                TimeSpan delay = TimeSpan.FromMilliseconds((int)Math.Pow(2, attempt) * this.fragmentRetryDelay);

                                                Log.WriteRetryMessage(
                                                    LOGTAG,
                                                    "MicrosoftGraphFragmentRetryIn",
                                                    ex,
                                                    "Uploading fragment {0}/{1} of remote file {2} failed and will be retried in {3}",
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    remotename,
                                                    delay);

                                                await Task.Delay(delay).ConfigureAwait(false);
                                                continue;
                                            }
                                            else if (ex.StatusCode == HttpStatusCode.NotFound)
                                            {
                                                // 404 is a special case indicating the upload session no longer exists, so the fragment shouldn't be retried.
                                                // Instead we'll let the caller re-attempt the whole file.
                                                await this.ThrowUploadSessionException(
                                                    uploadSession,
                                                    createSessionResponse,
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    ex,
                                                    cancelToken).ConfigureAwait(false);
                                            }
                                            else if ((int)ex.StatusCode >= 400 && (int)ex.StatusCode < 500)
                                            {
                                                // If a 4xx error code is hit, we should retry without the exponential backoff attempt.
                                                Log.WriteRetryMessage(
                                                    LOGTAG,
                                                    "MicrosoftGraphFragmentRetry",
                                                    ex,
                                                    "Uploading fragment {0}/{1} of remote file {2} failed and will be retried",
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    remotename);

                                                continue;
                                            }
                                            else
                                            {
                                                // Other errors should be rethrown
                                                await this.ThrowUploadSessionException(
                                                    uploadSession,
                                                    createSessionResponse,
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    ex,
                                                    cancelToken).ConfigureAwait(false);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            // Any other exceptions should also cause the upload session to be canceled.
                                            await this.ThrowUploadSessionException(
                                                uploadSession,
                                                createSessionResponse,
                                                fragmentNumber,
                                                fragmentCount,
                                                ex,
                                                cancelToken).ConfigureAwait(false);
                                        }

                                        // If we successfully sent this piece, then we can break out of the retry loop
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    await m_retryAfter.WaitForRetryAfterAsync(cancelToken).ConfigureAwait(false);
                    using (HttpWebResponse createSessionResponse = await this.m_oAuthHelper.GetResponseWithoutExceptionAsync(createSessionUrl, cancelToken, MicrosoftGraphBackend.dummyUploadSession, HttpMethod.Post.ToString()).ConfigureAwait(false))
                    {
                        UploadSession uploadSession = this.ParseResponse<UploadSession>(createSessionResponse);

                        // If the stream's total length is less than the chosen fragment size, then we should make the buffer only as large as the stream.
                        int bufferSize = (int)Math.Min(this.fragmentSize, stream.Length);

                        long read = 0;
                        for (long offset = 0; offset < stream.Length; offset += read)
                        {
                            // If the stream isn't long enough for this to be a full buffer, then limit the length
                            long currentBufferSize = bufferSize;
                            if (stream.Length < offset + bufferSize)
                            {
                                currentBufferSize = stream.Length - offset;
                            }

                            using (Stream subStream = new ReadLimitLengthStream(stream, offset, currentBufferSize))
                            {
                                read = subStream.Length;

                                int fragmentCount = (int)Math.Ceiling((double)stream.Length / bufferSize);
                                int retryCount = this.fragmentRetryCount;
                                for (int attempt = 0; attempt < retryCount; attempt++)
                                {
                                    await m_retryAfter.WaitForRetryAfterAsync(cancelToken).ConfigureAwait(false);

                                    int fragmentNumber = (int)(offset / bufferSize);
                                    Log.WriteVerboseMessage(
                                        LOGTAG,
                                        "MicrosoftGraphFragmentUpload",
                                        "Uploading fragment {0}/{1} of remote file {2}",
                                        fragmentNumber,
                                        fragmentCount,
                                        remotename);

                                    // The uploaded put requests will error if they are authenticated
                                    var request = new AsyncHttpRequest(this.m_oAuthHelper.CreateRequest(uploadSession.UploadUrl, HttpMethod.Put.ToString(), true));
                                    request.Request.ContentLength = read;
                                    request.Request.Headers.Set(HttpRequestHeader.ContentRange, new ContentRangeHeaderValue(offset, offset + read - 1, stream.Length).ToString());
                                    request.Request.ContentType = "application/octet-stream";

                                    using (var requestStream = request.GetRequestStream(read))
                                    {
                                        await Utility.Utility.CopyStreamAsync(subStream, requestStream, cancelToken).ConfigureAwait(false);
                                    }

                                    try
                                    {
                                        using (var response = await this.m_oAuthHelper.GetResponseWithoutExceptionAsync(request, cancelToken).ConfigureAwait(false))
                                        {
                                            // Note: On the last request, the json result includes the default properties of the item that was uploaded
                                            this.ParseResponse<UploadSession>(response);
                                        }
                                    }
                                    catch (MicrosoftGraphException ex)
                                    {
                                        if (subStream.Position != 0)
                                        {
                                            if (subStream.CanSeek)
                                            {
                                                // Make sure to reset the substream to its start in case this is a retry
                                                subStream.Seek(0, SeekOrigin.Begin);
                                            }
                                            else
                                            {
                                                // If any of the source stream was read and the substream can't be seeked back to the beginning,
                                                // then the internal retry mechanism can't be used and the caller will have to retry this whole file.
                                                // Should we consider signaling to the graph API that we're abandoning this upload session?
                                                await this.ThrowUploadSessionException(
                                                    uploadSession,
                                                    createSessionResponse,
                                                    fragmentNumber,
                                                    fragmentCount,
                                                    ex,
                                                    cancelToken).ConfigureAwait(false);
                                            }
                                        }

                                        // Error handling based on recommendations here:
                                        // https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession#best-practices
                                        if (attempt >= retryCount - 1)
                                        {
                                            // We've used up all our retry attempts
                                            await this.ThrowUploadSessionException(
                                                uploadSession,
                                                createSessionResponse,
                                                fragmentNumber,
                                                fragmentCount,
                                                ex,
                                                cancelToken).ConfigureAwait(false);
                                        }
                                        else if ((int)ex.StatusCode >= 500 && (int)ex.StatusCode < 600)
                                        {
                                            // If a 5xx error code is hit, we should use an exponential backoff strategy before retrying.
                                            // To make things simpler, we just use the current attempt number as the exponential factor.
                                            // If there was a Retry-After header, we'll wait for that right before sending the next request as well.
                                            TimeSpan delay = TimeSpan.FromMilliseconds((int)Math.Pow(2, attempt) * this.fragmentRetryDelay);

                                            Log.WriteRetryMessage(
                                                LOGTAG,
                                                "MicrosoftGraphFragmentRetryIn",
                                                ex,
                                                "Uploading fragment {0}/{1} of remote file {2} failed and will be retried in {3}",
                                                fragmentNumber,
                                                fragmentCount,
                                                remotename,
                                                delay);

                                            await Task.Delay(delay).ConfigureAwait(false);
                                            continue;
                                        }
                                        else if (ex.StatusCode == HttpStatusCode.NotFound)
                                        {
                                            // 404 is a special case indicating the upload session no longer exists, so the fragment shouldn't be retried.
                                            // Instead we'll let the caller re-attempt the whole file.
                                            await this.ThrowUploadSessionException(
                                                uploadSession,
                                                createSessionResponse,
                                                fragmentNumber,
                                                fragmentCount,
                                                ex,
                                                cancelToken).ConfigureAwait(false);
                                        }
                                        else if ((int)ex.StatusCode >= 400 && (int)ex.StatusCode < 500)
                                        {
                                            // If a 4xx error code is hit, we should retry without the exponential backoff attempt.
                                            Log.WriteRetryMessage(
                                                LOGTAG,
                                                "MicrosoftGraphFragmentRetry",
                                                ex,
                                                "Uploading fragment {0}/{1} of remote file {2} failed and will be retried",
                                                fragmentNumber,
                                                fragmentCount,
                                                remotename);

                                            continue;
                                        }
                                        else
                                        {
                                            // Other errors should be rethrown
                                            await this.ThrowUploadSessionException(
                                                uploadSession,
                                                createSessionResponse,
                                                fragmentNumber,
                                                fragmentCount,
                                                ex,
                                                cancelToken).ConfigureAwait(false);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        // Any other exceptions should also cause the upload session to be canceled.
                                        await this.ThrowUploadSessionException(
                                            uploadSession,
                                            createSessionResponse,
                                            fragmentNumber,
                                            fragmentCount,
                                            ex,
                                            cancelToken).ConfigureAwait(false);
                                    }

                                    // If we successfully sent this piece, then we can break out of the retry loop
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        public void Delete(string remotename)
        {
            try
            {
                m_retryAfter.WaitForRetryAfter();
                string deleteUrl = string.Format("{0}/root:{1}{2}", this.DrivePrefix, this.RootPath, NormalizeSlashes(remotename));
                if (this.m_client != null)
                {
                    using (var response = this.m_client.DeleteAsync(deleteUrl).Await())
                    {
                        this.CheckResponse(response);
                    }
                }
                else
                {
                    using (var response = this.m_oAuthHelper.GetResponseWithoutException(deleteUrl, null, HttpMethod.Delete.ToString()))
                    {
                        this.CheckResponse(response);
                    }
                }
            }
            catch (DriveItemNotFoundException ex)
            {
                // Wrap the existing item not found error in a 'FolderMissingException'
                throw new FileMissingException(ex);
            }
        }

        public void Test()
        {
            try
            {
                string rootPath = string.Format("{0}/root:{1}", this.DrivePrefix, this.RootPath);
                this.Get<DriveItem>(rootPath);
            }
            catch (DriveItemNotFoundException ex)
            {
                // Wrap the existing item not found error in a 'FolderMissingException'
                throw new FolderMissingException(ex);
            }
        }

        public void Dispose()
        {
            if (this.m_client != null)
            {
                this.m_client.Dispose();
            }
        }

        protected virtual string GetRootPathFromUrl(string url)
        {
            // Extract out the path to the backup root folder from the given URI
            var uri = new Utility.Uri(url);

            return Utility.Uri.UrlDecode(uri.HostAndPath);
        }

        protected T Get<T>(string url)
        {
            return this.SendRequest<T>(HttpMethod.Get, url);
        }

        protected T Post<T>(string url, T body) where T : class
        {
            return this.SendRequest(HttpMethod.Post, url, body);
        }

        protected T Patch<T>(string url, T body) where T : class
        {
            return this.SendRequest(PatchMethod, url, body);
        }

        private T SendRequest<T>(HttpMethod method, string url)
        {
            if (this.m_client != null)
            {
                using (var request = new HttpRequestMessage(method, url))
                {
                    return this.SendRequest<T>(request);
                }
            }
            else
            {
                m_retryAfter.WaitForRetryAfter();
                using (var response = this.m_oAuthHelper.GetResponseWithoutException(url, null, method.ToString()))
                {
                    return this.ParseResponse<T>(response);
                }
            }
        }

        private T SendRequest<T>(HttpMethod method, string url, T body) where T : class
        {
            if (this.m_client != null)
            {
                using (var request = new HttpRequestMessage(method, url))
                using (request.Content = this.PrepareContent(body))
                {
                    return this.SendRequest<T>(request);
                }
            }
            else
            {
                m_retryAfter.WaitForRetryAfter();
                using (var response = this.m_oAuthHelper.GetResponseWithoutException(url, body, method.ToString()))
                {
                    return this.ParseResponse<T>(response);
                }
            }
        }

        private T SendRequest<T>(HttpRequestMessage request)
        {
            m_retryAfter.WaitForRetryAfter();
            using (var response = this.m_client.SendAsync(request).Await())
            {
                return this.ParseResponse<T>(response);
            }
        }
        
        private IEnumerable<T> Enumerate<T>(string url)
        {
            string nextUrl = url;
            while (!string.IsNullOrEmpty(nextUrl))
            {
                GraphCollection<T> results;
                try
                {
                    results = this.Get<GraphCollection<T>>(nextUrl);
                }
                catch (DriveItemNotFoundException ex)
                {
                    // If there's an 'item not found' exception here, it means the root folder didn't exist.
                    throw new FolderMissingException(ex);
                }

                foreach (T result in results.Value)
                {
                    yield return result;
                }

                nextUrl = results.ODataNextLink;
            }
        }

        private void CheckResponse(HttpResponseMessage response)
        {
            m_retryAfter.SetRetryAfter(response.Headers.RetryAfter);

            if (!response.IsSuccessStatusCode)
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    // It looks like this is an 'item not found' exception, so wrap it in a new exception class to make it easier to pick things out.
                    throw new DriveItemNotFoundException(response);
                }
                else
                {
                    // Throw a wrapper exception to make it easier for the caller to look at specific status codes, etc.
                    throw new MicrosoftGraphException(response);
                }
            }
        }

        private void CheckResponse(HttpWebResponse response)
        {
            string retryAfterHeader = response.Headers[HttpResponseHeader.RetryAfter];
            if (retryAfterHeader != null && RetryConditionHeaderValue.TryParse(retryAfterHeader, out RetryConditionHeaderValue retryAfter))
            {
                m_retryAfter.SetRetryAfter(retryAfter);
            }

            if (!((int)response.StatusCode >= 200 && (int)response.StatusCode < 300))
            {
                if (response.StatusCode == HttpStatusCode.NotFound)
                {
                    // It looks like this is an 'item not found' exception, so wrap it in a new exception class to make it easier to pick things out.
                    throw new DriveItemNotFoundException(response);
                }
                else
                {
                    // Throw a wrapper exception to make it easier for the caller to look at specific status codes, etc.
                    throw new MicrosoftGraphException(response);
                }
            }
        }

        private T ParseResponse<T>(HttpResponseMessage response)
        {
            this.CheckResponse(response);
            using (Stream responseStream = response.Content.ReadAsStreamAsync().Await())
            using (StreamReader reader = new StreamReader(responseStream))
            using (JsonTextReader jsonReader = new JsonTextReader(reader))
            {
                return this.m_serializer.Deserialize<T>(jsonReader);
            }
        }

        private T ParseResponse<T>(HttpWebResponse response)
        {
            this.CheckResponse(response);
            using (Stream responseStream = response.GetResponseStream())
            using (StreamReader reader = new StreamReader(responseStream))
            using (JsonTextReader jsonReader = new JsonTextReader(reader))
            {
                return this.m_serializer.Deserialize<T>(jsonReader);
            }
        }

        private async Task ThrowUploadSessionException(
            UploadSession uploadSession,
            HttpResponseMessage createSessionResponse,
            int fragment,
            int fragmentCount,
            Exception ex,
            CancellationToken cancelToken)
        {
            // Before throwing the exception, cancel the upload session
            // The uploaded delete request will error if it is authenticated
            using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, uploadSession.UploadUrl))
            using (HttpResponseMessage response = await this.m_client.SendAsync(request, false, cancelToken).ConfigureAwait(false))
            {
                // Note that the response body should always be empty in this case.
                this.ParseResponse<UploadSession>(response);
            }

            throw new UploadSessionException(createSessionResponse, fragment, fragmentCount, ex);
        }

        private async Task ThrowUploadSessionException(
            UploadSession uploadSession,
            HttpWebResponse createSessionResponse,
            int fragment,
            int fragmentCount,
            Exception ex,
            CancellationToken cancelToken)
        {
            // Before throwing the exception, cancel the upload session
            // The uploaded delete request will error if it is authenticated
            var request = new AsyncHttpRequest(this.m_oAuthHelper.CreateRequest(uploadSession.UploadUrl, HttpMethod.Delete.ToString(), true));
            using (var response = await this.m_oAuthHelper.GetResponseWithoutExceptionAsync(request, cancelToken).ConfigureAwait(false))
            {
                // Note that the response body should always be empty in this case.
                this.ParseResponse<UploadSession>(response);
            }

            throw new UploadSessionException(createSessionResponse, fragment, fragmentCount, ex);
        }

        /// <summary>
        /// Normalizes the slashes in a url fragment. For example:
        ///   "" => ""
        ///   "test" => "/test"
        ///   "test/" => "/test"
        ///   "a\b" => "/a/b"
        /// </summary>
        /// <param name="url">Url fragment to normalize</param>
        /// <returns>Normalized fragment</returns>
        private static string NormalizeSlashes(string url)
        {
            url = url.Replace('\\', '/');

            if (url.Length != 0 && !url.StartsWith("/", StringComparison.Ordinal))
                url = "/" + url;

            if (url.EndsWith("/", StringComparison.Ordinal))
                url = url.Substring(0, url.Length - 1);

            return url;
        }

        private StringContent PrepareContent<T>(T body)
        {
            if (body != null)
            {
                return new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");
            }
            else
            {
                return null;
            }
        }
    }
}