diff --git a/Fluzm/Router.cs b/Fluzm/Router.cs index 1444c50..e15265a 100644 --- a/Fluzm/Router.cs +++ b/Fluzm/Router.cs @@ -3,11 +3,13 @@ public class Router { private Dictionary> routes = new(); - private Func rejectDelegate; + private readonly IOPath publicDirPath = "./public"; + private readonly IOPath homepagePath = "./public/html/index.html"; + private readonly IOPath pare404Path = "./public/html/404.html"; public Router() { - routes.Add("/", ctx => + routes.Add("UwU", ctx => { var buffer = "

UwU

".ToBytes(); ctx.Response.ContentLength64 = buffer.Length; @@ -15,18 +17,64 @@ public class Router ctx.Response.OutputStream.Flush(); return 200; }); - rejectDelegate = ctx => - { - ctx.Response.ContentLength64 = 0; - return 500; - }; } public bool TryResolve(HttpListenerContext ctx) { - int status = routes.TryGetValue(ctx.Request.Url!.AbsolutePath, out var routeDelegate) - ? routeDelegate(ctx) - : rejectDelegate(ctx); + int status; + if (routes.TryGetValue(ctx.Request.Url!.AbsolutePath, out var routeDelegate)) + status = routeDelegate(ctx); + else if(ctx.Request.HttpMethod == "GET") + { + string urlPath = ctx.Request.Url.AbsolutePath; + var ext = Path.Extension(urlPath).Str; + IOPath filePath; + switch (ext) + { + case "html": + filePath = Path.Concat(publicDirPath, "html", urlPath); + ctx.Response.Headers.Set("Content-Type", "text/html"); + break; + case "css": + filePath = Path.Concat(publicDirPath, "css", urlPath); + ctx.Response.Headers.Set("Content-Type", "text/css"); + break; + case "js": + filePath = Path.Concat(publicDirPath, "js", urlPath); + ctx.Response.Headers.Set("Content-Type", "text/javascript"); + break; + default: + if (urlPath == "/") + { + filePath = homepagePath; + ctx.Response.Headers.Set("Content-Type", "text/html"); + break; + } + filePath = Path.Concat(publicDirPath, urlPath); + ctx.Response.Headers.Set("Content-Type", "binary/octet-stream"); + ctx.Response.Headers.Set("Content-Disposition", "attachment filename="+filePath.LastName()); + break; + } + + if (File.Exists(filePath)) + status = 200; + else + { + status = 404; + filePath = Path.Concat(publicDirPath, "html", pare404Path); + ctx.Response.Headers.Set("Content-Type", "text/html"); + } + + var fileStream = File.OpenRead(filePath); + ctx.Response.ContentLength64 = fileStream.Length; + fileStream.CopyTo(ctx.Response.OutputStream); + } + // reject all incorrect requests + else + { + status = 500; + ctx.Response.ContentLength64 = 0; + } ctx.Response.StatusCode = status; ctx.Response.OutputStream.Flush(); ctx.Response.OutputStream.Close();