HttpBase.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //------------------------------------------------------------------------------
  2. // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
  3. // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
  4. // CSDN博客:https://blog.csdn.net/qq_40374647
  5. // 哔哩哔哩视频:https://space.bilibili.com/94253567
  6. // Gitee源代码仓库:https://gitee.com/RRQM_Home
  7. // Github源代码仓库:https://github.com/RRQM
  8. // API首页:https://www.yuque.com/rrqm/touchsocket/index
  9. // 交流QQ群:234762506
  10. // 感谢您的下载和使用
  11. //------------------------------------------------------------------------------
  12. //------------------------------------------------------------------------------
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Text.RegularExpressions;
  18. using TouchSocket.Core;
  19. using TouchSocket.Sockets;
  20. namespace TouchSocket.Http
  21. {
  22. /// <summary>
  23. /// Http基础头部
  24. /// </summary>
  25. public abstract class HttpBase : BlockReader, IRequestInfo
  26. {
  27. /// <summary>
  28. /// 服务器版本
  29. /// </summary>
  30. public static readonly string ServerVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  31. /// <summary>
  32. /// 内容长度
  33. /// </summary>
  34. protected long m_contentLength;
  35. private static readonly byte[] m_rnrnCode = Encoding.UTF8.GetBytes("\r\n\r\n");
  36. private IgnoreCaseNameValueCollection m_headers;
  37. /// <summary>
  38. /// 构造函数
  39. /// </summary>
  40. public HttpBase()
  41. {
  42. ReadTimeout = 1000 * 30;
  43. }
  44. /// <summary>
  45. /// 能否写入。
  46. /// </summary>
  47. public abstract bool CanWrite { get; }
  48. /// <summary>
  49. /// 客户端
  50. /// </summary>
  51. public abstract ITcpClientBase Client { get; }
  52. /// <summary>
  53. /// 内容填充完成
  54. /// </summary>
  55. public bool? ContentComplated { get; protected set; } = null;
  56. /// <summary>
  57. /// int类型,内容长度
  58. /// </summary>
  59. public int ContentLen
  60. {
  61. get => (int)m_contentLength;
  62. set => m_contentLength = value;
  63. }
  64. /// <summary>
  65. /// 内容长度
  66. /// </summary>
  67. public long ContentLength
  68. {
  69. get => m_contentLength;
  70. set => m_contentLength = value;
  71. }
  72. /// <summary>
  73. /// 内容类型
  74. /// </summary>
  75. public string ContentType { get; set; }
  76. /// <summary>
  77. /// 传递标识
  78. /// </summary>
  79. public object Flag { get; set; }
  80. /// <summary>
  81. /// 请求头集合
  82. /// </summary>
  83. public IgnoreCaseNameValueCollection Headers
  84. {
  85. get
  86. {
  87. m_headers ??= new IgnoreCaseNameValueCollection();
  88. return m_headers;
  89. }
  90. }
  91. /// <summary>
  92. /// 协议名称,默认HTTP
  93. /// </summary>
  94. public string Protocols { get; set; } = "HTTP";
  95. /// <summary>
  96. /// HTTP协议版本,默认1.1
  97. /// </summary>
  98. public string ProtocolVersion { get; set; } = "1.1";
  99. /// <summary>
  100. /// 请求行
  101. /// </summary>
  102. public string RequestLine { get; private set; }
  103. /// <summary>
  104. /// 获取头值
  105. /// </summary>
  106. /// <param name="fieldName"></param>
  107. /// <returns></returns>
  108. public string GetHeader(string fieldName)
  109. {
  110. return GetHeaderByKey(fieldName);
  111. }
  112. /// <summary>
  113. /// 获取头集合的值
  114. /// </summary>
  115. /// <param name="header"></param>
  116. /// <returns></returns>
  117. public string GetHeader(HttpHeaders header)
  118. {
  119. var fieldName = header.GetDescription();
  120. if (fieldName == null) return null;
  121. return Headers.Get(fieldName);
  122. }
  123. /// <summary>
  124. /// <inheritdoc/>
  125. /// </summary>
  126. /// <param name="byteBlock"></param>
  127. /// <param name="length"></param>
  128. /// <returns></returns>
  129. public bool ParsingHeader(ByteBlock byteBlock, int length)
  130. {
  131. int index = byteBlock.Buffer.IndexOfFirst(byteBlock.Pos, length, m_rnrnCode);
  132. if (index > 0)
  133. {
  134. int headerLength = index - byteBlock.Pos;
  135. ReadHeaders(byteBlock.Buffer, byteBlock.Pos, headerLength);
  136. byteBlock.Pos += headerLength;
  137. return true;
  138. }
  139. else
  140. {
  141. return false;
  142. }
  143. }
  144. /// <summary>
  145. /// 从Request中持续读取数据。
  146. /// </summary>
  147. /// <param name="buffer"></param>
  148. /// <param name="offset"></param>
  149. /// <param name="count"></param>
  150. /// <returns></returns>
  151. public override int Read(byte[] buffer, int offset, int count)
  152. {
  153. return base.Read(buffer, offset, count);
  154. }
  155. /// <summary>
  156. /// 从内存中读取
  157. /// </summary>
  158. /// <param name="buffer"></param>
  159. /// <param name="offset"></param>
  160. /// <param name="length"></param>
  161. public void ReadHeaders(byte[] buffer, int offset, int length)
  162. {
  163. string data = Encoding.UTF8.GetString(buffer, offset, length);
  164. string[] rows = Regex.Split(data, "\r\n");
  165. //Request URL & Method & Version
  166. RequestLine = rows[0];
  167. //Request Headers
  168. GetRequestHeaders(rows);
  169. long.TryParse(GetHeader(HttpHeaders.ContentLength), out m_contentLength);
  170. LoadHeaderProterties();
  171. }
  172. /// <summary>
  173. /// 设置一次性内容
  174. /// </summary>
  175. /// <param name="content"></param>
  176. /// <returns></returns>
  177. public abstract void SetContent(byte[] content);
  178. /// <summary>
  179. /// 设置请求头
  180. /// </summary>
  181. /// <param name="fieldName"></param>
  182. /// <param name="value"></param>
  183. public HttpBase SetHeaderByKey(string fieldName, string value)
  184. {
  185. if (string.IsNullOrEmpty(fieldName)) return this;
  186. Headers.Add(fieldName, value);
  187. return this;
  188. }
  189. /// <summary>
  190. /// 获取一次性内容。
  191. /// </summary>
  192. /// <returns></returns>
  193. public abstract bool TryGetContent(out byte[] content);
  194. /// <summary>
  195. /// 持续写入内容。
  196. /// </summary>
  197. /// <param name="buffer"></param>
  198. /// <param name="offset"></param>
  199. /// <param name="count"></param>
  200. public abstract void WriteContent(byte[] buffer, int offset, int count);
  201. internal bool InternalInput(byte[] buffer, int offset, int length)
  202. {
  203. return Input(buffer, offset, length);
  204. }
  205. /// <summary>
  206. /// <inheritdoc/>
  207. /// </summary>
  208. /// <param name="disposing"></param>
  209. protected override void Dispose(bool disposing)
  210. {
  211. if (!DisposedValue && CanRead)
  212. {
  213. TryGetContent(out _);
  214. }
  215. base.Dispose(disposing);
  216. }
  217. /// <summary>
  218. /// 读取信息
  219. /// </summary>
  220. protected abstract void LoadHeaderProterties();
  221. private string GetHeaderByKey(string fieldName)
  222. {
  223. if (string.IsNullOrEmpty(fieldName)) return null;
  224. return Headers.Get(fieldName);
  225. }
  226. private void GetRequestHeaders(IEnumerable<string> rows)
  227. {
  228. if (rows == null || rows.Count() <= 0)
  229. {
  230. return;
  231. }
  232. foreach (var item in rows)
  233. {
  234. string[] kv = item.SplitFirst(':');
  235. if (kv.Length == 2)
  236. {
  237. string key = kv[0].ToLower();
  238. Headers.Add(key, kv[1]);
  239. }
  240. }
  241. ContentType = GetHeader(HttpHeaders.ContentType);
  242. }
  243. }
  244. }