mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-04-23 00:32:50 +08:00
bit of refactoring
This commit is contained in:
parent
900c18908f
commit
e55b27de0f
@ -48,6 +48,22 @@ namespace Ocelot.Request.Middleware
|
||||
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;
|
||||
return _request;
|
||||
}
|
||||
|
@ -42,31 +42,12 @@ namespace Ocelot.Requester
|
||||
{
|
||||
return httpClient;
|
||||
}
|
||||
bool useCookies = context.DownstreamReRoute.HttpHandlerOptions.UseCookieContainer;
|
||||
HttpClientHandler httpclientHandler;
|
||||
// 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,
|
||||
};
|
||||
}
|
||||
|
||||
var handler = CreateHandler(context);
|
||||
|
||||
if (context.DownstreamReRoute.DangerousAcceptAnyServerCertificateValidator)
|
||||
{
|
||||
httpclientHandler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true;
|
||||
handler.ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) => true;
|
||||
|
||||
_logger
|
||||
.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
|
||||
: TimeSpan.FromMilliseconds(context.DownstreamReRoute.QosOptions.TimeoutValue);
|
||||
|
||||
_httpClient = new HttpClient(CreateHttpMessageHandler(httpclientHandler, context.DownstreamReRoute))
|
||||
_httpClient = new HttpClient(CreateHttpMessageHandler(handler, context.DownstreamReRoute))
|
||||
{
|
||||
Timeout = timeout
|
||||
};
|
||||
@ -86,6 +67,41 @@ namespace Ocelot.Requester
|
||||
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()
|
||||
{
|
||||
_cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24));
|
||||
|
@ -32,24 +32,7 @@ namespace Ocelot.Requester
|
||||
|
||||
try
|
||||
{
|
||||
var message = 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);
|
||||
var response = await httpClient.SendAsync(context.DownstreamRequest.ToHttpRequestMessage());
|
||||
return new OkResponse<HttpResponseMessage>(response);
|
||||
}
|
||||
catch (TimeoutRejectedException exception)
|
||||
|
Loading…
x
Reference in New Issue
Block a user