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("Asset not found

"); sb.Append(target.GetName().Name); sb.Append(" available assets

"); foreach (string asset in target.GetManifestResourceNames()) { sb.Append("
  • "); sb.Append(asset); sb.Append("
  • "); } sb.Append(""); 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(); } } }