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.

180 lines
4.7 KiB

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