Exemple #1
0
        public ActionResult ExecuteWithWS(HttpRequest request, HttpApiServer server, JToken token)
        {
            ActionResult result = new ActionResult();
            JToken       url    = token["url"];

            WebSockets.DataFrame dataFrame = server.CreateDataFrame(result);
            if (url == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} ws not support, url info notfound!", request.RemoteIPAddress);
                }
                result.Code  = 403;
                result.Error = "not support, url info notfound!";
                request.Session.Send(dataFrame);
                return(result);
            }
            result.Url = url.Value <string>();
            string baseurl = result.Url;

            if (server.ServerConfig.UrlIgnoreCase)
            {
                baseurl = HttpParse.CharToLower(result.Url);
            }
            if (baseurl[0] != '/')
            {
                baseurl = "/" + baseurl;
            }
            result.Url = baseurl;
            JToken data = token["params"];

            if (data == null)
            {
                data = (JToken)Newtonsoft.Json.JsonConvert.DeserializeObject("{}");
            }
            JToken requestid = data["_requestid"];

            if (requestid != null)
            {
                result.ID = requestid.Value <string>();
            }
            ActionHandler handler = GetAction(baseurl);

            if (handler == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, request.Session, "{0} ws execute {1} notfound", request.RemoteIPAddress, result.Url);
                }
                result.Code  = 404;
                result.Error = "url " + baseurl + " notfound!";
                request.Session.Send(dataFrame);
            }
            else
            {
                try
                {
                    Data.DataContxt dataContxt = new Data.DataContxt();
                    DataContextBind.BindJson(dataContxt, data);
                    WebsocketJsonContext dc = new WebsocketJsonContext(server, request, dataContxt);
                    dc.ActionUrl = baseurl;
                    dc.RequestID = result.ID;
                    ActionContext         context               = new ActionContext(handler, dc, this);
                    long                  startTime             = server.BaseServer.GetRunTime();
                    WSActionResultHandler wSActionResultHandler = new WSActionResultHandler(dc, server, request, result, dataFrame, startTime);
                    // ExecuteActionContext(context, wSActionResultHandler);
                    context.Execute(wSActionResultHandler);
                }
                catch (Exception e_)
                {
                    if (server.EnableLog(EventArgs.LogType.Error))
                    {
                        server.BaseServer.Log(EventArgs.LogType.Error, request.Session, "{0} ws execute {1} inner error {2}@{3}", request.RemoteIPAddress, request.Url, e_.Message, e_.StackTrace);
                    }
                    result.Code  = 500;
                    result.Error = e_.Message;
                    if (server.ServerConfig.OutputStackTrace)
                    {
                        result.StackTrace = e_.StackTrace;
                    }
                    dataFrame.Send(request.Session);
                }
            }
            return(result);
        }
Exemple #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, $"{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);
                    }
                    // ExecuteActionContext(context, actionResult);
                    context.Execute(actionResult);
                }
                catch (Exception e_)
                {
                    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.ServerConfig.OutputStackTrace);
                    response.Result(result);
                }
            }
        }
Exemple #3
0
        public void ExecuteWithWS(HttpRequest request, HttpApiServer server, JToken token)
        {
            ActionResult result = new ActionResult();
            JToken       url    = token["url"];

            WebSockets.DataFrame dataFrame = server.CreateDataFrame(result);
            if (url == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, null, $"Websocket {request.ID} {request.RemoteIPAddress} process error action url info notfound!");
                }
                result.Code  = 403;
                result.Error = "not support, url info notfound!";
                request.Session.Send(dataFrame);
                return;
            }
            result.Url = url.Value <string>();
            string baseurl = result.Url;

            if (server.Options.UrlIgnoreCase)
            {
                baseurl = HttpParse.CharToLower(result.Url);
            }
            if (baseurl[0] != '/')
            {
                baseurl = "/" + baseurl;
            }
            result.Url = baseurl;
            JToken data = token["params"];

            if (data == null)
            {
                data = (JToken)Newtonsoft.Json.JsonConvert.DeserializeObject("{}");
            }
            JToken requestid = data["_requestid"];

            if (requestid != null)
            {
                result.ID = requestid.Value <string>();
            }

            ActionHandler handler = GetAction(baseurl);

            if (handler == null)
            {
                if (server.EnableLog(EventArgs.LogType.Warring))
                {
                    server.BaseServer.Log(EventArgs.LogType.Warring, null, $"Websocket {request.ID} {request.RemoteIPAddress} ws execute {result.Url} notfound!");
                }
                result.Code  = 404;
                result.Error = "url " + baseurl + " notfound!";
                request.Session.Send(dataFrame);
            }
            else
            {
                try
                {
                    Data.DataContxt dataContxt = new Data.DataContxt();
                    DataContextBind.BindJson(dataContxt, data);
                    WebsocketJsonContext dc = new WebsocketJsonContext(server, request, dataContxt);
                    dc.ActionUrl = baseurl;
                    dc.RequestID = result.ID;
                    if (!Server.OnActionExecuting(dc))
                    {
                        return;
                    }
                    ActionContext         context               = new ActionContext(handler, dc, this);
                    long                  startTime             = server.BaseServer.GetRunTime();
                    WSActionResultHandler wSActionResultHandler = new WSActionResultHandler(dc, server, request, result, dataFrame, startTime);
                    if (!handler.HasValidation || handler.ValidateParamters(context.Parameters, out (Validations.ValidationBase, ParameterInfo)error))
                    {
                        context.Execute(wSActionResultHandler);
                    }
                    else
                    {
                        server.ValidationOutputHandler.Execute(dc, wSActionResultHandler, error.Item1, error.Item2);
                    }
                }