HttpSocketClient.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 TouchSocket.Core;
  14. using TouchSocket.Sockets;
  15. namespace TouchSocket.Http
  16. {
  17. /// <summary>
  18. /// http辅助类
  19. /// </summary>
  20. public class HttpSocketClient : SocketClient, IHttpSocketClient
  21. {
  22. /// <summary>
  23. /// 构造函数
  24. /// </summary>
  25. public HttpSocketClient()
  26. {
  27. Protocol = Protocol.Http;
  28. }
  29. /// <summary>
  30. /// <inheritdoc/>
  31. /// </summary>
  32. /// <param name="e"></param>
  33. protected override void OnConnecting(OperationEventArgs e)
  34. {
  35. SetDataHandlingAdapter(new HttpServerDataHandlingAdapter());
  36. base.OnConnecting(e);
  37. }
  38. /// <summary>
  39. /// <inheritdoc/>
  40. /// </summary>
  41. /// <param name="byteBlock"></param>
  42. /// <param name="requestInfo"></param>
  43. protected override void HandleReceivedData(ByteBlock byteBlock, IRequestInfo requestInfo)
  44. {
  45. if (requestInfo is HttpRequest request)
  46. {
  47. OnReceivedHttpRequest(request);
  48. }
  49. }
  50. /// <summary>
  51. /// 当收到到Http请求时。覆盖父类方法将不会触发插件。
  52. /// </summary>
  53. protected virtual void OnReceivedHttpRequest(HttpRequest request)
  54. {
  55. HttpContextEventArgs args = new HttpContextEventArgs(new HttpContext(request));
  56. switch (request.Method)
  57. {
  58. case TouchSocketHttpUtility.Get:
  59. {
  60. PluginsManager.Raise<IHttpPlugin>("OnGet", this, args);
  61. break;
  62. }
  63. case TouchSocketHttpUtility.Post:
  64. {
  65. PluginsManager.Raise<IHttpPlugin>("OnPost", this, args);
  66. break;
  67. }
  68. case TouchSocketHttpUtility.Put:
  69. {
  70. PluginsManager.Raise<IHttpPlugin>("OnPut", this, args);
  71. break;
  72. }
  73. case TouchSocketHttpUtility.Delete:
  74. {
  75. PluginsManager.Raise<IHttpPlugin>("OnDelete", this, args);
  76. break;
  77. }
  78. default:
  79. PluginsManager.Raise<IHttpPlugin>("OnReceivedOtherHttpRequest", this, args);
  80. break;
  81. }
  82. }
  83. }
  84. }