Пример #1
0
        public bool Read(PipeStream stream, Cookies cookies)
        {
            IndexOfResult index = stream.IndexOf(HeaderType.LINE_BYTES);

            while (index.End != null)
            {
                if (index.Length == 2)
                {
                    stream.ReadFree(2);
                    return(true);
                }
                else
                {
                    ReadOnlySpan <Char> line = HttpParse.ReadCharLine(index);
                    stream.ReadFree(index.Length);
                    if (line[0] == 'C' && line[5] == 'e' && line[1] == 'o' && line[2] == 'o' && line[3] == 'k' && line[4] == 'i')
                    {
                        HttpParse.AnalyzeCookie(line.Slice(8, line.Length - 8), cookies);
                    }
                    else
                    {
                        Tuple <string, string> result = HttpParse.AnalyzeHeader(line);
                        Add(result.Item1, result.Item2);
                    }
                }
                index = stream.IndexOf(HeaderType.LINE_BYTES);
            }
            return(false);
        }
Пример #2
0
        private void ResponseHeader(PipeStream pipeStream)
        {
            if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info))
            {
                Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"gateway {Request.RemoteIPAddress} {Request.Method} {Request.Url} request {Server.Host}:{Server.Port} netstream receiving");
            }
            PipeStream agentStream = GetRequestStream();

            if (Status == RequestStatus.RespondingHeader)
            {
                var indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                while (indexof.End != null)
                {
                    pipeStream.Read(mBuffer, 0, indexof.Length);

                    if (indexof.Length == 2)
                    {
                        if (Request.VersionNumber == "1.0" && Request.KeepAlive)
                        {
                            agentStream.Write(Gateway.KEEP_ALIVE, 0, Gateway.KEEP_ALIVE.Length);
                        }
                        UrlRoute.Pluginer.HeaderWriting(Request, Response, mResultHeader);
                        mResultHeader.Write(agentStream);
                        agentStream.Write(mBuffer, 0, indexof.Length);
                        Status = RequestStatus.RespondingBody;
                        if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info))
                        {
                            Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"gateway {Request.RemoteIPAddress} {Request.Method} {Request.Url} request {Server.Host}:{Server.Port} netstream header received");
                        }
                        return;
                    }
                    else
                    {
                        var header = HttpParse.AnalyzeHeader(new ReadOnlySpan <byte>(mBuffer, 0, indexof.Length - 2));
                        if (string.Compare(header.Item1, HeaderTypeFactory.TRANSFER_ENCODING, true) == 0 && string.Compare(header.Item2, "chunked", true) == 0)
                        {
                            mTransferEncoding = true;
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.CONTENT_LENGTH, true) == 0)
                        {
                            mRequestLength = int.Parse(header.Item2);
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.SERVER, true) == 0)
                        {
                            //agentStream.Write(Gateway.GATEWAY_SERVER_HEDER, 0, Gateway.GATEWAY_SERVER_HEDER.Length);
                            mResultHeader.Add(HeaderTypeFactory.SERVER, "Bumblebee(BeetleX)");
                        }
                        else
                        {
                            mResultHeader.Add(header.Item1, header.Item2);
                            //if (UrlRoute.Gateway.OnHeaderWriting(Request, Response, agentStream, Server, header.Item1, header.Item2))
                            //{
                            //    agentStream.Write(mBuffer, 0, indexof.Length);
                            //}
                        }
                    }
                    indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                }
            }
        }
Пример #3
0
 private void ResponseHeader(PipeStream pipeStream)
 {
     if (Status == RequestStatus.RespondingHeader)
     {
         var indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
         while (indexof.End != null)
         {
             pipeStream.Read(mBuffer, 0, indexof.Length);
             if (indexof.Length == 2)
             {
                 Status = RequestStatus.RespondingBody;
                 return;
             }
             else
             {
                 var header = HttpParse.AnalyzeHeader(new ReadOnlySpan <byte>(mBuffer, 0, indexof.Length - 2));
                 Header.Add(new Property {
                     Name = header.Item1, Value = header.Item2
                 });
                 if (string.Compare(header.Item1, HeaderTypeFactory.TRANSFER_ENCODING, true) == 0 && string.Compare(header.Item2, "chunked", true) == 0)
                 {
                     mTransferEncoding = true;
                 }
                 if (string.Compare(header.Item1, HeaderTypeFactory.CONTENT_LENGTH, true) == 0)
                 {
                     mRequestLength = int.Parse(header.Item2);
                 }
             }
             indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
         }
     }
 }
Пример #4
0
        private void OnReadResponseHeader(PipeStream pipeStream)
        {
            if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info))
            {
                Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"Gateway {Request.ID} {Request.RemoteIPAddress} {Request.Method} {Request.Url} -> {Server.Host}:{Server.Port} response stream reading");
            }
            PipeStream agentStream = GetRequestStream();

            if (Status == RequestStatus.RespondingHeader)
            {
                mClientAgent.Status = TcpClientAgentStatus.ResponseReciveHeader;
                var indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                while (indexof.End != null)
                {
                    pipeStream.Read(mBuffer, 0, indexof.Length);

                    if (indexof.Length == 2)
                    {
                        if (Request.VersionNumber == "1.0" && Request.KeepAlive)
                        {
                            agentStream.Write(Gateway.KEEP_ALIVE, 0, Gateway.KEEP_ALIVE.Length);
                        }
                        UrlRoute.Pluginer.HeaderWriting(Request, Response, mResponseHeader);
                        mResponseHeader.Write(agentStream);
                        if (Server.Gateway.OutputServerAddress)
                        {
                            agentStream.Write("Logic-Server: " + Server.ServerName + "\r\n");
                        }
                        agentStream.Write(mBuffer, 0, indexof.Length);
                        Status = RequestStatus.RespondingBody;
                        if (Request.Server.EnableLog(BeetleX.EventArgs.LogType.Info))
                        {
                            Request.Server.Log(BeetleX.EventArgs.LogType.Info, $"gateway {Request.ID} {Request.RemoteIPAddress} {Request.Method} {Request.Url} -> {Server.Host}:{Server.Port} response stream read header ");
                        }
                        return;
                    }
                    else
                    {
                        var header = HttpParse.AnalyzeHeader(new ReadOnlySpan <byte>(mBuffer, 0, indexof.Length - 2));
                        if (string.Compare(header.Item1, HeaderTypeFactory.TRANSFER_ENCODING, true) == 0 && string.Compare(header.Item2, "chunked", true) == 0)
                        {
                            mTransferEncoding = true;
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.CONTENT_LENGTH, true) == 0)
                        {
                            mRequestLength = int.Parse(header.Item2);
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.SERVER, true) == 0)
                        {
                            mResponseHeader.Add(HeaderTypeFactory.SERVER, "Bumblebee(BeetleX)");
                        }
                        else
                        {
                            mResponseHeader.Add(header.Item1, header.Item2);
                        }
                    }
                    indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                }
            }
        }
Пример #5
0
        private void ResponseHeader(PipeStream pipeStream)
        {
            PipeStream agentStream = GetRequestStream();

            if (Status == RequestStatus.RespondingHeader)
            {
                var indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                while (indexof.End != null)
                {
                    pipeStream.Read(mBuffer, 0, indexof.Length);

                    if (indexof.Length == 2)
                    {
                        if (Request.VersionNumber == "1.0" && Request.KeepAlive)
                        {
                            agentStream.Write(Gateway.KEEP_ALIVE, 0, Gateway.KEEP_ALIVE.Length);
                        }
                        UrlRoute.Gateway.OnHeaderWrited(Request, Response, agentStream, Server);
                        agentStream.Write(mBuffer, 0, indexof.Length);
                        Status = RequestStatus.RespondingBody;
                        return;
                    }
                    else
                    {
                        var header = HttpParse.AnalyzeHeader(new ReadOnlySpan <byte>(mBuffer, 0, indexof.Length - 2));
                        if (string.Compare(header.Item1, HeaderTypeFactory.TRANSFER_ENCODING, true) == 0 && string.Compare(header.Item2, "chunked", true) == 0)
                        {
                            mTransferEncoding = true;
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.CONTENT_LENGTH, true) == 0)
                        {
                            mRequestLength = int.Parse(header.Item2);
                        }
                        if (string.Compare(header.Item1, HeaderTypeFactory.SERVER, true) == 0)
                        {
                            agentStream.Write(Gateway.GATEWAY_SERVER_HEDER, 0, Gateway.GATEWAY_SERVER_HEDER.Length);
                        }
                        else
                        {
                            if (UrlRoute.Gateway.OnHeaderWriting(Request, Response, agentStream, Server, header.Item1, header.Item2))
                            {
                                agentStream.Write(mBuffer, 0, indexof.Length);
                            }
                        }
                    }
                    indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
                }
            }
        }
Пример #6
0
        private void ReadMask(PipeStream stream)
        {
            IndexOfResult result = stream.IndexOf((int)this.Length);
            int           index  = 0;

            if (result.Start.ID == result.End.ID)
            {
                index = MarkBytes(result.Start.Bytes, result.StartPostion, result.EndPostion, index);
            }
            else
            {
                index = MarkBytes(result.Start.Bytes, result.StartPostion, result.Start.Length - 1, index);
                IMemoryBlock next = result.Start.NextMemory;
                while (next != null)
                {
                    if (next.ID == result.End.ID)
                    {
                        index = MarkBytes(next.Bytes, 0, result.EndPostion, index);
                        break;
                    }
                    else
                    {
                        index = MarkBytes(next.Bytes, 0, next.Length - 1, index);
                    }
                    next = result.Start.NextMemory;
                }
            }
        }
Пример #7
0
        public static bool ReadLine(this PipeStream stream, out Span <char> line)
        {
            //line = default;
            //if (stringBuffer == null)
            //    stringBuffer = new StringBuffer();
            //int count = stream.IndexOf(HeaderTypeFactory.LINE_BYTES, stringBuffer.Bytes);
            //if (count > 0)
            //{
            //    stream.ReadFree(count);
            //    var len = Encoding.ASCII.GetChars(stringBuffer.Bytes, 0, count - 2, stringBuffer.Chars, 0);
            //    line = new Span<char>(stringBuffer.Chars, 0, len);
            //    return true;
            //}
            //return false;
            line = default;
            if (stringBuffer == null)
            {
                stringBuffer = new StringBuffer();
            }
            var indexof = stream.IndexOf(HeaderTypeFactory.LINE_BYTES);

            if (indexof.End != null)
            {
                stream.Read(stringBuffer.Bytes, 0, indexof.Length);
                var len = Encoding.ASCII.GetChars(stringBuffer.Bytes, 0, indexof.Length - 2, stringBuffer.Chars, 0);
                line = new Span <char>(stringBuffer.Chars, 0, len);
                return(true);
            }
            return(false);
        }
Пример #8
0
        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            PipeStream pipeStream = e.Session.Stream.ToPipeStream();
            HttpToken  token      = (HttpToken)e.Session.Tag;
            var        result     = pipeStream.IndexOf(_line.Data);

            while (result.End != null)
            {
                if (result.Length == 2)
                {
                    if (token.CurrentRequest != null)
                    {
                        token.Requests.Enqueue(token.CurrentRequest);
                        token.CurrentRequest = null;
                    }
                    pipeStream.ReadFree(result.Length);
                }
                else
                {
                    if (token.CurrentRequest == null)
                    {
                        token.CurrentRequest = new RequestData();
                        var buffer = System.Buffers.ArrayPool <byte> .Shared.Rent(result.Length);

                        pipeStream.Read(buffer, 0, result.Length);
                        token.CurrentRequest.Data = new ArraySegment <byte>(buffer, 0, result.Length);
                    }
                    else
                    {
                        pipeStream.ReadFree(result.Length);
                    }
                }
                if (pipeStream.Length > 0)
                {
                    result = pipeStream.IndexOf(_line.Data);
                }
                else
                {
                    break;
                }
            }
            if (pipeStream.Length == 0 && token.CurrentRequest == null)
            {
                ProcessReqeusts(token, pipeStream, e.Session);
            }
        }
Пример #9
0
 private void ResponseStatus(PipeStream pipeStream)
 {
     if (Status == RequestStatus.Responding)
     {
         var indexof = pipeStream.IndexOf(HeaderTypeFactory.LINE_BYTES);
         if (indexof.EofData != null)
         {
             pipeStream.Read(mBuffer, 0, indexof.Length);
             GetRequestStream().Write(mBuffer, 0, indexof.Length);
             var result = HttpParse.AnalyzeResponseLine(new ReadOnlySpan <byte>(mBuffer, 0, indexof.Length - 2));
             Code   = result.Item2;
             Status = RequestStatus.RespondingHeader;
         }
     }
 }
Пример #10
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;
            }
        }
Пример #11
0
        public void Decode(IClient client, Stream stream)
        {
            PipeStream pstream = stream.ToPipeStream();

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

            if (index.End != null)
            {
                object data;
                mSize = index.Length - EofData.Length;
                data  = OnRead(client, pstream);
                Completed?.Invoke(client, data);
                pstream.ReadFree(EofData.Length);
                goto START;
            }
        }
Пример #12
0
 private void LoadMethod(PipeStream stream)
 {
     if (mState == LoadedState.None)
     {
         IndexOfResult index = stream.IndexOf(HeaderType.LINE_BYTES);
         if (index.End != null)
         {
             ReadOnlySpan <Char> line = HttpParse.ReadCharLine(index);
             stream.ReadFree(index.Length);
             Tuple <string, string, string> result = HttpParse.AnalyzeRequestLine(line);
             Method      = result.Item1;
             Url         = result.Item2;
             BaseUrl     = HttpParse.GetBaseUrlToLower(Url);
             Ext         = HttpParse.GetBaseUrlExt(BaseUrl);
             HttpVersion = result.Item3;
             HttpParse.AnalyzeQueryString(Url, mQueryString);
             mState = LoadedState.Method;
         }
     }
 }
Пример #13
0
        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            PipeStream pipeStream = e.Session.Stream.ToPipeStream();
            HttpToken  token      = (HttpToken)e.Session.Tag;
            var        result     = pipeStream.IndexOf(_line.Data);

            if (result.End == null)
            {
                return;
            }
            int len = result.Length;

            pipeStream.Read(token.Buffer, 0, len);
            ReadOnlySpan <byte> line   = new Span <byte>(token.Buffer, 0, len);
            ReadOnlySpan <byte> http   = line;
            ReadOnlySpan <byte> method = line;
            ReadOnlySpan <byte> url    = line;
            int offset2 = 0;
            int count   = 0;

            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == _Space)
                {
                    if (count != 0)
                    {
                        url     = line.Slice(offset2, i - offset2);
                        offset2 = i + 1;
                        http    = line.Slice(offset2, line.Length - offset2 - 2);
                        break;
                    }
                    method  = line.Slice(offset2, i - offset2);
                    offset2 = i + 1;
                    count++;
                }
            }
            OnStartLine(http, method, url, e.Session, token, pipeStream);
        }
Пример #14
0
        private void OnProcess(PipeStream pipeStream, HttpToken token, ISession sessino)
        {
            var result = pipeStream.IndexOf(_line.Data);

            if (result.End == null)
            {
                return;
            }
            int len = result.Length;

            pipeStream.Read(token.Buffer, 0, len);
            ReadOnlySpan <byte> line   = new Span <byte>(token.Buffer, 0, len);
            ReadOnlySpan <byte> http   = line;
            ReadOnlySpan <byte> method = line;
            ReadOnlySpan <byte> url    = line;
            int offset2 = 0;
            int count   = 0;

            for (int i = 0; i < line.Length; i++)
            {
                if (line[i] == _Space)
                {
                    if (count != 0)
                    {
                        url     = line.Slice(offset2, i - offset2);
                        offset2 = i + 1;
                        http    = line.Slice(offset2, line.Length - offset2 - 2);
                        break;
                    }
                    method  = line.Slice(offset2, i - offset2);
                    offset2 = i + 1;
                    count++;
                }
            }
            OnStartLine(http, method, url, sessino, token, pipeStream);
        }
Пример #15
0
        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            PipeStream pipeStream = e.Session.Stream.ToPipeStream();
            HttpToken  token      = (HttpToken)e.Session.Tag;
            var        result     = pipeStream.IndexOf(_line.Data);

            while (result.End != null)
            {
                if (result.Length == 2)
                {
                    if (token.CurrentRequest != null)
                    {
                        token.Requests.Enqueue(token.CurrentRequest);
                        token.CurrentRequest = null;
                        token.ThreadDispatcher.Enqueue(token);
                    }
                    pipeStream.ReadFree(result.Length);
                }
                else
                {
                    if (token.CurrentRequest == null)
                    {
                        var request = new RequestData();

                        byte[] buffer = null;
                        if (Program.Debug)
                        {
                            buffer = new byte[result.Length];
                        }
                        else
                        {
                            buffer = System.Buffers.ArrayPool <byte> .Shared.Rent(result.Length);
                        }
                        pipeStream.Read(buffer, 0, result.Length);
                        request.Data = new ArraySegment <byte>(buffer, 0, result.Length);
                        AnalysisAction(request);
                        if (request.Action == ActionType.Plaintext)
                        {
                            token.CurrentRequest = request;
                        }
                        else
                        {
                            pipeStream.ReadFree((int)pipeStream.Length);
                            OnStartRequest(request, e.Session, token, pipeStream);
                            return;
                        }
                    }
                    else
                    {
                        pipeStream.ReadFree(result.Length);
                    }
                }
                if (pipeStream.Length > 0)
                {
                    result = pipeStream.IndexOf(_line.Data);
                }
                else
                {
                    break;
                }
            }
        }