Code cleanup for network module.

This commit is contained in:
2023-07-11 00:02:14 +08:00
parent c9b348dd4f
commit d89423caf5
4 changed files with 38 additions and 7 deletions

View File

@@ -8,7 +8,7 @@ using System.Net.Sockets;
using System.Text;
namespace Cryville.Common.Network.Http11 {
public class Http11Client {
public class Http11Client : IDisposable {
private readonly string _directHost;
protected string DirectHost { get { return _directHost; } }
@@ -48,13 +48,23 @@ namespace Cryville.Common.Network.Http11 {
public virtual void Close() {
TcpClient.Close();
}
public void Dispose() {
Dispose(true);
}
public virtual void Dispose(bool disposing) {
if (disposing) {
Close();
}
}
public Http11Response Request(string method, Uri uri, string body = null, Encoding encoding = null) {
string struri = GetUri(uri).PathAndQuery;
return Request(Stream, method, struri, body, encoding);
}
public Http11Response Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) {
Http11Response Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) {
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (var h in Headers)
headers.Add(h.Key, h.Value);

View File

@@ -4,7 +4,7 @@ using System.IO;
using System.Text;
namespace Cryville.Common.Network.Http11 {
public class Http11Response {
public class Http11Response : IDisposable {
static readonly char[] spchar = new char[]{ ' ' };
public string HttpVersion { get; private set; }
public string StatusCode { get; private set; }
@@ -28,6 +28,16 @@ namespace Cryville.Common.Network.Http11 {
}
}
public void Dispose() {
Dispose(true);
}
public virtual void Dispose(bool disposing) {
if (disposing) {
MessageBody.Dispose();
}
}
public override string ToString() {
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
}

View File

@@ -72,8 +72,8 @@ namespace Cryville.Common.Network.Http11 {
public void ReadChunk() {
if (_chunk != null && _chunk.Length == 0) return;
string[] chunkHeader = Http11Response.ReadLine(_reader).Split(';');
int chunkSize = int.Parse(chunkHeader[0], NumberStyles.HexNumber);
if (chunkSize == -1)
int chunkSize;
if (!int.TryParse(chunkHeader[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out chunkSize))
throw new IOException("Corrupted chunk received");
if (chunkSize == 0) {
_chunk = new byte[0];

View File

@@ -2,6 +2,7 @@ using Org.BouncyCastle.Security;
using Org.BouncyCastle.Tls;
using Org.BouncyCastle.Tls.Crypto;
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
using System;
using System.Collections;
using System.IO;
using System.Linq;
@@ -9,7 +10,7 @@ using System.Text;
using BcTlsClient = Org.BouncyCastle.Tls.TlsClient;
namespace Cryville.Common.Network {
public class TlsClient {
public class TlsClient : IDisposable {
readonly TlsClientProtocol _protocol;
readonly BcTlsClient _tlsClient;
public Stream Stream { get; private set; }
@@ -27,6 +28,16 @@ namespace Cryville.Common.Network {
_protocol.Close();
}
public void Dispose() {
Dispose(true);
}
public virtual void Dispose(bool disposing) {
if (disposing) {
Close();
}
}
private class InternalTlsClient : DefaultTlsClient {
readonly string _host;