Skip to content

Commit

Permalink
HTTP.ServerTo -> impl two way milddeware
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jun 27, 2024
1 parent 11b46ec commit 334da6a
Showing 1 changed file with 44 additions and 79 deletions.
123 changes: 44 additions & 79 deletions src/http/partials/Server/HTTP.ServerTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Netly.Interfaces;

Expand Down Expand Up @@ -198,100 +197,66 @@ private async Task HandleConnection(HttpListenerContext context)
var response = new ServerResponse(context.Response);
var notFoundMessage = DefaultHtmlBody($"[{request.Method.Method.ToUpper()}] {request.Path}");

var skipNextMiddleware = false;

void RunMiddlewares(IHTTP.MiddlewareDescriptor[] middlewares)
var middlewares = _server.Middleware.Middlewares.ToList();
Console.WriteLine("Middleware found: " + middlewares.Count);
if (middlewares.Count > 0)
{
foreach (var middleware in middlewares)
var descriptors = middlewares.FindAll(x =>
{
if (skipNextMiddleware) break;

var @continue = middleware.Callback(request, response);

if (!@continue)
{
skipNextMiddleware = true;
response.Send(500, "Internal Server Error");
break;
}
}
}

// GLOBAL MIDDLEWARES

#region GLOBAL MIDDLEWARE

NetlyEnvironment.Logger.Create("Global middleware. Search middlewares...");

var globalMiddlewares = _server.Middleware.Middlewares.ToList().FindAll(x =>
{
if (x.Path == HTTP.Middleware.GlobalPath)
// is only global path
return true;

return false;
});
// allow global middleware
if (x.Path == HTTP.Middleware.GlobalPath) return true;

if (!skipNextMiddleware)
{
RunMiddlewares(globalMiddlewares.ToArray());
NetlyEnvironment.Logger.Create(
$"[END] running global middleware (next: {!skipNextMiddleware})");
}
if (!x.UseParams)
// simple path compare
return Path.ComparePath(request.Path, x.Path);

if (skipNextMiddleware) return;
// compare custom path
var result = Path.ParseParam(x.Path, request.Path);

#endregion

// LOCAL MIDDLEWARES

#region LOCAL MIDDLEWARE
// custom path is valid.
if (result.Valid)
{
// set values in request object
foreach (var myParam in result.Params)
{
// only add value if not exist for prevent exception "Key in use!"
// optimization: if key exist it mean that it was added before with other callback
if (request.Params.ContainsKey(myParam.Key)) break;
// save params on object
request.Params.Add(myParam.Key, myParam.Value);
}

NetlyEnvironment.Logger.Create("Local middleware. Search middlewares...");
return true;
}

var localMiddlewares = _server.Middleware.Middlewares.ToList().FindAll(x =>
{
// only local middleware is allowing
if (x.Path == HTTP.Middleware.GlobalPath) return false;
return false;
}).Select(x => (MiddlewareDescriptor)x).ToList();

if (!x.UseParams)
// simple path compare
return Path.ComparePath(request.Path, x.Path);
if (descriptors.Count > 0)
{
Console.WriteLine("Middleware des found: " + descriptors.Count);

// compare custom path
var result = Path.ParseParam(x.Path, request.Path);
int count = descriptors.Count;

// custom path is valid.
if (result.Valid)
{
// set values in request object
foreach (var myParam in result.Params)
for (int i = 0; i < count; i++)
{
// only add value if not exist for prevent exception "Key in use!"
// optimization: if key exist it mean that it was added before with other callback
if (request.Params.ContainsKey(myParam.Key)) break;
// save params on object
request.Params.Add(myParam.Key, myParam.Value);
var descriptor = descriptors[i];

try
{
descriptor.Next = descriptors[i + 1];
}
catch
{
descriptor.Next = null;
}
}

return true;
descriptors[0].Callback(request, response,
() => descriptors[0].Next.Execute(request, response));
}

return false;
});

if (!skipNextMiddleware)
{
RunMiddlewares(localMiddlewares.ToArray());
NetlyEnvironment.Logger.Create(
$"[END] running local middleware (next: {!skipNextMiddleware})");
}

if (skipNextMiddleware) return;

#endregion


// SEARCH ROUTE
var myPaths = _server._map.m_mapList.FindAll(x =>
{
Expand Down

0 comments on commit 334da6a

Please sign in to comment.