Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception caused by automatic redirection resulting in the destructio… #776

Open
wants to merge 1 commit into
base: master-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}