using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Reflection; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.HttpSys; using static CompServ.CompServConst; namespace CompServ { public static class ModelExtensionMethod { public static async Task ExtractCompressRequestAsync(this HttpRequest request) { if (int.TryParse(request.Headers[RequestHeaderCompressThreads], out var cThread) == false) cThread = 1; if (int.TryParse(request.Headers[RequestHeaderCompressLevel], out var cLevel) == false) cLevel = 0; var msInput = request.ContentLength.HasValue ? new MemoryStream((int)request.ContentLength.Value) : new MemoryStream(); request.Body.Position = 0; await request.Body.CopyToAsync(msInput); return new CompressRequestModel { Level = cLevel, Threads = cThread, DataToCompress = msInput.ToArray() }; } public static HttpRequestMessage BuildRequestMessage(this CompressRequestModel model) { return new HttpRequestMessage(HttpMethod.Post, ApiPathWorkerCompress) { Headers = { {RequestHeaderCompressLevel,model.Level.ToString()}, {RequestHeaderCompressThreads,model.Threads.ToString()}, }, Version = HttpVersion.Version20, VersionPolicy = HttpVersionPolicy.RequestVersionExact, Content = new ByteArrayContent(model.DataToCompress.Array!, model.DataToCompress.Offset, model.DataToCompress.Count) }; } public static HttpRequestMessage BuildRequestMessage(this DecompressRequestModel model) { return new HttpRequestMessage(HttpMethod.Post, ApiPathWorkerDecompress) { Version = HttpVersion.Version20, VersionPolicy = HttpVersionPolicy.RequestVersionExact, Content = new ByteArrayContent(model.DataToDecompress.Array!, model.DataToDecompress.Offset, model.DataToDecompress.Count), }; } public static HttpRequestMessage BuildWorkerRegisterRequestMessage(int port, int ratio) { return new HttpRequestMessage(HttpMethod.Post, ApiPathHubRegisterWorker) { Headers = { { RequestHeaderWorkerPort, port.ToString() }, { RequestHeaderWorkerRatio, ratio.ToString() } }, Content = new ByteArrayContent(Array.Empty()), Version = HttpVersion.Version20, VersionPolicy = HttpVersionPolicy.RequestVersionExact, }; } public static HttpRequestMessage BuildWorkerUnRegisterRequestMessage(int port) { return new HttpRequestMessage(HttpMethod.Post, ApiPathHubUnRegisterWorker) { Headers = { { RequestHeaderWorkerPort, port.ToString() } }, Content = new ByteArrayContent(Array.Empty()), Version = HttpVersion.Version20, VersionPolicy = HttpVersionPolicy.RequestVersionExact, }; } public static (int? port, int? ratio) ExtractWorkerRegister(this HttpRequest request) { var p = int.TryParse(request.Headers[RequestHeaderWorkerPort], out var port) ? (int?)port : null; var r = int.TryParse(request.Headers[RequestHeaderWorkerRatio], out var ratio) ? (int?)ratio : null; return (p, r); } public static HttpRequestMessage BuildClientGetWorkerRequest() { return new HttpRequestMessage(HttpMethod.Get, ApiPathHubGetWorker) { Content = new ByteArrayContent(Array.Empty()), Version = HttpVersion.Version20, VersionPolicy = HttpVersionPolicy.RequestVersionExact, }; } } }