Ejemplo n.º 1
0
        public int Read(out int statusCode)
        {
            var read   = _stream.Read(_buffer, 0, _buffer.Length);
            var length = read;

            statusCode    = HttpHelper.GetStatusCode(_buffer, 0, read);
            _responseType = HttpHelper.GetResponseType(_buffer, 0, read);

            if (_responseType == ResponseType.ContentLength)
            {
                var responseLength = HttpHelperContentLength.GetResponseLength(_buffer, 0, read);

                while (length < responseLength)
                {
                    length += _stream.Read(_buffer, 0, _buffer.Length);
                }
            }
            else if (_responseType == ResponseType.Chunked)
            {
                while (!HttpHelperChunked.IsEndOfChunkedStream(_buffer, read))
                {
                    read    = _stream.Read(_buffer, 0, _buffer.Length);
                    length += read;
                }
            }
            else
            {
                throw new UnknownResponseTypeException();
            }

            return(length);
        }
Ejemplo n.º 2
0
        public int ReadPipelined(out int statusCode)
        {
            if (_bufferIndex == 0)
            {
                _read = _stream.Read(_buffer, 0, _buffer.Length);
            }

            _responseType = HttpHelper.GetResponseType(_buffer, _bufferIndex, _read);

            while (_responseType == ResponseType.Unknown)
            {
                // Shift the buffer if we are running out of space
                if (_bufferIndex > _buffer.Length / 2)
                {
                    Buffer.BlockCopy(_buffer, _bufferIndex, _buffer, 0, _read - _bufferIndex);
                    _read       -= _bufferIndex;
                    _bufferIndex = 0;
                }

                _read        += _stream.Read(_buffer, _read, _buffer.Length - _read);
                _responseType = HttpHelper.GetResponseType(_buffer, _bufferIndex, _read);
            }

            statusCode = HttpHelper.GetStatusCode(_buffer, _bufferIndex, _read);

            if (_responseType == ResponseType.ContentLength)
            {
                var length         = _read - _bufferIndex;
                var responseLength = HttpHelperContentLength.GetResponseLength(_buffer, _bufferIndex, _read);

                while (responseLength < 0)
                {
                    // Shift the buffer if we are running out of space
                    if (_bufferIndex > _buffer.Length / 2)
                    {
                        Buffer.BlockCopy(_buffer, _bufferIndex, _buffer, 0, _read - _bufferIndex);
                        _read       -= _bufferIndex;
                        _bufferIndex = 0;
                    }

                    _read         += _stream.Read(_buffer, _read, _buffer.Length - _read);
                    length         = _read - _bufferIndex;
                    responseLength = HttpHelperContentLength.GetResponseLength(_buffer, _bufferIndex, _read);
                }

                while (length < responseLength)
                {
                    _read   = _stream.Read(_buffer, 0, _buffer.Length);
                    length += _read;
                }

                var end = _read - (length - responseLength);
                _bufferIndex = _read > end ? end : 0;
                return(responseLength);
            }
            else if (_responseType == ResponseType.Chunked)
            {
                var length    = 0;
                var streamEnd = HttpHelperChunked.SeekEndOfChunkedStream(_buffer, _bufferIndex, _read);

                if (streamEnd >= 0)
                {
                    length = streamEnd - _bufferIndex;
                }

                while (streamEnd < 0)
                {
                    _read     = _stream.Read(_buffer, 0, _buffer.Length);
                    length   += _read;
                    streamEnd = HttpHelperChunked.SeekEndOfChunkedStream(_buffer, 0, _read);

                    if (streamEnd >= 0)
                    {
                        length += streamEnd;
                    }
                }

                _bufferIndex = _read > streamEnd ? streamEnd : 0;
                return(length);
            }
            else
            {
                throw new UnknownResponseTypeException();
            }
        }