HttpHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;
  37. write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
  38. write.Write(length.ToString(), false);
  39. write.Flush();
  40. }
  41. private async Task OnCompleted(IFiberRw<HttpToken> fiberRw, WriteBytes write)
  42. {
  43. Task<int> WSend()
  44. {
  45. var length = write.Stream.Length - fiberRw.UserToken.HttpHandlerPostion;
  46. write.Stream.Position = fiberRw.UserToken.ContentPostion.postion;
  47. write.Write(length.ToString(), false);
  48. write.Flush(false);
  49. return fiberRw.Flush();
  50. }
  51. if (fiberRw.UserToken != null)
  52. await await fiberRw.Sync.Ask(WSend);
  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. public async Task Receive(IFiberRw<HttpToken> fiberRw, Memory<byte> memory_r, WriteBytes write)
  64. {
  65. var data = (await fiberRw.ReadLine(memory_r));
  66. ReadHander(fiberRw,ref data,ref write);
  67. fiberRw.StreamReadFormat.Position = fiberRw.StreamReadFormat.Length;
  68. }
  69. private void ReadHander(IFiberRw<HttpToken> fiberRw,ref Memory<byte> linedata,ref WriteBytes write)
  70. {
  71. var token = fiberRw.UserToken;
  72. ReadOnlySpan<byte> line = linedata.Span;
  73. ReadOnlySpan<byte> url = line;
  74. int offset2 = 0;
  75. int count = 0;
  76. for (int i = 0; i < line.Length; i++)
  77. {
  78. if (line[i] == _Space)
  79. {
  80. if (count != 0)
  81. {
  82. url = line.Slice(offset2, i - offset2);
  83. break;
  84. }
  85. offset2 = i + 1;
  86. count++;
  87. }
  88. }
  89. int queryIndex = AnalysisUrl(url);
  90. ReadOnlySpan<byte> queryString = default;
  91. ReadOnlySpan<byte> baseUrl;
  92. if (queryIndex > 0)
  93. {
  94. baseUrl = url.Slice(0, queryIndex);
  95. queryString = url.Slice(queryIndex + 1, url.Length - queryIndex - 1);
  96. }
  97. else
  98. {
  99. baseUrl = url;
  100. }
  101. OnWriteHeader(ref write);
  102. if (baseUrl.Length == _path_Plaintext.Length && baseUrl.StartsWith(_path_Plaintext))
  103. {
  104. write.Write(_headerContentTypeText.Data, 0, _headerContentTypeText.Length);
  105. OnWriteContentLength(write, token);
  106. Plaintext(fiberRw, write);
  107. }
  108. else if (baseUrl.Length == _path_Json.Length && baseUrl.StartsWith(_path_Json))
  109. {
  110. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  111. OnWriteContentLength(write, token);
  112. Json(fiberRw, write);
  113. }
  114. else if (baseUrl.Length == _path_Db.Length && baseUrl.StartsWith(_path_Db))
  115. {
  116. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  117. OnWriteContentLength(write, token);
  118. db(fiberRw, write);
  119. }
  120. else if (baseUrl.Length == _path_Queries.Length && baseUrl.StartsWith(_path_Queries))
  121. {
  122. write.Write(_headerContentTypeJson.Data, 0, _headerContentTypeJson.Length);
  123. OnWriteContentLength(write, token);
  124. queries(Encoding.ASCII.GetString(queryString),fiberRw, write);
  125. }
  126. else if (baseUrl.Length == _path_Fortunes.Length && baseUrl.StartsWith(_path_Fortunes))
  127. {
  128. write.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  129. OnWriteContentLength(write, token);
  130. fortunes(fiberRw, write);
  131. }
  132. else
  133. {
  134. write.Write(_headerContentTypeHtml.Data, 0, _headerContentTypeHtml.Length);
  135. OnWriteContentLength(write, token);
  136. Default( fiberRw, ref write);
  137. }
  138. }
  139. private void OnWriteHeader(ref WriteBytes write)
  140. {
  141. write.Write(_httpsuccess.Data, 0, _httpsuccess.Length);
  142. write.Write(_headerServer.Data, 0, _headerServer.Length);
  143. ArraySegment<byte> date = GMTDate.Default.DATE;
  144. write.Write(date.Array, date.Offset, date.Count);
  145. }
  146. private void OnWriteContentLength(WriteBytes write, HttpToken token)
  147. {
  148. write.Write(_headerContentLength.Data, 0, _headerContentLength.Length);
  149. token.ContentPostion = write.Allocate(LenData);
  150. write.Write(_2line, 0, 4);
  151. token.HttpHandlerPostion = (int)write.Stream.Position;
  152. }
  153. }
  154. }