Skip to content

Commit

Permalink
feat: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuunny committed Mar 8, 2024
1 parent e175f5b commit 97d4949
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 95 deletions.
20 changes: 14 additions & 6 deletions Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,16 @@ public async Task<string> ToFile(string localPath, string filename = null)

protected virtual void Dispose(bool disposing)
{
if (!disposing || IsDisposed)
return;

IsDisposed = true;
if (!IsDisposed)
{
if (disposing)
{
Stream?.Close();
Stream?.Dispose();
}

Stream?.Close();
Stream?.Dispose();
IsDisposed = true;
}
}

public void Dispose()
Expand All @@ -201,4 +204,9 @@ public void Dispose()

GC.SuppressFinalize(this);
}

~Content()
{
Dispose(false);
}
}
36 changes: 22 additions & 14 deletions HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private bool _canReconnect
public HttpClient()
{
if (EnableCookies && Cookies == null)
Cookies = new NameValueCollection();
Cookies = [];
}

public HttpClient(CancellationToken token) : this()
Expand All @@ -161,7 +161,7 @@ public HttpClient(string baseUrl)
BaseUrl = baseUrl;

if (EnableCookies && Cookies == null)
Cookies = new NameValueCollection();
Cookies = [];
}

public HttpClient(string baseUrl, CancellationToken token) : this(baseUrl)
Expand Down Expand Up @@ -661,23 +661,31 @@ public void Close()

protected virtual void Dispose(bool disposing)
{
if (!disposing || IsDisposed)
return;
if (!IsDisposed)
{
if (disposing)
{
Content?.Dispose();
_response?.Content?.Dispose();

IsDisposed = true;
if (_cancellationTokenRegistration != default)
_cancellationTokenRegistration.Dispose();
}

Close();
Close();

Content?.Dispose();
_response = null;
_proxyClient = null;

_response?.Content?.Dispose();
Cookies = null;
Headers = null;
TempHeaders = null;
RedirectHistory = null;

Connection = null;
NetworkStream = null;
CommonStream = null;
Content = null;

if (_cancellationTokenRegistration != default)
_cancellationTokenRegistration.Dispose();
IsDisposed = true;
}
}

public void Dispose()
Expand All @@ -689,6 +697,6 @@ public void Dispose()

~HttpClient()
{
Dispose();
Dispose(false);
}
}
78 changes: 39 additions & 39 deletions HttpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,28 @@ private static string UserAgent(HttpSystem system, HttpBrowser browser)

private static string GenerateBrowserVersion(HttpBrowser browser)
{
string[] chrome = new[]
{
"70.0.3538", "71.0", "72.0", "69.0.3497.32", "70.0.3538", "71.0.3578", "72.0.3626", "73.0.3683",
"74.0.3729", "75.0.3770", "76.0.3809", "77.0.3865", "78.0.3904", "79.0.3945", "80.0.3987", "81.0.4044",
"83.0.4103", "84.0.4147", "85.0.4183", "86.0.4240", "87.0.4280", "88.0"
};

string[] opera = new[]
{
"57.0.3098.14", "56.0.3051.52", "58.0.3111.0", "56.0.3051.43", "55.0.2994.34",
"57.0.3098.1", "54.0.2952.64", "53.0.2907.68", "54.0.2952.60", "53.0.2907.99"
};

string[] safari = new[]
{
"14.0", "13.1.2", "12.1.2"
};

string[] edge = new[]
{
"17.17134", "16.16299", "15.15063", "14.14393", "13.10586", "12.10240"
};
string[] chrome =
[
"70.0.3538", "71.0", "72.0", "69.0.3497.32", "70.0.3538", "71.0.3578", "72.0.3626", "73.0.3683",
"74.0.3729", "75.0.3770", "76.0.3809", "77.0.3865", "78.0.3904", "79.0.3945", "80.0.3987", "81.0.4044",
"83.0.4103", "84.0.4147", "85.0.4183", "86.0.4240", "87.0.4280", "88.0"
];

string[] opera =
[
"57.0.3098.14", "56.0.3051.52", "58.0.3111.0", "56.0.3051.43", "55.0.2994.34",
"57.0.3098.1", "54.0.2952.64", "53.0.2907.68", "54.0.2952.60", "53.0.2907.99"
];

string[] safari =
[
"14.0", "13.1.2", "12.1.2"
];

string[] edge =
[
"17.17134", "16.16299", "15.15063", "14.14393", "13.10586", "12.10240"
];

return browser switch
{
Expand All @@ -144,23 +144,23 @@ private static string GenerateBrowserVersion(HttpBrowser browser)

private static string GenerateVersionOS(HttpSystem system)
{
string[] windows = new[] { "10.0", "6.2", "6.1", "6.3" };
string[] linux = new[] { "i686", "x86_64" };

string[] mac = new[]
{
"10_9", "10_9_1", "10_9_2", "10_9_3", "10_9_4", "10_9_5", "10_10", "10_10_1",
"10_10_2", "10_10_3", "10_10_4", "10_10_5", "10_11", "10_11_1", "10_11_2",
"10_11_3", "10_11_4", "10_11_5", "10.11.6", "10_12", "10_12_1", "10_12_2",
"10_12_3","10_12_4", "10_12_5", "10_12_6", "10_13", "10_13_1", "10_13_2",
"10_13_3", "10_13_4", "10_13_5", "10_13_6", "10_14_0", "10.15", "11.0"
};

string[] chromeOS = new[]
{
"10575.58.0", "10718.71.2", "10718.88.2", "10895.78.0", "10895.78.0", "11021.45.0", "11151.4.0",
"11167.0.0", "10895.56.0", "11021.34.0", "11166.0.0"
};
string[] windows = ["10.0", "6.2", "6.1", "6.3"];
string[] linux = ["i686", "x86_64"];

string[] mac =
[
"10_9", "10_9_1", "10_9_2", "10_9_3", "10_9_4", "10_9_5", "10_10", "10_10_1",
"10_10_2", "10_10_3", "10_10_4", "10_10_5", "10_11", "10_11_1", "10_11_2",
"10_11_3", "10_11_4", "10_11_5", "10.11.6", "10_12", "10_12_1", "10_12_2",
"10_12_3","10_12_4", "10_12_5", "10_12_6", "10_13", "10_13_1", "10_13_2",
"10_13_3", "10_13_4", "10_13_5", "10_13_6", "10_14_0", "10.15", "11.0"
];

string[] chromeOS =
[
"10575.58.0", "10718.71.2", "10718.88.2", "10895.78.0", "10895.78.0", "11021.45.0", "11151.4.0",
"11167.0.0", "10895.56.0", "11021.34.0", "11166.0.0"
];

return system switch
{
Expand Down
7 changes: 1 addition & 6 deletions Proxy/ProxyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ private protected static byte GetAddressType(string host)

private protected static byte[] GetPortBytes(int port)
{
byte[] bytes = new byte[2];

bytes[0] = (byte)(port / 256);
bytes[1] = (byte)(port % 256);

return bytes;
return [(byte)(port / 256), (byte)(port % 256)];
}
}
3 changes: 1 addition & 2 deletions Proxy/Socks4Proxy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
Expand All @@ -23,7 +22,7 @@ private protected override async Task<ConnectionResult> SendCommand(NetworkStrea

byte[] address = GetIPAddressBytes(destinationHost);
byte[] port = GetPortBytes(destinationPort);
byte[] userId = string.IsNullOrEmpty(UserId) ? Array.Empty<byte>() : Encoding.ASCII.GetBytes(UserId);
byte[] userId = string.IsNullOrEmpty(UserId) ? [] : Encoding.ASCII.GetBytes(UserId);

byte[] request = new byte[9 + userId.Length];
byte[] response = new byte[8];
Expand Down
12 changes: 3 additions & 9 deletions Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Yove.Http;

internal class Receiver
internal class Receiver(int size, Stream stream)
{
private byte[] _buffer { get; }
private byte[] _buffer { get; } = new byte[size];
private byte[] _temporaryBuffer = new byte[1024];
private Stream _stream { get; }
private Stream _stream { get; } = stream;
private int _length { get; set; }
public int Position { get; set; }

Expand All @@ -17,12 +17,6 @@ public bool HasData
get { return (_length - Position) != 0; }
}

public Receiver(int size, Stream stream)
{
_buffer = new byte[size];
_stream = stream;
}

public string Get(bool readLine)
{
int currentPosition = 0;
Expand Down
24 changes: 6 additions & 18 deletions StreamWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace Yove.Http;

internal class StreamWrapper : Stream
internal class StreamWrapper(Stream stream, Receiver content) : Stream
{
public Stream Stream { get; set; }
public Receiver Content { get; set; }
public Stream Stream { get; set; } = stream;
public Receiver Content { get; set; } = content;

public int BytesRead { get; private set; }
public int TotalBytesRead { get; set; }
Expand Down Expand Up @@ -64,12 +64,6 @@ public override long Position
}
}

public StreamWrapper(Stream stream, Receiver content)
{
Stream = stream;
Content = content;
}

public override void Flush()
{
Stream.Flush();
Expand Down Expand Up @@ -121,10 +115,10 @@ public override void Write(byte[] buffer, int offset, int count)
}
}

internal class EventStreamWrapper : Stream
internal class EventStreamWrapper(Stream stream, int bufferSize) : Stream
{
private Stream _stream { get; }
private int _bufferSize { get; }
private Stream _stream { get; } = stream;
private int _bufferSize { get; } = bufferSize;

public Action<int> ReadBytesCallback { get; set; }
public Action<int> WriteBytesCallback { get; set; }
Expand Down Expand Up @@ -181,12 +175,6 @@ public override long Position
}
}

public EventStreamWrapper(Stream stream, int bufferSize)
{
_stream = stream;
_bufferSize = bufferSize;
}

public override void Flush()
{
_stream.Flush();
Expand Down
2 changes: 1 addition & 1 deletion Yove.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageId>Yove.Http</PackageId>
<VersionPrefix>1.7.6</VersionPrefix>
<VersionPrefix>1.7.7</VersionPrefix>
<VersionSuffix>beta</VersionSuffix>
<Authors>Sunny</Authors>
<NeutralLanguage>en-US, ru-RU</NeutralLanguage>
Expand Down

0 comments on commit 97d4949

Please sign in to comment.