You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
4.8 KiB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. #if NETFX
  5. using System.Web;
  6. #endif
  7. namespace Apewer.Web
  8. {
  9. /// <summary></summary>
  10. public partial class ApiProgram
  11. {
  12. private static ApiInvoker _invoker = new ApiInvoker() { Logger = new Logger(), Options = new ApiOptions() };
  13. /// <summary>调用器。</summary>
  14. public static ApiInvoker Invoker { get => _invoker; }
  15. /// <summary>API 选项。</summary>
  16. public static ApiOptions Options { get => _invoker.Options; }
  17. /// <summary>日志记录器。</summary>
  18. public static Logger Logger { get => _invoker.Logger; }
  19. /// <summary>获取或设置 API 入口。</summary>
  20. public static ApiEntries Entries { get => _invoker.Entries; set => _invoker.Entries = value; }
  21. private Action _initializer = null;
  22. /// <summary></summary>
  23. public ApiProgram(Action initializer = null)
  24. {
  25. _initializer = initializer;
  26. }
  27. }
  28. // Listener
  29. public partial class ApiProgram
  30. {
  31. static HttpListener _listener = null;
  32. /// <summary>在后台启动 Listener,返回错误消息。</summary>
  33. public static string Listener(string prefix)
  34. {
  35. if (_listener != null) return "无法重复启动默认 Listener 实例。";
  36. _listener = new HttpListener();
  37. _listener.Prefix = prefix;
  38. _listener.Action = (context) => _invoker.Invoke(new HttpListenerProvider(context));
  39. var exception = _listener.Start();
  40. if (exception != null) return exception.Message();
  41. return null;
  42. }
  43. }
  44. // Mini
  45. public partial class ApiProgram
  46. {
  47. static MiniServer _mini = null;
  48. /// <summary>启动 Mini 服务器,返回错误消息。</summary>
  49. public static string Mini(int port = 80, bool await = true)
  50. {
  51. if (_mini != null) return "无法重复启动实例。";
  52. _mini = new MiniServer();
  53. _mini.Handler = context => _invoker.Invoke(new MiniProvider(context));
  54. _mini.Run(port, await);
  55. return null;
  56. }
  57. }
  58. #if NETCORE
  59. // Kestrel & HTTP.Sys
  60. public partial class ApiProgram
  61. {
  62. static AspNetCoreServer _aspnetcore = null;
  63. class Startup : AspNetCoreStartup
  64. {
  65. public override void OnContext(Microsoft.AspNetCore.Http.HttpContext context)
  66. {
  67. if (_invoker == null) return;
  68. _invoker.Invoke(new AspNetCoreProvider(context));
  69. }
  70. public override bool UseCompression => Options == null ? false : Options.AllowCompression;
  71. }
  72. /// <summary>启动 Kestrel 服务器。</summary>
  73. public static void Kestrel(int port = 80)
  74. {
  75. if (_aspnetcore != null) return;
  76. var server = new KestrelServer();
  77. server.Port = port;
  78. server.Run<Startup>();
  79. _aspnetcore = server;
  80. }
  81. /// <summary>启动 HTTP.sys 服务器。</summary>
  82. public static void HttpSys(int port = 80, string ip = null)
  83. {
  84. if (_aspnetcore != null) return;
  85. var server = new HttpSysServer();
  86. server.Port = port;
  87. server.Run<Startup>();
  88. _aspnetcore = server;
  89. }
  90. }
  91. #endif
  92. #if NETFX
  93. // IIS
  94. public partial class ApiProgram : IHttpHandler, IHttpModule
  95. {
  96. #region IHttpHandler
  97. static bool _initialized = false;
  98. /// <summary></summary>
  99. public bool IsReusable { get { return false; } }
  100. /// <summary></summary>
  101. public void ProcessRequest(HttpContext context)
  102. {
  103. if (!_initialized)
  104. {
  105. _initialized = true;
  106. _initializer?.Invoke();
  107. }
  108. _invoker.Invoke(new WebsiteProvider(context));
  109. }
  110. #endregion
  111. #region IHttpModule
  112. /// <summary></summary>
  113. public void Dispose() { }
  114. /// <summary></summary>
  115. public void Init(HttpApplication application)
  116. {
  117. application.PreSendRequestHeaders += (s, e) =>
  118. {
  119. // 从 Response 头中移除 Server 和 X-Powered-By 属性。
  120. var response = application.Response;
  121. var keys = new List<string>(response.Headers.AllKeys);
  122. if (keys.Contains("Server")) response.Headers.Remove("Server");
  123. if (keys.Contains("X-Powered-By")) response.Headers.Remove("X-Powered-By");
  124. };
  125. }
  126. IAsyncResult BeginEventHandler(object sender, EventArgs e, AsyncCallback cb, object extraData)
  127. {
  128. return null;
  129. }
  130. void EndEventHandler(IAsyncResult ar)
  131. {
  132. }
  133. #endregion
  134. }
  135. #endif
  136. }