Skip to content

Commit

Permalink
improve HTTP.ServerTo code
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jun 29, 2024
1 parent db0b0a1 commit 79e97cd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
31 changes: 15 additions & 16 deletions src/http/partials/Server/HTTP.ServerTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -240,37 +240,35 @@ 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
{
descriptor.Next = null;
}
}

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
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);

Expand Down
14 changes: 7 additions & 7 deletions src/http/partials/Server/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapDescriptor> m_mapList;
public readonly List<MapDescriptor> MapList;

public readonly Server m_server;
public readonly Server Server;

public Map(Server server)
{
m_server = server;
m_mapList = new List<MapDescriptor>();
Server = server;
MapList = new List<MapDescriptor>();
}

public void WebSocket(string path, Action<IHTTP.ServerRequest, IHTTP.WebSocket> callback)
Expand All @@ -38,7 +38,7 @@ public void All(string path, Action<IHTTP.ServerRequest, IHTTP.ServerResponse> c
Add
(
path,
ALL_MEHOD,
AllMethod,
false,
callback,
null
Expand Down Expand Up @@ -168,7 +168,7 @@ private void Add
httpCallback,
websocketCallback
);
m_mapList.Add(map);
MapList.Add(map);
}
}
}
Expand Down

0 comments on commit 79e97cd

Please sign in to comment.