Skip to content

Sync changes from runtime #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Shared/runtime/Http2/Hpack/HPackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace System.Net.Http.HPack
{
internal class HPackDecoder
{
private enum State
private enum State : byte
{
Ready,
HeaderFieldIndex,
Expand Down
7 changes: 2 additions & 5 deletions src/Shared/runtime/Http2/Hpack/HeaderField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@ public HeaderField(ReadOnlySpan<byte> name, ReadOnlySpan<byte> value)
// We should revisit our allocation strategy here so we don't need to allocate per entry
// and we have a cap to how much allocation can happen per dynamic table
// (without limiting the number of table entries a server can provide within the table size limit).
Name = new byte[name.Length];
name.CopyTo(Name);

Value = new byte[value.Length];
value.CopyTo(Value);
Name = name.ToArray();
Value = value.ToArray();
}

public byte[] Name { get; }
Expand Down
8 changes: 8 additions & 0 deletions src/Shared/runtime/Http3/QPack/QPackDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ private void OnByte(byte b, IHttpHeadersHandler handler)

if (_integerDecoder.BeginTryDecode((byte)prefixInt, LiteralHeaderFieldWithoutNameReferencePrefix, out intResult))
{
if (intResult == 0)
{
throw new QPackDecodingException(SR.Format(SR.net_http_invalid_header_name, ""));
}
OnStringLength(intResult, State.HeaderName);
}
else
Expand Down Expand Up @@ -303,6 +307,10 @@ private void OnByte(byte b, IHttpHeadersHandler handler)
case State.HeaderNameLength:
if (_integerDecoder.TryDecode(b, out intResult))
{
if (intResult == 0)
{
throw new QPackDecodingException(SR.Format(SR.net_http_invalid_header_name, ""));
}
OnStringLength(intResult, nextState: State.HeaderName);
}
break;
Expand Down
738 changes: 738 additions & 0 deletions src/Shared/runtime/NetEventSource.Common.cs

Large diffs are not rendered by default.

226 changes: 226 additions & 0 deletions src/Shared/runtime/Quic/Implementations/Mock/MockConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers.Binary;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Quic.Implementations.Mock
{
internal sealed class MockConnection : QuicConnectionProvider
{
private readonly bool _isClient;
private bool _disposed = false;
private IPEndPoint _remoteEndPoint;
private IPEndPoint _localEndPoint;
private object _syncObject = new object();
private Socket _socket = null;
private IPEndPoint _peerListenEndPoint = null;
private TcpListener _inboundListener = null;
private long _nextOutboundBidirectionalStream;
private long _nextOutboundUnidirectionalStream;

// Constructor for outbound connections
internal MockConnection(IPEndPoint remoteEndPoint, SslClientAuthenticationOptions sslClientAuthenticationOptions, IPEndPoint localEndPoint = null)
{
_remoteEndPoint = remoteEndPoint;
_localEndPoint = localEndPoint;

_isClient = true;
_nextOutboundBidirectionalStream = 0;
_nextOutboundUnidirectionalStream = 2;
}

// Constructor for accepted inbound connections
internal MockConnection(Socket socket, IPEndPoint peerListenEndPoint, TcpListener inboundListener)
{
_isClient = false;
_nextOutboundBidirectionalStream = 1;
_nextOutboundUnidirectionalStream = 3;
_socket = socket;
_peerListenEndPoint = peerListenEndPoint;
_inboundListener = inboundListener;
_localEndPoint = (IPEndPoint)socket.LocalEndPoint;
_remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;
}

internal override bool Connected
{
get
{
CheckDisposed();

return _socket != null;
}
}

internal override IPEndPoint LocalEndPoint => new IPEndPoint(_localEndPoint.Address, _localEndPoint.Port);

internal override IPEndPoint RemoteEndPoint => new IPEndPoint(_remoteEndPoint.Address, _remoteEndPoint.Port);

internal override SslApplicationProtocol NegotiatedApplicationProtocol => throw new NotImplementedException();

internal override async ValueTask ConnectAsync(CancellationToken cancellationToken = default)
{
CheckDisposed();

if (Connected)
{
// TODO: Exception text
throw new InvalidOperationException("Already connected");
}

Socket socket = new Socket(_remoteEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(_remoteEndPoint).ConfigureAwait(false);
socket.NoDelay = true;

_localEndPoint = (IPEndPoint)socket.LocalEndPoint;

// Listen on a new local endpoint for inbound streams
TcpListener inboundListener = new TcpListener(_localEndPoint.Address, 0);
inboundListener.Start();
int inboundListenPort = ((IPEndPoint)inboundListener.LocalEndpoint).Port;

// Write inbound listen port to socket so server can read it
byte[] buffer = new byte[4];
BinaryPrimitives.WriteInt32LittleEndian(buffer, inboundListenPort);
await socket.SendAsync(buffer, SocketFlags.None).ConfigureAwait(false);

// Read first 4 bytes to get server listen port
int bytesRead = 0;
do
{
bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None).ConfigureAwait(false);
} while (bytesRead != buffer.Length);

int peerListenPort = BinaryPrimitives.ReadInt32LittleEndian(buffer);
IPEndPoint peerListenEndPoint = new IPEndPoint(((IPEndPoint)socket.RemoteEndPoint).Address, peerListenPort);

_socket = socket;
_peerListenEndPoint = peerListenEndPoint;
_inboundListener = inboundListener;
}

internal override QuicStreamProvider OpenUnidirectionalStream()
{
long streamId;
lock (_syncObject)
{
streamId = _nextOutboundUnidirectionalStream;
_nextOutboundUnidirectionalStream += 4;
}

return new MockStream(this, streamId, bidirectional: false);
}

internal override QuicStreamProvider OpenBidirectionalStream()
{
long streamId;
lock (_syncObject)
{
streamId = _nextOutboundBidirectionalStream;
_nextOutboundBidirectionalStream += 4;
}

return new MockStream(this, streamId, bidirectional: true);
}

internal override long GetRemoteAvailableUnidirectionalStreamCount()
{
throw new NotImplementedException();
}

internal override long GetRemoteAvailableBidirectionalStreamCount()
{
throw new NotImplementedException();
}

internal async Task<Socket> CreateOutboundMockStreamAsync(long streamId)
{
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
await socket.ConnectAsync(_peerListenEndPoint).ConfigureAwait(false);
socket.NoDelay = true;

// Write stream ID to socket so server can read it
byte[] buffer = new byte[8];
BinaryPrimitives.WriteInt64LittleEndian(buffer, streamId);
await socket.SendAsync(buffer, SocketFlags.None).ConfigureAwait(false);

return socket;
}

internal override async ValueTask<QuicStreamProvider> AcceptStreamAsync(CancellationToken cancellationToken = default)
{
CheckDisposed();

Socket socket = await _inboundListener.AcceptSocketAsync().ConfigureAwait(false);

// Read first bytes to get stream ID
byte[] buffer = new byte[8];
int bytesRead = 0;
do
{
bytesRead += await socket.ReceiveAsync(buffer.AsMemory().Slice(bytesRead), SocketFlags.None).ConfigureAwait(false);
} while (bytesRead != buffer.Length);

long streamId = BinaryPrimitives.ReadInt64LittleEndian(buffer);

bool clientInitiated = ((streamId & 0b01) == 0);
if (clientInitiated == _isClient)
{
throw new Exception($"Wrong initiator on accepted stream??? streamId={streamId}, _isClient={_isClient}");
}

bool bidirectional = ((streamId & 0b10) == 0);
return new MockStream(socket, streamId, bidirectional: bidirectional);
}

internal override ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken = default)
{
Dispose();
return default;
}

private void CheckDisposed()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(QuicConnection));
}
}

private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_socket?.Dispose();
_socket = null;

_inboundListener?.Stop();
_inboundListener = null;
}

// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.

_disposed = true;
}
}

~MockConnection()
{
Dispose(false);
}

public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Net.Security;

namespace System.Net.Quic.Implementations.Mock
{
internal sealed class MockImplementationProvider : QuicImplementationProvider
{
internal override QuicListenerProvider CreateListener(QuicListenerOptions options)
{
return new MockListener(options.ListenEndPoint, options.ServerAuthenticationOptions);
}

internal override QuicConnectionProvider CreateConnection(QuicClientConnectionOptions options)
{
return new MockConnection(options.RemoteEndPoint, options.ClientAuthenticationOptions, options.LocalEndPoint);
}
}
}
Loading