Compare commits

...

2 commits

Author SHA1 Message Date
uwaa
6b5665f415 add connection logging example code 2024-11-04 15:00:43 +00:00
uwaa
50bb60e3e5 Update HTTP 2024-11-04 14:59:33 +00:00
2 changed files with 36 additions and 1 deletions

2
HTTP

@ -1 +1 @@
Subproject commit d48ce6742a2d9d5d41c21f86d8366a2e7210d953 Subproject commit d94fa95162fee2d986f3c29fc3e04d4d85dacf9d

View file

@ -2,6 +2,8 @@
using System.IO; using System.IO;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using Uwaa.HTTP.Websockets; using Uwaa.HTTP.Websockets;
using Uwaa.HTTP.Responses; using Uwaa.HTTP.Responses;
using Uwaa.HTTP.Routing; using Uwaa.HTTP.Routing;
@ -19,6 +21,12 @@ static class Program
Router router = CreateRouter(); Router router = CreateRouter();
HttpServer httpServer = new HttpServer(80, null, router); HttpServer httpServer = new HttpServer(80, null, router);
HttpServer httpsServer = new HttpServer(443, cert, router); HttpServer httpsServer = new HttpServer(443, cert, router);
httpServer.OnConnectionBegin += LogConnect;
httpServer.OnConnectionEnd += LogDisconnect;
httpsServer.OnConnectionBegin += LogConnect;
httpsServer.OnConnectionEnd += LogDisconnect;
_ = httpServer.Start(); _ = httpServer.Start();
_ = httpsServer.Start(); _ = httpsServer.Start();
@ -37,6 +45,7 @@ static class Program
subpath.Add("bar", new StaticEndpoint(new OK("A \"bar\" example sub page"))); subpath.Add("bar", new StaticEndpoint(new OK("A \"bar\" example sub page")));
Router router = new Router(); Router router = new Router();
router.Add(LogRequest);
router.Add(new CORS()); router.Add(new CORS());
router.Add("custom", new CustomRoute()); router.Add("custom", new CustomRoute());
router.Add("subpath", subpath); router.Add("subpath", subpath);
@ -47,6 +56,32 @@ static class Program
return router; return router;
} }
/// <summary>
/// Logs a new connection to the console.
/// </summary>
static void LogConnect(TcpClient client)
{
Console.WriteLine($"{client.Client.RemoteEndPoint} connected");
}
/// <summary>
/// Logs a disconnection to the console.
/// </summary>
static void LogDisconnect(IPEndPoint endpoint)
{
Console.WriteLine($"{endpoint} disconnected");
}
/// <summary>
/// Logs a request to the console.
/// </summary>
static Task<HttpResponse?> LogRequest(HttpRequest req)
{
Console.WriteLine($"{req.Endpoint.Address}: {req.Method} {req.Path}");
return Task.FromResult<HttpResponse?>(null);
}
/// <summary> /// <summary>
/// Root endpoint: / /// Root endpoint: /
/// </summary> /// </summary>