Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.4KB

  1. using System.IO;
  2. using System.Net;
  3. using System.Reflection;
  4. using System.Text;
  5. namespace CLre_server.WebStatus
  6. {
  7. public static class AssetEndpoints
  8. {
  9. [WebEndpoint("/")]
  10. public static void LandingPage(HttpListenerContext ctx)
  11. {
  12. ctx.Response.Headers.Add("Content-Type", "text/html");
  13. Asset(ctx, "CLre_server.WebStatus.Assets.index.html");
  14. }
  15. [WebEndpoint("/asset")]
  16. public static void AllAssets(HttpListenerContext ctx)
  17. {
  18. ctx.Response.Headers.Add("Content-Type", "text/html");
  19. Asset(ctx, "");
  20. }
  21. [WebEndpoint("/asset/404")]
  22. public static void Asset404(HttpListenerContext ctx)
  23. {
  24. ctx.Response.Headers.Add("Content-Type", "text/html");
  25. Asset(ctx, "CLre_server.WebStatus.Assets.error404.html");
  26. }
  27. private static bool Asset(HttpListenerContext ctx, string name)
  28. {
  29. Assembly asm = Assembly.GetCallingAssembly();
  30. Stream resource = asm.GetManifestResourceStream(name);
  31. if (resource == null)
  32. {
  33. string assetStr = ListAssetsHtml(asm);
  34. byte[] output = Encoding.UTF8.GetBytes(assetStr);
  35. ctx.Response.OutputStream.Write(output, 0, output.Length);
  36. return false;
  37. }
  38. resource.CopyTo(ctx.Response.OutputStream);
  39. return true;
  40. }
  41. private static string ListAssetsHtml(Assembly target)
  42. {
  43. StringBuilder sb = new StringBuilder("<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Asset not found</title></head><body><h1>");
  44. sb.Append(target.GetName().Name);
  45. sb.Append(" available assets</h1>");
  46. foreach (string asset in target.GetManifestResourceNames())
  47. {
  48. sb.Append("<li>");
  49. sb.Append(asset);
  50. sb.Append("</li>");
  51. }
  52. sb.Append("</ul></body></html>");
  53. return sb.ToString();
  54. }
  55. private static string ListAssetsText(Assembly target)
  56. {
  57. StringBuilder sb = new StringBuilder(target.FullName);
  58. foreach (string asset in target.GetManifestResourceNames())
  59. {
  60. sb.Append("\n\t");
  61. sb.Append(asset);
  62. }
  63. return sb.ToString();
  64. }
  65. }
  66. }