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.

155 lines
6.4 KiB

4 years ago
3 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
4 years ago
11 months ago
4 years ago
4 years ago
2 years ago
11 months ago
9 months ago
11 months ago
4 years ago
11 months ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Web;
  5. namespace Apewer.Web
  6. {
  7. /// <summary>选项。</summary>
  8. public sealed class ApiOptions
  9. {
  10. /// <summary>允许压缩。</summary>
  11. /// <remarks>默认值:不允许。</remarks>
  12. public bool AllowCompression { get; set; } = false;
  13. /// <summary>设置 Access-Control-Max-Age 的值。</summary>
  14. /// <remarks>默认值:60。</remarks>
  15. public int AccessControlMaxAge { get; set; } = 60;
  16. /// <summary>允许枚举输出 Applications 或 Functions。</summary>
  17. /// <remarks>默认值:不允许,不输出列表。</remarks>
  18. public bool AllowEnumerate { get; set; } = false;
  19. /// <summary>允许解析 favicon.ico 请求。</summary>
  20. /// <remarks>默认值:不允许,响应空。</remarks>
  21. public bool AllowFavIcon { get; set; } = false;
  22. /// <summary>允许解析 OPTIONS 请求。</summary>
  23. /// <remarks>默认值:不允许,直接返回 0 字节的 text/plain 内容。</remarks>
  24. public bool AllowOptions { get; set; } = false;
  25. /// <summary>允许解析 robots.txt 请求。</summary>
  26. /// <remarks>默认值:不允许,拒绝搜索引擎收录根目录。</remarks>
  27. public bool AllowRobots { get; set; } = false;
  28. // /// <summary>允许同步 IO。</summary>
  29. // /// <remarks>
  30. // /// <para>默认值:允许。</para>
  31. // /// <para>允许:使用同步方法写入 Response.Body,可能会导致线程不足而崩溃。</para>
  32. // /// <para>不允许:必须用异步方法写入 Response.Body。</para>
  33. // /// </remarks>
  34. // public bool AllowSynchronousIO { get; set; } = true;
  35. /// <summary>默认的结果渲染器。</summary>
  36. /// <remarks>默认值:NULL</remarks>
  37. public Action<ApiContext, object> DefaultRenderer { get; set; }
  38. /// <summary>对输出的 Json 对象缩进。</summary>
  39. /// <remarks>默认值:不缩进。</remarks>
  40. public bool JsonIndent { get; set; } = false;
  41. /// <summary>默认的结果渲染器。</summary>
  42. /// <remarks>默认值:NULL</remarks>
  43. public Action<ApiContext, Json> JsonRenderer { get; set; }
  44. /// <summary>限制最大请求的字节数。</summary>
  45. /// <remarks>默认值:-1,不使用 ApiOptions 限制。</remarks>
  46. public long MaxRequestBody { get; set; } = -1;
  47. /// <summary>输出前的检查。</summary>
  48. public ApiPreOutput PreOutput { get; set; }
  49. /// <summary>默认的结果渲染器。</summary>
  50. /// <remarks>默认值:NULL(返回值不为空时视为错误信息)</remarks>
  51. public Action<ApiContext, string> TextRenderer { get; set; }
  52. /// <summary>在响应头中设置 Content-Security-Policy,要求浏览器升级资源链接,使用 HTTPS。</summary>
  53. /// <remarks>默认值:不要求。在 HTTPS 页面中,不自动升级 HTTP 资源。</remarks>
  54. public bool UpgradeHttps { get; set; } = false;
  55. /// <summary>使用反射。</summary>
  56. /// <remarks>默认值:使用</remarks>
  57. public bool UseReflection { get; set; } = true;
  58. /// <summary>使用路由。</summary>
  59. /// <remarks>默认值:使用</remarks>
  60. public bool UseRoute { get; set; } = true;
  61. /// <summary>在响应中包含 Access-Control 属性。</summary>
  62. /// <remarks>默认值:不包含。</remarks>
  63. public bool WithAccessControl { get; set; } = false;
  64. /// <summary>在响应中包含时间属性。</summary>
  65. /// <remarks>默认值:不包含。</remarks>
  66. public bool WithClock { get; set; } = false;
  67. /// <summary>允许响应标头中包含 X-Content-Type-Options: nosiff。</summary>
  68. /// <remarks>默认值:不包含。当设置默认控制器时自动启用此属性。</remarks>
  69. public bool WithContentTypeOptions { get; set; } = false;
  70. /// <summary>在响应中包含执行 API 的持续时间。</summary>
  71. /// <remarks>默认值:不包含。</remarks>
  72. public bool WithDuration { get; set; } = false;
  73. /// <summary>允许响应中包含 Exception 对象的属性。</summary>
  74. /// <remarks>默认值:不允许。</remarks>
  75. public bool WithException { get; set; } = false;
  76. /// <summary>允许输出 Application 列表时包含模块名称。</summary>
  77. /// <remarks>默认值:不包含。</remarks>
  78. public bool WithModuleName { get; set; } = false;
  79. /// <summary>允许在枚举 Function 时包含参数信息。</summary>
  80. /// <remarks>默认值:不包含。</remarks>
  81. public bool WithParameters { get; set; } = false;
  82. /// <summary>在响应中包含 Application 和 Function 属性。</summary>
  83. /// <remarks>默认值:不包含。</remarks>
  84. public bool WithTarget { get; set; } = false;
  85. /// <summary>允许输出 Application 列表时包含类型名称。</summary>
  86. /// <remarks>默认值:不包含。</remarks>
  87. public bool WithTypeName { get; set; } = false;
  88. #region 默认控制器,可用于静态控制器。
  89. private Type _default = null;
  90. /// <summary>获取已设置的控制器类型,当未匹配到 Application 或 Function 时候,使用此类型创建控制器。</summary>
  91. public Type Default { get => _default; }
  92. /// <summary>设置控制器类型,当未匹配到 Application 或 Function 时候,使用此类型创建控制器。</summary>
  93. public void SetDefault<T>() where T : ApiController, new() => _default = typeof(T);
  94. /// <summary>取消默认控制器类型。</summary>
  95. public void SetDefault() => _default = null;
  96. #endregion
  97. /// <summary>创建默认选项。</summary>
  98. /// <remarks>
  99. /// 在 Debug 模式中默认设置以下选项
  100. /// <br />JsonIndent = TRUE
  101. /// <br />WithException = TRUE
  102. /// <br />WithDuration = TRUE
  103. /// <br />WithParameters = TRUE
  104. /// </remarks>
  105. public ApiOptions()
  106. {
  107. Debug();
  108. }
  109. [Conditional("DEBUG")]
  110. void Debug()
  111. {
  112. JsonIndent = true;
  113. WithException = true;
  114. WithDuration = true;
  115. WithParameters = true;
  116. }
  117. }
  118. }