bit of refactoring

This commit is contained in:
Tom Gardham-Pallister 2018-05-24 22:56:08 +01:00
parent 900c18908f
commit e55b27de0f
3 changed files with 56 additions and 41 deletions

View File

@ -48,6 +48,22 @@ namespace Ocelot.Request.Middleware
Scheme = Scheme Scheme = Scheme
}; };
/**
* According to https://tools.ietf.org/html/rfc7231
* GET,HEAD,DELETE,CONNECT,TRACE
* Can have body but server can reject the request.
* And MS HttpClient in Full Framework actually rejects it.
* see #366 issue
**/
#if NET461 || NET462 || NET47 || NET471 || NET472
if (_request.Method == HttpMethod.Get ||
_request.Method == HttpMethod.Head ||
_request.Method == HttpMethod.Delete ||
_request.Method == HttpMethod.Trace)
{
_request.Content = null;
}
#endif
_request.RequestUri = uriBuilder.Uri; _request.RequestUri = uriBuilder.Uri;
return _request; return _request;
} }

View File

@ -42,31 +42,12 @@ namespace Ocelot.Requester
{ {
return httpClient; return httpClient;
} }
bool useCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer;
HttpClientHandler httpclientHandler; var handler = CreateHandler(context);
// Dont' create the CookieContainer if UseCookies is not set ot the HttpClient will complain
// under .Net Full Framework
if (useCookies)
{
httpclientHandler = new HttpClientHandler
{
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
CookieContainer = new CookieContainer()
};
}
else
{
httpclientHandler = new HttpClientHandler
{
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
};
}
if (context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator) if (context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator)
{ {
httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true; handler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true;
_logger _logger
.LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamReRoute, UpstreamPathTemplate: {context.DownstreamReRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {context.DownstreamReRoute.DownstreamPathTemplate}"); .LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamReRoute, UpstreamPathTemplate: {context.DownstreamReRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {context.DownstreamReRoute.DownstreamPathTemplate}");
@ -76,7 +57,7 @@ namespace Ocelot.Requester
? _defaultTimeout ? _defaultTimeout
: TimeSpan.FromMilliseconds(context.DownstreamReRoute.QosOptions.TimeoutValue); : TimeSpan.FromMilliseconds(context.DownstreamReRoute.QosOptions.TimeoutValue);
_httpClient = new HttpClient(CreateHttpMessageHandler(httpclientHandler, context.DownstreamReRoute)) _httpClient = new HttpClient(CreateHttpMessageHandler(handler, context.DownstreamReRoute))
{ {
Timeout = timeout Timeout = timeout
}; };
@ -86,6 +67,41 @@ namespace Ocelot.Requester
return _client; return _client;
} }
private HttpClientHandler CreateHandler(DownstreamContext context)
{
// Dont' create the CookieContainer if UseCookies is not set or the HttpClient will complain
// under .Net Full Framework
bool useCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer;
if (useCookies)
{
return UseCookiesHandler(context);
}
else
{
return UseNonCookiesHandler(context);
}
}
private HttpClientHandler UseNonCookiesHandler(DownstreamContext context)
{
return new HttpClientHandler
{
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
};
}
private HttpClientHandler UseCookiesHandler(DownstreamContext context)
{
return new HttpClientHandler
{
AllowAutoRedirect = context.DownstreamReRoute.HttpHandlerOptions.AllowAutoRedirect,
UseCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer,
CookieContainer = new CookieContainer()
};
}
public void Save() public void Save()
{ {
_cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24)); _cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24));

View File

@ -32,24 +32,7 @@ namespace Ocelot.Requester
try try
{ {
var message = context.DownstreamRequest.ToHttpRequestMessage(); var response = await httpClient.SendAsync(context.DownstreamRequest.ToHttpRequestMessage());
/**
* According to https://tools.ietf.org/html/rfc7231
* GET,HEAD,DELETE,CONNECT,TRACE
* Can have body but server can reject the request.
* And MS HttpClient in Full Framework actually rejects it.
* see #366 issue
**/
if (message.Method == HttpMethod.Get ||
message.Method == HttpMethod.Head ||
message.Method == HttpMethod.Delete ||
message.Method == HttpMethod.Trace)
{
message.Content = null;
}
_logger.LogDebug(string.Format("Sending {0}", message));
var response = await httpClient.SendAsync(message);
return new OkResponse<HttpResponseMessage>(response); return new OkResponse<HttpResponseMessage>(response);
} }
catch (TimeoutRejectedException exception) catch (TimeoutRejectedException exception)