mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-04-03 20:08:21 +00:00
WIP
This commit is contained in:
@@ -37,22 +37,22 @@ using Esiur.Misc;
|
||||
using System.Security.Cryptography;
|
||||
using Esiur.Core;
|
||||
using Esiur.Net.Packets.WebSocket;
|
||||
using Esiur.Net.Packets.HTTP;
|
||||
using Esiur.Net.Packets.Http;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPConnection : NetworkConnection
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpConnection : NetworkConnection
|
||||
{
|
||||
|
||||
|
||||
|
||||
public bool WSMode { get; internal set; }
|
||||
public HTTPServer Server { get; internal set; }
|
||||
public HttpServer Server { get; internal set; }
|
||||
|
||||
public WebsocketPacket WSRequest { get; set; }
|
||||
public HTTPRequestPacket Request { get; set; }
|
||||
public HTTPResponsePacket Response { get; } = new HTTPResponsePacket();
|
||||
public HttpRequestPacket Request { get; set; }
|
||||
public HttpResponsePacket Response { get; } = new HttpResponsePacket();
|
||||
|
||||
HTTPSession session;
|
||||
HttpSession session;
|
||||
|
||||
public KeyList<string, object> Variables { get; } = new KeyList<string, object>();
|
||||
|
||||
@@ -80,7 +80,7 @@ public class HTTPConnection : NetworkConnection
|
||||
}
|
||||
else
|
||||
{
|
||||
var rp = new HTTPRequestPacket();
|
||||
var rp = new HttpRequestPacket();
|
||||
var pSize = rp.Parse(data, 0, (uint)data.Length);
|
||||
if (pSize > 0)
|
||||
{
|
||||
@@ -115,7 +115,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return ok;
|
||||
}
|
||||
|
||||
public static bool Upgrade(HTTPRequestPacket request, HTTPResponsePacket response)
|
||||
public static bool Upgrade(HttpRequestPacket request, HttpResponsePacket response)
|
||||
{
|
||||
if (IsWebsocketRequest(request))
|
||||
{
|
||||
@@ -132,7 +132,7 @@ public class HTTPConnection : NetworkConnection
|
||||
response.Headers["Sec-WebSocket-Protocol"] = request.Headers["Sec-WebSocket-Protocol"];
|
||||
|
||||
|
||||
response.Number = HTTPResponseCode.Switching;
|
||||
response.Number = HttpResponseCode.Switching;
|
||||
response.Text = "Switching Protocols";
|
||||
|
||||
return true;
|
||||
@@ -141,7 +141,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return false;
|
||||
}
|
||||
|
||||
public HTTPServer Parent
|
||||
public HttpServer Parent
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -173,7 +173,7 @@ public class HTTPConnection : NetworkConnection
|
||||
Send();
|
||||
}
|
||||
|
||||
public void Send(HTTPComposeOption Options = HTTPComposeOption.AllCalculateLength)
|
||||
public void Send(HttpComposeOption Options = HttpComposeOption.AllCalculateLength)
|
||||
{
|
||||
if (Response.Handled)
|
||||
return;
|
||||
@@ -211,7 +211,7 @@ public class HTTPConnection : NetworkConnection
|
||||
// Create a new one
|
||||
session = Server.CreateSession(Global.GenerateCode(12), 60 * 20);
|
||||
|
||||
HTTPCookie cookie = new HTTPCookie("SID", session.Id);
|
||||
HttpCookie cookie = new HttpCookie("SID", session.Id);
|
||||
cookie.Expires = DateTime.MaxValue;
|
||||
cookie.Path = "/";
|
||||
cookie.HttpOnly = true;
|
||||
@@ -226,7 +226,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return IsWebsocketRequest(this.Request);
|
||||
}
|
||||
|
||||
public static bool IsWebsocketRequest(HTTPRequestPacket request)
|
||||
public static bool IsWebsocketRequest(HttpRequestPacket request)
|
||||
{
|
||||
if (request.Headers.ContainsKey("connection")
|
||||
&& request.Headers["connection"].ToLower().Contains("upgrade")
|
||||
@@ -254,7 +254,7 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
if (BL == 0)
|
||||
{
|
||||
if (Request.Method == HTTPMethod.UNKNOWN)
|
||||
if (Request.Method == Packets.Http.HttpMethod.UNKNOWN)
|
||||
{
|
||||
Close();
|
||||
return;
|
||||
@@ -313,7 +313,7 @@ public class HTTPConnection : NetworkConnection
|
||||
{
|
||||
if (!Server.Execute(this))
|
||||
{
|
||||
Response.Number = HTTPResponseCode.InternalServerError;
|
||||
Response.Number = HttpResponseCode.InternalServerError;
|
||||
Send("Bad Request");
|
||||
Close();
|
||||
}
|
||||
@@ -362,7 +362,7 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
Response.Number = HTTPResponseCode.NotFound;
|
||||
Response.Number = HttpResponseCode.NotFound;
|
||||
Send("File Not Found");
|
||||
return true;
|
||||
}
|
||||
@@ -376,10 +376,10 @@ public class HTTPConnection : NetworkConnection
|
||||
var ims = DateTime.Parse(Request.Headers["if-modified-since"]);
|
||||
if ((fileEditTime - ims).TotalSeconds < 2)
|
||||
{
|
||||
Response.Number = HTTPResponseCode.NotModified;
|
||||
Response.Number = HttpResponseCode.NotModified;
|
||||
Response.Headers.Clear();
|
||||
//Response.Text = "Not Modified";
|
||||
Send(HTTPComposeOption.SpecifiedHeadersOnly);
|
||||
Send(HttpComposeOption.SpecifiedHeadersOnly);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -391,12 +391,12 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
|
||||
|
||||
Response.Number = HTTPResponseCode.OK;
|
||||
Response.Number = HttpResponseCode.OK;
|
||||
// Fri, 30 Oct 2007 14:19:41 GMT
|
||||
Response.Headers["Last-Modified"] = fileEditTime.ToString("ddd, dd MMM yyyy HH:mm:ss");
|
||||
FileInfo fi = new FileInfo(filename);
|
||||
Response.Headers["Content-Length"] = fi.Length.ToString();
|
||||
Send(HTTPComposeOption.SpecifiedHeadersOnly);
|
||||
Send(HttpComposeOption.SpecifiedHeadersOnly);
|
||||
|
||||
//var fd = File.ReadAllBytes(filename);
|
||||
|
||||
|
||||
@@ -35,9 +35,9 @@ using Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
namespace Esiur.Net.Http;
|
||||
|
||||
public abstract class HTTPFilter : IResource
|
||||
public abstract class HttpFilter : IResource
|
||||
{
|
||||
public Instance Instance
|
||||
{
|
||||
@@ -60,14 +60,14 @@ public abstract class HTTPFilter : IResource
|
||||
}
|
||||
*/
|
||||
|
||||
public abstract AsyncReply<bool> Execute(HTTPConnection sender);
|
||||
public abstract AsyncReply<bool> Execute(HttpConnection sender);
|
||||
|
||||
public virtual void ClientConnected(HTTPConnection HTTP)
|
||||
public virtual void ClientConnected(HttpConnection HTTP)
|
||||
{
|
||||
//return false;
|
||||
}
|
||||
|
||||
public virtual void ClientDisconnected(HTTPConnection HTTP)
|
||||
public virtual void ClientDisconnected(HttpConnection HTTP)
|
||||
{
|
||||
//return false;
|
||||
}
|
||||
|
||||
@@ -40,25 +40,25 @@ using Esiur.Resource;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Esiur.Net.Packets.HTTP;
|
||||
using Esiur.Net.Packets.Http;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpServer : NetworkServer<HttpConnection>, IResource
|
||||
{
|
||||
Dictionary<string, HTTPSession> sessions = new Dictionary<string, HTTPSession>();
|
||||
HTTPFilter[] filters = new HTTPFilter[0];
|
||||
Dictionary<string, HttpSession> sessions = new Dictionary<string, HttpSession>();
|
||||
HttpFilter[] filters = new HttpFilter[0];
|
||||
|
||||
Dictionary<HTTPMethod, List<RouteInfo>> routes = new()
|
||||
Dictionary<Packets.Http.HttpMethod, List<RouteInfo>> routes = new()
|
||||
{
|
||||
[HTTPMethod.GET] = new List<RouteInfo>(),
|
||||
[HTTPMethod.POST] = new List<RouteInfo>(),
|
||||
[HTTPMethod.HEAD] = new List<RouteInfo>(),
|
||||
[HTTPMethod.OPTIONS] = new List<RouteInfo>(),
|
||||
[HTTPMethod.UNKNOWN] = new List<RouteInfo>(),
|
||||
[HTTPMethod.DELETE] = new List<RouteInfo>(),
|
||||
[HTTPMethod.TRACE] = new List<RouteInfo>(),
|
||||
[HTTPMethod.CONNECT] = new List<RouteInfo>(),
|
||||
[HTTPMethod.PUT] = new List<RouteInfo>()
|
||||
[Packets.Http.HttpMethod.GET] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.POST] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.HEAD] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.OPTIONS] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.UNKNOWN] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.DELETE] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.TRACE] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.CONNECT] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.PUT] = new List<RouteInfo>()
|
||||
};
|
||||
|
||||
//List<RouteInfo> GetRoutes = new List<RouteInfo>();
|
||||
@@ -86,7 +86,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
var last = ps.LastOrDefault();
|
||||
|
||||
if (last != null && last.ParameterType == typeof(HTTPConnection))
|
||||
if (last != null && last.ParameterType == typeof(HttpConnection))
|
||||
{
|
||||
|
||||
SenderIndex = ps.Length - 1;
|
||||
@@ -101,7 +101,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public bool Invoke(HTTPConnection sender)
|
||||
public bool Invoke(HttpConnection sender)
|
||||
{
|
||||
var match = Pattern.Match(sender.Request.URL);
|
||||
|
||||
@@ -176,9 +176,9 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public HTTPSession CreateSession(string id, int timeout)
|
||||
public HttpSession CreateSession(string id, int timeout)
|
||||
{
|
||||
var s = new HTTPSession();
|
||||
var s = new HttpSession();
|
||||
|
||||
s.Set(id, timeout);
|
||||
|
||||
@@ -214,7 +214,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
return Cookie;
|
||||
}
|
||||
|
||||
protected override void ClientDisconnected(HTTPConnection connection)
|
||||
protected override void ClientDisconnected(HttpConnection connection)
|
||||
{
|
||||
foreach (var filter in filters)
|
||||
filter.ClientDisconnected(connection);
|
||||
@@ -222,7 +222,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
|
||||
|
||||
internal bool Execute(HTTPConnection sender)
|
||||
internal bool Execute(HttpConnection sender)
|
||||
{
|
||||
if (!sender.WSMode)
|
||||
foreach (var route in routes[sender.Request.Method])
|
||||
@@ -243,14 +243,14 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
public void MapGet(string pattern, Delegate handler)
|
||||
{
|
||||
var regex = Global.GetRouteRegex(pattern);
|
||||
var list = routes[HTTPMethod.GET];
|
||||
var list = routes[Packets.Http.HttpMethod.GET];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
public void MapPost(string pattern, Delegate handler)
|
||||
{
|
||||
var regex = Global.GetRouteRegex(pattern);
|
||||
var list = routes[HTTPMethod.POST];
|
||||
var list = routes[Packets.Http.HttpMethod.POST];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
else if (trigger == ResourceTrigger.SystemInitialized)
|
||||
{
|
||||
filters = await Instance.Children<HTTPFilter>();
|
||||
filters = await Instance.Children<HttpFilter>();
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -337,19 +337,19 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public override void Add(HTTPConnection connection)
|
||||
public override void Add(HttpConnection connection)
|
||||
{
|
||||
connection.Server = this;
|
||||
base.Add(connection);
|
||||
}
|
||||
|
||||
public override void Remove(HTTPConnection connection)
|
||||
public override void Remove(HttpConnection connection)
|
||||
{
|
||||
connection.Server = null;
|
||||
base.Remove(connection);
|
||||
}
|
||||
|
||||
protected override void ClientConnected(HTTPConnection connection)
|
||||
protected override void ClientConnected(HttpConnection connection)
|
||||
{
|
||||
//if (filters.Length == 0 && routes.)
|
||||
//{
|
||||
|
||||
@@ -35,11 +35,11 @@ using Esiur.Data;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Core;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPSession : IDestructible //<T> where T : TClient
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpSession : IDestructible //<T> where T : TClient
|
||||
{
|
||||
public delegate void SessionModifiedEvent(HTTPSession session, string key, object oldValue, object newValue);
|
||||
public delegate void SessionEndedEvent(HTTPSession session);
|
||||
public delegate void SessionModifiedEvent(HttpSession session, string key, object oldValue, object newValue);
|
||||
public delegate void SessionEndedEvent(HttpSession session);
|
||||
|
||||
private string id;
|
||||
private Timer timer;
|
||||
@@ -58,7 +58,7 @@ public class HTTPSession : IDestructible //<T> where T : TClient
|
||||
get { return variables; }
|
||||
}
|
||||
|
||||
public HTTPSession()
|
||||
public HttpSession()
|
||||
{
|
||||
variables = new KeyList<string, object>();
|
||||
variables.OnModified += new KeyList<string, object>.Modified(VariablesModified);
|
||||
|
||||
Reference in New Issue
Block a user