Skip to content
This repository has been archived by the owner on Mar 27, 2020. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
elcattivo committed Mar 11, 2018
1 parent 6d53016 commit 332b665
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
16 changes: 8 additions & 8 deletions CloudFlareUtilities/ClearanceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected override void Dispose(bool disposing)
/// <returns>The task object representing the asynchronous operation.</returns>
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var IdCookieBefore = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, IdCookieName).FirstOrDefault();
var idCookieBefore = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, IdCookieName).FirstOrDefault();
var clearanceCookieBefore = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, ClearanceCookieName).FirstOrDefault();

EnsureClientHeader(request);
Expand All @@ -99,13 +99,13 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
if (IsClearanceRequired(response))
throw new CloudFlareClearanceException(retries);

var IdCookieAfter = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, IdCookieName).FirstOrDefault();
var idCookieAfter = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, IdCookieName).FirstOrDefault();
var clearanceCookieAfter = ClientHandler.CookieContainer.GetCookiesByName(request.RequestUri, ClearanceCookieName).FirstOrDefault();

// inject set-cookie headers in case the cookies changed
if (IdCookieAfter != null && IdCookieAfter != IdCookieBefore)
if (idCookieAfter != null && idCookieAfter != idCookieBefore)
{
response.Headers.Add(HttpHeader.SetCookie, IdCookieAfter.ToHeaderValue());
response.Headers.Add(HttpHeader.SetCookie, idCookieAfter.ToHeaderValue());
}
if (clearanceCookieAfter != null && clearanceCookieAfter != clearanceCookieBefore)
{
Expand Down Expand Up @@ -167,8 +167,7 @@ private async Task PassClearance(HttpResponseMessage response, CancellationToken

var clearanceRequest = new HttpRequestMessage(HttpMethod.Get, clearanceUri);

IEnumerable<string> userAgent;
if (response.RequestMessage.Headers.TryGetValues(HttpHeader.UserAgent, out userAgent))
if (response.RequestMessage.Headers.TryGetValues(HttpHeader.UserAgent, out var userAgent))
clearanceRequest.Headers.Add(HttpHeader.UserAgent, userAgent);

var passResponse = await _client.SendAsync(clearanceRequest, cancellationToken).ConfigureAwait(false);
Expand All @@ -180,9 +179,10 @@ private void SaveIdCookie(HttpResponseMessage response)
var cookies = response.Headers
.Where(pair => pair.Key == HttpHeader.SetCookie)
.SelectMany(pair => pair.Value)
.Where(cookie => cookie.StartsWith($"{IdCookieName}="));
.Where(cookie => cookie.StartsWith($"{IdCookieName}="))
.ToList();

if (cookies.Count() == 0)
if (!cookies.Any())
return;

// Expire any old cloudflare cookies.
Expand Down
5 changes: 3 additions & 2 deletions CloudFlareUtilities/HttpMessageHandlerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ internal static class HttpMessageHandlerExtensions
{
public static HttpMessageHandler GetMostInnerHandler(this HttpMessageHandler self)
{
var delegatingHandler = self as DelegatingHandler;
return delegatingHandler == null ? self : delegatingHandler.InnerHandler.GetMostInnerHandler();
return self is DelegatingHandler handler
? handler.InnerHandler.GetMostInnerHandler()
: self;
}
}
}

0 comments on commit 332b665

Please sign in to comment.