Skip to content

Commit

Permalink
Improvements (and corrections on some tiny merge errors during) based…
Browse files Browse the repository at this point in the history
… on rebasing...
  • Loading branch information
rassilon committed Nov 2, 2023
1 parent a4ce540 commit 29f21d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
11 changes: 10 additions & 1 deletion src/RestSharp/Options/RestClientRedirectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class RestClientRedirectionOptions {
/// redirect from HTTPS to HTTP.
/// </summary>
public bool FollowRedirectsToInsecure { get; set; } = false;

/// <summary>
/// Set to true (default), when you want to include the originally
/// requested headers in redirected requests.
Expand All @@ -32,8 +33,9 @@ public class RestClientRedirectionOptions {
/// Authorization header to the redirected destination.
/// </summary>
public bool ForwardAuthorization { get; set; } = false;

/// <summary>
/// Set to true (default), when you want to include cookie3s from the
/// Set to true (default), when you want to include cookies from the
/// CookieContainer on the redirected URL.
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -88,6 +90,13 @@ public class RestClientRedirectionOptions {
/// fragment should inherit the fragment from the original URI.
/// </remarks>
public bool ForwardFragment { get; set; } = true;

/// <summary>
/// Set to true (default), to allow the HTTP Method used on the original request to
/// be replaced with GET when the status code 303 (HttpStatusCode.RedirectMethod)
/// was returned. Setting this to false will disallow the altering of the verb.
/// </summary>
public bool AllowRedirectMethodStatusCodeToAlterVerb { get; set; } = true;

/// <summary>
/// HttpStatusCodes that trigger redirect processing. Defaults to MovedPermanently (301),
Expand Down
36 changes: 11 additions & 25 deletions src/RestSharp/RestClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,12 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
using var cts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, cancellationToken);

var ct = cts.Token;


HttpResponseMessage? responseMessage;
// Make sure we have a cookie container if not provided in the request
CookieContainer cookieContainer = request.CookieContainer ??= new CookieContainer();

var headers = new RequestHeaders()
.AddHeaders(request.Parameters)
.AddHeaders(DefaultParameters)
.AddAcceptHeader(AcceptedContentTypes)
.AddCookieHeaders(url, cookieContainer)
.AddCookieHeaders(url, Options.CookieContainer);
HttpResponseMessage? responseMessage = null;
var cookieContainer = request.CookieContainer ??= new CookieContainer();

message.AddHeaders(headers);
if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false);
await OnBeforeRequest(message).ConfigureAwait(false);

try {
// Make sure we have a cookie container if not provided in the request
var cookieContainer = request.CookieContainer ??= new CookieContainer();

var headers = new RequestHeaders()
.AddHeaders(request.Parameters)
Expand All @@ -123,7 +109,6 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
.AddCookieHeaders(url, Options.CookieContainer);

bool foundCookies = false;
HttpResponseMessage? responseMessage = null;

do {
using var requestContent = new RequestContent(this, request);
Expand All @@ -137,10 +122,12 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
using var message = PrepareRequestMessage(httpMethod, url, content, headers);

if (request.OnBeforeRequest != null) await request.OnBeforeRequest(message).ConfigureAwait(false);
await OnBeforeRequest(message).ConfigureAwait(false);

responseMessage = await HttpClient.SendAsync(message, request.CompletionOption, ct).ConfigureAwait(false);

if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false);
await OnAfterRequest(responseMessage).ConfigureAwait(false);

if (!IsRedirect(Options.RedirectOptions, responseMessage)) {
break;
Expand Down Expand Up @@ -171,15 +158,17 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo

// Disallow automatic redirection from secure to non-secure schemes
// based on the option setting:
if (HttpUtilities.IsSupportedSecureScheme(requestUri.Scheme)
if (HttpUtilities.IsSupportedSecureScheme(originalUrl.Scheme)
&& !HttpUtilities.IsSupportedSecureScheme(location.Scheme)
&& !Options.RedirectOptions.FollowRedirectsToInsecure) {
// TODO: Log here...
break;
}

if (responseMessage.StatusCode == HttpStatusCode.RedirectMethod) {
// TODO: Add RedirectionOptions property for this decision:
// This is the expected behavior for this status code, but
// ignore it if requested from the RedirectOptions:
if (responseMessage.StatusCode == HttpStatusCode.RedirectMethod
&& Options.RedirectOptions.AllowRedirectMethodStatusCodeToAlterVerb) {
httpMethod = HttpMethod.Get;
}

Expand All @@ -199,10 +188,8 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
if (!Options.RedirectOptions.ForceForwardBody) {
// HttpClient RedirectHandler sets request.Content to null here:
message.Content = null;
// HttpClient Redirect handler also does this:
//if (message.Headers.TansferEncodingChunked == true) {
// request.Headers.TransferEncodingChunked = false;
//}
// HttpClient Redirect handler also foribly removes
// a Transfer-Encoding of chunked in this case.
Parameter? transferEncoding = request.Parameters.TryFind(KnownHeaders.TransferEncoding);
if (transferEncoding != null
&& transferEncoding.Type == ParameterType.HttpHeader
Expand Down Expand Up @@ -237,7 +224,6 @@ async Task<HttpResponse> ExecuteRequestAsync(RestRequest request, CancellationTo
if (request.OnAfterRequest != null) await request.OnAfterRequest(responseMessage).ConfigureAwait(false);
await OnAfterRequest(responseMessage).ConfigureAwait(false);
return new HttpResponse(responseMessage, url, cookieContainer, null, timeoutCts.Token);

}

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion test/RestSharp.Tests.Integrated/Server/TestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public HttpServer(ITestOutputHelper? output = null) {
_app.MapGet("headers", HeaderHandlers.HandleHeaders);
_app.MapGet("request-echo", async context => await context.Request.BodyReader.AsStream().CopyToAsync(context.Response.BodyWriter.AsStream()));
_app.MapDelete("delete", () => new TestResponse { Message = "Works!" });
_app.MapGet("redirect", () => Results.Redirect("/success", false, true));

// Cookies
_app.MapGet("get-cookies", CookieHandlers.HandleCookies);
Expand Down

0 comments on commit 29f21d9

Please sign in to comment.