Ejemplo n.º 1
0
        public void Execute(byte[] buffer, int offset, int count)
        {
            if (isCompleted)
            {
                throw new InvalidOperationException("Parser is done");
            }

            if (!hasStarted)
            {
                inHeaders  = true;
                hasStarted = true;

                this.handler.OnResponseBegin();
            }

            if (!inHeaders)
            {
                if (!hasEntityData)
                {
                    this.isCompleted = true;
                    this.handler.OnResponseEnd();
                    return;
                }

                if (!inEntityData)
                {
                    inEntityData = true;
                    handler.OnEntityStart();
                }

                if (count > 0)
                {
                    this.handler.OnEntityData(buffer, offset, count);
                    this.entityDataWritten += count;
                }

                if (count == 0 || this.entityDataWritten == this.contentLength)
                {
                    inEntityData = false;
                    isCompleted  = true;
                    this.handler.OnEntityEnd();
                    this.handler.OnResponseEnd();
                }

                return;
            }

            int bufferLeft = parseBuffer.Length - parseBufferWritten;

            if (bufferLeft <= 0)
            {
                throw new FormatException("Response headers exceeded maximum allowed length");
            }

            if (count > bufferLeft)
            {
                this.Execute(buffer, offset, bufferLeft);
                this.Execute(buffer, offset + bufferLeft, count - bufferLeft);

                return;
            }

            Array.Copy(buffer, offset, parseBuffer, parseBufferWritten, count);
            parseBufferWritten += count;

            int endOfHeaders = IndexOf(parseBuffer, 0, parseBufferWritten, 13, 10, 13, 10);

            if (endOfHeaders >= 0)
            {
                ParseHeaders(parseBuffer, 0, endOfHeaders + 4);

                this.inHeaders = false;

                if (endOfHeaders + 4 < parseBufferWritten)
                {
                    this.Execute(parseBuffer, endOfHeaders + 4, parseBufferWritten - (endOfHeaders + 4));
                }
                else
                {
                    if (!hasEntityData)
                    {
                        this.isCompleted = true;
                        this.handler.OnResponseEnd();
                        return;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Execute(byte[] buffer, int offset, int count)
        {
            if (_isCompleted)
            {
                throw new InvalidOperationException("Parser is done");
            }

            if (!_hasStarted)
            {
                _inHeaders  = true;
                _hasStarted = true;

                _handler.OnResponseBegin();
            }

            if (!_inHeaders)
            {
                if (!_hasEntityData)
                {
                    _isCompleted = true;
                    _handler.OnResponseEnd();
                    return;
                }

                if (!_inEntityData)
                {
                    _inEntityData = true;
                    _handler.OnEntityStart();
                }

                if (count > 0)
                {
                    _handler.OnEntityData(buffer, offset, count);
                    _entityDataWritten += count;
                }

                if (count == 0 || _entityDataWritten == _contentLength)
                {
                    _inEntityData = false;
                    _isCompleted  = true;
                    _handler.OnEntityEnd();
                    _handler.OnResponseEnd();
                }

                return;
            }

            var bufferLeft = _parseBuffer.Length - _parseBufferWritten;

            if (bufferLeft <= 0)
            {
                throw new FormatException("Response headers exceeded maximum allowed length");
            }

            if (count > bufferLeft)
            {
                Execute(buffer, offset, bufferLeft);
                Execute(buffer, offset + bufferLeft, count - bufferLeft);

                return;
            }

            Array.Copy(buffer, offset, _parseBuffer, _parseBufferWritten, count);
            _parseBufferWritten += count;

            var endOfHeaders = IndexOf(_parseBuffer, 0, _parseBufferWritten, 13, 10, 13, 10);

            if (endOfHeaders >= 0)
            {
                ParseHeaders(_parseBuffer, 0, endOfHeaders + 4);

                _inHeaders = false;

                if (endOfHeaders + 4 < _parseBufferWritten)
                {
                    Execute(_parseBuffer, endOfHeaders + 4, _parseBufferWritten - (endOfHeaders + 4));
                }
                else
                {
                    if (!_hasEntityData)
                    {
                        _isCompleted = true;
                        _handler.OnResponseEnd();
                    }
                }
            }
        }