HttpApplication.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Text;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Hosting.Server;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Http.Features;
  7. using appMpower.Kestrel;
  8. namespace appMpower
  9. {
  10. public class HttpApplication : IHttpApplication<IFeatureCollection>
  11. {
  12. public static readonly byte[] _plainText = Encoding.UTF8.GetBytes("Hello, World!");
  13. private readonly static JsonMessageSerializer _jsonMessageSerializer = new JsonMessageSerializer();
  14. private readonly static WorldSerializer _worldSerializer = new WorldSerializer();
  15. private readonly static CachedWorldSerializer _cachedWorldSerializer = new CachedWorldSerializer();
  16. public IFeatureCollection CreateContext(IFeatureCollection featureCollection)
  17. {
  18. return featureCollection;
  19. }
  20. public async Task ProcessRequestAsync(IFeatureCollection featureCollection)
  21. {
  22. var request = featureCollection as IHttpRequestFeature;
  23. var httpResponse = featureCollection as IHttpResponseFeature;
  24. var httpResponseBody = featureCollection as IHttpResponseBodyFeature;
  25. PathString pathString = request.Path;
  26. if (pathString.HasValue)
  27. {
  28. int pathStringLength = pathString.Value.Length;
  29. string pathStringStart = pathString.Value.Substring(1, 1);
  30. if (pathStringLength == 10 && pathStringStart == "p")
  31. {
  32. //await PlainText.RenderAsync(httpResponse.Headers, httpResponseBody.Writer, _plainText);
  33. PlainText.Render(httpResponse.Headers, httpResponseBody, _plainText);
  34. return;
  35. }
  36. else if (pathStringLength == 5 && pathStringStart == "j")
  37. {
  38. Json.RenderOne(httpResponse.Headers, httpResponseBody.Writer, new JsonMessage { message = "Hello, World!" }, _jsonMessageSerializer);
  39. return;
  40. }
  41. else if (pathStringLength == 3 && pathStringStart == "d")
  42. {
  43. Json.RenderOne(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadSingleQueryRow(), _worldSerializer);
  44. return;
  45. }
  46. else if (pathStringLength == 8 && pathStringStart == "q")
  47. {
  48. int count = 1;
  49. if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
  50. {
  51. count = 1;
  52. }
  53. else if (count > 500)
  54. {
  55. count = 500;
  56. }
  57. #if ADO
  58. Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadMultipleQueriesRows(count), _worldSerializer);
  59. #else
  60. Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.ReadMultipleRows(count), _worldSerializer);
  61. #endif
  62. return;
  63. }
  64. else if (pathStringLength == 9 && pathStringStart == "f")
  65. {
  66. await FortunesView.Render(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadFortunesRows());
  67. return;
  68. }
  69. else if (pathStringLength == 8 && pathStringStart == "u")
  70. {
  71. int count = 1;
  72. if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
  73. {
  74. count = 1;
  75. }
  76. else if (count > 500)
  77. {
  78. count = 500;
  79. }
  80. Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadMultipleUpdatesRows(count), _worldSerializer);
  81. return;
  82. }
  83. else if (pathStringLength == 14 && pathStringStart == "c")
  84. {
  85. int count = 1;
  86. if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
  87. {
  88. count = 1;
  89. }
  90. else if (count > 500)
  91. {
  92. count = 500;
  93. }
  94. Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadCachedQueries(count), _cachedWorldSerializer);
  95. return;
  96. }
  97. }
  98. }
  99. public void DisposeContext(IFeatureCollection featureCollection, Exception exception)
  100. {
  101. }
  102. }
  103. }