Skip to content

Commit

Permalink
Exception caused by automatic redirection resulting in the destructio…
Browse files Browse the repository at this point in the history
…n of HttpContent in .NET Framework 4.6.x and 4.7.x is fixed.

see:
tmenier#644
dotnet/runtime#14612
dotnet/corefx#19082
  • Loading branch information
hezlog committed Nov 11, 2023
1 parent 9b9d899 commit 7b100bb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Flurl.Http/Flurl.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks>netstandard2.0;net461;net472</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>Flurl.Http</PackageId>
<Version>3.2.4</Version>
<Version>3.2.5</Version>
<Authors>Todd Menier</Authors>
<Description>A fluent, portable, testable HTTP client library.</Description>
<PackageProjectUrl>https://flurl.dev</PackageProjectUrl>
Expand Down
7 changes: 4 additions & 3 deletions src/Flurl.Http/FlurlRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,14 @@ public async Task<IFlurlResponse> SendAsync(HttpMethod verb, HttpContent content
var ct = GetCancellationTokenWithTimeout(cancellationToken, out var cts);

try {
var cloneContent = await content.CloneAsync().ConfigureAwait(false);
var response = await Client.HttpClient.SendAsync(request, completionOption, ct).ConfigureAwait(false);
call.HttpResponseMessage = response;
call.HttpResponseMessage.RequestMessage = request;
call.Response = new FlurlResponse(call.HttpResponseMessage, CookieJar);

if (call.Succeeded) {
var redirResponse = await ProcessRedirectAsync(call, cancellationToken, completionOption).ConfigureAwait(false);
var redirResponse = await ProcessRedirectAsync(call, cloneContent, cancellationToken, completionOption).ConfigureAwait(false);
return redirResponse ?? call.Response;
}
else
Expand Down Expand Up @@ -231,7 +232,7 @@ private CancellationToken GetCancellationTokenWithTimeout(CancellationToken orig
}
}

private async Task<IFlurlResponse> ProcessRedirectAsync(FlurlCall call, CancellationToken cancellationToken, HttpCompletionOption completionOption) {
private async Task<IFlurlResponse> ProcessRedirectAsync(FlurlCall call, HttpContent content, CancellationToken cancellationToken, HttpCompletionOption completionOption) {
if (Settings.Redirects.Enabled)
call.Redirect = GetRedirect(call);

Expand Down Expand Up @@ -266,7 +267,7 @@ private async Task<IFlurlResponse> ProcessRedirectAsync(FlurlCall call, Cancella
try {
return await redir.SendAsync(
changeToGet ? HttpMethod.Get : call.HttpRequestMessage.Method,
changeToGet ? null : call.HttpRequestMessage.Content,
changeToGet ? null : content,
ct,
completionOption).ConfigureAwait(false);
}
Expand Down
38 changes: 38 additions & 0 deletions src/Flurl.Http/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Flurl.Http
{
/// <summary>
/// Extension methods of HttpContent
/// </summary>
public static class HttpContentExtensions
{
/// <summary>Get a copy of the request content.</summary>
/// <param name="content">The content to copy.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <remarks>Note that cloning content isn't possible after it's dispatched, because the stream is automatically disposed after the request.</remarks>
internal static async Task<HttpContent> CloneAsync(this HttpContent content, CancellationToken cancellationToken = default) {
if (content == null)
return null;

Stream stream = new MemoryStream();
await content
.CopyToAsync(stream
#if NET5_0_OR_GREATER
, cancellationToken
#endif
)
.ConfigureAwait(false);
stream.Position = 0;

StreamContent clone = new StreamContent(stream);
foreach (var header in content.Headers)
clone.Headers.Add(header.Key, header.Value);

return clone;
}
}
}

0 comments on commit 7b100bb

Please sign in to comment.