Example #1
0
        private IEnumerable <BytesWraper> ReceiveMessageBodyChunkedZip()
        {
            BytesWraper     bytesWraper   = new BytesWraper();
            ZipWraperStream streamWrapper = new ZipWraperStream(_request.ClientStream, _receiverHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                int    bufferSize = _request.TcpClient.ReceiveBufferSize;
                byte[] buffer     = bytesWraper.Value = new byte[bufferSize];
                while (true)
                {
                    string text = _receiverHelper.ReadLine();
                    if (!(text == "\r\n"))
                    {
                        text = text.Trim(' ', '\r', '\n');
                        if (text == string.Empty)
                        {
                            break;
                        }
                        int blockLength;
                        try
                        {
                            blockLength = Convert.ToInt32(text, 16);
                        }
                        catch (Exception ex)
                        {
                            if (ex is FormatException || ex is OverflowException)
                            {
                                throw NewHttpException(string.Format(Resources.HttpException_WrongChunkedBlockLength, text), ex);
                            }
                            throw;
                        }
                        if (blockLength == 0)
                        {
                            break;
                        }
                        streamWrapper.TotalBytesRead = 0;
                        streamWrapper.LimitBytesRead = blockLength;
                        while (true)
                        {
                            int num = stream.Read(buffer, 0, bufferSize);
                            if (num == 0)
                            {
                                if (streamWrapper.TotalBytesRead == blockLength)
                                {
                                    break;
                                }
                                WaitData();
                            }
                            else
                            {
                                bytesWraper.Length = num;
                                yield return(bytesWraper);
                            }
                        }
                    }
                }
            }
            /*Error near IL_01c5: Unexpected return in MoveNext()*/;
        }
Example #2
0
        private IEnumerable <BytesWraper> ReceiveMessageBodyZip(int contentLength)
        {
            var bytesWraper   = new BytesWraper();
            var streamWrapper = new ZipWraperStream(commonStream, receiveHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                byte[] buffer = new byte[bufferSize];
                bytesWraper.Value = buffer;

                while (true)
                {
                    int bytesRead = stream.Read(buffer, 0, bufferSize);
                    if (bytesRead == 0)
                    {
                        if (streamWrapper.TotalBytesRead == contentLength)
                        {
                            yield break;
                        }
                        else
                        {
                            WaitData();
                            continue;
                        }
                    }
                    bytesWraper.Length = bytesRead;
                    yield return(bytesWraper);
                }
            }
        }
Example #3
0
        private IEnumerable <BytesWraper> ReceiveMessageBodyZip(int contentLength)
        {
            BytesWraper     bytesWraper   = new BytesWraper();
            ZipWraperStream streamWrapper = new ZipWraperStream(_request.ClientStream, _receiverHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                int    bufferSize = _request.TcpClient.ReceiveBufferSize;
                byte[] buffer     = bytesWraper.Value = new byte[bufferSize];
                while (true)
                {
                    int num = stream.Read(buffer, 0, bufferSize);
                    if (num == 0)
                    {
                        if (streamWrapper.TotalBytesRead == contentLength)
                        {
                            break;
                        }
                        WaitData();
                    }
                    else
                    {
                        bytesWraper.Length = num;
                        yield return(bytesWraper);
                    }
                }
            }
        }
Example #4
0
        // Загрузка тела сообщения известной длины.
        private IEnumerable <BytesWraper> ReceiveMessageBody(int contentLength)
        {
            //Stream stream = _request.ClientStream;
            var bytesWraper = new BytesWraper();

            byte[] buffer = new byte[bufferSize];
            bytesWraper.Value = buffer;

            int totalBytesRead = 0;

            while (totalBytesRead != contentLength)
            {
                int bytesRead;
                if (receiveHelper.HasData)
                {
                    bytesRead = receiveHelper.Read(buffer, 0, bufferSize);
                }
                else
                {
                    bytesRead = commonStream.Read(buffer, 0, bufferSize);
                }
                if (bytesRead == 0)
                {
                    WaitData();
                }
                else
                {
                    totalBytesRead    += bytesRead;
                    bytesWraper.Length = bytesRead;
                    yield return(bytesWraper);
                }
            }
        }
Example #5
0
        private IEnumerable <BytesWraper> ReceiveMessageBody(Stream stream)
        {
            BytesWraper bytesWraper = new BytesWraper();
            int         bufferSize  = _request.TcpClient.ReceiveBufferSize;

            byte[] buffer       = bytesWraper.Value = new byte[bufferSize];
            int    begBytesRead = 0;

            if (stream is GZipStream || stream is DeflateStream)
            {
                begBytesRead = stream.Read(buffer, 0, bufferSize);
            }
            else
            {
                if (_receiverHelper.HasData)
                {
                    begBytesRead = _receiverHelper.Read(buffer, 0, bufferSize);
                }
                if (begBytesRead < bufferSize)
                {
                    begBytesRead += stream.Read(buffer, begBytesRead, bufferSize - begBytesRead);
                }
            }
            bytesWraper.Length = begBytesRead;
            yield return(bytesWraper);

            bool isHtml = FindSignature(buffer, begBytesRead, _openHtmlSignature);

            if (isHtml && FindSignature(buffer, begBytesRead, _closeHtmlSignature))
            {
                yield break;
            }
            int num;

            while (true)
            {
                num = stream.Read(buffer, 0, bufferSize);
                if (isHtml)
                {
                    if (num == 0)
                    {
                        WaitData();
                        continue;
                    }
                    if (FindSignature(buffer, num, _closeHtmlSignature))
                    {
                        break;
                    }
                }
                else if (num == 0)
                {
                    yield break;
                }
                bytesWraper.Length = num;
                yield return(bytesWraper);
            }
            bytesWraper.Length = num;
            yield return(bytesWraper);
        }
Example #6
0
        private IEnumerable <BytesWraper> ReceiveMessageBodyChunked()
        {
            Stream      stream      = _request.ClientStream;
            BytesWraper bytesWraper = new BytesWraper();
            int         bufferSize  = _request.TcpClient.ReceiveBufferSize;

            byte[] buffer = bytesWraper.Value = new byte[bufferSize];
            while (true)
            {
                string text = _receiverHelper.ReadLine();
                if (text == "\r\n")
                {
                    continue;
                }
                text = text.Trim(' ', '\r', '\n');
                if (text == string.Empty)
                {
                    break;
                }
                int totalBytesRead = 0;
                int blockLength;
                try
                {
                    blockLength = Convert.ToInt32(text, 16);
                }
                catch (Exception ex)
                {
                    if (ex is FormatException || ex is OverflowException)
                    {
                        throw NewHttpException(string.Format(Resources.HttpException_WrongChunkedBlockLength, text), ex);
                    }
                    throw;
                }
                if (blockLength == 0)
                {
                    break;
                }
                while (totalBytesRead != blockLength)
                {
                    int num = blockLength - totalBytesRead;
                    if (num > bufferSize)
                    {
                        num = bufferSize;
                    }
                    int num2 = (!_receiverHelper.HasData) ? stream.Read(buffer, 0, num) : _receiverHelper.Read(buffer, 0, num);
                    if (num2 == 0)
                    {
                        WaitData();
                        continue;
                    }
                    totalBytesRead    += num2;
                    bytesWraper.Length = num2;
                    yield return(bytesWraper);
                }
            }
        }
Example #7
0
        private IEnumerable <BytesWraper> ReceiveMessageBody(int contentLength)
        {
            Stream      stream      = _request.ClientStream;
            BytesWraper bytesWraper = new BytesWraper();
            int         bufferSize  = _request.TcpClient.ReceiveBufferSize;

            byte[] buffer         = bytesWraper.Value = new byte[bufferSize];
            int    totalBytesRead = 0;

            while (totalBytesRead != contentLength)
            {
                int num = (!_receiverHelper.HasData) ? stream.Read(buffer, 0, bufferSize) : _receiverHelper.Read(buffer, 0, bufferSize);
                if (num == 0)
                {
                    WaitData();
                    continue;
                }
                totalBytesRead    += num;
                bytesWraper.Length = num;
                yield return(bytesWraper);
            }
        }
Example #8
0
        private IEnumerable<BytesWraper> ReceiveMessageBodyChunkedZip()
        {
            var bytesWraper = new BytesWraper();
            var streamWrapper = new ZipWraperStream
                (_request.ClientStream, _receiverHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                int bufferSize = _request.TcpClient.ReceiveBufferSize;
                byte[] buffer = new byte[bufferSize];

                bytesWraper.Value = buffer;

                while (true)
                {
                    string line = _receiverHelper.ReadLine();

                    // Если достигнут конец блока.
                    if (line.Equals(
                        HttpHelper.NewLine, StringComparison.Ordinal))
                    {
                        continue;
                    }

                    line = line.Trim(' ', '\r', '\n');

                    // Если достигнут конец данных.
                    if (line.Equals("0", StringComparison.Ordinal))
                    {
                        yield break;
                    }

                    int blockLength;

                    #region Задаём длину блока

                    try
                    {
                        blockLength = Convert.ToInt32(line, 16);
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is OverflowException)
                        {
                            throw NewHttpException(string.Format(
                                Resources.HttpException_WrongChunkedBlockLength, line), ex);
                        }

                        throw;
                    }

                    #endregion

                    streamWrapper.TotalBytesRead = 0;
                    streamWrapper.LimitBytesRead = blockLength;

                    while (true)
                    {
                        int bytesRead = stream.Read(buffer, 0, bufferSize);

                        if (bytesRead == 0)
                        {
                            if (streamWrapper.TotalBytesRead == blockLength)
                            {
                                break;
                            }
                            else
                            {
                                WaitData();

                                continue;
                            }
                        }

                        bytesWraper.Length = bytesRead;
                        yield return bytesWraper;
                    }
                }
            }
        }
Example #9
0
        private IEnumerable<BytesWraper> ReceiveMessageBodyZip(int contentLength)
        {
            var bytesWraper = new BytesWraper();
            var streamWrapper = new ZipWraperStream(
                _request.ClientStream, _receiverHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                int bufferSize = _request.TcpClient.ReceiveBufferSize;
                byte[] buffer = new byte[bufferSize];

                bytesWraper.Value = buffer;

                while (true)
                {
                    int bytesRead = stream.Read(buffer, 0, bufferSize);

                    if (bytesRead == 0)
                    {
                        if (streamWrapper.TotalBytesRead == contentLength)
                        {
                            yield break;
                        }
                        else
                        {
                            WaitData();

                            continue;
                        }
                    }

                    bytesWraper.Length = bytesRead;
                    yield return bytesWraper;
                }
            }
        }
Example #10
0
        // Загрузка тела сообщения частями.
        private IEnumerable<BytesWraper> ReceiveMessageBodyChunked()
        {
            Stream stream = _request.ClientStream;
            var bytesWraper = new BytesWraper();

            int bufferSize = _request.TcpClient.ReceiveBufferSize;
            byte[] buffer = new byte[bufferSize];

            bytesWraper.Value = buffer;

            while (true)
            {
                string line = _receiverHelper.ReadLine();

                // Если достигнут конец блока.
                if (line.Equals(
                    HttpHelper.NewLine, StringComparison.Ordinal))
                {
                    continue;
                }

                line = line.Trim(' ', '\r', '\n');

                // Если достигнут конец тела сообщения.
                if (line.Equals("0", StringComparison.Ordinal))
                {
                    yield break;
                }

                int blockLength;
                int totalBytesRead = 0;

                #region Задаём длину блока

                try
                {
                    blockLength = Convert.ToInt32(line, 16);
                }
                catch (Exception ex)
                {
                    if (ex is FormatException || ex is OverflowException)
                    {
                        throw NewHttpException(string.Format(
                            Resources.HttpException_WrongChunkedBlockLength, line), ex);
                    }

                    throw;
                }

                #endregion

                while (totalBytesRead != blockLength)
                {
                    int length = blockLength - totalBytesRead;

                    if (length > bufferSize)
                    {
                        length = bufferSize;
                    }

                    int bytesRead;

                    if (_receiverHelper.HasData)
                    {
                        bytesRead = _receiverHelper.Read(buffer, 0, length);
                    }
                    else
                    {
                        bytesRead = stream.Read(buffer, 0, length);
                    }

                    if (bytesRead == 0)
                    {
                        WaitData();
                    }
                    else
                    {
                        totalBytesRead += bytesRead;

                        bytesWraper.Length = bytesRead;
                        yield return bytesWraper;
                    }
                }
            }
        }
Example #11
0
        // Загрузка тела сообщения известной длины.
        private IEnumerable<BytesWraper> ReceiveMessageBody(int contentLength)
        {
            Stream stream = _request.ClientStream;
            var bytesWraper = new BytesWraper();

            int bufferSize = _request.TcpClient.ReceiveBufferSize;
            byte[] buffer = new byte[bufferSize];

            bytesWraper.Value = buffer;

            int totalBytesRead = 0;

            while (totalBytesRead != contentLength)
            {
                int bytesRead;

                if (_receiverHelper.HasData)
                {
                    bytesRead = _receiverHelper.Read(buffer, 0, bufferSize);
                }
                else
                {
                    bytesRead = stream.Read(buffer, 0, bufferSize);
                }

                if (bytesRead == 0)
                {
                    WaitData();
                }
                else
                {
                    totalBytesRead += bytesRead;

                    bytesWraper.Length = bytesRead;
                    yield return bytesWraper;
                }
            }
        }
Example #12
0
        // Загрузка тела сообщения неизвестной длины.
        private IEnumerable<BytesWraper> ReceiveMessageBody(Stream stream)
        {
            var bytesWraper = new BytesWraper();

            int bufferSize = _request.TcpClient.ReceiveBufferSize;
            byte[] buffer = new byte[bufferSize];

            bytesWraper.Value = buffer;

            int begBytesRead = 0;

            // Считываем начальные данные из тела сообщения.
            if (stream is GZipStream || stream is DeflateStream)
            {
                begBytesRead = stream.Read(buffer, 0, bufferSize);
            }
            else
            {
                if (_receiverHelper.HasData)
                {
                    begBytesRead = _receiverHelper.Read(buffer, 0, bufferSize);
                }

                if (begBytesRead < bufferSize)
                {
                    begBytesRead += stream.Read(buffer, begBytesRead, bufferSize - begBytesRead);
                }
            }

            // Возвращаем начальные данные.
            bytesWraper.Length = begBytesRead;
            yield return bytesWraper;

            // Проверяем, есть ли открывающий тег '<html'.
            // Если есть, то считываем данные то тех пор, пока не встретим закрывающий тек '</html>'.
            bool isHtml = FindSignature(buffer, begBytesRead, _openHtmlSignatureBytes);

            if (isHtml)
            {
                bool found = FindSignature(buffer, begBytesRead, _closeHtmlSignatureBytes);

                // Проверяем, есть ли в начальных данных закрывающий тег.
                if (found)
                {
                    yield break;
                }
            }

            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, bufferSize);

                // Если тело сообщения представляет HTML.
                if (isHtml)
                {
                    if (bytesRead == 0)
                    {
                        WaitData();

                        continue;
                    }

                    bool found = FindSignature(buffer, bytesRead, _closeHtmlSignatureBytes);

                    if (found)
                    {
                        bytesWraper.Length = bytesRead;
                        yield return bytesWraper;

                        yield break;
                    }
                }
                else if (bytesRead == 0)
                {
                    yield break;
                }

                bytesWraper.Length = bytesRead;
                yield return bytesWraper;
            }
        }
Example #13
0
        private IEnumerable <BytesWraper> ReceiveMessageBodyChunkedZip()
        {
            var bytesWraper   = new BytesWraper();
            var streamWrapper = new ZipWraperStream(commonStream, receiveHelper);

            using (Stream stream = GetZipStream(streamWrapper))
            {
                byte[] buffer = new byte[bufferSize];
                bytesWraper.Value = buffer;
                while (true)
                {
                    string line = receiveHelper.ReadLine();
                    // Если достигнут конец блока.
                    if (line == newLine)
                    {
                        continue;
                    }

                    line = line.Trim(' ', '\r', '\n');
                    // Если достигнут конец тела сообщения.
                    if (line == string.Empty)
                    {
                        yield break;
                    }

                    int blockLength;
                    #region Задаём длину блока
                    try
                    {
                        blockLength = Convert.ToInt32(line, 16);
                    }
                    catch (Exception ex)
                    {
                        if (ex is FormatException || ex is OverflowException)
                        {
                            //throw NewHttpException(string.Format(
                            //Resources.HttpException_WrongChunkedBlockLength, line), ex);
                        }
                        throw;
                    }
                    #endregion
                    // Если достигнут конец тела сообщения.
                    if (blockLength == 0)
                    {
                        yield break;
                    }

                    streamWrapper.TotalBytesRead = 0;
                    streamWrapper.LimitBytesRead = blockLength;
                    while (true)
                    {
                        int bytesRead = stream.Read(buffer, 0, bufferSize);
                        if (bytesRead == 0)
                        {
                            if (streamWrapper.TotalBytesRead == blockLength)
                            {
                                break;
                            }
                            else
                            {
                                WaitData();
                                continue;
                            }
                        }
                        bytesWraper.Length = bytesRead;
                        yield return(bytesWraper);
                    }
                }
            }
        }
Example #14
0
        // Загрузка тела сообщения частями.
        private IEnumerable <BytesWraper> ReceiveMessageBodyChunked()
        {
            //Stream stream = _request.ClientStream;
            var bytesWraper = new BytesWraper();

            byte[] buffer = new byte[this.bufferSize];
            bytesWraper.Value = buffer;
            while (true)
            {
                string line = receiveHelper.ReadLine();
                // Если достигнут конец блока.
                if (line == newLine)
                {
                    continue;
                }

                line = line.Trim(' ', '\r', '\n');
                // Если достигнут конец тела сообщения.
                if (line == string.Empty)
                {
                    yield break;
                }

                int blockLength;
                int totalBytesRead = 0;
                #region Задаём длину блока
                try
                {
                    blockLength = Convert.ToInt32(line, 16);
                }
                catch (Exception ex)
                {
                    if (ex is FormatException || ex is OverflowException)
                    {
                        //throw NewHttpException(string.Format(
                        //Resources.HttpException_WrongChunkedBlockLength, line), ex);
                    }
                    throw;
                }
                #endregion
                // Если достигнут конец тела сообщения.
                if (blockLength == 0)
                {
                    yield break;
                }

                while (totalBytesRead != blockLength)
                {
                    int length = blockLength - totalBytesRead;
                    if (length > bufferSize)
                    {
                        length = bufferSize;
                    }
                    int bytesRead;
                    if (receiveHelper.HasData)
                    {
                        bytesRead = receiveHelper.Read(buffer, 0, length);
                    }
                    else
                    {
                        bytesRead = commonStream.Read(buffer, 0, length);
                    }
                    if (bytesRead == 0)
                    {
                        WaitData();
                    }
                    else
                    {
                        totalBytesRead    += bytesRead;
                        bytesWraper.Length = bytesRead;
                        yield return(bytesWraper);
                    }
                }
            }
        }
Example #15
0
        // Загрузка тела сообщения неизвестной длины.
        private IEnumerable <BytesWraper> ReceiveMessageBody(Stream stream)
        {
            var bytesWraper = new BytesWraper();

            byte[] buffer = new byte[this.bufferSize];
            bytesWraper.Value = buffer;
            int begBytesRead = 0;

            // Считываем начальные данные из тела сообщения.
            if (stream is GZipStream || stream is DeflateStream)
            {
                begBytesRead = stream.Read(buffer, 0, bufferSize);
            }
            else
            {
                if (receiveHelper.HasData)
                {
                    begBytesRead = receiveHelper.Read(buffer, 0, bufferSize);
                }
                if (begBytesRead < bufferSize)
                {
                    begBytesRead += stream.Read(buffer, begBytesRead, bufferSize - begBytesRead);
                }
            }
            // Возвращаем начальные данные.
            bytesWraper.Length = begBytesRead;
            yield return(bytesWraper);

            // Проверяем, есть ли открывающий тег '<html'.
            // Если есть, то считываем данные то тех пор, пока не встретим закрывающий тек '</html>'.
            bool isHtml = FindSignature(buffer, begBytesRead, openHtmlSignature);

            if (isHtml)
            {
                bool found = FindSignature(buffer, begBytesRead, closeHtmlSignature);
                // Проверяем, есть ли в начальных данных закрывающий тег.
                if (found)
                {
                    yield break;
                }
            }
            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, bufferSize);
                // Если тело сообщения представляет HTML.
                if (isHtml)
                {
                    if (bytesRead == 0)
                    {
                        WaitData();
                        continue;
                    }
                    bool found = FindSignature(buffer, bytesRead, closeHtmlSignature);
                    if (found)
                    {
                        bytesWraper.Length = bytesRead;
                        yield return(bytesWraper);

                        yield break;
                    }
                }
                else if (bytesRead == 0)
                {
                    yield break;
                }
                bytesWraper.Length = bytesRead;
                yield return(bytesWraper);
            }
        }
Example #16
0
        // Считывания тела сообщения неизвестной длины.
        private IEnumerable<BytesWraper> ReceiveMessageBody(Stream stream, bool isHtml)
        {
            var bytesWraper = new BytesWraper();

            int bufferSize = _request.TcpClient.ReceiveBufferSize;
            byte[] buffer = new byte[bufferSize];

            bytesWraper.Value = buffer;

            while (true)
            {
                int bytesRead = stream.Read(buffer, 0, bufferSize);

                #region Проверка на конец тела сообщения

                // Если тело сообщения содержит HTML.
                if (isHtml)
                {
                    if (bytesRead == 0)
                    {
                        WaitData();

                        continue;
                    }

                    bool found = false;
                    int newBufferLength = (buffer.Length - _signatureBytes.Length) + 1;

                    #region Поиск сигнатуры

                    for (int bufferIndex = 0; bufferIndex < newBufferLength && !found; ++bufferIndex)
                    {
                        if (buffer[bufferIndex] == 0)
                        {
                            continue;
                        }

                        for (int signatureIndex = 0; signatureIndex < _signatureBytes.Length; ++signatureIndex)
                        {
                            byte b = buffer[bufferIndex + signatureIndex];

                            // Если достигнуты буквы сигнатуры.
                            if (signatureIndex >= 2)
                            {
                                char c = (char)b;

                                // Приводим символ к нижнему регистру для правильного сравнения.
                                b = (byte)char.ToLower(c);
                            }

                            if (_signatureBytes[signatureIndex] == b)
                            {
                                found = true;
                            }
                            else
                            {
                                found = false;
                                break;
                            }
                        }
                    }

                    #endregion

                    if (found)
                    {
                        bytesWraper.Length = bytesRead;
                        yield return bytesWraper;

                        yield break;
                    }
                }
                else
                {
                    if (bytesRead == 0)
                    {
                        yield break;
                    }
                }

                #endregion

                bytesWraper.Length = bytesRead;
                yield return bytesWraper;
            }
        }