예제 #1
0
        public void Execute(HttpRequest request, HttpResponse response, HttpApiServer server)
        {
            ActionHandler handler = GetAction(request.BaseUrl);

            if (handler == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, $"{request.RemoteIPAddress} {request.Method} {request.Url}  not found");
                }
                if (!server.OnHttpRequesNotfound(request, response).Cancel)
                {
                    NotFoundResult notFoundResult = new NotFoundResult($"{request.Method} {request.Url} not found");
                    response.Result(notFoundResult);
                }
            }
            else
            {
                try
                {
                    if (request.Method != handler.Method)
                    {
                        if (request.Method == HttpParse.OPTIONS_TAG && handler.OptionsAttribute != null)
                        {
                            if (server.EnableLog(EventArgs.LogType.Info))
                            {
                                server.BaseServer.Log(EventArgs.LogType.Info, request.Session, $"{request.RemoteIPAddress}{request.Method}{request.Url} request");
                            }
                            response.Result(handler.OptionsAttribute);
                        }
                        else
                        {
                            if (server.EnableLog(EventArgs.LogType.Warring))
                            {
                                server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, $"{request.RemoteIPAddress}{request.Method} {request.Url} not support");
                            }
                            NotSupportResult notSupportResult = new NotSupportResult($"{request.Method}{request.Url} not support");
                            response.Result(notSupportResult);
                        }
                        return;
                    }
                    if (!handler.NoConvert && handler.DataConvert == null)
                    {
                        handler.DataConvert = DataContextBind.GetConvertAttribute(request.ContentType);
                    }
                    if (!handler.NoConvert)
                    {
                        handler.DataConvert.Execute(request.Data, request);
                    }
                    HttpContext pc        = new HttpContext(server, request, response, request.Data);
                    long        startTime = server.BaseServer.GetRunTime();
                    pc.ActionUrl = request.BaseUrl;
                    HttpActionResultHandler actionResult = new HttpActionResultHandler(Server, request, response, startTime);
                    ActionContext           context      = new ActionContext(handler, pc, this);
                    if (handler.OptionsAttribute != null)
                    {
                        handler.OptionsAttribute.SetResponse(request, response);
                    }
                    context.Execute(actionResult);
                }
                catch (Exception e_)
                {
                    handler.IncrementError();
                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        server.Log(EventArgs.LogType.Error, $"{request.RemoteIPAddress} http {request.Method} { request.Url} inner error {e_.Message}@{e_.StackTrace}");
                    }
                    InnerErrorResult result = new InnerErrorResult($"http execute {request.BaseUrl} error ", e_, server.Options.OutputStackTrace);
                    response.Result(result);
                }
            }
        }
예제 #2
0
        public void Execute(HttpRequest request, HttpResponse response, HttpApiServer server)
        {
            ActionHandler handler = GetAction(request.BaseUrl);

            if (handler == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} execute {1} action  not found", request.ClientIPAddress, request.Url);
                }
                if (!server.OnHttpRequesNotfound(request, response).Cancel)
                {
                    NotFoundResult notFoundResult = new NotFoundResult("{0} action not found", request.Url);
                    response.Result(notFoundResult);
                }
            }
            else
            {
                try
                {
                    if (request.Method != handler.Method)
                    {
                        if (server.EnableLog(EventArgs.LogType.Warring))
                        {
                            server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} execute {1} action  {1} not support", request.ClientIPAddress, request.Url, request.Method);
                        }
                        NotSupportResult notSupportResult = new NotSupportResult("{0} action not support {1}", request.Url, request.Method);
                        response.Result(notSupportResult);
                        return;
                    }
                    if (handler.DataConvert != null)
                    {
                        handler.DataConvert.Execute(request.Data, request);
                    }
                    HttpContext pc        = new HttpContext(server, request, response, request.Data);
                    long        startTime = server.BaseServer.GetRunTime();
                    pc.ActionUrl = request.BaseUrl;
                    ActionContext context = new ActionContext(handler, pc);
                    context.Execute();
                    if (!response.AsyncResult)
                    {
                        object result = context.Result;
                        response.Result(result);
                        if (server.EnableLog(EventArgs.LogType.Info))
                        {
                            server.BaseServer.Log(EventArgs.LogType.Info, request.Session, "{0} http execute {1} action use time:{2}ms", request.ClientIPAddress,
                                                  request.BaseUrl, server.BaseServer.GetRunTime() - startTime);
                        }
                    }
                }
                catch (Exception e_)
                {
                    InnerErrorResult result = new InnerErrorResult(e_, server.ServerConfig.OutputStackTrace);
                    response.Result(result);

                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        response.Session.Server.Log(EventArgs.LogType.Error, response.Session, "{0} execute {1} action inner error {2}@{3}", request.ClientIPAddress, request.Url, e_.Message, e_.StackTrace);
                    }
                }
            }
        }