HttpHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using BeetleX;
  2. using BeetleX.Buffers;
  3. using BeetleX.EventArgs;
  4. using SpanJson;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace PlatformBenchmarks
  10. {
  11. public partial class HttpHandler : ServerHandlerBase
  12. {
  13. private static readonly AsciiString _line = new AsciiString("\r\n");
  14. private static readonly AsciiString _2line = new AsciiString("\r\n\r\n");
  15. private static readonly AsciiString _httpsuccess = new AsciiString("HTTP/1.1 200 OK\r\n");
  16. private static readonly AsciiString _headerServer = "Server: B\r\n";
  17. private static readonly AsciiString _headerContentLength = "Content-Length: ";
  18. private static readonly AsciiString _headerContentLengthZero = "Content-Length: 0\r\n";
  19. private static readonly AsciiString _headerContentTypeText = "Content-Type: text/plain\r\n";
  20. private static readonly AsciiString _headerContentTypeHtml = "Content-Type: text/html; charset=UTF-8\r\n";
  21. private static readonly AsciiString _headerContentTypeJson = "Content-Type: application/json\r\n";
  22. private static readonly AsciiString _path_Json = "/json";
  23. private static readonly AsciiString _path_Db = "/db";
  24. private static readonly AsciiString _path_Queries = "/queries";
  25. private static readonly AsciiString _path_Plaintext = "/plaintext";
  26. private static readonly AsciiString _path_Updates = "/updates";
  27. private static readonly AsciiString _path_Fortunes = "/fortunes";
  28. private static readonly AsciiString _result_plaintext = "Hello, World!";
  29. private static readonly AsciiString _cached_worlds = "/cached-worlds";
  30. private readonly static uint _jsonPayloadSize = (uint)System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(new JsonMessage { message = "Hello, World!" }, SerializerOptions).Length;
  31. private readonly static AsciiString _jsonPreamble =
  32. _httpsuccess
  33. + _headerContentTypeJson
  34. + _headerServer
  35. + _headerContentLength + _jsonPayloadSize.ToString() + _line;
  36. private readonly static AsciiString _plaintextPreamble =
  37. _httpsuccess
  38. + _headerContentTypeText
  39. + _headerServer
  40. + _headerContentLength + _result_plaintext.Length.ToString() + _line;
  41. private readonly static AsciiString _jsonResultPreamble =
  42. _httpsuccess
  43. + _headerContentTypeJson
  44. + _headerServer
  45. + _headerContentLength;
  46. private readonly static AsciiString _HtmlResultPreamble =
  47. _httpsuccess
  48. + _headerContentTypeHtml
  49. + _headerServer
  50. + _headerContentLength;
  51. private static byte _Space = 32;
  52. public const int _LengthSize = 8;
  53. private static byte _question = 63;
  54. public HttpHandler()
  55. {
  56. RequestDispatchs = new BeetleX.Dispatchs.DispatchCenter<HttpToken>(OnRequest, Math.Min(Environment.ProcessorCount, 16));
  57. }
  58. private BeetleX.Dispatchs.DispatchCenter<HttpToken> RequestDispatchs;
  59. public override void Connected(IServer server, ConnectedEventArgs e)
  60. {
  61. base.Connected(server, e);
  62. e.Session.Socket.NoDelay = true;
  63. var token = new HttpToken();
  64. token.ThreadDispatcher = RequestDispatchs.Next();
  65. token.Session = e.Session;
  66. token.Db = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  67. e.Session.Tag = token;
  68. }
  69. private int AnalysisUrl(ReadOnlySpan<byte> url)
  70. {
  71. for (int i = 0; i < url.Length; i++)
  72. {
  73. if (url[i] == _question)
  74. return i;
  75. }
  76. return -1;
  77. }
  78. private void OnRequest(HttpToken token)
  79. {
  80. if (token.Requests.TryDequeue(out RequestData result))
  81. {
  82. OnStartRequest(result, token.Session, token, token.Session.Stream.ToPipeStream());
  83. }
  84. }
  85. public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
  86. {
  87. base.SessionReceive(server, e);
  88. PipeStream pipeStream = e.Session.Stream.ToPipeStream();
  89. HttpToken token = (HttpToken)e.Session.Tag;
  90. var result = pipeStream.IndexOfLine();
  91. while (result.End != null)
  92. {
  93. if (result.Length == 2)
  94. {
  95. pipeStream.ReadFree(result.Length);
  96. OnStartRequest(token.CurrentRequest, e.Session, token, pipeStream);
  97. }
  98. else
  99. {
  100. if (token.CurrentRequest == null)
  101. {
  102. var request = new RequestData();
  103. byte[] buffer = null;
  104. buffer = new byte[result.Length];
  105. pipeStream.Read(buffer, 0, result.Length);
  106. request.Data = new ArraySegment<byte>(buffer, 0, result.Length);
  107. AnalysisAction(request);
  108. if (request.Action == ActionType.Plaintext)
  109. {
  110. token.CurrentRequest = request;
  111. }
  112. else
  113. {
  114. token.CurrentRequest = request;
  115. pipeStream.ReadFree((int)pipeStream.Length);
  116. OnStartRequest(request, e.Session, token, pipeStream);
  117. return;
  118. }
  119. }
  120. else
  121. {
  122. pipeStream.ReadFree(result.Length);
  123. }
  124. }
  125. if (pipeStream.Length > 0)
  126. result = pipeStream.IndexOfLine();
  127. else
  128. break;
  129. }
  130. }
  131. private void AnalysisAction(RequestData requestData)
  132. {
  133. var line = _line.AsSpan();
  134. int len = requestData.Data.Count;
  135. var receiveData = requestData.GetSpan();
  136. ReadOnlySpan<byte> http = line;
  137. ReadOnlySpan<byte> method = line;
  138. ReadOnlySpan<byte> url = line;
  139. int offset2 = 0;
  140. int count = 0;
  141. for (int i = 0; i < len; i++)
  142. {
  143. if (receiveData[i] == line[0])
  144. {
  145. http = receiveData.Slice(offset2, i - offset2);
  146. break;
  147. }
  148. else
  149. {
  150. if (receiveData[i] == _Space)
  151. {
  152. if (count != 0)
  153. {
  154. url = receiveData.Slice(offset2, i - offset2);
  155. offset2 = i + 1;
  156. }
  157. else
  158. {
  159. method = receiveData.Slice(offset2, i - offset2);
  160. offset2 = i + 1;
  161. count++;
  162. }
  163. }
  164. }
  165. }
  166. int queryIndex = AnalysisUrl(url);
  167. ReadOnlySpan<byte> baseUrl = default;
  168. ReadOnlySpan<byte> queryString = default;
  169. if (queryIndex > 0)
  170. {
  171. baseUrl = url.Slice(0, queryIndex);
  172. queryString = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
  173. requestData.QueryString = Encoding.ASCII.GetString(queryString);
  174. }
  175. else
  176. {
  177. baseUrl = url;
  178. }
  179. if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
  180. {
  181. requestData.Action = ActionType.Plaintext;
  182. }
  183. else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
  184. {
  185. requestData.Action = ActionType.Json;
  186. }
  187. else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
  188. {
  189. requestData.Action = ActionType.Db;
  190. }
  191. else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
  192. {
  193. requestData.Action = ActionType.Queries;
  194. }
  195. else if (baseUrl.Length == _cached_worlds.Length && baseUrl.StartsWith(_cached_worlds))
  196. {
  197. requestData.Action = ActionType.Caching;
  198. }
  199. else if (baseUrl.Length == _path_Updates.Length && baseUrl.StartsWith(_path_Updates))
  200. {
  201. requestData.Action = ActionType.Updates;
  202. }
  203. else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
  204. {
  205. requestData.Action = ActionType.Fortunes;
  206. }
  207. else
  208. {
  209. requestData.Action = ActionType.Other;
  210. }
  211. }
  212. public virtual async Task OnStartRequest(RequestData data, ISession session, HttpToken token, PipeStream stream)
  213. {
  214. ActionType type = data.Action;
  215. if (type == ActionType.Plaintext)
  216. {
  217. await Plaintext(stream, token, session);
  218. }
  219. else if (type == ActionType.Json)
  220. {
  221. await Json(stream, token, session);
  222. }
  223. else if (type == ActionType.Db)
  224. {
  225. await db(stream, token, session);
  226. }
  227. else if (type == ActionType.Queries)
  228. {
  229. await queries(data.QueryString, stream, token, session);
  230. }
  231. else if (type == ActionType.Caching)
  232. {
  233. await caching(data.QueryString, stream, token, session);
  234. }
  235. else if (type == ActionType.Updates)
  236. {
  237. await updates(data.QueryString, stream, token, session);
  238. }
  239. else if (type == ActionType.Fortunes)
  240. {
  241. await fortunes(stream, token, session);
  242. }
  243. else
  244. {
  245. await Default(stream, token, session);
  246. }
  247. }
  248. private void OnCompleted(PipeStream stream, ISession session, HttpToken token)
  249. {
  250. var type = token.CurrentRequest.Action;
  251. if (type != ActionType.Plaintext && type != ActionType.Json)
  252. {
  253. token.FullLength((stream.CacheLength - token.ContentPostion).ToString());
  254. }
  255. if (token.Requests.IsEmpty && stream.Length == 0)
  256. session.Stream.Flush();
  257. token.CurrentRequest = null;
  258. }
  259. }
  260. }