Fixed connection issues.

This commit is contained in:
Pachira 2022-04-07 20:59:52 +02:00
parent 01a5dbccd6
commit 5f479a17e0
5 changed files with 23 additions and 8 deletions

View file

@ -4,7 +4,6 @@ using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Threading;
namespace Hamakaze {
public class HttpConnection : IDisposable {
@ -18,9 +17,9 @@ namespace Hamakaze {
public string Host { get; }
public bool IsSecure { get; }
public bool HasTimedOut => MaxRequests == 0 || (DateTimeOffset.Now - LastOperation) > MaxIdle;
public bool HasTimedOut => MaxRequests < 1 || (DateTimeOffset.Now - LastOperation) > MaxIdle;
public int MaxRequests { get; set; } = -1;
public int? MaxRequests { get; set; } = null;
public TimeSpan MaxIdle { get; set; } = TimeSpan.MaxValue;
public DateTimeOffset LastOperation { get; private set; } = DateTimeOffset.Now;
@ -45,14 +44,19 @@ namespace Hamakaze {
if(IsSecure) {
SslStream = new SslStream(NetworkStream, false, (s, ce, ch, e) => e == SslPolicyErrors.None, null);
Stream = SslStream;
SslStream.AuthenticateAsClient(Host, null, SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13, true);
SslStream.AuthenticateAsClient(
Host,
null,
SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13,
true
);
} else
Stream = NetworkStream;
}
public void MarkUsed() {
LastOperation = DateTimeOffset.Now;
if(MaxRequests > 0)
if(MaxRequests != null)
--MaxRequests;
}

View file

@ -12,6 +12,13 @@ namespace Hamakaze {
public HttpConnectionManagerLockException() : base(@"Failed to lock the connection manager in time.") { }
}
public class HttpRequestMessageException : HttpException {
public HttpRequestMessageException(string message) : base(message) { }
}
public class HttpRequestMessageStreamException : HttpRequestMessageException {
public HttpRequestMessageStreamException() : base(@"Provided Stream is not writable.") { }
}
public class HttpTaskException : HttpException {
public HttpTaskException(string message) : base(message) { }
}

View file

@ -157,6 +157,9 @@ namespace Hamakaze {
}
public void WriteTo(Stream stream, Action<long, long> onProgress = null) {
if(!stream.CanWrite)
throw new HttpRequestMessageStreamException();
using(StreamWriter sw = new(stream, new ASCIIEncoding(), leaveOpen: true)) {
sw.NewLine = "\r\n";
sw.Write(Method);

View file

@ -127,7 +127,7 @@ namespace Hamakaze {
for(; ; ) {
byt = stream.ReadByte();
if(byt == -1 && ms.Length == 0)
return null;
throw new IOException(@"readLine: There is no data.");
ms.WriteByte((byte)byt);

View file

@ -131,7 +131,7 @@ namespace Hamakaze {
try {
Request.WriteTo(Connection.Stream, (p, t) => OnUploadProgress?.Invoke(this, p, t));
break;
} catch(IOException ex) {
} catch(HttpRequestMessageStreamException ex) {
Connection.Dispose();
Connection = Connections.GetConnection(Request.Host, endPoint, Request.IsSecure);
@ -162,7 +162,8 @@ namespace Hamakaze {
return;
}
if(Response.Connection == HttpConnectionHeader.CLOSE)
if(Response.Connection == HttpConnectionHeader.CLOSE
|| Response.ProtocolVersion.CompareTo(@"1.1") < 0)
Connection.Dispose();
if(Response == null)
Error(new HttpTaskRequestFailedException());