Task IRoutingProcessor.RouteRequest(IOwinContext context, Func <Task> next)
        {
            _segmentTestingFramework.SegmentRequest(context);

            var runable = _requestRouter.Route(context, Trace);

            if (runable == null)
            {
                return(next());
            }

            context.SetFeature(runable);

            if (!string.IsNullOrEmpty(runable.RequiredPermission) &&
                string.IsNullOrEmpty(runable.SecureResource))
            {
                Trace(context, () => GetType().Name + " runnable requires the user to have the " + runable.RequiredPermission + " permission");

                var upstreamAuthorization = context.GetFeature <IUpstreamAuthorization>();
                if (upstreamAuthorization != null)
                {
                    upstreamAuthorization.AddRequiredPermission(runable.RequiredPermission);
                }
            }

            if (runable.AllowAnonymous)
            {
                Trace(context, () => GetType().Name + " runnable allows anonymous requests");
            }
            else
            {
                Trace(context, () => GetType().Name + " runnable does not allow anonymous requests");

                var upstreamIdentification = context.GetFeature <IUpstreamIdentification>();
                if (upstreamIdentification != null)
                {
                    upstreamIdentification.AllowAnonymous = false;
                }
            }

            if (!string.IsNullOrEmpty(runable.CacheCategory))
            {
                Trace(context, () => GetType().Name + " runnable is in cache category " + runable.CacheCategory);

                var upstreamOutputCache = context.GetFeature <IUpstreamOutputCache>();
                if (upstreamOutputCache != null && upstreamOutputCache.CachedContentIsAvailable)
                {
                    Trace(context, () => GetType().Name + " output cache has cached output");

                    var timeInCache = upstreamOutputCache.TimeInCache;
                    if (timeInCache.HasValue && timeInCache.Value > _maximumCacheTime)
                    {
                        Trace(context, () => GetType().Name + " cached output is too old, discarding");
                        upstreamOutputCache.UseCachedContent = false;
                    }
                }
            }

            return(null);
        }
        Task IRoutingProcessor.RouteRequest(IOwinContext context, Func <Task> next)
        {
            var runable = _requestRouter.Route(context);

            if (runable == null)
            {
                return(next());
            }

            context.SetFeature(runable);

            if (!string.IsNullOrEmpty(runable.RequiredPermission))
            {
                var upstreamAuthorization = context.GetFeature <IUpstreamAuthorization>();
                if (upstreamAuthorization != null)
                {
                    upstreamAuthorization.AddRequiredPermission(runable.RequiredPermission);
                }
            }

            if (!runable.AllowAnonymous)
            {
                var upstreamIdentification = context.GetFeature <IUpstreamIdentification>();
                if (upstreamIdentification != null)
                {
                    upstreamIdentification.AllowAnonymous = false;
                }
            }

            if (!string.IsNullOrEmpty(runable.CacheCategory))
            {
                var upstreamOutputCache = context.GetFeature <IUpstreamOutputCache>();
                if (upstreamOutputCache.CachedContentIsAvailable)
                {
                    var timeInCache = upstreamOutputCache.TimeInCache;
                    if (timeInCache.HasValue && timeInCache.Value > _maximumCacheTime)
                    {
                        upstreamOutputCache.UseCachedContent = false;
                    }
                }
            }

            return(null);
        }
        public Task Invoke(IOwinContext context, Func <Task> next)
        {
            var debug = context.Request.Query["debug"];

            if (string.IsNullOrEmpty(debug))
            {
                return(next());
            }

            var runable = _requestRouter.Route(context, (c, t) => { });

            if (runable == null)
            {
                context.Response.StatusCode = 404;
                return(context.Response.WriteAsync("No routes match this request"));
            }

            var debugInfo = runable.GetDebugInfo(0, -1);

            if (string.Equals("svg", debug))
            {
                var svg = new DebugSvgDrawing();
                return(svg.Write(context, debugInfo));
            }

            if (string.Equals("html", debug))
            {
                var htmlPage = new DebugHtmlPage(_htmlWriterFactory, _renderContextFactory);
                return(htmlPage.Write(context, debugInfo));
            }

            if (string.Equals("xml", debug))
            {
                return(WriteXml(context, debugInfo));
            }

            if (string.Equals("json", debug))
            {
                return(WriteJson(context, debugInfo));
            }

            var accept = context.Request.Accept;

            if (!string.IsNullOrEmpty(accept))
            {
                if (accept.Contains("image/svg+xml"))
                {
                    var svg = new DebugSvgDrawing();
                    return(svg.Write(context, debugInfo));
                }

                if (accept.Contains("text/html"))
                {
                    var htmlPage = new DebugHtmlPage(_htmlWriterFactory, _renderContextFactory);
                    return(htmlPage.Write(context, debugInfo));
                }

                if (accept.Contains("application/xml"))
                {
                    return(WriteXml(context, debugInfo));
                }
            }

            return(WriteJson(context, debugInfo));
        }