HttpHandler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Buffers;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using ZYSocket;
  6. using ZYSocket.FiberStream;
  7. namespace PlatformBenchmarks
  8. {
  9. public partial class HttpHandler
  10. {
  11. private static AsciiString _line = new AsciiString("\r\n");
  12. private static AsciiString _2line = new AsciiString("\r\n\r\n");
  13. private static AsciiString _httpsuccess = new AsciiString("HTTP/1.1 200 OK\r\n");
  14. private static readonly AsciiString _headerServer = "Server: zysocket\r\n";
  15. private static readonly AsciiString _headerContentLength = "Content-Length: ";
  16. private static readonly AsciiString _headerContentLengthZero = "Content-Length: 0\r\n";
  17. private static readonly AsciiString _headerContentTypeText = "Content-Type: text/plain\r\n";
  18. private static readonly AsciiString _headerContentTypeHtml = "Content-Type: text/html; charset=UTF-8\r\n";
  19. private static readonly AsciiString _headerContentTypeJson = "Content-Type: application/json\r\n";
  20. private static readonly AsciiString _path_Json = "/json";
  21. private static readonly AsciiString _path_Db = "/db";
  22. private static readonly AsciiString _path_Queries = "/queries";
  23. private static readonly AsciiString _path_Plaintext = "/plaintext";
  24. private static readonly AsciiString _path_Fortunes = "/fortunes";
  25. private static readonly AsciiString _result_plaintext = "Hello, World!";
  26. private static readonly byte[] LenData = new byte[10] { 32, 32, 32, 32, 32, 32, 32, 32, 32, 32 };
  27. private static byte _Space = 32;
  28. private static byte _question = 63;
  29. public HttpHandler()
  30. {
  31. }
  32. public void Default(IFiberRw<HttpToken> fiberRw,ref WriteBytes write)
  33. {
  34. write.Write("<b> zysocket server</b><hr/>");
  35. write.Write($"error not found!");
  36. OnCompleted(fiberRw, write);
  37. }
  38. private async void OnCompleted(IFiberRw<HttpToken> fiberRw, WriteBytes write)
  39. {
  40. Task<int> WSend()
  41. {
  42. var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;
  43. write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
  44. write.Write(length.ToString(), false);
  45. write.Flush(false);
  46. return fiberRw.Flush();
  47. }
  48. if (fiberRw.UserToken != null)
  49. await await fiberRw.Sync.Ask(WSend);
  50. }
  51. private int AnalysisUrl(ReadOnlySpan<byte> url)
  52. {
  53. for (int i = 0; i < url.Length; i++)
  54. {
  55. if (url[i] == _question)
  56. return i;
  57. }
  58. return -1;
  59. }
  60. public async Task Receive(IFiberRw<HttpToken> fiberRw, Memory<byte> memory_r, WriteBytes write)
  61. {
  62. var data = (await fiberRw.ReadLine(memory_r));
  63. ReadHander(fiberRw,ref data,ref write);
  64. fiberRw.StreamReadFormat.Position = fiberRw.StreamReadFormat.Length;
  65. }
  66. private void ReadHander(IFiberRw<HttpToken> fiberRw,ref Memory<byte> linedata,ref WriteBytes write)
  67. {
  68. var token = fiberRw.UserToken;
  69. ReadOnlySpan<byte> line = linedata.Span;
  70. ReadOnlySpan<byte> url = line;
  71. int offset2 = 0;
  72. int count = 0;
  73. for (int i = 0; i < line.Length; i++)
  74. {
  75. if (line[i] == _Space)
  76. {
  77. if (count != 0)
  78. {
  79. url = line.Slice(offset2, i - offset2);
  80. offset2 = i + 1;
  81. break;
  82. }
  83. offset2 = i + 1;
  84. count++;
  85. }
  86. }
  87. int queryIndex = AnalysisUrl(url);
  88. ReadOnlySpan<byte> queryString = default;
  89. ReadOnlySpan<byte> baseUrl;
  90. if (queryIndex > 0)
  91. {
  92. baseUrl = url.Slice(0, queryIndex);
  93. queryString = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
  94. }
  95. else
  96. {
  97. baseUrl = url;
  98. }
  99. OnWriteHeader(ref write);
  100. if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
  101. {
  102. write.Write(_headerContentTypeText.Data, 0, _headerContentTypeText.Length);
  103. OnWriteContentLength(write, token);
  104. Plaintext(fiberRw, ref write);
  105. }
  106. else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
  107. {
  108. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  109. OnWriteContentLength(write, token);
  110. Json(fiberRw, ref write);
  111. }
  112. else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
  113. {
  114. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  115. OnWriteContentLength(write, token);
  116. db(fiberRw, write);
  117. }
  118. else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
  119. {
  120. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  121. OnWriteContentLength(write, token);
  122. queries(Encoding.ASCII.GetString(queryString),fiberRw, write);
  123. }
  124. else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
  125. {
  126. write.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  127. OnWriteContentLength(write, token);
  128. fortunes(fiberRw, write);
  129. }
  130. else
  131. {
  132. write.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  133. OnWriteContentLength(write, token);
  134. Default( fiberRw, ref write);
  135. }
  136. }
  137. private void OnWriteHeader(ref WriteBytes write)
  138. {
  139. write.Write(_httpsuccess.Data, 0, _httpsuccess.Length);
  140. write.Write(_headerServer.Data, 0, _headerServer.Length);
  141. ArraySegment<byte> date = GMTDate.Default.DATE;
  142. write.Write(date.Array, date.Offset, date.Count);
  143. }
  144. private void OnWriteContentLength(WriteBytes write, HttpToken token)
  145. {
  146. write.Write(_headerContentLength.Data, 0, _headerContentLength.Length);
  147. token.ContentPostion = write.Allocate(LenData);
  148. write.Write(_2line, 0, 4);
  149. token.HttpHandlerPostion = (int)write.Stream.Position;
  150. }
  151. }
  152. }