Ejemplo n.º 1
0
 protected virtual void OnProcessResource(HttpRequest request, HttpResponse response)
 {
     if (request.Method == HttpParse.GET_TAG)
     {
         try
         {
             mResourceCenter.ProcessFile(request, response);
         }
         catch (Exception e_)
         {
             if (EnableLog(LogType.Error))
             {
                 BaseServer.Error(e_, request.Session, $"{request.RemoteIPAddress} {request.Method} {request.BaseUrl} file error {e_.Message}");
                 InnerErrorResult result = new InnerErrorResult($"response file error ", e_, ServerConfig.OutputStackTrace);
                 response.Result(result);
             }
         }
     }
     else
     {
         if (EnableLog(LogType.Info))
         {
             Log(LogType.Info, $"{request.RemoteIPAddress}{request.Method} {request.Url} not support");
         }
         NotSupportResult notSupport = new NotSupportResult($"{request.Method} {request.Url} not support");
         response.Result(notSupport);
     }
 }
Ejemplo n.º 2
0
        public void Error(Exception e_, EventArgs.LogType logType = EventArgs.LogType.Error)
        {
            if (Server.EnableLog(logType))
            {
                Server.Log(logType,
                           $"{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);
        }
Ejemplo n.º 3
0
 private void OnHttpDecode(ISession session, PipeStream pstream)
 {
     if (mRequest == null)
     {
         mRequest = mServer.CreateRequest(session);
     }
     if (mRequest.Read(pstream) == LoadedState.Completed)
     {
         try
         {
             Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
         }
         finally
         {
             mRequest = null;
         }
         //if (pstream.Length == 0)
         //    return;
         //goto START;
         return;
     }
     else
     {
         if (session.Server.EnableLog(LogType.Info))
         {
             session.Server.Log(LogType.Info, session, $"{session.RemoteEndPoint} Multi receive to http request");
         }
         if ((int)mRequest.State < (int)LoadedState.Header && pstream.Length > 1024 * 4)
         {
             if (session.Server.EnableLog(LogType.Warring))
             {
                 session.Server.Log(LogType.Warring, session, "{0} http header too long!", session.RemoteEndPoint);
             }
             session.Dispose();
         }
         else if (mRequest.Length > mServerConfig.MaxBodyLength)
         {
             if (session.Server.EnableLog(LogType.Warring))
             {
                 session.Server.Log(LogType.Warring, session, "{0} http body too long!", session.RemoteEndPoint);
             }
             HttpToken token = (HttpToken)session.Tag;
             token.KeepAlive = false;
             var response = mRequest.CreateResponse();
             InnerErrorResult innerErrorResult = new InnerErrorResult("413", "Request Entity Too Large");
             response.Result(innerErrorResult);
             //session.Dispose();
             return;
         }
         return;
     }
 }
Ejemplo n.º 4
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
                {
                    HttpContext pc        = new HttpContext(server, request, response);
                    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);
                    }
                }
            }
        }
Ejemplo n.º 5
0
 protected virtual void OnProcessResource(HttpRequest request, HttpResponse response)
 {
     if (string.Compare(request.Method, "GET", true) == 0)
     {
         try
         {
             mResourceCenter.ProcessFile(request, response);
         }
         catch (Exception e_)
         {
             if (EnableLog(LogType.Error))
             {
                 BaseServer.Error(e_, request.Session, "{0} response file error {1}", request.ClientIPAddress, e_.Message);
                 InnerErrorResult result = new InnerErrorResult(e_, ServerConfig.OutputStackTrace);
                 response.Result(result);
             }
         }
     }
     else
     {
         NotSupportResult notSupport = new NotSupportResult("{0} method {1} not support", request.Url, request.Method);
         response.Result(notSupport);
     }
 }
Ejemplo n.º 6
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);
                }
            }
        }
Ejemplo n.º 7
0
        private void OnRequestHandler(PacketDecodeCompletedEventArgs e)
        {
            try
            {
                HttpToken token = (HttpToken)e.Session.Tag;
                if (token.WebSocket)
                {
                    OnWebSocketRequest(token.Request, e.Session, (WebSockets.DataFrame)e.Message);
                }
                else
                {
                    HttpRequest request = (HttpRequest)e.Message;

                    if (EnableLog(LogType.Info))
                    {
                        mServer.Log(LogType.Info, null, $"HTTP {request.ID} {request.RemoteIPAddress} {request.Method} {request.Url}");
                    }
                    if (EnableLog(LogType.Debug))
                    {
                        mServer.Log(LogType.Debug, e.Session, $"HTTP {request.ID} {request.RemoteIPAddress} {request.Method} {request.Url} detail {request.ToString()}");
                    }
                    request.Server = this;
                    HttpResponse response = request.CreateResponse();
                    token.KeepAlive = request.KeepAlive;
                    if (!mIPLimit.ValidateRPS(request))
                    {
                        token.KeepAlive = false;
                        InnerErrorResult innerErrorResult = new InnerErrorResult("400", $"{request.RemoteIPAddress} request limit!");
                        response.Result(innerErrorResult);
                        return;
                    }
                    if (token.FirstRequest && string.Compare(request.Header[HeaderTypeFactory.UPGRADE], "websocket", true) == 0)
                    {
                        token.FirstRequest = false;
                        OnWebSocketConnect(request, response);
                    }
                    else
                    {
                        token.FirstRequest = false;
                        if (!Options.IOQueueEnabled)
                        {
                            OnHttpRequest(request, response);
                        }
                        else
                        {
                            IOQueueProcessArgs args = new IOQueueProcessArgs
                            {
                                Request  = request,
                                Response = response
                            };
                            token.IOQueue.Enqueue(args);
                        }
                    }
                }
            }
            catch (Exception e_)
            {
                if (EnableLog(LogType.Error))
                {
                    mServer.Error(e_, e.Session, $"HTTP {e.Session.RemoteEndPoint} {0} OnRequestHandler error {e_.Message}@{e_.StackTrace}");
                }
            }
        }
Ejemplo n.º 8
0
 private void OnHttpDecode(ISession session, PipeStream pstream)
 {
     mReceives++;
     if (mRequest == null)
     {
         mRequest    = mServer.CreateRequest(session);
         mRequest.ID = HttpRequest.GetID();
     }
     if (mRequest.Read(pstream) == LoadedState.Completed)
     {
         try
         {
             Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
         }
         finally
         {
             mRequest  = null;
             mReceives = 0;
         }
         return;
     }
     else
     {
         HttpToken token = (HttpToken)session.Tag;
         if (session.Server.EnableLog(LogType.Info))
         {
             session.Server.Log(LogType.Info, session, $"HTTP {mRequest.ID} {session.RemoteEndPoint} request from multi receive");
         }
         if (mRequest.State == LoadedState.None)
         {
             if (mReceives > 2 || pstream.FirstBuffer.Length < 32)
             {
                 if (session.Server.EnableLog(LogType.Warring))
                 {
                     session.Server.Log(LogType.Warring, session, $"HTTP {mRequest.ID} {session.RemoteEndPoint} receive data error!");
                 }
                 token.KeepAlive = false;
                 var response = mRequest.CreateResponse();
                 InnerErrorResult innerErrorResult = new InnerErrorResult("400", "Request http receive data error!");
                 response.Result(innerErrorResult);
                 return;
             }
             var  span     = pstream.FirstBuffer.Memory.Slice(0, 10).Span;
             var  method   = Encoding.ASCII.GetString(span.ToArray());
             bool ismethod = method.IndexOf("GET") == 0 || method.IndexOf("POST") == 0 || method.IndexOf("HEAD") == 0 ||
                             method.IndexOf("PUT") == 0 || method.IndexOf("DELETE") == 0 || method.IndexOf("CONNECT") == 0 || method.IndexOf("OPTIONS") == 0 ||
                             method.IndexOf("TRACE") == 0;
             if (!ismethod)
             {
                 if (session.Server.EnableLog(LogType.Warring))
                 {
                     session.Server.Log(LogType.Warring, session, $"HTTP {mRequest.ID} {session.RemoteEndPoint} protocol data error!");
                 }
                 token.KeepAlive = false;
                 var response = mRequest.CreateResponse();
                 InnerErrorResult innerErrorResult = new InnerErrorResult("400", "Request http protocol data error!");
                 response.Result(innerErrorResult);
                 return;
             }
         }
         if ((int)mRequest.State < (int)LoadedState.Header && (pstream.Length > 1024 * 4 || mReceives > 20))
         {
             if (session.Server.EnableLog(LogType.Warring))
             {
                 session.Server.Log(LogType.Warring, session, $"HTTP {mRequest.ID} {session.RemoteEndPoint} header too long!");
             }
             token.KeepAlive = false;
             var response = mRequest.CreateResponse();
             InnerErrorResult innerErrorResult = new InnerErrorResult("400", "Request header too large");
             response.Result(innerErrorResult);
         }
         else if (mRequest.Length > mServerConfig.MaxBodyLength)
         {
             if (session.Server.EnableLog(LogType.Warring))
             {
                 session.Server.Log(LogType.Warring, session, $"HTTP {mRequest.ID} {session.RemoteEndPoint} body too long!");
             }
             token.KeepAlive = false;
             var response = mRequest.CreateResponse();
             InnerErrorResult innerErrorResult = new InnerErrorResult("400", "Request entity too large");
             response.Result(innerErrorResult);
             return;
         }
         return;
     }
 }
Ejemplo n.º 9
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.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;
                    ActionContext context = new ActionContext(handler, pc, this);
                    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($"http execute {request.BaseUrl} action error ", 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);
                    }
                }
            }
        }