mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-05-06 03:32:57 +00:00
MapGet, ...
This commit is contained in:
parent
9a174f406f
commit
f258464063
@ -9,7 +9,7 @@
|
||||
<Product>Esiur Entity Framework Extension</Product>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageId>Esiur.Stores.EntityCore</PackageId>
|
||||
<Version>1.2.9</Version>
|
||||
<Version>1.3.0</Version>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
<PackageProjectUrl>http://www.esiur.com</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/esiur/esiur-dotnet/</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Version>1.5.4</Version>
|
||||
<Version>1.5.5</Version>
|
||||
<PackageId>Esiur.Stores.MongoDB</PackageId>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -40,6 +40,8 @@ using Esiur.Resource.Template;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Esiur.Proxy;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Test
|
||||
{
|
||||
@ -49,12 +51,38 @@ namespace Test
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
|
||||
|
||||
Warehouse.GetTemplateByType(typeof(MyChildRecord));
|
||||
//foreach (var a in ma)
|
||||
// Console.WriteLine(a);
|
||||
|
||||
//var route = "users/{id:int:min(1)}/{name:string}.jpg";
|
||||
//var route = "users/{id}/{name}.jpg";
|
||||
//var rr = getRouteRegex(route);
|
||||
|
||||
|
||||
|
||||
// var m = rr.Match("users/222/fun.jpg");
|
||||
|
||||
// Console.WriteLine(m.Value);
|
||||
|
||||
// var escaped = Regex.Escape(route);
|
||||
|
||||
// var replace = Regex.Replace(route, @"\{([^}]*)\}", @"\{([^}]*)\}");
|
||||
|
||||
// var ss = route.Split('{', '}');
|
||||
|
||||
//var regex = "users/\"
|
||||
//Regex regex = new Regex(@"\(([^()]+)\)*");
|
||||
|
||||
//foreach (Match match in regex.Matches("You id is (1) and your number is (0000000000)"))
|
||||
//{
|
||||
// Console.WriteLine(match.Value);
|
||||
//}
|
||||
|
||||
// Create stores to keep objects.
|
||||
var system = await Warehouse.Put("mem", new MemoryStore());
|
||||
var server = await Warehouse.Put("mem/server", new DistributedServer());
|
||||
var web = await Warehouse.Put("mem/web", new HTTPServer() { Port = 8888});
|
||||
|
||||
var service = await Warehouse.Put("mem/service", new MyService());
|
||||
var res1 = await Warehouse.Put("mem/service/r1", new MyResource() { Description = "Testing 1", CategoryId = 10 });
|
||||
var res2 = await Warehouse.Put("mem/service/r2", new MyResource() { Description = "Testing 2", CategoryId = 11 });
|
||||
@ -65,9 +93,20 @@ namespace Test
|
||||
service.ChildResource = res3;
|
||||
service.Resources = new MyResource[] { res1, res2, res1 , res3 };
|
||||
|
||||
//web.MapGet("/{action}/{age}", (int age, string action, HTTPConnection sender) =>
|
||||
//{
|
||||
// Console.WriteLine($"AGE: {age} ACTION: {action}");
|
||||
|
||||
// sender.Response.Number = Esiur.Net.Packets.HTTPResponsePacket.ResponseCode.NotFound;
|
||||
// sender.Send("Not found");
|
||||
//});
|
||||
|
||||
web.MapGet("/", (HTTPConnection sender) => {
|
||||
sender.Send("Hello");
|
||||
});
|
||||
|
||||
var ok = await Warehouse.Open();
|
||||
|
||||
Console.WriteLine(String.Join(" ", service.Instance.Template.ClassId.ToByteArray()));
|
||||
// Start testing
|
||||
TestClient(service);
|
||||
// TestClient(service);
|
||||
|
Loading…
x
Reference in New Issue
Block a user