GetHandler() публичный Метод

public GetHandler ( HttpContext ctx, string requestType, string url, string pathTranslated ) : IHttpHandler
ctx System.Web.HttpContext
requestType string
url string
pathTranslated string
Результат IHttpHandler
        protected override async Task ProcessRequestAsync(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl))
            {
                return;
            }

            RequestContext.Instance.StartRequestContext();

            var operationName = context.Request.GetOperationName().UrlDecode();

            var httpReq = context.ToRequest(operationName);
            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                await HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);

                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            throw new NotImplementedException($"Cannot execute handler: {handler} at PathInfo: {httpReq.PathInfo}");
        }
Пример #2
0
        public virtual Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";

            var httpReq = context.ToRequest(operationName);
            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    return(next());
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return(task);
            }

            return(next());
        }
Пример #3
0
        protected override Task ProcessRequestAsync(HttpListenerContext context)
        {
            if (string.IsNullOrEmpty(context.Request.RawUrl))
            {
                return(((object)null).AsTaskResult());
            }

            var operationName = context.Request.GetOperationName();

            var httpReq = context.ToRequest(operationName);
            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                task.ContinueWith(x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return(task);
            }

            return(new NotImplementedException("Cannot execute handler: " + handler + " at PathInfo: " + httpReq.PathInfo)
                   .AsTaskException());
        }
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    await next();
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

            RequestContext.Instance.StartRequestContext();

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);

            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                await HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);

                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
Пример #5
0
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            if (NetCoreHandler != null)
            {
                var handled = await NetCoreHandler(context);

                if (handled)
                {
                    return;
                }
            }

            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                var includedInPathInfo = pathInfo.IndexOf(mode, StringComparison.Ordinal) == 1;
                var includedInPathBase = context.Request.PathBase.HasValue &&
                                         context.Request.PathBase.Value.IndexOf(mode, StringComparison.Ordinal) == 1;
                if (!includedInPathInfo && !includedInPathBase)
                {
                    await next();

                    return;
                }

                if (includedInPathInfo)
                {
                    pathInfo = pathInfo.Substring(mode.Length + 1);
                }
            }

            RequestContext.Instance.StartRequestContext();

            NetCoreRequest httpReq;
            IResponse      httpRes;

            System.Web.IHttpHandler handler;

            try
            {
                httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
                httpReq.RequestAttributes = httpReq.GetAttributes() | RequestAttributes.Http;

                httpRes = httpReq.Response;
                handler = HttpHandlerFactory.GetHandler(httpReq);

                if (BeforeNextMiddleware != null)
                {
                    var holdNext = next;
                    next = async() => {
                        await BeforeNextMiddleware(httpReq);
                        await holdNext();
                    };
                }
            }
            catch (Exception ex) //Request Initialization error
            {
                var logFactory = context.Features.Get <ILoggerFactory>();
                if (logFactory != null)
                {
                    var log = logFactory.CreateLogger(GetType());
                    log.LogError(default(EventId), ex, ex.Message);
                }

                context.Response.ContentType = MimeTypes.PlainText;
                await context.Response.WriteAsync($"{ex.GetType().Name}: {ex.Message}");

                if (Config.DebugMode)
                {
                    await context.Response.WriteAsync($"\nStackTrace:\n{ex.StackTrace}");
                }
                return;
            }

            if (handler is IServiceStackHandler serviceStackHandler)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();

                    return;
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                if (serviceStackHandler is RestHandler restHandler)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                try
                {
                    await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                }
                catch (Exception ex)
                {
                    var logFactory = context.Features.Get <ILoggerFactory>();
                    if (logFactory != null)
                    {
                        var log = logFactory.CreateLogger(GetType());
                        log.LogError(default(EventId), ex, ex.Message);
                    }
                }
                finally
                {
                    httpRes.Close();
                }
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
        public virtual async Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    await next();
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

            RequestContext.Instance.StartRequestContext();

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);

            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;

            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    await next();

                    return;
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                try
                {
                    await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                }
                catch (Exception ex)
                {
                    var logFactory = context.Features.Get <ILoggerFactory>();
                    var log        = logFactory.CreateLogger(GetType());
                    log.LogError(default(EventId), ex, ex.Message);
                }
                finally
                {
                    httpRes.Close();
                }
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return;
            }

            await next();
        }
Пример #7
0
        public virtual Task ProcessRequest(HttpContext context, Func <Task> next)
        {
            //Keep in sync with Kestrel/AppSelfHostBase.cs
            var operationName = context.Request.GetOperationName().UrlDecode() ?? "Home";
            var pathInfo      = context.Request.Path.HasValue
                ? context.Request.Path.Value
                : "/";

            var mode = Config.HandlerFactoryPath;

            if (!string.IsNullOrEmpty(mode))
            {
                if (pathInfo.IndexOf(mode, StringComparison.Ordinal) != 1)
                {
                    return(next());
                }

                pathInfo = pathInfo.Substring(mode.Length + 1);
            }

#if NETSTANDARD1_6
            // This fixes problems if the RequestContext.Instance.Items was touched on startup or outside of request context.
            // It would turn it into a static dictionary instead flooding request with each-others values.
            // This can already happen if I register a Funq.Container Request Scope type and Resolve it on startup.
            RequestContext.Instance.StartRequestContext();
#endif

            var httpReq = new NetCoreRequest(context, operationName, RequestAttributes.None, pathInfo);
            httpReq.RequestAttributes = httpReq.GetAttributes();

            var httpRes = httpReq.Response;
            var handler = HttpHandlerFactory.GetHandler(httpReq);

            var serviceStackHandler = handler as IServiceStackHandler;
            if (serviceStackHandler != null)
            {
                if (serviceStackHandler is NotFoundHttpHandler)
                {
                    return(next());
                }

                if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
                {
                    operationName = serviceStackHandler.RequestName;
                }

                var restHandler = serviceStackHandler as RestHandler;
                if (restHandler != null)
                {
                    httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
                }

                var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
                HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
                //Matches Exceptions handled in HttpListenerBase.InitTask()

                return(task);
            }

            return(next());
        }