Commit b25d572a authored by Honfika's avatar Honfika

Small fixes

parent dad04602
...@@ -49,12 +49,12 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -49,12 +49,12 @@ namespace Titanium.Web.Proxy.EventArguments
if (readChunkTrail) if (readChunkTrail)
{ {
// read the chunk trail of the previous chunk // read the chunk trail of the previous chunk
string s = baseStream.ReadLineAsync().Result; string? s = baseStream.ReadLineAsync().Result;
} }
readChunkTrail = true; readChunkTrail = true;
string chunkHead = baseStream.ReadLineAsync().Result; string? chunkHead = baseStream.ReadLineAsync().Result;
int idx = chunkHead.IndexOf(";", StringComparison.Ordinal); int idx = chunkHead.IndexOf(";", StringComparison.Ordinal);
if (idx >= 0) if (idx >= 0)
{ {
...@@ -73,7 +73,9 @@ namespace Titanium.Web.Proxy.EventArguments ...@@ -73,7 +73,9 @@ namespace Titanium.Web.Proxy.EventArguments
bytesRemaining = -1; bytesRemaining = -1;
// chunk trail // chunk trail
baseStream.ReadLineAsync().Wait(); var task = baseStream.ReadLineAsync();
if (!task.IsCompleted)
task.AsTask().Wait();
} }
} }
......
...@@ -53,13 +53,13 @@ namespace Titanium.Web.Proxy ...@@ -53,13 +53,13 @@ namespace Titanium.Web.Proxy
if (await HttpHelper.IsConnectMethod(clientStream, BufferPool, cancellationToken) == 1) if (await HttpHelper.IsConnectMethod(clientStream, BufferPool, cancellationToken) == 1)
{ {
// read the first line HTTP command // read the first line HTTP command
string httpCmd = await clientStream.ReadLineAsync(cancellationToken); string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
{ {
return; return;
} }
Request.ParseRequestLine(httpCmd, out string _, out string httpUrl, out var version); Request.ParseRequestLine(httpCmd!, out string _, out string httpUrl, out var version);
var httpRemoteUri = new Uri("http://" + httpUrl); var httpRemoteUri = new Uri("http://" + httpUrl);
connectHostname = httpRemoteUri.Host; connectHostname = httpRemoteUri.Host;
...@@ -303,7 +303,7 @@ namespace Titanium.Web.Proxy ...@@ -303,7 +303,7 @@ namespace Titanium.Web.Proxy
if (connectArgs != null && await HttpHelper.IsPriMethod(clientStream, BufferPool, cancellationToken) == 1) if (connectArgs != null && await HttpHelper.IsPriMethod(clientStream, BufferPool, cancellationToken) == 1)
{ {
// todo // todo
string httpCmd = await clientStream.ReadLineAsync(cancellationToken); string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (httpCmd == "PRI * HTTP/2.0") if (httpCmd == "PRI * HTTP/2.0")
{ {
connectArgs.HttpClient.ConnectRequest!.TunnelType = TunnelType.Http2; connectArgs.HttpClient.ConnectRequest!.TunnelType = TunnelType.Http2;
......
...@@ -197,7 +197,7 @@ namespace Titanium.Web.Proxy.Helpers ...@@ -197,7 +197,7 @@ namespace Titanium.Web.Proxy.Helpers
{ {
while (true) while (true)
{ {
string chunkHead = await reader.ReadLineAsync(cancellationToken); string? chunkHead = await reader.ReadLineAsync(cancellationToken);
int idx = chunkHead.IndexOf(";"); int idx = chunkHead.IndexOf(";");
if (idx >= 0) if (idx >= 0)
{ {
......
...@@ -121,8 +121,7 @@ namespace Titanium.Web.Proxy.Http ...@@ -121,8 +121,7 @@ namespace Titanium.Web.Proxy.Http
} }
} }
internal static void ParseResponseLine(string httpStatus, out Version version, out int statusCode, internal static void ParseResponseLine(string httpStatus, out Version version, out int statusCode, out string statusDescription)
out string statusDescription)
{ {
int firstSpace = httpStatus.IndexOf(' '); int firstSpace = httpStatus.IndexOf(' ');
if (firstSpace == -1) if (firstSpace == -1)
......
...@@ -11,6 +11,7 @@ using System.Text; ...@@ -11,6 +11,7 @@ using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Titanium.Web.Proxy.EventArguments; using Titanium.Web.Proxy.EventArguments;
using Titanium.Web.Proxy.Exceptions;
using Titanium.Web.Proxy.Extensions; using Titanium.Web.Proxy.Extensions;
using Titanium.Web.Proxy.Helpers; using Titanium.Web.Proxy.Helpers;
using Titanium.Web.Proxy.Http; using Titanium.Web.Proxy.Http;
...@@ -364,7 +365,8 @@ namespace Titanium.Web.Proxy.Network.Tcp ...@@ -364,7 +365,8 @@ namespace Titanium.Web.Proxy.Network.Tcp
await writer.WriteRequestAsync(connectRequest, cancellationToken: cancellationToken); await writer.WriteRequestAsync(connectRequest, cancellationToken: cancellationToken);
string httpStatus = await stream.ReadLineAsync(cancellationToken); string httpStatus = await stream.ReadLineAsync(cancellationToken)
?? throw new ServerConnectionException("Server connection was closed.");
Response.ParseResponseLine(httpStatus, out _, out int statusCode, out string statusDescription); Response.ParseResponseLine(httpStatus, out _, out int statusCode, out string statusDescription);
......
...@@ -64,8 +64,7 @@ namespace Titanium.Web.Proxy ...@@ -64,8 +64,7 @@ namespace Titanium.Web.Proxy
} }
// read the request line // read the request line
string httpCmd = await clientStream.ReadLineAsync(cancellationToken); string? httpCmd = await clientStream.ReadLineAsync(cancellationToken);
if (string.IsNullOrEmpty(httpCmd)) if (string.IsNullOrEmpty(httpCmd))
{ {
return; return;
...@@ -82,7 +81,7 @@ namespace Titanium.Web.Proxy ...@@ -82,7 +81,7 @@ namespace Titanium.Web.Proxy
{ {
try try
{ {
Request.ParseRequestLine(httpCmd, out string httpMethod, out string httpUrl, out var version); Request.ParseRequestLine(httpCmd!, out string httpMethod, out string httpUrl, out var version);
// Read the request headers in to unique and non-unique header collections // Read the request headers in to unique and non-unique header collections
await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers, await HeaderParser.ReadHeaders(clientStream, args.HttpClient.Request.Headers,
......
...@@ -124,7 +124,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -124,7 +124,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
return result; return result;
} }
public Task<string?> ReadLineAsync(CancellationToken cancellationToken = default) public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default)
{ {
return CustomBufferedStream.ReadLineInternalAsync(this, bufferPool, cancellationToken); return CustomBufferedStream.ReadLineInternalAsync(this, bufferPool, cancellationToken);
} }
......
...@@ -141,7 +141,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -141,7 +141,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// </summary> /// </summary>
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
Task<string?> ICustomStreamReader.ReadLineAsync(CancellationToken cancellationToken) ValueTask<string?> ICustomStreamReader.ReadLineAsync(CancellationToken cancellationToken)
{ {
return CustomBufferedStream.ReadLineInternalAsync(this, bufferPool, cancellationToken); return CustomBufferedStream.ReadLineInternalAsync(this, bufferPool, cancellationToken);
} }
......
...@@ -157,7 +157,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -157,7 +157,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns> /// <returns>
/// A task that represents the asynchronous copy operation. /// A task that represents the asynchronous copy operation.
/// </returns> /// </returns>
public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken = default) public override async Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
{ {
if (bufferLength > 0) if (bufferLength > 0)
{ {
...@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -176,7 +176,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// <returns> /// <returns>
/// A task that represents the asynchronous flush operation. /// A task that represents the asynchronous flush operation.
/// </returns> /// </returns>
public override Task FlushAsync(CancellationToken cancellationToken = default) public override Task FlushAsync(CancellationToken cancellationToken)
{ {
return BaseStream.FlushAsync(cancellationToken); return BaseStream.FlushAsync(cancellationToken);
} }
...@@ -201,7 +201,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -201,7 +201,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// less than the requested number, or it can be 0 (zero) /// less than the requested number, or it can be 0 (zero)
/// if the end of the stream has been reached. /// if the end of the stream has been reached.
/// </returns> /// </returns>
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default) public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{ {
if (bufferLength == 0) if (bufferLength == 0)
{ {
...@@ -346,7 +346,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -346,7 +346,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// A task that represents the asynchronous write operation. /// A task that represents the asynchronous write operation.
/// </returns> /// </returns>
[DebuggerStepThrough] [DebuggerStepThrough]
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken = default) public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{ {
OnDataWrite(buffer, offset, count); OnDataWrite(buffer, offset, count);
...@@ -558,7 +558,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -558,7 +558,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// Read a line from the byte stream /// Read a line from the byte stream
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Task<string?> ReadLineAsync(CancellationToken cancellationToken = default) public ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default)
{ {
return ReadLineInternalAsync(this, bufferPool, cancellationToken); return ReadLineInternalAsync(this, bufferPool, cancellationToken);
} }
...@@ -567,7 +567,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -567,7 +567,7 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// Read a line from the byte stream /// Read a line from the byte stream
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
internal static async Task<string?> ReadLineInternalAsync(ICustomStreamReader reader, IBufferPool bufferPool, CancellationToken cancellationToken = default) internal static async ValueTask<string?> ReadLineInternalAsync(ICustomStreamReader reader, IBufferPool bufferPool, CancellationToken cancellationToken = default)
{ {
byte lastChar = default; byte lastChar = default;
......
...@@ -74,6 +74,6 @@ namespace Titanium.Web.Proxy.StreamExtended.Network ...@@ -74,6 +74,6 @@ namespace Titanium.Web.Proxy.StreamExtended.Network
/// Read a line from the byte stream /// Read a line from the byte stream
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
Task<string?> ReadLineAsync(CancellationToken cancellationToken = default); ValueTask<string?> ReadLineAsync(CancellationToken cancellationToken = default);
} }
} }
...@@ -31,11 +31,8 @@ namespace Titanium.Web.Proxy ...@@ -31,11 +31,8 @@ namespace Titanium.Web.Proxy
string httpStatus; string httpStatus;
try try
{ {
httpStatus = await serverConnection.Stream.ReadLineAsync(cancellationToken); httpStatus = await serverConnection.Stream.ReadLineAsync(cancellationToken)
if (httpStatus == null) ?? throw new ServerConnectionException("Server connection was closed.");
{
throw new ServerConnectionException("Server connection was closed.");
}
} }
catch (Exception e) when (!(e is ServerConnectionException)) catch (Exception e) when (!(e is ServerConnectionException))
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment