Example #1
0
        public void Decode(ISession session, Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

Start:
            object data;
            HandshakeStatus handshakeStatus = mServer.SessionStatus[session.ID];

            switch (handshakeStatus)
            {
                #region 握手
            case HandshakeStatus.RTMP_HANDSHAKE_0:
                if (pstream.Length < 1537)
                {
                    return;
                }
                data = OnC0C1Reader(session, pstream);
                Completed?.Invoke(this, mCompletedArgs.SetInfo(session, data));
                break;

            case HandshakeStatus.RTMP_HANDSHAKE_1:
                data = OnC2Reader(session, pstream);
                Completed?.Invoke(this, mCompletedArgs.SetInfo(session, data));
                break;

            case HandshakeStatus.RTMP_HANDSHAKE_2:
                #endregion
            default:
                break;
            }
            goto Start;
        }
Example #2
0
        private void OnHttpDecode(ISession session, PipeStream pstream)
        {
START:
            if (mRequest == null)
            {
                mRequest = new HttpRequest(session, this.Serializer);
            }
            if (mRequest.Read(pstream) == LoadedState.Completed)
            {
                int length  = mRequest.Length;
                int slength = (int)pstream.Length;
                if (string.Compare(mRequest.Method, "GET", true) == 0 || string.Compare(mRequest.Method, "POST", true) == 0)
                {
                    Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
                    if (pstream.Length == slength)
                    {
                        pstream.ReadFree(length);
                    }
                }
                else
                {
                    mRequest.CreateResponse().NotSupport();
                    if (session.Server.EnableLog(LogType.Warring))
                    {
                        session.Server.Log(LogType.Warring, session, "{0} {1} {2} not support", session.RemoteEndPoint, mRequest.Method, mRequest.Url);
                    }
                    HttpToken token = (HttpToken)session.Tag;
                    token.KeepAlive = false;
                }
                mRequest = null;
                if (pstream.Length == 0)
                {
                    return;
                }
                goto START;
            }
            else
            {
                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);
                    }
                    session.Dispose();
                    return;
                }
                return;
            }
        }
Example #3
0
        private void OnHttpDecode(ISession session, PipeStream pstream)
        {
START:
            if (mRequest == null)
            {
                mRequest = mServer.CreateRequest(session); //new HttpRequest(session, mServer);
            }
            if (mRequest.Read(pstream) == LoadedState.Completed)
            {
                int length = mRequest.Length;
                if (mRequest.Method == HttpParse.POST_TAG || mRequest.Method == HttpParse.GET_TAG ||
                    mRequest.Method == HttpParse.PUT_TAG || mRequest.Method == HttpParse.DELETE_TAG
                    )
                {
                    Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
                }
                else
                {
                    if (session.Server.EnableLog(LogType.Warring))
                    {
                        session.Server.Log(LogType.Warring, session, "{0} {1} {2} not support", session.RemoteEndPoint, mRequest.Method, mRequest.Url);
                    }
                    HttpToken token = (HttpToken)session.Tag;
                    token.KeepAlive = false;
                    NotSupportResult notSupport = new NotSupportResult("Method:" + mRequest.Method + " not supper");
                    mRequest.CreateResponse().Result(notSupport);
                }
                mRequest = null;
                if (pstream.Length == 0)
                {
                    return;
                }
                goto START;
            }
            else
            {
                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);
                    }
                    session.Dispose();
                    return;
                }
                return;
            }
        }
Example #4
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;
     }
 }
Example #5
0
        private void OnHttpDecode(ISession session, PipeStream pstream)
        {
START:
            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;
            }
            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);
                    }
                    session.Dispose();
                    return;
                }
                return;
            }
        }
Example #6
0
        public void Decode(ISession session, System.IO.Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

START:
            object data;

            if (mSize == 0)
            {
                if (SizeType == FixedSizeType.INT)
                {
                    if (pstream.Length < 4)
                    {
                        return;
                    }
                    mSize = pstream.ReadInt32();
                }
                else
                {
                    if (pstream.Length < 2)
                    {
                        return;
                    }
                    mSize = pstream.ReadInt16();
                }
            }
            if (pstream.Length < mSize)
            {
                return;
            }
            data  = OnRead(session, pstream);
            mSize = 0;
            Completed?.Invoke(this, mCompletedArgs.SetInfo(session, data));
            goto START;
        }
Example #7
0
        public void Decode(ISession session, Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

            while (true)
            {
                if (mRequest == null)
                {
                    mRequest        = new RPCPacket();
                    mRequest.Sesion = session;
                }
                if (mRequest.Read(Options, pstream))
                {
                    mCompletedArgs.SetInfo(session, mRequest);
                    try
                    {
                        Completed?.Invoke(this, mCompletedArgs);
                    }
                    finally
                    {
                        mRequest = null;
                    }
                }
                else
                {
                    return;
                }
            }
        }
Example #8
0
        public void Decode(ISession session, IBinaryReader reader)
        {
START:
            try
            {
                object data;
                lock (reader.Stream)
                {
                    if (mSize == 0)
                    {
                        if (reader.Length < 4)
                        {
                            return;
                        }
                        mSize = reader.ReadInt32();
                    }
                    if (reader.Length < mSize)
                    {
                        return;
                    }
                    Type type     = TypeHandler.ReadType(reader);
                    int  bodySize = reader.ReadInt32();
                    data = reader.Stream.Deserialize(bodySize, type);
                    Message msg = data as Message;
                    if (msg != null)
                    {
                        msg.Track("message decode start");
                        msg.IsLocal = false;
                        int  datasize = reader.ReadInt32();
                        Type dataType = TypeHandler.GetType(msg.DataType);
                        msg.Data = reader.Stream.Deserialize(datasize, dataType);
                        msg.Track("message decode completed");
                    }

                    mSize = 0;
                }
                try
                {
                    if (Completed != null)
                    {
                        Completed(this, mCompletedEventArgs.SetInfo(session, data));
                    }
                }
                catch (Exception e_)
                {
                    session.Server.Error(e_, session, "session packet process object error!");
                }
                if (reader.Length == 0)
                {
                    return;
                }
                goto START;
            }
            catch (Exception e_)
            {
                session.Server.Error(e_, session, "session packet decode error!");
                session.Dispose();
            }
        }
Example #9
0
        public void Decode(ISession session, Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

            if (pstream.Length > mServerConfig.MaxBodyLength)
            {
                session.Server.Log(LogType.Error, session, "http body too long!");
                session.Dispose();
                return;
            }
START:
            if (!mLodingDataPacket)
            {
                if (mConnectionRequest == null)
                {
                    mConnectionRequest = new HttpRequest(session, mBodySerializer);
                }
                if (mConnectionRequest.Read(pstream) == LoadedState.Completed)
                {
                    mLodingDataPacket = true;
                    Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mConnectionRequest));
                    if (pstream.Length > 0)
                    {
                        goto START;
                    }
                }
            }
            else
            {
                if (mDataPacket == null)
                {
                    mDataPacket = new DataFrame();
                    mDataPacket.DataPacketSerializer = this.mDataPacketSerializer;
                }
                if (mDataPacket.Read(pstream) == DataPacketLoadStep.Completed)
                {
                    DataFrame data = mDataPacket;
                    mDataPacket = null;
                    Completed?.Invoke(this, mCompletedArgs.SetInfo(session, data));
                    if (pstream.Length > 0)
                    {
                        goto START;
                    }
                }
            }
        }
Example #10
0
        private void OnHttpDecode(ISession session, PipeStream pstream)
        {
START:
            if (mRequest == null)
            {
                mRequest = new HttpRequest(session, this.Serializer);
            }
            if (mRequest.Read(pstream) == LoadedState.Completed)
            {
                int length  = mRequest.Length;
                int slength = (int)pstream.Length;
                if (string.Compare(mRequest.Method, "GET", true) == 0 || string.Compare(mRequest.Method, "POST", true) == 0)
                {
                    Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
                    if (pstream.Length == slength)
                    {
                        pstream.ReadFree(length);
                    }
                }
                else
                {
                    mRequest.CreateResponse().NotSupport();
                    if (length > 0)
                    {
                        pstream.ReadFree(length);
                    }
                }
                mRequest = null;
                if (pstream.Length == 0)
                {
                    return;
                }
                goto START;
            }
            else
            {
                if (mRequest.Length > mServerConfig.MaxBodyLength)
                {
                    session.Dispose();
                    return;
                }
                return;
            }
        }
Example #11
0
        public void Decode(ISession session, IBinaryReader reader)
        {
START:
            try
            {
                object data;
                if (mSize == 0)
                {
                    if (reader.Length < 4)
                    {
                        return;
                    }
                    mSize = reader.ReadInt32();
                }
                if (reader.Length < mSize)
                {
                    return;
                }
                Type   type     = TypeHandler.ReadType(reader);
                int    bodySize = reader.ReadInt32();
                string strData  = reader.ReadString(bodySize);
                //if (type.Name.IndexOf("StatisticalReport") >= 0)
                //{
                //	Console.WriteLine(strData);
                //}
                data  = Newtonsoft.Json.JsonConvert.DeserializeObject(strData, type);                //reader.Stream.Deserialize(bodySize, type);
                mSize = 0;
                try
                {
                    if (Completed != null)
                    {
                        Completed(this, mCompletedEventArgs.SetInfo(session, data));
                    }
                }
                catch (Exception e_)
                {
                    session.Server.Error(e_, session, "session packet process object error!");
                }

                goto START;
            }
            catch (Exception e_)
            {
                session.Server.Error(e_, session, "session packet decode error!");
                session.Dispose();
            }
        }
Example #12
0
        public void Decode(ISession session, System.IO.Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

START:
            object data;
            var index = pstream.IndexOf(EofData);

            if (index.End != null)
            {
                mSize = index.Length - EofData.Length;
                data  = OnRead(session, pstream);
                Completed?.Invoke(this, mCompletedArgs.SetInfo(session, data));
                pstream.ReadFree(EofData.Length);
                goto START;
            }
        }
Example #13
0
        public void Decode(ISession session, Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

START:
            if (mRequest == null)
            {
                mRequest = new HttpRequest(session, this.Serializer);
            }
            if (mRequest.Read(pstream) == LoadedState.Completed)
            {
                Completed?.Invoke(this, mCompletedArgs.SetInfo(session, mRequest));
                mRequest = null;
                if (pstream.Length == 0)
                {
                    return;
                }
                goto START;
            }
            else
            {
                return;
            }
        }
Example #14
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;
     }
 }