ResponseHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. namespace ServerLib
  9. {
  10. public static class ResponseHelper
  11. {
  12. public static HttpResponse FromFile(this HttpResponse response, string fileName)
  13. {
  14. if (!File.Exists(fileName))
  15. {
  16. response.SetContent("<html><body><h1>404 - Not Found</h1></body></html>");
  17. response.StatusCode = "404";
  18. response.Content_Type = "text/html";
  19. return response;
  20. }
  21. var content = File.ReadAllBytes(fileName);
  22. var contentType = GetMimeFromFile(fileName);
  23. response.SetContent(content);
  24. response.Content_Type = contentType;
  25. response.StatusCode = "200";
  26. return response;
  27. }
  28. public static HttpResponse FromXML(this HttpResponse response, string xmlText)
  29. {
  30. response.SetContent(xmlText);
  31. response.Content_Type = "text/xml";
  32. response.StatusCode = "200";
  33. return response;
  34. }
  35. public static HttpResponse FromXML<T>(this HttpResponse response, T entity) where T : class
  36. {
  37. return response.FromXML("");
  38. }
  39. public static HttpResponse FromJSON(this HttpResponse response, string jsonText)
  40. {
  41. response.SetContent(jsonText);
  42. response.Content_Type = "text/json";
  43. response.StatusCode = "200";
  44. return response;
  45. }
  46. public static HttpResponse FromJSON<T>(this HttpResponse response, T entity) where T : class
  47. {
  48. return response.FromJSON("");
  49. }
  50. public static HttpResponse FromText(this HttpResponse response, string text)
  51. {
  52. response.SetContent(text);
  53. response.Content_Type = "text/plain";
  54. response.StatusCode = "200";
  55. return response;
  56. }
  57. private static string GetMimeFromFile(string filePath)
  58. {
  59. IntPtr mimeout;
  60. if (!File.Exists(filePath))
  61. throw new FileNotFoundException(string.Format("File {0} can't be found at GameLogic.", filePath));
  62. int MaxContent = (int)new FileInfo(filePath).Length;
  63. if (MaxContent > 4096) MaxContent = 4096;
  64. byte[] buf = new byte[MaxContent];
  65. using (FileStream fs = File.OpenRead(filePath))
  66. {
  67. fs.Read(buf, 0, MaxContent);
  68. fs.Close();
  69. }
  70. int result = FindMimeFromData(IntPtr.Zero, filePath, buf, MaxContent, null, 0, out mimeout, 0);
  71. if (result != 0)
  72. throw Marshal.GetExceptionForHR(result);
  73. string mime = Marshal.PtrToStringUni(mimeout);
  74. Marshal.FreeCoTaskMem(mimeout);
  75. return mime;
  76. }
  77. [DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]
  78. static extern int FindMimeFromData(IntPtr pBC,
  79. [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,
  80. [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1, SizeParamIndex = 3)]
  81. byte[] pBuffer,
  82. int cbSize,
  83. [MarshalAs(UnmanagedType.LPWStr)]
  84. string pwzMimeProposed,
  85. int dwMimeFlags,
  86. out IntPtr ppwzMimeOut,
  87. int dwReserved);
  88. }
  89. }