public Task <bool> ShouldProcessRequest(HttpRequestMessage requestMessage, RequestRouterConfigurationBase configuration, CancellationToken cancellationToken) { var config = configuration as TRequest; if (config == null) { return(Task.FromResult(false)); } return(ShouldProcessRequest(requestMessage, config, cancellationToken)); }
protected override Task <HttpResponseMessage> ProcessRequest(HttpRequestMessage requestMessage, RequestRouterConfiguration configuration, CancellationToken cancellationToken) { RequestRouterConfigurationBase realConfiguration = string.IsNullOrWhiteSpace(configuration.Service) ? (RequestRouterConfigurationBase)((HostRouterConfiguration)configuration) : (RequestRouterConfigurationBase)((ConsulServiceRouterConfiguration)configuration); var handler = _requestHandlerFactory.GetHandler(); try { return(handler.HandleRequest(requestMessage, configuration, cancellationToken)); } finally { _requestHandlerFactory.Release(handler); } }
protected Uri CreateTargetUri(Uri sourceUri, RequestRouterConfigurationBase config, DnsEndPoint endPoint) { var builder = new UriBuilder(sourceUri) { Port = config.Port ?? endPoint.Port, Host = endPoint.Host, }; if (!string.IsNullOrWhiteSpace(config.Scheme)) { builder.Scheme = config.Scheme; } return(builder.Uri); }
public Task <HttpResponseMessage> ProcessRequest(HttpRequestMessage requestMessage, RequestRouterConfigurationBase configuration, CancellationToken cancellationToken) { return(ProcessRequest(requestMessage, configuration as TRequest, cancellationToken)); }
protected virtual HttpRequestMessage PrepareRequestForForwarding(HttpRequestMessage request, Uri realTarget, RequestRouterConfigurationBase configuration, string configHost = null) { foreach (var header in configuration.Headers ?? new Dictionary <string, string>()) { if (request.Headers.Any(x => string.Equals(x.Key, header.Key, StringComparison.InvariantCultureIgnoreCase))) { continue; } request.Headers.TryAddWithoutValidation(header.Key, header.Value); } request.Headers.Add("X-Forwarded-For", request.GetOwinContext().Request.RemoteIpAddress); request.Headers.Add("X-Original-Uri", request.RequestUri.ToString()); request.Headers.Add("X-Forwarded-Proto", request.RequestUri.Scheme); request.Headers.Add("X-Forwarded-Host", request.Headers.Host); string host = null; if (!configuration.Headers?.TryGetValue("Host", out host) ?? false) { host = null; } request.Headers.Host = host ?? configHost ?? (realTarget.IsDefaultPort ? realTarget.Host : $"{realTarget.Host}:{realTarget.Port}"); request.RequestUri = realTarget; if (request.Method == HttpMethod.Get) { request.Content = null; } return(request); }
public async Task <HttpResponseMessage> HandleRequest(HttpRequestMessage request, RequestRouterConfigurationBase configuration, CancellationToken cancellationToken) { var localModules = new List <IRequestHandlerModule>(_modules); var response = request.CreateResponse(HttpStatusCode.BadGateway); var originalUri = request.RequestUri; try { foreach (var module in localModules) { if (await module.ShouldProcessRequest(request, configuration, cancellationToken)) { response = await module.ProcessRequest(request, configuration, cancellationToken); break; } } } catch (Exception ex) { Logger.Warn("An exception occured processing a request.", ex); } Logger.Info($"{request.GetOwinContext().Request.RemoteIpAddress} {request.Method} {originalUri} - {response.StatusCode}"); return(response); }