HttpHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.IndexOfLine();
  76. while (result.End != null)
  77. {
  78. if (result.Length == 2)
  79. {
  80. pipeStream.ReadFree(result.Length);
  81. if (Program.Debug)
  82. {
  83. if (token.CurrentRequest != null)
  84. {
  85. token.Requests.Enqueue(token.CurrentRequest);
  86. token.CurrentRequest = null;
  87. token.ThreadDispatcher.Enqueue(token);
  88. }
  89. }
  90. else
  91. {
  92. OnStartRequest(token.CurrentRequest, e.Session, token, pipeStream);
  93. }
  94. }
  95. else
  96. {
  97. if (token.CurrentRequest == null)
  98. {
  99. var request = new RequestData();
  100. byte[] buffer = null;
  101. buffer = new byte[result.Length];
  102. pipeStream.Read(buffer, 0, result.Length);
  103. request.Data = new ArraySegment<byte>(buffer, 0, result.Length);
  104. AnalysisAction(request);
  105. if (request.Action == ActionType.Plaintext)
  106. {
  107. token.CurrentRequest = request;
  108. }
  109. else
  110. {
  111. pipeStream.ReadFree((int)pipeStream.Length);
  112. OnStartRequest(request, e.Session, token, pipeStream);
  113. return;
  114. }
  115. }
  116. else
  117. {
  118. pipeStream.ReadFree(result.Length);
  119. }
  120. }
  121. if (pipeStream.Length > 0)
  122. result = pipeStream.IndexOfLine();
  123. else
  124. break;
  125. }
  126. }
  127. private void AnalysisAction(RequestData requestData)
  128. {
  129. var line = _line.AsSpan();
  130. int len = requestData.Data.Count;
  131. var receiveData = requestData.GetSpan();
  132. ReadOnlySpan<byte> http = line;
  133. ReadOnlySpan<byte> method = line;
  134. ReadOnlySpan<byte> url = line;
  135. int offset2 = 0;
  136. int count = 0;
  137. for (int i = 0; i < len; i++)
  138. {
  139. if (receiveData[i] == line[0])
  140. {
  141. http = receiveData.Slice(offset2, i - offset2);
  142. break;
  143. }
  144. else
  145. {
  146. if (receiveData[i] == _Space)
  147. {
  148. if (count != 0)
  149. {
  150. url = receiveData.Slice(offset2, i - offset2);
  151. offset2 = i + 1;
  152. }
  153. else
  154. {
  155. method = receiveData.Slice(offset2, i - offset2);
  156. offset2 = i + 1;
  157. count++;
  158. }
  159. }
  160. }
  161. }
  162. int queryIndex = AnalysisUrl(url);
  163. ReadOnlySpan<byte> baseUrl = default;
  164. ReadOnlySpan<byte> queryString = default;
  165. if (queryIndex > 0)
  166. {
  167. baseUrl = url.Slice(0, queryIndex);
  168. queryString = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
  169. requestData.QueryString = Encoding.ASCII.GetString(queryString);
  170. }
  171. else
  172. {
  173. baseUrl = url;
  174. }
  175. if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
  176. {
  177. requestData.Action = ActionType.Plaintext;
  178. }
  179. else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
  180. {
  181. requestData.Action = ActionType.Json;
  182. }
  183. else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
  184. {
  185. requestData.Action = ActionType.Db;
  186. }
  187. else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
  188. {
  189. requestData.Action = ActionType.Queries;
  190. }
  191. else if (baseUrl.Length == _cached_worlds.Length && baseUrl.StartsWith(_cached_worlds))
  192. {
  193. requestData.Action = ActionType.Caching;
  194. }
  195. else if (baseUrl.Length == _path_Updates.Length && baseUrl.StartsWith(_path_Updates))
  196. {
  197. requestData.Action = ActionType.Updates;
  198. }
  199. else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
  200. {
  201. requestData.Action = ActionType.Fortunes;
  202. }
  203. else
  204. {
  205. requestData.Action = ActionType.Other;
  206. }
  207. }
  208. public virtual async Task OnStartRequest(RequestData data, ISession session, HttpToken token, PipeStream stream)
  209. {
  210. OnWriteHeader(stream, token);
  211. ActionType type = data.Action;
  212. if (type == ActionType.Plaintext)
  213. {
  214. stream.Write(_headerContentTypeText.Data, 0, _headerContentTypeText.Length);
  215. OnWriteContentLength(stream, token);
  216. await Plaintext(stream, token, session);
  217. }
  218. else if (type == ActionType.Json)
  219. {
  220. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  221. OnWriteContentLength(stream, token);
  222. await Json(stream, token, session);
  223. }
  224. else if (type == ActionType.Db)
  225. {
  226. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  227. OnWriteContentLength(stream, token);
  228. await db(stream, token, session);
  229. }
  230. else if (type == ActionType.Queries)
  231. {
  232. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  233. OnWriteContentLength(stream, token);
  234. await queries(data.QueryString, stream, token, session);
  235. }
  236. else if (type == ActionType.Caching)
  237. {
  238. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  239. OnWriteContentLength(stream, token);
  240. await caching(data.QueryString, stream, token, session);
  241. }
  242. else if (type == ActionType.Updates)
  243. {
  244. stream.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  245. OnWriteContentLength(stream, token);
  246. await updates(data.QueryString, stream, token, session);
  247. }
  248. else if (type == ActionType.Fortunes)
  249. {
  250. stream.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  251. OnWriteContentLength(stream, token);
  252. await fortunes(stream, token, session);
  253. }
  254. else
  255. {
  256. stream.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  257. OnWriteContentLength(stream, token);
  258. await Default(stream, token, session);
  259. }
  260. }
  261. private void OnWriteHeader(PipeStream stream, HttpToken token)
  262. {
  263. stream.Write(_httpsuccess.Data, 0, _httpsuccess.Length);
  264. stream.Write(_headerServer.Data, 0, _headerServer.Length);
  265. ArraySegment<byte> date = GMTDate.Default.DATE;
  266. stream.Write(date.Array, date.Offset, date.Count);
  267. }
  268. private void OnWriteContentLength(PipeStream stream, HttpToken token)
  269. {
  270. stream.Write(_headerContentLength.Data, 0, _headerContentLength.Length);
  271. token.ContentLength = stream.Allocate(10);
  272. stream.Write(_2line, 0, 4);
  273. token.ContentPostion = stream.CacheLength;
  274. }
  275. private void OnCompleted(PipeStream stream, ISession session, HttpToken token)
  276. {
  277. token.FullLength((stream.CacheLength - token.ContentPostion).ToString());
  278. if (token.Requests.IsEmpty && stream.Length == 0)
  279. session.Stream.Flush();
  280. }
  281. }
  282. }