Ejemplo n.º 1
0
        async Task EnsureChunkHeaderWasReadAsync(
            CancellationToken cancellationToken)
        {
            while (_currentChunkHeader == null)
            {
                cancellationToken.ThrowIfCancellationRequested();

                // Read
                var bytesRead = await _requestStream.ReadAsync(_readBuffer, 0, _readBuffer.Length);

                HttpPrematureFinishException.ThrowIfZero(bytesRead);

                // Append the data we just read to the header builder.
                // If header is built after this call, we are done here.
                int contentStartIndex;
                if (_chunkHeaderBuilder.AppendBuffer(_readBuffer, 0, bytesRead, out contentStartIndex))
                {
                    // Set current chunk metadata
                    _currentChunkHeader         = _chunkHeaderBuilder.Result;
                    _currentChunkRemainingBytes = _chunkHeaderBuilder.Result.Length;

                    // If we read some bytes from the body, rollback those read.
                    _requestStream.TryRollbackFromIndex(
                        _readBuffer,
                        srcLength: bytesRead,
                        startIndex: contentStartIndex);
                }
            }
        }
        bool HandleBytesRead(int bytesRead, HttpHeader header)
        {
            int nextLineStartIndex;

            if (_lineBuilder.AppendBuffer(_readBuffer, 0, bytesRead, out nextLineStartIndex))
            {
                // The chunk trailer always ended with a blank line,
                // so as soon as we found a blank line, stop reading.
                if (string.IsNullOrWhiteSpace(_lineBuilder.Result))
                {
                    return(true);
                }

                // Line found! Let's try to parse it into a key-value,
                // and append it to our header;
                AddToHeader(_lineBuilder.Result, header);

                // If we read some data of the next line, rollback
                _httpMessageBodyStream.TryRollbackFromIndex(
                    _readBuffer,
                    startIndex: nextLineStartIndex,
                    srcLength: bytesRead);

                // Get ready to read the next line.
                _lineBuilder.Reset();
            }

            return(false);
        }
Ejemplo n.º 3
0
        async Task ReceiveHeaderAsync()
        {
            RequireNullHeader();

            var headerBuilder = HttpHeaderBuilderFactory.CreateRequestHeaderBuilder();
            var buffer        = new byte[_tcpSettings.ReadWriteBufferSize];

            // Keep reading socket until header received
            while (null == _requestHeader)
            {
                var bytesRead = await _requestStream.ReadAsync(buffer, 0,
                                                               buffer.Length);

                HttpPrematureFinishException.ThrowIfZero(bytesRead);

                int bodyStartIndex;
                if (headerBuilder.AppendBuffer(buffer, 0, bytesRead,
                                               out bodyStartIndex))
                {
                    // Done!
                    // Set the header
                    _requestHeader = headerBuilder.Result;

                    // The remaining bytes of the buffer, those belongs to the body.
                    _requestStream.TryRollbackFromIndex(buffer,
                                                        srcLength: bytesRead, startIndex: bodyStartIndex);
                }
            }
        }