Skip to content

Commit

Permalink
fix: namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuunny committed Nov 26, 2022
1 parent 57ee144 commit 63bd4fe
Show file tree
Hide file tree
Showing 30 changed files with 275 additions and 233 deletions.
4 changes: 2 additions & 2 deletions Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

using Newtonsoft.Json.Linq;

using Yove.HttpClient.Exceptions;
using Yove.Http.Exceptions;

namespace Yove.HttpClient;
namespace Yove.Http;

[ConfigureAwait(false)]
public class Content : IDisposable
Expand Down
2 changes: 1 addition & 1 deletion Content/ByteContent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace Yove.HttpClient;
namespace Yove.Http;

public class ByteContent : HttpContent
{
Expand Down
2 changes: 1 addition & 1 deletion Content/FileContent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace Yove.HttpClient;
namespace Yove.Http;

public class FileContent : StreamContent
{
Expand Down
2 changes: 1 addition & 1 deletion Content/HttpContent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;

namespace Yove.HttpClient;
namespace Yove.Http;

public abstract class HttpContent
{
Expand Down
2 changes: 1 addition & 1 deletion Content/MultipartContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO;
using System.Text;

namespace Yove.HttpClient;
namespace Yove.Http;

public class MultipartContent : HttpContent, IEnumerable<HttpContent>, IDisposable
{
Expand Down
2 changes: 1 addition & 1 deletion Content/StreamContent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.IO;

namespace Yove.HttpClient;
namespace Yove.Http;

public class StreamContent : HttpContent
{
Expand Down
2 changes: 1 addition & 1 deletion Content/StringContent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Text;

namespace Yove.HttpClient;
namespace Yove.Http;

public class StringContent : ByteContent
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/ConnectionResult.cs → Enums/ConnectionResult.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient.Proxy;
namespace Yove.Http.Proxy;

public enum ConnectionResult
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/HttpBrowser.cs → Enums/HttpBrowser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient;
namespace Yove.Http;

public enum HttpBrowser
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/HttpMethod.cs → Enums/HttpMethod.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient;
namespace Yove.Http;

public enum HttpMethod
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/HttpStatusCode.cs → Enums/HttpStatusCode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient;
namespace Yove.Http;

public enum HttpStatusCode
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/HttpSystem.cs → Enums/HttpSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient;
namespace Yove.Http;

public enum HttpSystem
{
Expand Down
2 changes: 1 addition & 1 deletion Enum/ProxyType.cs → Enums/ProxyType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient;
namespace Yove.Http;

public enum ProxyType
{
Expand Down
2 changes: 1 addition & 1 deletion Events/DownloadEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Yove.HttpClient.Events;
namespace Yove.Http.Events;

public class DownloadEvent : EventArgs
{
Expand Down
2 changes: 1 addition & 1 deletion Events/UploadEvent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Yove.HttpClient.Events;
namespace Yove.Http.Events;

public class UploadEvent : EventArgs
{
Expand Down
2 changes: 1 addition & 1 deletion Exceptions/HttpRequestException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Yove.HttpClient.Exceptions;
namespace Yove.Http.Exceptions;

public class HttpRequestException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Exceptions/HttpResponseException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Yove.HttpClient.Exceptions;
namespace Yove.Http.Exceptions;

public class HttpResponseException : Exception
{
Expand Down
2 changes: 1 addition & 1 deletion Exceptions/ProxyException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Runtime.Serialization;

namespace Yove.HttpClient.Exceptions;
namespace Yove.Http.Exceptions;

public class HttpProxyException : Exception
{
Expand Down
38 changes: 19 additions & 19 deletions HttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

using Newtonsoft.Json.Linq;

using Yove.HttpClient.Events;
using Yove.HttpClient.Exceptions;
using Yove.HttpClient.Models;
using Yove.HttpClient.Proxy;
using Yove.Http.Events;
using Yove.Http.Exceptions;
using Yove.Http.Models;
using Yove.Http.Proxy;

namespace Yove.HttpClient;
namespace Yove.Http;

[ConfigureAwait(false)]
public class HttpClient : IDisposable
Expand All @@ -29,7 +29,7 @@ public class HttpClient : IDisposable
public NameValueCollection Headers = new();
public NameValueCollection TempHeaders = new();

public NameValueCollection Cookie { get; set; }
public NameValueCollection Cookies { get; set; }

public string BaseUrl { get; set; }
public string Username { get; set; }
Expand All @@ -47,7 +47,7 @@ public class HttpClient : IDisposable
public bool EnableAutoRedirect { get; set; } = true;
public bool RedirectOnlyIfOtherDomain { get; set; }
public bool EnableProtocolError { get; set; } = true;
public bool EnableCookie { get; set; } = true;
public bool EnableCookies { get; set; } = true;
public bool EnableReconnect { get; set; } = true;
public bool HasConnection { get; set; }
public bool IsDisposed { get; set; }
Expand Down Expand Up @@ -142,8 +142,8 @@ private bool _canReconnect

public HttpClient()
{
if (EnableCookie && Cookie == null)
Cookie = new NameValueCollection();
if (EnableCookies && Cookies == null)
Cookies = new NameValueCollection();
}

public HttpClient(CancellationToken token) : this()
Expand All @@ -158,8 +158,8 @@ public HttpClient(string baseUrl)
{
BaseUrl = baseUrl;

if (EnableCookie && Cookie == null)
Cookie = new NameValueCollection();
if (EnableCookies && Cookies == null)
Cookies = new NameValueCollection();
}

public HttpClient(string baseUrl, CancellationToken token) : this(baseUrl)
Expand Down Expand Up @@ -271,8 +271,8 @@ public async Task<HttpResponse> Raw(HttpMethod method, string url, HttpContent b
if (!url.StartsWith("https://") && !url.StartsWith("http://") && !string.IsNullOrEmpty(BaseUrl))
url = $"{BaseUrl.TrimEnd('/')}/{url}";

if (!EnableCookie && Cookie != null)
Cookie = null;
if (!EnableCookies && Cookies != null)
Cookies = null;

Method = method;
Content = body;
Expand Down Expand Up @@ -561,12 +561,12 @@ private string GenerateHeaders(HttpMethod method, long contentLength = 0, string
rawHeaders["Connection"] = "close";
}

if (Cookie?.Count > 0)
if (Cookies?.Count > 0)
{
string cookieBuilder = string.Empty;

foreach (string cookie in Cookie)
cookieBuilder += $"{cookie}={Cookie[cookie]}; ";
foreach (string cookie in Cookies)
cookieBuilder += $"{cookie}={Cookies[cookie]}; ";

rawHeaders["Cookie"] = cookieBuilder.TrimEnd();
}
Expand Down Expand Up @@ -603,8 +603,8 @@ public void AddRawCookie(string source)
if (string.IsNullOrEmpty(source))
throw new NullReferenceException("Value is null or empty.");

if (!EnableCookie)
throw new HttpRequestException("Cookie is disabled.");
if (!EnableCookies)
throw new HttpRequestException("Cookies is disabled.");

if (source.Contains("Cookie:", StringComparison.OrdinalIgnoreCase))
source = source.Replace("Cookie:", "", StringComparison.OrdinalIgnoreCase).Trim();
Expand All @@ -615,7 +615,7 @@ public void AddRawCookie(string source)
string value = cookie.Split('=')[1]?.Trim();

if (key != null && value != null)
Cookie[key] = value;
Cookies[key] = value;
}
}

Expand Down
17 changes: 8 additions & 9 deletions HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

using Fody;

using Yove.HttpClient.Exceptions;
using Yove.HttpClient.Models;
using Yove.Http.Exceptions;
using Yove.Http.Models;

namespace Yove.HttpClient;
namespace Yove.Http;

[ConfigureAwait(false)]
public class HttpResponse
Expand All @@ -20,7 +20,7 @@ public class HttpResponse
private Receiver _content { get; }

public NameValueCollection Headers = new();
public NameValueCollection Cookie = new();
public NameValueCollection Cookies = new();
public List<RedirectItem> RedirectHistory = new();

public string ContentType { get; }
Expand Down Expand Up @@ -196,10 +196,10 @@ internal HttpResponse(HttpClient httpClient)

if (!string.IsNullOrEmpty(cookieName))
{
Cookie[cookieName] = cookieValue;
Cookies[cookieName] = cookieValue;

if (_request.EnableCookie && _request.Cookie != null)
_request.Cookie[cookieName] = cookieValue;
if (_request.EnableCookies && _request.Cookies != null)
_request.Cookies[cookieName] = cookieValue;
}
}
else
Expand Down Expand Up @@ -252,8 +252,7 @@ internal async Task<MemoryStream> GetBodyContent()
if (outputStream == null && ContentLength.HasValue)
outputStream = await ReceiveStandartBody(false);

if (outputStream == null)
outputStream = await ReceiveUnsizeBody(_request.CommonStream);
outputStream ??= await ReceiveUnsizeBody(_request.CommonStream);

if (outputStream != null && Content.Stream.Length > 0)
Content.Stream.Position = 0;
Expand Down
2 changes: 1 addition & 1 deletion HttpUtils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace Yove.HttpClient;
namespace Yove.Http;

public static class HttpUtils
{
Expand Down
2 changes: 1 addition & 1 deletion Models/RedirectHistory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Yove.HttpClient.Models;
namespace Yove.Http.Models;

public record RedirectItem
{
Expand Down
48 changes: 48 additions & 0 deletions Proxy/HttpProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

using Yove.Http.Exceptions;

namespace Yove.Http.Proxy;

public class HttpProxy : ProxyClient
{
public HttpProxy() { }
public HttpProxy(string host, int port, ProxyType type) : this($"{host}:{port}", type) { }
public HttpProxy(string proxy, ProxyType type) : base(proxy, type) { }

private protected override async Task<ConnectionResult> SendCommand(NetworkStream networkStream, string destinationHost, int destinationPort)
{
if (destinationPort == 80)
return ConnectionResult.OK;

byte[] requestBytes = Encoding.ASCII.GetBytes($"CONNECT {destinationHost}:{destinationPort} HTTP/1.1\r\n\r\n");

networkStream.Write(requestBytes, 0, requestBytes.Length);

await WaitStream(networkStream);

StringBuilder responseBuilder = new();

byte[] buffer = new byte[100];

while (networkStream.DataAvailable)
{
int readBytes = networkStream.Read(buffer, 0, 100);

responseBuilder.Append(Encoding.ASCII.GetString(buffer, 0, readBytes));
}

if (responseBuilder.Length == 0)
throw new HttpProxyException("Received empty response.");

HttpStatusCode statusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), HttpUtils.Parser(" ", responseBuilder.ToString(), " ")?.Trim());

if (statusCode != HttpStatusCode.OK)
return ConnectionResult.InvalidProxyResponse;

return ConnectionResult.OK;
}
}
Loading

0 comments on commit 63bd4fe

Please sign in to comment.