HttpContext.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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;
  14. namespace TouchSocket.Http
  15. {
  16. /// <summary>
  17. /// Http上下文
  18. /// </summary>
  19. public class HttpContext
  20. {
  21. private HttpResponse m_response;
  22. /// <summary>
  23. /// 构造函数
  24. /// </summary>
  25. /// <param name="request"></param>
  26. public HttpContext(HttpRequest request)
  27. {
  28. Request = request ?? throw new ArgumentNullException(nameof(request));
  29. }
  30. /// <summary>
  31. /// 构造函数
  32. /// </summary>
  33. /// <param name="request"></param>
  34. /// <param name="response"></param>
  35. public HttpContext(HttpRequest request, HttpResponse response)
  36. {
  37. Request = request ?? throw new ArgumentNullException(nameof(request));
  38. m_response = response ?? throw new ArgumentNullException(nameof(response));
  39. }
  40. /// <summary>
  41. /// Http请求
  42. /// </summary>
  43. public HttpRequest Request { get; }
  44. /// <summary>
  45. /// Http响应
  46. /// </summary>
  47. public HttpResponse Response
  48. {
  49. get
  50. {
  51. lock (this)
  52. {
  53. if (m_response == null)
  54. {
  55. m_response = new HttpResponse(Request.Client, true);
  56. }
  57. return m_response;
  58. }
  59. }
  60. }
  61. }
  62. }