HttpHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 AsciiString _line = new AsciiString("\r\n");
  14. private static AsciiString _2line = new AsciiString("\r\n\r\n");
  15. private static AsciiString _httpsuccess = new AsciiString("HTTP/1.1 200 OK\r\n");
  16. private static readonly AsciiString _headerServer = "Server: TFB\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 static byte _Space = 32;
  31. private static byte _question = 63;
  32. public HttpHandler()
  33. {
  34. RequestDispatchs = new BeetleX.Dispatchs.DispatchCenter<HttpToken>(OnRequest, Math.Min(Environment.ProcessorCount, 16));
  35. }
  36. private BeetleX.Dispatchs.DispatchCenter<HttpToken> RequestDispatchs;
  37. public Task Default(PipeStream stream, HttpToken token, ISession session)
  38. {
  39. stream.Write("<b> beetlex server</b><hr/>");
  40. stream.Write("path not found!");
  41. OnCompleted(stream, session, token);
  42. return Task.CompletedTask;
  43. }
  44. public override void Connected(IServer server, ConnectedEventArgs e)
  45. {
  46. base.Connected(server, e);
  47. e.Session.Socket.NoDelay = true;
  48. var token = new HttpToken();
  49. token.ThreadDispatcher = RequestDispatchs.Next();
  50. token.Session = e.Session;
  51. token.Db = new RawDb(new ConcurrentRandom(), Npgsql.NpgsqlFactory.Instance);
  52. e.Session.Tag = token;
  53. }
  54. private int AnalysisUrl(ReadOnlySpan<byte> url)
  55. {
  56. for (int i = 0; i < url.Length; i++)
  57. {
  58. if (url[i] == _question)
  59. return i;
  60. }
  61. return -1;
  62. }
  63. private void OnRequest(HttpToken token)
  64. {
  65. if (token.Requests.TryDequeue(out RequestData result))
  66. {
  67. OnStartRequest(result, token.Session, token, token.Session.Stream.ToPipeStream());
  68. }
  69. }
  70. public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
  71. {
  72. base.SessionReceive(server, e);
  73. PipeStream pipeStream = e.Session.Stream.ToPipeStream();
  74. HttpToken token = (HttpToken)e.Session.Tag;
  75. var result = pipeStream.IndexOf(_line.Data);
  76. while (result.End != null)
  77. {
  78. if (result.Length == 2)
  79. {
  80. if (token.CurrentRequest != null)
  81. {
  82. token.Requests.Enqueue(token.CurrentRequest);
  83. token.CurrentRequest = null;
  84. token.ThreadDispatcher.Enqueue(token);
  85. }
  86. pipeStream.ReadFree(result.Length);
  87. }
  88. else
  89. {
  90. if (token.CurrentRequest == null)
  91. {
  92. var request = new RequestData();
  93. byte[] buffer = null;
  94. if (Program.Debug)
  95. buffer = new byte[result.Length];
  96. else
  97. buffer = System.Buffers.ArrayPool<byte>.Shared.Rent(result.Length);
  98. pipeStream.Read(buffer, 0, result.Length);
  99. request.Data = new ArraySegment<byte>(buffer, 0, result.Length);
  100. AnalysisAction(request);
  101. if (request.Action == ActionType.Plaintext)
  102. {
  103. token.CurrentRequest = request;
  104. }
  105. else
  106. {
  107. pipeStream.ReadFree((int)pipeStream.Length);
  108. OnStartRequest(request, e.Session, token, pipeStream);
  109. return;
  110. }
  111. }
  112. else
  113. {
  114. pipeStream.ReadFree(result.Length);
  115. }
  116. }
  117. if (pipeStream.Length > 0)
  118. result = pipeStream.IndexOf(_line.Data);
  119. else
  120. break;
  121. }
  122. }
  123. private void AnalysisAction(RequestData requestData)
  124. {
  125. var line = _line.AsSpan();
  126. int len = requestData.Data.Count;
  127. var receiveData = requestData.GetSpan();
  128. ReadOnlySpan<byte> http = line;
  129. ReadOnlySpan<byte> method = line;
  130. ReadOnlySpan<byte> url = line;
  131. int offset2 = 0;
  132. int count = 0;
  133. for (int i = 0; i < len; i++)
  134. {
  135. if (receiveData[i] == line[0])
  136. {
  137. http = receiveData.Slice(offset2, i - offset2);
  138. break;
  139. }
  140. else
  141. {
  142. if (receiveData[i] == _Space)
  143. {
  144. if (count != 0)
  145. {
  146. url = receiveData.Slice(offset2, i - offset2);
  147. offset2 = i + 1;
  148. }
  149. else
  150. {
  151. method = receiveData.Slice(offset2, i - offset2);
  152. offset2 = i + 1;
  153. count++;
  154. }
  155. }
  156. }
  157. }
  158. int queryIndex = AnalysisUrl(url);
  159. ReadOnlySpan<byte> baseUrl = default;
  160. ReadOnlySpan<byte> queryString = default;
  161. if (queryIndex > 0)
  162. {
  163. baseUrl = url.Slice(0, queryIndex);
  164. queryString = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
  165. requestData.QueryString = Encoding.ASCII.GetString(queryString);
  166. }
  167. else
  168. {
  169. baseUrl = url;
  170. }
  171. if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
  172. {
  173. requestData.Action = ActionType.Plaintext;
  174. }
  175. else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
  176. {
  177. requestData.Action = ActionType.Json;
  178. }
  179. else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
  180. {
  181. requestData.Action = ActionType.Db;
  182. }
  183. else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
  184. {
  185. requestData.Action = ActionType.Queries;
  186. }
  187. else if (baseUrl.Length == _cached_worlds.Length && baseUrl.StartsWith(_cached_worlds))
  188. {
  189. requestData.Action = ActionType.Caching;
  190. }
  191. else if (baseUrl.Length == _path_Updates.Length && baseUrl.StartsWith(_path_Updates))
  192. {
  193. requestData.Action = ActionType.Updates;
  194. }
  195. else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
  196. {
  197. requestData.Action = ActionType.Fortunes;
  198. }
  199. else
  200. {
  201. requestData.Action = ActionType.Other;
  202. }
  203. }
  204. public virtual async Task OnStartRequest(RequestData data, ISession session, HttpToken token, PipeStream stream)
  205. {
  206. OnWriteHeader(stream, token);
  207. ActionType type = data.Action;
  208. if (type == ActionType.Plaintext)
  209. {
  210. stream.Write(_headerContentTypeText.Data, 0, _headerContentTypeText.Length);
  211. OnWriteContentLength(stream, token);
  212. await Plaintext(stream, token, session);
  213. }
  214. else if (type == ActionType.Json)
  215. {
  216. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  217. OnWriteContentLength(stream, token);
  218. await Json(stream, token, session);
  219. }
  220. else if (type == ActionType.Db)
  221. {
  222. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  223. OnWriteContentLength(stream, token);
  224. await db(stream, token, session);
  225. }
  226. else if (type == ActionType.Queries)
  227. {
  228. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  229. OnWriteContentLength(stream, token);
  230. await queries(data.QueryString, stream, token, session);
  231. }
  232. else if (type == ActionType.Caching)
  233. {
  234. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  235. OnWriteContentLength(stream, token);
  236. await caching(data.QueryString, stream, token, session);
  237. }
  238. else if (type == ActionType.Updates)
  239. {
  240. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  241. OnWriteContentLength(stream, token);
  242. await updates(data.QueryString, stream, token, session);
  243. }
  244. else if (type == ActionType.Fortunes)
  245. {
  246. stream.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  247. OnWriteContentLength(stream, token);
  248. await fortunes(stream, token, session);
  249. }
  250. else
  251. {
  252. stream.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  253. OnWriteContentLength(stream, token);
  254. await Default(stream, token, session);
  255. }
  256. if (!Program.Debug)
  257. data.Dispose();
  258. }
  259. private void OnWriteHeader(PipeStream stream, HttpToken token)
  260. {
  261. stream.Write(_httpsuccess.Data, 0, _httpsuccess.Length);
  262. stream.Write(_headerServer.Data, 0, _headerServer.Length);
  263. ArraySegment<byte> date = GMTDate.Default.DATE;
  264. stream.Write(date.Array, date.Offset, date.Count);
  265. }
  266. private void OnWriteContentLength(PipeStream stream, HttpToken token)
  267. {
  268. stream.Write(_headerContentLength.Data, 0, _headerContentLength.Length);
  269. token.ContentLength = stream.Allocate(10);
  270. stream.Write(_2line, 0, 4);
  271. token.ContentPostion = stream.CacheLength;
  272. }
  273. private void OnCompleted(PipeStream stream, ISession session, HttpToken token)
  274. {
  275. token.FullLength((stream.CacheLength - token.ContentPostion).ToString());
  276. if (token.Requests.IsEmpty)
  277. session.Stream.Flush();
  278. }
  279. }
  280. }