DefaultHttpServicePlugin.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. using TouchSocket.Sockets;
  15. namespace TouchSocket.Http
  16. {
  17. /// <summary>
  18. /// 默认的Http服务。为Http做兜底拦截。该插件应该最后添加。
  19. /// </summary>
  20. public class DefaultHttpServicePlugin : HttpPluginBase<HttpSocketClient>
  21. {
  22. /// <summary>
  23. /// 默认的Http服务。为Http做兜底拦截。该插件应该最后添加。
  24. /// </summary>
  25. public DefaultHttpServicePlugin()
  26. {
  27. Order = int.MinValue;
  28. }
  29. /// <summary>
  30. /// <inheritdoc/>
  31. /// </summary>
  32. /// <param name="sender"></param>
  33. /// <param name="e"></param>
  34. /// <exception cref="Exception"></exception>
  35. protected override void OnLoadingConfig(object sender, ConfigEventArgs e)
  36. {
  37. if (!(sender is IService))
  38. {
  39. throw new Exception("该插件仅可用于服务器。");
  40. }
  41. base.OnLoadingConfig(sender, e);
  42. }
  43. /// <summary>
  44. /// <inheritdoc/>
  45. /// </summary>
  46. /// <param name="client"></param>
  47. /// <param name="e"></param>
  48. protected override void OnGet(HttpSocketClient client, HttpContextEventArgs e)
  49. {
  50. e.Context.Response.UrlNotFind().Answer();
  51. base.OnGet(client, e);
  52. }
  53. /// <summary>
  54. /// <inheritdoc/>
  55. /// </summary>
  56. /// <param name="client"></param>
  57. /// <param name="e"></param>
  58. protected override void OnPost(HttpSocketClient client, HttpContextEventArgs e)
  59. {
  60. e.Context.Response.UrlNotFind().Answer();
  61. base.OnPost(client, e);
  62. }
  63. /// <summary>
  64. /// <inheritdoc/>
  65. /// </summary>
  66. /// <param name="client"></param>
  67. /// <param name="e"></param>
  68. protected override void OnPut(HttpSocketClient client, HttpContextEventArgs e)
  69. {
  70. e.Context.Response.UrlNotFind().Answer();
  71. base.OnPut(client, e);
  72. }
  73. /// <summary>
  74. /// <inheritdoc/>
  75. /// </summary>
  76. /// <param name="client"></param>
  77. /// <param name="e"></param>
  78. protected override void OnDelete(HttpSocketClient client, HttpContextEventArgs e)
  79. {
  80. e.Context.Response.UrlNotFind().Answer();
  81. base.OnDelete(client, e);
  82. }
  83. /// <summary>
  84. /// <inheritdoc/>
  85. /// </summary>
  86. /// <param name="client"></param>
  87. /// <param name="e"></param>
  88. protected override void OnReceivedOtherHttpRequest(HttpSocketClient client, HttpContextEventArgs e)
  89. {
  90. switch (e.Context.Request.Method.ToUpper())
  91. {
  92. case "OPTIONS":
  93. {
  94. e.Context.Response
  95. .SetStatus()
  96. .SetHeader("Access-Control-Allow-Origin", "*")
  97. .SetHeader("Access-Control-Allow-Headers", "*")
  98. .SetHeader("Allow", "OPTIONS, GET, POST")
  99. .SetHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
  100. .Answer();
  101. break;
  102. }
  103. default:
  104. e.Context.Response.UrlNotFind().Answer();
  105. break;
  106. }
  107. base.OnReceivedOtherHttpRequest(client, e);
  108. }
  109. }
  110. }