Unofficial CardLife revival project, pronounced like "celery"
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

74 lignes
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. }