mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
MapGet, ...
This commit is contained in:
@ -307,6 +307,9 @@ public static class Codec
|
||||
else if (genericType == typeof(ValueTuple<,>)
|
||||
|| genericType == typeof(ValueTuple<,,>)
|
||||
|| genericType == typeof(ValueTuple<,,,>)
|
||||
|| genericType == typeof(ValueTuple<,,,,>)
|
||||
|| genericType == typeof(ValueTuple<,,,,,>)
|
||||
|| genericType == typeof(ValueTuple<,,,,,,>)
|
||||
)
|
||||
{
|
||||
var (hdr, data) = DataSerializer.TupleComposer(valueOrSource, connection);
|
||||
|
@ -6,7 +6,7 @@
|
||||
<Copyright>Ahmed Kh. Zamil</Copyright>
|
||||
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>2.1.1</Version>
|
||||
<Version>2.2.1</Version>
|
||||
<RepositoryUrl>https://github.com/esiur/esiur-dotnet</RepositoryUrl>
|
||||
<Authors>Ahmed Kh. Zamil</Authors>
|
||||
<AssemblyVersion></AssemblyVersion>
|
||||
|
@ -38,6 +38,9 @@ using Esiur.Core;
|
||||
using Esiur.Net.Packets;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using Esiur.Resource;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
@ -45,6 +48,128 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
Dictionary<string, HTTPSession> sessions = new Dictionary<string, HTTPSession>();
|
||||
HTTPFilter[] filters = new HTTPFilter[0];
|
||||
|
||||
Dictionary<HTTPRequestPacket.HTTPMethod, List<RouteInfo>> routes = new()
|
||||
{
|
||||
[HTTPRequestPacket.HTTPMethod.GET] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.POST] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.HEAD] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.OPTIONS] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.UNKNOWN] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.DELETE] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.TRACE] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.CONNECT] = new List<RouteInfo>(),
|
||||
[HTTPRequestPacket.HTTPMethod.PUT] = new List<RouteInfo>()
|
||||
};
|
||||
|
||||
//List<RouteInfo> GetRoutes = new List<RouteInfo>();
|
||||
//List<RouteInfo> PostRoutes = new List<RouteInfo>();
|
||||
|
||||
|
||||
class RouteInfo
|
||||
{
|
||||
public Delegate Handler;
|
||||
public Regex Pattern;
|
||||
Dictionary<string, ParameterInfo> ParameterIndex = new();
|
||||
int? SenderIndex;
|
||||
//bool HasSender;
|
||||
int ArgumentsCount;
|
||||
|
||||
public RouteInfo(Delegate handler, Regex pattern)
|
||||
{
|
||||
|
||||
Pattern = pattern;
|
||||
Handler = handler;
|
||||
|
||||
var ps = handler.Method.GetParameters();
|
||||
|
||||
ArgumentsCount = ps.Length;
|
||||
|
||||
var last = ps.LastOrDefault();
|
||||
|
||||
if (last != null && last.ParameterType == typeof(HTTPConnection))
|
||||
{
|
||||
|
||||
SenderIndex = ps.Length - 1;
|
||||
for (var i = 0; i < ps.Length - 1; i++)
|
||||
ParameterIndex[ps[i].Name] = ps[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (var i = 0; i < ps.Length; i++)
|
||||
ParameterIndex[ps[i].Name] = ps[i];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool Invoke(HTTPConnection sender)
|
||||
{
|
||||
var match = Pattern.Match(sender.Request.URL);
|
||||
|
||||
if (!match.Success)
|
||||
return false;
|
||||
|
||||
var args = new object[ArgumentsCount];
|
||||
|
||||
foreach (var kv in ParameterIndex)
|
||||
{
|
||||
var g = match.Groups[kv.Key];
|
||||
args[kv.Value.Position] = DC.CastConvert(g.Value, kv.Value.ParameterType);
|
||||
}
|
||||
|
||||
if (SenderIndex != null)
|
||||
args[(int)SenderIndex] = sender;
|
||||
|
||||
var rt = Handler.DynamicInvoke(args);
|
||||
|
||||
if (rt is bool)
|
||||
return (bool)rt;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
struct VarInfo
|
||||
{
|
||||
public string Pre;
|
||||
public string Post;
|
||||
public string VarName;
|
||||
|
||||
public string Build()
|
||||
{
|
||||
return Regex.Escape(Pre) + @"(?<" + VarName + @">[^\{]*)" + Regex.Escape(Post);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static Regex getRouteRegex(string url)
|
||||
{
|
||||
var sc = Regex.Match(url, @"([^\{]*)\{([^\}]*)\}([^\{]*)");
|
||||
|
||||
List<VarInfo> vars = new List<VarInfo>();
|
||||
|
||||
while (sc.Success)
|
||||
{
|
||||
vars.Add(new VarInfo()
|
||||
{
|
||||
Pre = sc.Groups[1].Value,
|
||||
VarName = sc.Groups[2].Value,
|
||||
Post = sc.Groups[3].Value
|
||||
});
|
||||
sc = sc.NextMatch();
|
||||
}
|
||||
|
||||
if (vars.Count > 0)
|
||||
{
|
||||
return new Regex("^" + String.Join("", vars.Select(x => x.Build()).ToArray()) + "$");
|
||||
}
|
||||
else
|
||||
{
|
||||
return new Regex("^" + Regex.Escape(url) + "$");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Instance Instance
|
||||
{
|
||||
get;
|
||||
@ -149,9 +274,19 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
internal bool Execute(HTTPConnection sender)
|
||||
{
|
||||
foreach (var route in routes[sender.Request.Method])
|
||||
{
|
||||
if (route.Invoke(sender))
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
foreach (var resource in filters)
|
||||
if (resource.Execute(sender).Wait(30000))
|
||||
return true;
|
||||
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -159,9 +294,17 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
public void MapGet(string pattern, Delegate handler)
|
||||
{
|
||||
// if (p)
|
||||
var regex = getRouteRegex(pattern);
|
||||
var list = routes[HTTPRequestPacket.HTTPMethod.GET];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
public void MapPost(string pattern, Delegate handler)
|
||||
{
|
||||
var regex = getRouteRegex(pattern);
|
||||
var list = routes[HTTPRequestPacket.HTTPMethod.POST];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
/*
|
||||
protected override void SessionEnded(NetworkSession session)
|
||||
@ -260,11 +403,11 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
protected override void ClientConnected(HTTPConnection connection)
|
||||
{
|
||||
if (filters.Length == 0)
|
||||
{
|
||||
connection.Close();
|
||||
return;
|
||||
}
|
||||
//if (filters.Length == 0 && routes.)
|
||||
//{
|
||||
// connection.Close();
|
||||
// return;
|
||||
//}
|
||||
|
||||
foreach (var resource in filters)
|
||||
{
|
||||
|
@ -357,7 +357,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
|
||||
if (ready)
|
||||
{
|
||||
var rt = packet.Parse(msg, offset, ends);
|
||||
Console.WriteLine("Rec: " + chunkId + " " + packet.ToString());
|
||||
//Console.WriteLine("Rec: " + chunkId + " " + packet.ToString());
|
||||
|
||||
/*
|
||||
if (packet.Command == IIPPacketCommand.Event)
|
||||
@ -696,7 +696,7 @@ public partial class DistributedConnection : NetworkConnection, IStore
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(msg.GetString(offset, ends - offset));
|
||||
//Console.WriteLine(msg.GetString(offset, ends - offset));
|
||||
|
||||
var rt = authPacket.Parse(msg, offset, ends);
|
||||
|
||||
|
@ -121,7 +121,7 @@ public abstract class NetworkServer<TConnection> : IDestructible where TConnecti
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("New Socket ... " + DateTime.Now);
|
||||
//Console.WriteLine("New Socket ... " + DateTime.Now);
|
||||
|
||||
var c = new TConnection();
|
||||
//c.OnClose += ClientDisconnectedEventReceiver;
|
||||
|
@ -79,6 +79,10 @@ public class TCPSocket : ISocket
|
||||
|
||||
public bool Begin()
|
||||
{
|
||||
// Socket destroyed
|
||||
if (receiveBuffer == null)
|
||||
return false;
|
||||
|
||||
if (began)
|
||||
return false;
|
||||
|
||||
@ -539,8 +543,7 @@ public class TCPSocket : ISocket
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Global.Counters["Sck_D_1"]++;
|
||||
|
||||
|
||||
Close();
|
||||
|
||||
receiveNetworkBuffer = null;
|
||||
@ -557,8 +560,7 @@ public class TCPSocket : ISocket
|
||||
OnDestroy?.Invoke(this);
|
||||
OnDestroy = null;
|
||||
|
||||
Global.Counters["Sck_D_2"]++;
|
||||
}
|
||||
}
|
||||
|
||||
public ISocket Accept()
|
||||
{
|
||||
|
@ -752,8 +752,6 @@ public class TypeTemplate
|
||||
od.className = data.GetString(offset + 1, data[offset]);
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
if (od.className == "Test.MyChildRecord")
|
||||
Console.WriteLine();
|
||||
|
||||
if (hasParent)
|
||||
{
|
||||
|
Reference in New Issue
Block a user