From d0979ebd5b8d369b5223a5c0a48b28b0797d4d6e Mon Sep 17 00:00:00 2001 From: uwaa Date: Mon, 28 Oct 2024 06:49:46 +0000 Subject: [PATCH] update --- Example/Program.cs | 76 ++++++++++++++++++++++++---------------------- MiniHTTP | 2 +- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/Example/Program.cs b/Example/Program.cs index 660e669..f703124 100644 --- a/Example/Program.cs +++ b/Example/Program.cs @@ -34,40 +34,53 @@ static class Program static Router CreateRouter() { - Router root = new Router(NotFound); - root.Use("*", new CORS()); - root.Add(HttpMethod.GET, "redirect", Redirect); - root.Static("www-static"); - root.Static("www-dynamic"); - root.Add(HttpMethod.GET, "", Root); - - Router nested = new Router(NotFound); - nested.Add(HttpMethod.GET, "example1", Nested1); - nested.Add(HttpMethod.GET, "example2", Nested2); - root.Use("nested", nested); - - return root; + //The order here matters + Router router = new Router(); + router.Add(new CORS()); + router.Add("/variables/:param1/:param2", Variables); + router.Add("/:file", Static.Create("www-static", "file")); + router.Add("/:file", Static.Create("www-dynamic", "file")); + router.Add("/", Root); + router.SetDefault(NotFound); + return router; } - static async Task Root(HttpRequest client, VariableCollection parameters) + /// + /// Root endpoint: / + /// + static async Task Root(HttpRequest req, RouteMatch route) { - if (client.Headers.TryGetValue("Upgrade", out string? connection) && connection.Equals("websocket", StringComparison.OrdinalIgnoreCase)) - return await Websocket(client); - else - return new OK(new HttpContent("text/html", await File.ReadAllBytesAsync("www-static/index.htm"))); + if (req.IsWebsocket) + return await Websocket(req, route); + + byte[] indexFile = await File.ReadAllBytesAsync("www-static/index.htm"); + HttpContent html = new HttpContent("text/html", indexFile); + return new OK(html); } - static Redirect Redirect(HttpRequest client, VariableCollection parameters) - { - return new Redirect("/"); - } - - static NotFound NotFound(HttpRequest client, VariableCollection parameters) + /// + /// HTTP 404 + /// + static NotFound NotFound(HttpRequest req, RouteMatch route) { return new NotFound("File not found"); } - static async Task Websocket(HttpRequest client) + /// + /// Variables endpoint + /// + static HttpResponse? Variables(HttpRequest req, RouteMatch route) + { + string? param1 = route.GetVariable("param1"); + string? param2 = route.GetVariable("param1"); + + return new OK($"Variable 1: {param1}\nVariable 2: {param2}"); + } + + /// + /// Websocket endpoint + /// + static async Task Websocket(HttpRequest client, RouteMatch route) { Websocket? ws = await client.UpgradeToWebsocket("test"); if (ws == null) @@ -75,10 +88,9 @@ static class Program DataFrame payload = await ws.Read(); - string result = payload.AsString(); - if (payload.Opcode != WSOpcode.Close) { + string result = payload.AsString(); await ws.Write(true, $"Echoing message: \"{result}\""); } @@ -86,14 +98,4 @@ static class Program return new Empty(); } - - static OK Nested1(HttpRequest client, VariableCollection parameters) - { - return new OK("Foo"); - } - - static OK Nested2(HttpRequest client, VariableCollection parameters) - { - return new OK("Bar"); - } } diff --git a/MiniHTTP b/MiniHTTP index bc8ce1a..2dacb80 160000 --- a/MiniHTTP +++ b/MiniHTTP @@ -1 +1 @@ -Subproject commit bc8ce1a19af6f398fcb05d597f356adf3e3f2e00 +Subproject commit 2dacb80d492609d4a4407e85981bbe0447b3435f