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); } } }
/// <summary> /// Read trailer and append extra trailer key-values into the specified header. /// </summary> public async Task ReadTrailerAsync(HttpHeader header) { if (header == null) { throw new ArgumentNullException(nameof(header)); } if (_isCompleted) { throw new InvalidOperationException("Trailer already read"); } // Keep reading until we find a blank line while (true) { // Read var bytesRead = await _httpMessageBodyStream.ReadAsync( _readBuffer, 0, _readBuffer.Length); HttpPrematureFinishException.ThrowIfZero(bytesRead); // Process if (HandleBytesRead(bytesRead, header)) { break; } } _isCompleted = true; }
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); } } }