|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net; |
| 4 | +using System.Net.Http; |
| 5 | + |
| 6 | +using Ocelot.Configuration; |
| 7 | + |
| 8 | +using Ocelot.Logging; |
| 9 | + |
| 10 | +namespace Ocelot.Requester |
| 11 | +{ |
| 12 | + public class HttpClientBuilder : IHttpClientBuilder |
| 13 | + { |
| 14 | + private readonly IDelegatingHandlerHandlerFactory _factory; |
| 15 | + private readonly IHttpClientCache _cacheHandlers; |
| 16 | + private readonly IOcelotLogger _logger; |
| 17 | + private DownstreamRoute _cacheKey; |
| 18 | + private HttpClient _httpClient; |
| 19 | + private IHttpClient _client; |
| 20 | + private readonly TimeSpan _defaultTimeout; |
| 21 | + |
| 22 | + public HttpClientBuilder( |
| 23 | + IDelegatingHandlerHandlerFactory factory, |
| 24 | + IHttpClientCache cacheHandlers, |
| 25 | + IOcelotLogger logger) |
| 26 | + { |
| 27 | + _factory = factory; |
| 28 | + _cacheHandlers = cacheHandlers; |
| 29 | + _logger = logger; |
| 30 | + |
| 31 | + // This is hardcoded at the moment but can easily be added to configuration |
| 32 | + // if required by a user request. |
| 33 | + _defaultTimeout = TimeSpan.FromSeconds(90); |
| 34 | + } |
| 35 | + |
| 36 | + public IHttpClient Create(DownstreamRoute downstreamRoute) |
| 37 | + { |
| 38 | + _cacheKey = downstreamRoute; |
| 39 | + |
| 40 | + var httpClient = _cacheHandlers.Get(_cacheKey); |
| 41 | + |
| 42 | + if (httpClient != null) |
| 43 | + { |
| 44 | + _client = httpClient; |
| 45 | + return httpClient; |
| 46 | + } |
| 47 | + |
| 48 | + var handler = CreateHandler(downstreamRoute); |
| 49 | + |
| 50 | + if (downstreamRoute.DangerousAcceptAnyServerCertificateValidator) |
| 51 | + { |
| 52 | + handler.ServerCertificateCustomValidationCallback = |
| 53 | + HttpClientHandler.DangerousAcceptAnyServerCertificateValidator; |
| 54 | + |
| 55 | + _logger |
| 56 | + .LogWarning($"You have ignored all SSL warnings by using DangerousAcceptAnyServerCertificateValidator for this DownstreamRoute, UpstreamPathTemplate: {downstreamRoute.UpstreamPathTemplate}, DownstreamPathTemplate: {downstreamRoute.DownstreamPathTemplate}"); |
| 57 | + } |
| 58 | + |
| 59 | + var timeout = downstreamRoute.QosOptions.TimeoutValue == 0 |
| 60 | + ? _defaultTimeout |
| 61 | + : TimeSpan.FromMilliseconds(downstreamRoute.QosOptions.TimeoutValue); |
| 62 | + |
| 63 | + _httpClient = new HttpClient(CreateHttpMessageHandler(handler, downstreamRoute)) |
| 64 | + { |
| 65 | + Timeout = timeout, |
| 66 | + }; |
| 67 | + |
| 68 | + _client = new HttpClientWrapper(_httpClient, downstreamRoute.ConnectionClose); |
| 69 | + |
| 70 | + return _client; |
| 71 | + } |
| 72 | + |
| 73 | + private static HttpClientHandler CreateHandler(DownstreamRoute downstreamRoute) |
| 74 | + { |
| 75 | + // Dont' create the CookieContainer if UseCookies is not set or the HttpClient will complain |
| 76 | + // under .Net Full Framework |
| 77 | + var useCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer; |
| 78 | + |
| 79 | + return useCookies ? UseCookiesHandler(downstreamRoute) : UseNonCookiesHandler(downstreamRoute); |
| 80 | + } |
| 81 | + |
| 82 | + private static HttpClientHandler UseNonCookiesHandler(DownstreamRoute downstreamRoute) |
| 83 | + { |
| 84 | + return new HttpClientHandler |
| 85 | + { |
| 86 | + AllowAutoRedirect = downstreamRoute.HttpHandlerOptions.AllowAutoRedirect, |
| 87 | + UseCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer, |
| 88 | + UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy, |
| 89 | + MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer, |
| 90 | + |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + private static HttpClientHandler UseCookiesHandler(DownstreamRoute downstreamRoute) |
| 95 | + { |
| 96 | + return new HttpClientHandler |
| 97 | + { |
| 98 | + AllowAutoRedirect = downstreamRoute.HttpHandlerOptions.AllowAutoRedirect, |
| 99 | + UseCookies = downstreamRoute.HttpHandlerOptions.UseCookieContainer, |
| 100 | + UseProxy = downstreamRoute.HttpHandlerOptions.UseProxy, |
| 101 | + MaxConnectionsPerServer = downstreamRoute.HttpHandlerOptions.MaxConnectionsPerServer, |
| 102 | + CookieContainer = new CookieContainer(), |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + public void Save() |
| 107 | + { |
| 108 | + _cacheHandlers.Set(_cacheKey, _client, TimeSpan.FromHours(24)); |
| 109 | + } |
| 110 | + |
| 111 | + private HttpMessageHandler CreateHttpMessageHandler(HttpMessageHandler httpMessageHandler, DownstreamRoute request) |
| 112 | + { |
| 113 | + //todo handle error |
| 114 | + var handlers = _factory.Get(request).Data; |
| 115 | + |
| 116 | + handlers |
| 117 | + .Select(handler => handler) |
| 118 | + .Reverse() |
| 119 | + .ToList() |
| 120 | + .ForEach(handler => |
| 121 | + { |
| 122 | + var delegatingHandler = handler(); |
| 123 | + delegatingHandler.InnerHandler = httpMessageHandler; |
| 124 | + httpMessageHandler = delegatingHandler; |
| 125 | + }); |
| 126 | + return httpMessageHandler; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments