diff --git a/Duplicati/Library/Backend/OAuthHelper/OAuthHttpClient.cs b/Duplicati/Library/Backend/OAuthHelper/OAuthHttpClient.cs index c81f53a28..4a5711136 100644 --- a/Duplicati/Library/Backend/OAuthHelper/OAuthHttpClient.cs +++ b/Duplicati/Library/Backend/OAuthHelper/OAuthHttpClient.cs @@ -60,10 +60,6 @@ namespace Duplicati.Library this.Timeout = HttpContextSettings.OperationTimeout; } - // We would also set AllowReadStreamBuffering = HttpContextSettings.BufferRequests, except HttpClient doesn't appear to expose this. - // However, starting in .NET 4.5, it looks like HttpClient doesn't buffer by default. - // https://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ - // Set the default user agent this.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("Duplicati", USER_AGENT_VERSION)); } @@ -82,13 +78,19 @@ namespace Duplicati.Library this.PreventAuthentication(request); } + // The HttpCompletionOptions are a nice way to emulate the BufferRequests behavior. + // When set to ResponseContentRead, async call will not complete until the response has been + // read and is cached in memory (somehow). + // When set to ResponseHeadersRead, it looks like both Mono and .NET don't buffer the result. + HttpCompletionOption httpCompletionOption = HttpContextSettings.BufferRequests ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead; + // The HttpClient.SendAsync method throws an OperationCanceledException when the timeout is exceeded. // In order to provide a more informative exception, we will detect this case and throw a TimeoutException // instead. This will also allow the BackendUploader to differentiate between cancellations requested by // the user and those generated by timeouts. try { - return await this.SendAsync(request, cancellationToken).ConfigureAwait(false); + return await this.SendAsync(request, httpCompletionOption, cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) when (!cancellationToken.IsCancellationRequested) {