This repository has been archived on 2023-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
hamakaze/Hamakaze/HttpClient.cs

301 lines
11 KiB
C#
Raw Permalink Normal View History

2022-04-06 09:25:49 +00:00
using Hamakaze.Headers;
2022-04-09 22:34:05 +00:00
using Hamakaze.WebSocket;
2022-04-06 09:25:49 +00:00
using System;
using System.Collections.Generic;
2022-04-09 22:34:05 +00:00
using System.Linq;
using System.Security.Cryptography;
using System.Text;
2022-04-06 09:25:49 +00:00
namespace Hamakaze {
public class HttpClient : IDisposable {
public const string PRODUCT_STRING = @"HMKZ";
public const string VERSION_MAJOR = @"1";
2022-04-09 22:34:05 +00:00
public const string VERSION_MINOR = @"1";
2022-04-06 09:25:49 +00:00
public const string USER_AGENT = PRODUCT_STRING + @"/" + VERSION_MAJOR + @"." + VERSION_MINOR;
2022-04-09 22:34:05 +00:00
private const string WS_GUID = @"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
private const string WS_PROTO = @"websocket";
private const int WS_RNG = 16;
2022-04-06 09:25:49 +00:00
private static HttpClient InstanceValue { get; set; }
public static HttpClient Instance {
get {
if(InstanceValue == null)
InstanceValue = new HttpClient();
return InstanceValue;
}
}
private HttpConnectionManager Connections { get; }
private HttpTaskManager Tasks { get; }
public string DefaultUserAgent { get; set; } = USER_AGENT;
public bool ReuseConnections { get; set; } = true;
public IEnumerable<HttpEncoding> AcceptedEncodings { get; set; } = new[] { HttpEncoding.GZip, HttpEncoding.Deflate, HttpEncoding.Brotli };
public HttpClient() {
Connections = new HttpConnectionManager();
Tasks = new HttpTaskManager();
}
public HttpTask CreateTask(
HttpRequestMessage request,
Action<HttpTask, HttpResponseMessage> onComplete = null,
Action<HttpTask, Exception> onError = null,
Action<HttpTask> onCancel = null,
Action<HttpTask, long, long> onDownloadProgress = null,
Action<HttpTask, long, long> onUploadProgress = null,
Action<HttpTask, HttpTask.TaskState> onStateChange = null,
bool disposeRequest = true,
bool disposeResponse = true
) {
if(request == null)
throw new ArgumentNullException(nameof(request));
if(string.IsNullOrWhiteSpace(request.UserAgent))
request.UserAgent = DefaultUserAgent;
if(!request.HasHeader(HttpAcceptEncodingHeader.NAME))
request.AcceptedEncodings = AcceptedEncodings;
2022-04-09 22:34:05 +00:00
if(!request.HasHeader(HttpConnectionHeader.NAME))
request.Connection = ReuseConnections ? HttpConnectionHeader.KEEP_ALIVE : HttpConnectionHeader.CLOSE;
2022-04-06 09:25:49 +00:00
HttpTask task = new(Connections, request, disposeRequest, disposeResponse);
if(onComplete != null)
task.OnComplete += onComplete;
if(onError != null)
task.OnError += onError;
if(onCancel != null)
task.OnCancel += onCancel;
if(onDownloadProgress != null)
task.OnDownloadProgress += onDownloadProgress;
if(onUploadProgress != null)
task.OnUploadProgress += onUploadProgress;
if(onStateChange != null)
task.OnStateChange += onStateChange;
return task;
}
public void RunTask(HttpTask task) {
Tasks.RunTask(task);
}
public void SendRequest(
HttpRequestMessage request,
Action<HttpTask, HttpResponseMessage> onComplete = null,
Action<HttpTask, Exception> onError = null,
Action<HttpTask> onCancel = null,
Action<HttpTask, long, long> onDownloadProgress = null,
Action<HttpTask, long, long> onUploadProgress = null,
Action<HttpTask, HttpTask.TaskState> onStateChange = null,
bool disposeRequest = true,
bool disposeResponse = true
) {
RunTask(CreateTask(request, onComplete, onError, onCancel, onDownloadProgress, onUploadProgress, onStateChange, disposeRequest, disposeResponse));
}
2022-04-09 22:34:05 +00:00
public void CreateWsClient(
string url,
Action<WsClient> onOpen,
Action<WsMessage> onMessage,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
) => CreateWsConnection(
2022-04-09 22:34:05 +00:00
url,
conn => onOpen(new WsClient(conn, onMessage, onError)),
onError,
protocols,
onResponse,
disposeRequest,
disposeResponse
);
public void CreateWsClient(
HttpRequestMessage request,
Action<WsClient> onOpen,
Action<WsMessage> onMessage,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
) => CreateWsConnection(
request,
conn => onOpen(new WsClient(conn, onMessage, onError)),
onError,
protocols,
onResponse,
disposeRequest,
disposeResponse
2022-04-09 22:34:05 +00:00
);
public void CreateWsConnection(
string url,
Action<WsConnection> onOpen,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
) => CreateWsConnection(
new HttpRequestMessage(@"GET", url),
onOpen,
onError,
protocols,
onResponse,
disposeRequest,
disposeResponse
);
public void CreateWsConnection(
HttpRequestMessage request,
Action<WsConnection> onOpen,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
2022-04-09 22:34:05 +00:00
) {
string key = Convert.ToBase64String(RandomNumberGenerator.GetBytes(WS_RNG));
request.Connection = HttpConnectionHeader.UPGRADE;
request.SetHeader(@"Cache-Control", @"no-cache");
request.SetHeader(@"Upgrade", WS_PROTO);
request.SetHeader(@"Sec-WebSocket-Key", key);
request.SetHeader(@"Sec-WebSocket-Version", @"13");
2022-04-09 22:34:05 +00:00
if(protocols?.Any() == true)
request.SetHeader(@"Sec-WebSocket-Protocol", string.Join(@", ", protocols));
2022-04-09 22:34:05 +00:00
SendRequest(
request,
2022-04-09 22:34:05 +00:00
(t, res) => {
try {
onResponse?.Invoke(res);
2022-04-09 22:34:05 +00:00
if(res.ProtocolVersion.CompareTo(@"1.1") < 0)
throw new HttpUpgradeProtocolVersionException(@"1.1", res.ProtocolVersion);
if(res.StatusCode != 101)
throw new HttpUpgradeUnexpectedStatusException(res.StatusCode);
if(res.Connection != HttpConnectionHeader.UPGRADE)
throw new HttpUpgradeUnexpectedHeaderException(
@"Connection",
HttpConnectionHeader.UPGRADE,
res.Connection
);
string hUpgrade = res.GetHeaderLine(@"Upgrade");
if(hUpgrade != WS_PROTO)
throw new HttpUpgradeUnexpectedHeaderException(@"Upgrade", WS_PROTO, hUpgrade);
string serverHashStr = res.GetHeaderLine(@"Sec-WebSocket-Accept");
byte[] expectHash = SHA1.HashData(Encoding.ASCII.GetBytes(key + WS_GUID));
if(string.IsNullOrWhiteSpace(serverHashStr))
throw new HttpUpgradeUnexpectedHeaderException(
@"Sec-WebSocket-Accept",
Convert.ToBase64String(expectHash),
serverHashStr
);
byte[] givenHash = Convert.FromBase64String(serverHashStr.Trim());
if(!expectHash.SequenceEqual(givenHash))
throw new HttpUpgradeInvalidHashException(Convert.ToBase64String(expectHash), serverHashStr);
onOpen(t.Connection.ToWebSocket());
} catch(Exception ex) {
onError(ex);
}
},
(t, ex) => onError(ex),
disposeRequest: disposeRequest,
disposeResponse: disposeResponse
2022-04-09 22:34:05 +00:00
);
}
2022-04-06 09:25:49 +00:00
public static void Send(
HttpRequestMessage request,
Action<HttpTask, HttpResponseMessage> onComplete = null,
Action<HttpTask, Exception> onError = null,
Action<HttpTask> onCancel = null,
Action<HttpTask, long, long> onDownloadProgress = null,
Action<HttpTask, long, long> onUploadProgress = null,
Action<HttpTask, HttpTask.TaskState> onStateChange = null,
bool disposeRequest = true,
bool disposeResponse = true
) => Instance.SendRequest(
request,
onComplete,
onError,
onCancel,
onDownloadProgress,
onUploadProgress,
onStateChange,
disposeRequest,
disposeResponse
);
public static void Connect(
string url,
Action<WsClient> onOpen,
Action<WsMessage> onMessage,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
) => Instance.CreateWsClient(
url,
onOpen,
onMessage,
onError,
protocols,
onResponse,
disposeRequest,
disposeResponse
);
public static void Connect(
HttpRequestMessage request,
Action<WsClient> onOpen,
Action<WsMessage> onMessage,
Action<Exception> onError,
IEnumerable<string> protocols = null,
Action<HttpResponseMessage> onResponse = null,
bool disposeRequest = true,
bool disposeResponse = true
) => Instance.CreateWsClient(
request,
onOpen,
onMessage,
onError,
protocols,
onResponse,
disposeRequest,
disposeResponse
);
2022-04-06 09:25:49 +00:00
private bool IsDisposed;
~HttpClient()
=> DoDispose();
public void Dispose() {
DoDispose();
GC.SuppressFinalize(this);
}
private void DoDispose() {
if(IsDisposed)
return;
IsDisposed = true;
Tasks.Dispose();
Connections.Dispose();
}
}
}