|
- using System.IO;
- using System.Net;
- using System.Reflection;
- using System.Text;
-
- namespace CLre_server.WebStatus
- {
- public static class AssetEndpoints
- {
- [WebEndpoint("/")]
- public static void LandingPage(HttpListenerContext ctx)
- {
- ctx.Response.Headers.Add("Content-Type", "text/html");
- Asset(ctx, "CLre_server.WebStatus.Assets.index.html");
- }
-
- [WebEndpoint("/asset")]
- public static void AllAssets(HttpListenerContext ctx)
- {
- ctx.Response.Headers.Add("Content-Type", "text/html");
- Asset(ctx, "");
- }
-
- [WebEndpoint("/asset/404")]
- public static void Asset404(HttpListenerContext ctx)
- {
- ctx.Response.Headers.Add("Content-Type", "text/html");
- Asset(ctx, "CLre_server.WebStatus.Assets.error404.html");
- }
-
- private static bool Asset(HttpListenerContext ctx, string name)
- {
- Assembly asm = Assembly.GetCallingAssembly();
- Stream resource = asm.GetManifestResourceStream(name);
- if (resource == null)
- {
- string assetStr = ListAssetsHtml(asm);
- byte[] output = Encoding.UTF8.GetBytes(assetStr);
- ctx.Response.OutputStream.Write(output, 0, output.Length);
- return false;
- }
- resource.CopyTo(ctx.Response.OutputStream);
- return true;
- }
-
- private static string ListAssetsHtml(Assembly target)
- {
- StringBuilder sb = new StringBuilder("<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Asset not found</title></head><body><h1>");
- sb.Append(target.GetName().Name);
- sb.Append(" available assets</h1>");
- foreach (string asset in target.GetManifestResourceNames())
- {
- sb.Append("<li>");
- sb.Append(asset);
- sb.Append("</li>");
- }
-
- sb.Append("</ul></body></html>");
- return sb.ToString();
- }
-
- private static string ListAssetsText(Assembly target)
- {
- StringBuilder sb = new StringBuilder(target.FullName);
- foreach (string asset in target.GetManifestResourceNames())
- {
- sb.Append("\n\t");
- sb.Append(asset);
- }
-
- return sb.ToString();
- }
- }
- }
|