123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Hosting.Server;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Http.Features;
- using appMpower.Kestrel;
- namespace appMpower
- {
- public class HttpApplication : IHttpApplication<IFeatureCollection>
- {
- public static readonly byte[] _plainText = Encoding.UTF8.GetBytes("Hello, World!");
- private readonly static JsonMessageSerializer _jsonMessageSerializer = new JsonMessageSerializer();
- private readonly static WorldSerializer _worldSerializer = new WorldSerializer();
- private readonly static CachedWorldSerializer _cachedWorldSerializer = new CachedWorldSerializer();
- public IFeatureCollection CreateContext(IFeatureCollection featureCollection)
- {
- return featureCollection;
- }
- public async Task ProcessRequestAsync(IFeatureCollection featureCollection)
- {
- var request = featureCollection as IHttpRequestFeature;
- var httpResponse = featureCollection as IHttpResponseFeature;
- var httpResponseBody = featureCollection as IHttpResponseBodyFeature;
- PathString pathString = request.Path;
- if (pathString.HasValue)
- {
- int pathStringLength = pathString.Value.Length;
- string pathStringStart = pathString.Value.Substring(1, 1);
- if (pathStringLength == 10 && pathStringStart == "p")
- {
- //await PlainText.RenderAsync(httpResponse.Headers, httpResponseBody.Writer, _plainText);
- PlainText.Render(httpResponse.Headers, httpResponseBody, _plainText);
- return;
- }
- else if (pathStringLength == 5 && pathStringStart == "j")
- {
- Json.RenderOne(httpResponse.Headers, httpResponseBody.Writer, new JsonMessage { message = "Hello, World!" }, _jsonMessageSerializer);
- return;
- }
- else if (pathStringLength == 3 && pathStringStart == "d")
- {
- Json.RenderOne(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadSingleQueryRow(), _worldSerializer);
- return;
- }
- else if (pathStringLength == 8 && pathStringStart == "q")
- {
- int count = 1;
- if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
- {
- count = 1;
- }
- else if (count > 500)
- {
- count = 500;
- }
- #if ADO
- Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadMultipleQueriesRows(count), _worldSerializer);
- #else
- Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.ReadMultipleRows(count), _worldSerializer);
- #endif
- return;
- }
- else if (pathStringLength == 9 && pathStringStart == "f")
- {
- await FortunesView.Render(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadFortunesRows());
- return;
- }
- else if (pathStringLength == 8 && pathStringStart == "u")
- {
- int count = 1;
- if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
- {
- count = 1;
- }
- else if (count > 500)
- {
- count = 500;
- }
- Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadMultipleUpdatesRows(count), _worldSerializer);
- return;
- }
- else if (pathStringLength == 14 && pathStringStart == "c")
- {
- int count = 1;
- if (!Int32.TryParse(request.QueryString.Substring(request.QueryString.LastIndexOf("=") + 1), out count) || count < 1)
- {
- count = 1;
- }
- else if (count > 500)
- {
- count = 500;
- }
- Json.RenderMany(httpResponse.Headers, httpResponseBody.Writer, await RawDb.LoadCachedQueries(count), _cachedWorldSerializer);
- return;
- }
- }
- }
- public void DisposeContext(IFeatureCollection featureCollection, Exception exception)
- {
- }
- }
- }
|