Skip to content

Commit 233704b

Browse files
committed
wip: ssl, cadente improvements and fixes
1 parent 24d9c25 commit 233704b

File tree

5 files changed

+329
-75
lines changed

5 files changed

+329
-75
lines changed

cadente/Sisk.Cadente/HttpHost.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,12 @@ private async void ReceiveClient ( IAsyncResult result ) {
127127

128128
private async Task HandleTcpClient ( TcpClient client ) {
129129

130-
bool clientIsAlive = true;
131130
try {
132-
client.ReceiveTimeout = TimeoutManager._ClientReadTimeoutSeconds;
133-
client.SendTimeout = TimeoutManager._ClientWriteTimeoutSeconds;
131+
int clientReadTimeoutMs = (int) TimeoutManager.ClientReadTimeout.TotalMilliseconds;
132+
int clientWriteTimeoutMs = (int) TimeoutManager.ClientWriteTimeout.TotalMilliseconds;
133+
134+
client.ReceiveTimeout = clientReadTimeoutMs;
135+
client.SendTimeout = clientWriteTimeoutMs;
134136

135137
if (Handler is null)
136138
return;
@@ -145,31 +147,11 @@ private async Task HandleTcpClient ( TcpClient client ) {
145147
connectionStream = clientStream;
146148
}
147149

148-
CancellationTokenSource disconnectToken = new CancellationTokenSource ();
149-
Task disconnectPoolingTask = Task.Factory.StartNew ( delegate {
150-
151-
try {
152-
int itcount = 0;
153-
while (clientIsAlive && itcount < 3) {
154-
if (client.Client.Poll ( 1, SelectMode.SelectRead )) {
155-
itcount++;
156-
}
157-
158-
Thread.Sleep ( 100 );
159-
}
160-
}
161-
catch {
162-
;
163-
}
164-
finally {
165-
disconnectToken.Cancel ();
166-
clientIsAlive = false;
167-
}
168-
169-
}, TaskCreationOptions.LongRunning );
170-
171150
IPEndPoint clientEndpoint = (IPEndPoint) client.Client.RemoteEndPoint!;
172-
HttpHostClient hostClient = new HttpHostClient ( clientEndpoint, disconnectToken.Token );
151+
HttpHostClient hostClient = new HttpHostClient ( clientEndpoint, CancellationToken.None );
152+
153+
connectionStream.ReadTimeout = clientReadTimeoutMs;
154+
connectionStream.WriteTimeout = clientWriteTimeoutMs;
173155

174156
using (HttpConnection connection = new HttpConnection ( hostClient, connectionStream, this, clientEndpoint )) {
175157

@@ -194,7 +176,6 @@ await sslStream.AuthenticateAsServerAsync (
194176
}
195177
}
196178
finally {
197-
clientIsAlive = false;
198179
client.Dispose ();
199180
}
200181
}

cadente/Sisk.Cadente/HttpHostTimeoutManager.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,15 @@ namespace Sisk.Cadente;
1414
/// </summary>
1515
public sealed class HttpHostTimeoutManager {
1616

17-
internal int _ClientReadTimeoutSeconds = 30;
18-
internal int _ClientWriteTimeoutSeconds = 30;
19-
2017
/// <summary>
2118
/// Gets or sets the timeout for client read operations.
2219
/// </summary>
23-
public TimeSpan ClientReadTimeout {
24-
get => TimeSpan.FromSeconds ( _ClientReadTimeoutSeconds );
25-
set => _ClientReadTimeoutSeconds = (int) value.TotalSeconds;
26-
}
20+
public TimeSpan ClientReadTimeout { get; set; } = TimeSpan.FromSeconds ( 30 );
2721

2822
/// <summary>
2923
/// Gets or sets the timeout for client write operations.
3024
/// </summary>
31-
public TimeSpan ClientWriteTimeout {
32-
get => TimeSpan.FromSeconds ( _ClientWriteTimeoutSeconds );
33-
set => _ClientWriteTimeoutSeconds = (int) value.TotalSeconds;
34-
}
25+
public TimeSpan ClientWriteTimeout { get; set; } = TimeSpan.FromSeconds ( 30 );
3526

3627
internal HttpHostTimeoutManager () { }
3728
}

cadente/Sisk.Cadente/Streams/HttpChunkedStream.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,30 @@ public override int Read ( byte [] buffer, int offset, int count ) {
4242
if (innerStreamReturnedZero)
4343
return 0;
4444

45-
Span<byte> readBuffer = stackalloc byte [ Math.Min ( count - 2, CHUNKED_MAX_SIZE ) ];
45+
Span<byte> destination = buffer.AsSpan ( offset, count );
46+
if (destination.Length < 2)
47+
throw new ArgumentException ( "The provided buffer slice must be at least 2 bytes long.", nameof ( count ) );
48+
49+
Span<byte> readBuffer = stackalloc byte [ Math.Min ( destination.Length - 2, CHUNKED_MAX_SIZE ) ];
4650
int read = _stream.Read ( readBuffer );
4751
byte [] readBytesEncoded = Encoding.ASCII.GetBytes ( $"{read:x}\r\n" );
4852

53+
if (destination.Length < readBytesEncoded.Length + read + 2)
54+
throw new ArgumentException ( "The provided buffer slice is not large enough to hold the chunked response.", nameof ( count ) );
55+
4956
if (read == 0) {
5057
innerStreamReturnedZero = true;
5158
}
5259

53-
Array.Copy (
54-
sourceArray: readBytesEncoded,
55-
sourceIndex: 0,
56-
destinationArray: buffer,
57-
destinationIndex: 0,
58-
length: readBytesEncoded.Length );
59-
60-
int copyStart = readBytesEncoded.Length;
60+
ReadOnlySpan<byte> headerSpan = readBytesEncoded.AsSpan ();
61+
headerSpan.CopyTo ( destination );
6162

62-
readBuffer [ 0..read ].CopyTo ( buffer.AsSpan () [ copyStart.. ] );
63+
int copyStart = headerSpan.Length;
64+
readBuffer [ 0..read ].CopyTo ( destination [ copyStart.. ] );
6365

64-
int bufferEnd = readBytesEncoded.Length + read;
65-
buffer [ bufferEnd + 0 ] = 0x0D;
66-
buffer [ bufferEnd + 1 ] = 0x0A;
66+
int bufferEnd = headerSpan.Length + read;
67+
destination [ bufferEnd + 0 ] = 0x0D;
68+
destination [ bufferEnd + 1 ] = 0x0A;
6769

6870
written += read;
6971

extensions/Sisk.SslProxy/CertificateUtil.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public static X509Certificate2 CreateTrustedDevelopmentCertificate ( params stri
7373
/// </summary>
7474
/// <param name="dnsNames">The certificate DNS names.</param>
7575
public static X509Certificate2 CreateDevelopmentCertificate ( params string [] dnsNames ) {
76+
if (dnsNames.Length == 0)
77+
throw new ArgumentException ( "At least one DNS name must be specified.", nameof ( dnsNames ) );
78+
7679
SubjectAlternativeNameBuilder sanBuilder = new SubjectAlternativeNameBuilder ();
7780
sanBuilder.AddIpAddress ( IPAddress.Loopback );
7881
sanBuilder.AddIpAddress ( IPAddress.IPv6Loopback );

0 commit comments

Comments
 (0)