Code cleanup for network module.
This commit is contained in:
@@ -8,7 +8,7 @@ using System.Net.Sockets;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Cryville.Common.Network.Http11 {
|
namespace Cryville.Common.Network.Http11 {
|
||||||
public class Http11Client {
|
public class Http11Client : IDisposable {
|
||||||
private readonly string _directHost;
|
private readonly string _directHost;
|
||||||
protected string DirectHost { get { return _directHost; } }
|
protected string DirectHost { get { return _directHost; } }
|
||||||
|
|
||||||
@@ -48,13 +48,23 @@ namespace Cryville.Common.Network.Http11 {
|
|||||||
public virtual void Close() {
|
public virtual void Close() {
|
||||||
TcpClient.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) {
|
public Http11Response Request(string method, Uri uri, string body = null, Encoding encoding = null) {
|
||||||
string struri = GetUri(uri).PathAndQuery;
|
string struri = GetUri(uri).PathAndQuery;
|
||||||
return Request(Stream, method, struri, body, encoding);
|
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);
|
var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
foreach (var h in Headers)
|
foreach (var h in Headers)
|
||||||
headers.Add(h.Key, h.Value);
|
headers.Add(h.Key, h.Value);
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using System.IO;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Cryville.Common.Network.Http11 {
|
namespace Cryville.Common.Network.Http11 {
|
||||||
public class Http11Response {
|
public class Http11Response : IDisposable {
|
||||||
static readonly char[] spchar = new char[]{ ' ' };
|
static readonly char[] spchar = new char[]{ ' ' };
|
||||||
public string HttpVersion { get; private set; }
|
public string HttpVersion { get; private set; }
|
||||||
public string StatusCode { 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() {
|
public override string ToString() {
|
||||||
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
|
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,8 +72,8 @@ namespace Cryville.Common.Network.Http11 {
|
|||||||
public void ReadChunk() {
|
public void ReadChunk() {
|
||||||
if (_chunk != null && _chunk.Length == 0) return;
|
if (_chunk != null && _chunk.Length == 0) return;
|
||||||
string[] chunkHeader = Http11Response.ReadLine(_reader).Split(';');
|
string[] chunkHeader = Http11Response.ReadLine(_reader).Split(';');
|
||||||
int chunkSize = int.Parse(chunkHeader[0], NumberStyles.HexNumber);
|
int chunkSize;
|
||||||
if (chunkSize == -1)
|
if (!int.TryParse(chunkHeader[0], NumberStyles.HexNumber, CultureInfo.InvariantCulture, out chunkSize))
|
||||||
throw new IOException("Corrupted chunk received");
|
throw new IOException("Corrupted chunk received");
|
||||||
if (chunkSize == 0) {
|
if (chunkSize == 0) {
|
||||||
_chunk = new byte[0];
|
_chunk = new byte[0];
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using Org.BouncyCastle.Security;
|
|||||||
using Org.BouncyCastle.Tls;
|
using Org.BouncyCastle.Tls;
|
||||||
using Org.BouncyCastle.Tls.Crypto;
|
using Org.BouncyCastle.Tls.Crypto;
|
||||||
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
|
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -9,7 +10,7 @@ using System.Text;
|
|||||||
using BcTlsClient = Org.BouncyCastle.Tls.TlsClient;
|
using BcTlsClient = Org.BouncyCastle.Tls.TlsClient;
|
||||||
|
|
||||||
namespace Cryville.Common.Network {
|
namespace Cryville.Common.Network {
|
||||||
public class TlsClient {
|
public class TlsClient : IDisposable {
|
||||||
readonly TlsClientProtocol _protocol;
|
readonly TlsClientProtocol _protocol;
|
||||||
readonly BcTlsClient _tlsClient;
|
readonly BcTlsClient _tlsClient;
|
||||||
public Stream Stream { get; private set; }
|
public Stream Stream { get; private set; }
|
||||||
@@ -27,6 +28,16 @@ namespace Cryville.Common.Network {
|
|||||||
_protocol.Close();
|
_protocol.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Dispose() {
|
||||||
|
Dispose(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void Dispose(bool disposing) {
|
||||||
|
if (disposing) {
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class InternalTlsClient : DefaultTlsClient {
|
private class InternalTlsClient : DefaultTlsClient {
|
||||||
readonly string _host;
|
readonly string _host;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user