コード例 #1
0
        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));
        }
コード例 #2
0
        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);
            }
        }
コード例 #3
0
        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);
        }
コード例 #4
0
 public Task <HttpResponseMessage> ProcessRequest(HttpRequestMessage requestMessage, RequestRouterConfigurationBase configuration, CancellationToken cancellationToken)
 {
     return(ProcessRequest(requestMessage, configuration as TRequest, cancellationToken));
 }
コード例 #5
0
        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);
        }
コード例 #6
0
        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);
        }