From 79e97cd959739d170aef5414079051eae8ed2182 Mon Sep 17 00:00:00 2001 From: "Alecio Furanze (Ale)" Date: Sat, 29 Jun 2024 02:04:34 +0200 Subject: [PATCH] improve HTTP.ServerTo code --- src/http/partials/Server/HTTP.ServerTo.cs | 31 +++++++++++------------ src/http/partials/Server/Map.cs | 14 +++++----- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/http/partials/Server/HTTP.ServerTo.cs b/src/http/partials/Server/HTTP.ServerTo.cs index 878caa1a..05148dbf 100644 --- a/src/http/partials/Server/HTTP.ServerTo.cs +++ b/src/http/partials/Server/HTTP.ServerTo.cs @@ -157,11 +157,11 @@ void AcceptCallback(IAsyncResult result) { var context = _listener.EndGetContext(result); - Task.Run(async () => + Task.Run(() => { try { - await HandleConnection(context); + HandleConnection(context); } catch (Exception e) { @@ -189,7 +189,7 @@ private static string DefaultHtmlBody(string content) return html; } - private async Task HandleConnection(HttpListenerContext context) + private void HandleConnection(HttpListenerContext context) { var request = new ServerRequest(context.Request); @@ -240,19 +240,19 @@ private async Task HandleConnection(HttpListenerContext context) } return false; - }).Select(x => (MiddlewareDescriptor)x).ToList(); + }).ToArray(); - if (descriptors.Count > 0) + if (descriptors.Length > 0) { - var count = descriptors.Count; + var count = descriptors.Length; for (var i = 0; i < count; i++) { - var descriptor = descriptors[i]; + var descriptor = (MiddlewareDescriptor)descriptors[i]; try { - descriptor.Next = descriptors[i + 1]; + descriptor.Next = (MiddlewareDescriptor)descriptors[i + 1]; } catch { @@ -260,17 +260,15 @@ private async Task HandleConnection(HttpListenerContext context) } } - var mainDescriptor = descriptors[0]; + var mainDescriptor = (MiddlewareDescriptor)descriptors[0]; mainDescriptor.Callback(request, response, () => mainDescriptor.Execute(request, response)); } } } - private async void MapMiddlewareCallback(HttpListenerContext context, ServerRequest request, + private void MapMiddlewareCallback(HttpListenerContext context, ServerRequest request, ServerResponse response, Action next) { - var notFoundMessage = DefaultHtmlBody($"[{request.Method.Method.ToUpper()}] {request.Path}"); - if (!response.IsOpened) { // request is already response by another middleware @@ -279,7 +277,8 @@ private async void MapMiddlewareCallback(HttpListenerContext context, ServerRequ } // SEARCH ROUTE - var map = _server.MyMap.m_mapList.FirstOrDefault(x => + + var map = _server.MyMap.MapList.FirstOrDefault(x => { // websocket connection if (request.IsWebSocket) @@ -291,7 +290,7 @@ private async void MapMiddlewareCallback(HttpListenerContext context, ServerRequ { // handle all method var handleMethod = - string.Equals(x.Method, Map.ALL_MEHOD, StringComparison.CurrentCultureIgnoreCase) + string.Equals(x.Method, Map.AllMethod, StringComparison.CurrentCultureIgnoreCase) || string.Equals(request.Method.Method, x.Method, StringComparison.CurrentCultureIgnoreCase); @@ -328,7 +327,7 @@ private async void MapMiddlewareCallback(HttpListenerContext context, ServerRequ if (map == null) { - response.Send(404, notFoundMessage); + response.Send(404, DefaultHtmlBody($"[{request.Method.Method.ToUpper()}] {request.Path}")); next(); return; } @@ -341,7 +340,7 @@ private async void MapMiddlewareCallback(HttpListenerContext context, ServerRequ } else // IS WEBSOCKET CONNECTION { - var ws = await context.AcceptWebSocketAsync(null); + var ws = context.AcceptWebSocketAsync(null).Result; var websocket = new WebSocket(ws.WebSocket, request); diff --git a/src/http/partials/Server/Map.cs b/src/http/partials/Server/Map.cs index 0b42fe1b..c2bef525 100644 --- a/src/http/partials/Server/Map.cs +++ b/src/http/partials/Server/Map.cs @@ -9,16 +9,16 @@ public partial class HTTP { internal class Map : IHTTP.Map { - internal const string ALL_MEHOD = "*"; + internal const string AllMethod = "*"; - public readonly List m_mapList; + public readonly List MapList; - public readonly Server m_server; + public readonly Server Server; public Map(Server server) { - m_server = server; - m_mapList = new List(); + Server = server; + MapList = new List(); } public void WebSocket(string path, Action callback) @@ -38,7 +38,7 @@ public void All(string path, Action c Add ( path, - ALL_MEHOD, + AllMethod, false, callback, null @@ -168,7 +168,7 @@ private void Add httpCallback, websocketCallback ); - m_mapList.Add(map); + MapList.Add(map); } } }