Esempio n. 1
0
        /// <summary>
        ///     调用
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        private static void CallTask(object arg)
        {
            HttpContext context = (HttpContext)arg;
            var         router  = new Router(context);

            //跨域支持
            if (router.Data.HttpMethod == "OPTIONS")
            {
                HttpProtocol.Cros(context.Response);
                return;
            }

            IoHandler.OnBegin(router.Data);
            try
            {
                //内容页转向
                if (router.Data.Uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    context.Response.Redirect(RouteOption.Option.SystemConfig.ContextHost +
                                              router.Data.Uri.LocalPath.Trim('/'));
                    return;
                }

                HttpProtocol.FormatResponse(context.Response);
                //命令
                if (InnerCommand(router.Data.Uri.LocalPath, context.Response))
                {
                    return;
                }

                //开始调用
                if (!router.SecurityChecker.PreCheck())
                {
                    router.Data.Status = RouteStatus.DenyAccess;
                    context.Response.WriteAsync(ApiResult.DenyAccessJson, Encoding.UTF8);
                }
                else
                {
                    // 正常调用
                    router.Call();
                    // 写入返回
                    router.WriteResult();
                    // 缓存
                    RouteChahe.CacheResult(router.Data);
                }
            }
            catch (Exception e)
            {
                router.Data.Status = RouteStatus.LocalError;
                ZeroTrace.WriteException("Route", e);
                IocHelper.Create <IRuntimeWaring>()?.Waring("Route", router.Data.Uri.LocalPath, e.Message);
                context.Response.WriteAsync(ApiResult.LocalErrorJson, Encoding.UTF8);
            }
            finally
            {
                IoHandler.OnEnd(router.Data);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 调用
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static void CallTask(HttpContext context)
        {
            var uri = context.Request.GetUri();

            try
            {
                HttpProtocol.FormatResponse(context.Response);
                //内容页转向
                if (uri.LocalPath.IndexOf(".", StringComparison.OrdinalIgnoreCase) > 0)
                {
                    context.Response.Redirect(AppConfig.Config.SystemConfig.ContextHost + uri.LocalPath.Trim('/'));
                    return;
                }
                //跨域支持
                if (context.Request.Method.ToUpper() == "OPTIONS")
                {
                    HttpProtocol.Cros(context.Response);
                    return;
                }
                //命令
                if (RouteCommand.InnerCommand(uri.LocalPath, context.Response))
                {
                    return;
                }
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RuntimeWaring.Waring("Route", uri.LocalPath, e.Message);
                context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8);
                return;
            }

            var router = new HttpRouter(context);

            HttpIoLog.OnBegin(router.Data);
            var counter = PerformanceCounter.OnBegin(router.Data);

            try
            {
                var checker = new SecurityChecker
                {
                    Request = context.Request
                };
                if (!checker.PreCheck())
                {
                    router.Data.Status = RouteStatus.DenyAccess;
                    context.Response.WriteAsync(RouteRuntime.Inner2Error, Encoding.UTF8);
                }
                else
                {
                    // 正常调用
                    router.Call();
                    LogRecorder.BeginStepMonitor("End");
                    // 写入返回
                    router.WriteResult();
                    // 缓存
                    RouteChahe.CacheResult(router.Data);
                }
            }
            catch (Exception e)
            {
                router.Data.Status = RouteStatus.LocalError;
                LogRecorder.Exception(e);
                RuntimeWaring.Waring("Route", uri.LocalPath, e.Message);
                context.Response.WriteAsync(RouteRuntime.InnerError, Encoding.UTF8);
            }
            finally
            {
                //计时
                counter.End(router.Data);
                HttpIoLog.OnEnd(router.Data);
            }
        }