Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit d8dce11

Browse files
committed
Nullable fixes
1 parent 8a0244b commit d8dce11

26 files changed

+80
-89
lines changed

src/Titanium.Web.Proxy/EventArguments/TunnelConnectEventArgs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public bool IsHttpsConnect
4545
/// <summary>
4646
/// Fired when decrypted data is sent within this session to server/client.
4747
/// </summary>
48-
public event EventHandler<DataEventArgs> DecryptedDataSent;
48+
public event EventHandler<DataEventArgs>? DecryptedDataSent;
4949

5050
/// <summary>
5151
/// Fired when decrypted data is received within this session from client/server.
5252
/// </summary>
53-
public event EventHandler<DataEventArgs> DecryptedDataReceived;
53+
public event EventHandler<DataEventArgs>? DecryptedDataReceived;
5454

5555
internal void OnDecryptedDataSent(byte[] buffer, int offset, int count)
5656
{

src/Titanium.Web.Proxy/ExplicitClientHandler.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
309309
connectArgs.HttpClient.ConnectRequest!.TunnelType = TunnelType.Http2;
310310

311311
// HTTP/2 Connection Preface
312-
string line = await clientStream.ReadLineAsync(cancellationToken);
312+
string? line = await clientStream.ReadLineAsync(cancellationToken);
313313
if (line != string.Empty)
314314
{
315315
throw new Exception($"HTTP/2 Protocol violation. Empty string expected, '{line}' received");
@@ -332,11 +332,9 @@ await TcpHelper.SendRaw(clientStream, connection.Stream, BufferPool,
332332
noCache: true, cancellationToken: cancellationToken);
333333
try
334334
{
335-
await connection.StreamWriter.WriteLineAsync("PRI * HTTP/2.0", cancellationToken);
336-
await connection.StreamWriter.WriteLineAsync(cancellationToken);
337-
await connection.StreamWriter.WriteLineAsync("SM", cancellationToken);
338-
await connection.StreamWriter.WriteLineAsync(cancellationToken);
339335
#if NETSTANDARD2_1
336+
var connectionPreface = new ReadOnlyMemory<byte>(Http2Helper.ConnectionPreface);
337+
await connection.StreamWriter.WriteAsync(connectionPreface, cancellationToken);
340338
await Http2Helper.SendHttp2(clientStream, connection.Stream,
341339
() => new SessionEventArgs(this, endPoint, cancellationTokenSource)
342340
{

src/Titanium.Web.Proxy/Extensions/SslExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ internal static Task AuthenticateAsServerAsync(this SslStream sslStream, SslServ
7777
}
7878
#endif
7979
}
80+
}
8081

8182
#if !NETSTANDARD2_1
83+
namespace System.Net.Security
84+
{
8285
internal enum SslApplicationProtocol
8386
{
8487
Http11,
8588
Http2
8689
}
8790

88-
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification = "Reviewed.")]
91+
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification =
92+
"Reviewed.")]
8993
internal class SslClientAuthenticationOptions
9094
{
9195
internal bool AllowRenegotiation { get; set; }
@@ -125,5 +129,5 @@ internal class SslServerAuthenticationOptions
125129

126130
internal EncryptionPolicy EncryptionPolicy { get; set; }
127131
}
128-
#endif
129132
}
133+
#endif

src/Titanium.Web.Proxy/Extensions/StreamExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static Task CopyToAsync(this Stream input, Stream output, Action<byte[]
3232
/// <param name="onCopy"></param>
3333
/// <param name="bufferPool"></param>
3434
/// <param name="cancellationToken"></param>
35-
internal static async Task CopyToAsync(this Stream input, Stream output, Action<byte[], int, int> onCopy,
35+
internal static async Task CopyToAsync(this Stream input, Stream output, Action<byte[], int, int>? onCopy,
3636
IBufferPool bufferPool, CancellationToken cancellationToken)
3737
{
3838
var buffer = bufferPool.GetBuffer();

src/Titanium.Web.Proxy/Helpers/HttpHelper.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public bool MoveNext()
6666
/// </summary>
6767
/// <param name="contentType"></param>
6868
/// <returns></returns>
69-
internal static Encoding GetEncodingFromContentType(string contentType)
69+
internal static Encoding GetEncodingFromContentType(string? contentType)
7070
{
7171
try
7272
{
@@ -108,7 +108,7 @@ internal static Encoding GetEncodingFromContentType(string contentType)
108108
return defaultEncoding;
109109
}
110110

111-
internal static ReadOnlyMemory<char> GetBoundaryFromContentType(string contentType)
111+
internal static ReadOnlyMemory<char> GetBoundaryFromContentType(string? contentType)
112112
{
113113
if (contentType != null)
114114
{
@@ -196,16 +196,14 @@ internal static Task<int> IsPriMethod(ICustomStreamReader clientStreamReader, IB
196196
private static async Task<int> startsWith(ICustomStreamReader clientStreamReader, IBufferPool bufferPool, string expectedStart, CancellationToken cancellationToken = default)
197197
{
198198
const int lengthToCheck = 10;
199-
byte[]? buffer = null;
200-
try
199+
if (bufferPool.BufferSize < lengthToCheck)
201200
{
202-
if (bufferPool.BufferSize < lengthToCheck)
203-
{
204-
throw new Exception($"Buffer is too small. Minimum size is {lengthToCheck} bytes");
205-
}
206-
207-
buffer = bufferPool.GetBuffer(bufferPool.BufferSize);
201+
throw new Exception($"Buffer is too small. Minimum size is {lengthToCheck} bytes");
202+
}
208203

204+
byte[] buffer = bufferPool.GetBuffer(bufferPool.BufferSize);
205+
try
206+
{
209207
bool isExpected = true;
210208
int i = 0;
211209
while (i < lengthToCheck)

src/Titanium.Web.Proxy/Helpers/HttpWriter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ private async Task writeBodyChunkedAsync(byte[] data, CancellationToken cancella
192192
/// <param name="onCopy"></param>
193193
/// <param name="cancellationToken"></param>
194194
/// <returns></returns>
195-
private async Task copyBodyChunkedAsync(ICustomStreamReader reader, Action<byte[], int, int> onCopy,
196-
CancellationToken cancellationToken)
195+
private async Task copyBodyChunkedAsync(ICustomStreamReader reader, Action<byte[], int, int>? onCopy, CancellationToken cancellationToken)
197196
{
198197
while (true)
199198
{
@@ -233,7 +232,7 @@ private async Task copyBodyChunkedAsync(ICustomStreamReader reader, Action<byte[
233232
/// <param name="onCopy"></param>
234233
/// <param name="cancellationToken"></param>
235234
/// <returns></returns>
236-
private async Task copyBytesFromStream(ICustomStreamReader reader, long count, Action<byte[], int, int> onCopy,
235+
private async Task copyBytesFromStream(ICustomStreamReader reader, long count, Action<byte[], int, int>? onCopy,
237236
CancellationToken cancellationToken)
238237
{
239238
var buffer = bufferPool.GetBuffer();

src/Titanium.Web.Proxy/Helpers/ProxyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private static string convertRegexReservedChars(string rawString)
158158
/// </summary>
159159
/// <param name="proxyServerValues"></param>
160160
/// <returns></returns>
161-
internal static List<HttpSystemProxyValue> GetSystemProxyValues(string proxyServerValues)
161+
internal static List<HttpSystemProxyValue> GetSystemProxyValues(string? proxyServerValues)
162162
{
163163
var result = new List<HttpSystemProxyValue>();
164164

@@ -167,7 +167,7 @@ internal static List<HttpSystemProxyValue> GetSystemProxyValues(string proxyServ
167167
return result;
168168
}
169169

170-
var proxyValues = proxyServerValues.Split(';');
170+
var proxyValues = proxyServerValues!.Split(';');
171171

172172
if (proxyValues.Length > 0)
173173
{

src/Titanium.Web.Proxy/Http/HeaderParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ internal static class HeaderParser
1010
internal static async Task ReadHeaders(ICustomStreamReader reader, HeaderCollection headerCollection,
1111
CancellationToken cancellationToken)
1212
{
13-
string tmpLine;
13+
string? tmpLine;
1414
while (!string.IsNullOrEmpty(tmpLine = await reader.ReadLineAsync(cancellationToken)))
1515
{
16-
int colonIndex = tmpLine.IndexOf(':');
16+
int colonIndex = tmpLine!.IndexOf(':');
1717
if (colonIndex == -1)
1818
{
1919
throw new Exception("Header line should contain a colon character.");

src/Titanium.Web.Proxy/Http/HttpWebClient.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ internal TcpServerConnection Connection
6060
/// <summary>
6161
/// Override UpStreamEndPoint for this request; Local NIC via request is made
6262
/// </summary>
63-
public IPEndPoint UpStreamEndPoint { get; set; }
63+
public IPEndPoint? UpStreamEndPoint { get; set; }
6464

6565
/// <summary>
6666
/// Headers passed with Connect.
@@ -182,11 +182,8 @@ internal async Task ReceiveResponse(CancellationToken cancellationToken)
182182
string httpStatus;
183183
try
184184
{
185-
httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken);
186-
if (httpStatus == null)
187-
{
188-
throw new ServerConnectionException("Server connection was closed.");
189-
}
185+
httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken) ??
186+
throw new ServerConnectionException("Server connection was closed.");
190187
}
191188
catch (Exception e) when (!(e is ServerConnectionException))
192189
{
@@ -195,7 +192,8 @@ internal async Task ReceiveResponse(CancellationToken cancellationToken)
195192

196193
if (httpStatus == string.Empty)
197194
{
198-
httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken);
195+
httpStatus = await Connection.Stream.ReadLineAsync(cancellationToken) ??
196+
throw new ServerConnectionException("Server connection was closed.");
199197
}
200198

201199
Response.ParseResponseLine(httpStatus, out var version, out int statusCode, out string statusDescription);

src/Titanium.Web.Proxy/Http2/Http2Helper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
using System.Diagnostics;
66
using System.IO;
77
using System.Net;
8+
using System.Text;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Titanium.Web.Proxy.Compression;
1112
using Titanium.Web.Proxy.EventArguments;
1213
using Titanium.Web.Proxy.Exceptions;
1314
using Titanium.Web.Proxy.Http;
1415
using Titanium.Web.Proxy.Http2.Hpack;
16+
using Decoder = Titanium.Web.Proxy.Http2.Hpack.Decoder;
17+
using Encoder = Titanium.Web.Proxy.Http2.Hpack.Encoder;
1518

1619
namespace Titanium.Web.Proxy.Http2
1720
{
1821
internal class Http2Helper
1922
{
23+
public static readonly byte[] ConnectionPreface = Encoding.ASCII.GetBytes("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n");
24+
2025
/// <summary>
2126
/// relays the input clientStream to the server at the specified host name and port with the given httpCmd and headers
2227
/// as prefix

0 commit comments

Comments
 (0)