Repository: dotnet/runtime
File: src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http3Connection.cs (C#)

Code before:

                        if (VariableLengthIntegerHelper.TryRead(buffer.ActiveSpan, out streamType, out int streamTypeLength))
                        {
                            // Successfully read the stream type.
                            buffer.Discard(streamTypeLength);
                            break;
                        }

                        // Need more data to read the full stream type.
                        buffer.EnsureAvailableSpace(VariableLengthIntegerHelper.MaximumEncodedLength);
                    }

                    // Check if this is a bidirectional stream (which we don't support from the server).
                    if (stream.CanWrite)
                    {
                        // Server initiated bidirectional streams are either push streams or extensions, and we support neither.
                        throw HttpProtocolException.CreateHttp3ConnectionException(Http3ErrorCode.StreamCreationError);
                    }

                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.Info(this, $"Received server-initiated unidirectional stream of type {streamType}");
                    }

                    // Process the stream based on its type.
                    switch (streamType)
                    {
                        case (long)Http3StreamType.Control:
                            if (Interlocked.Exchange(ref _haveServerControlStream, true))
                            {
                                // A second control stream has been received.
                                throw HttpProtocolException.CreateHttp3ConnectionException(Http3ErrorCode.StreamCreationError);
                            }

                            // Ownership of buffer is transferred to ProcessServerControlStreamAsync.
                            ArrayBuffer bufferCopy = buffer;
                            buffer = default;

                            await ProcessServerControlStreamAsync(stream, bufferCopy).ConfigureAwait(false);
                            return;
                        case (long)Http3StreamType.QPackDecoder:
                            if (Interlocked.Exchange(ref _haveServerQpackDecodeStream, true))
                            {
                                // A second QPack decode stream has been received.
                                throw HttpProtocolException.CreateHttp3ConnectionException(Http3ErrorCode.StreamCreationError);
                            }

                            // The stream must not be closed, but we aren't using QPACK right now -- ignore.
                            buffer.Dispose();
                            await stream.CopyToAsync(Stream.Null).ConfigureAwait(false);
                            return;

Review comment (suggestion):
@copilot instead of adding casts for all cases, use `switch ((Http3StreamType)streamType)` instead.

Also address https://github.com/dotnet/runtime/pull/122561/changes#r2627744277

Code after:
                            // A sender can close or reset a unidirectional stream unless otherwise specified. A receiver MUST
                            // tolerate unidirectional streams being closed or reset prior to the reception of the unidirectional
                            // stream header.
                            return;
                        }

                        buffer.Commit(bytesRead);

                        if (VariableLengthIntegerHelper.TryRead(buffer.ActiveSpan, out streamType, out int streamTypeLength))
                        {
                            // Successfully read the stream type.
                            buffer.Discard(streamTypeLength);
                            break;
                        }
                    }

                    if (NetEventSource.Log.IsEnabled())
                    {
                        NetEventSource.Info(this, $"Received server-initiated unidirectional stream of type {streamType}");
                    }

                    // Process the stream based on its type.
                    switch ((Http3StreamType)streamType)
                    {
                        case Http3StreamType.Control:
                            if (Interlocked.Exchange(ref _haveServerControlStream, true))
                            {
                                // A second control stream has been received.
                                throw HttpProtocolException.CreateHttp3ConnectionException(Http3ErrorCode.StreamCreationError);
                            }

                            // Ownership of buffer is transferred to ProcessServerControlStreamAsync.
                            ArrayBuffer bufferCopy = buffer;
                            buffer = default;

                            await ProcessServerControlStreamAsync(stream, bufferCopy).ConfigureAwait(false);
                            return;
                        case Http3StreamType.QPackDecoder:
                            if (Interlocked.Exchange(ref _haveServerQpackDecodeStream, true))
                            {
                                // A second QPack decode stream has been received.
                                throw HttpProtocolException.CreateHttp3ConnectionException(Http3ErrorCode.StreamCreationError);
                            }

                            // The stream must not be closed, but we aren't using QPACK right now -- ignore.
                            buffer.Dispose();
                            await stream.CopyToAsync(Stream.Null).ConfigureAwait(false);
                            return;
                        case Http3StreamType.QPackEncoder:
                            if (Interlocked.Exchange(ref _haveServerQpackEncodeStream, true))
                            {