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
						
					
					
				
			
		
		
		
			
			
			
		
		
	
	
							155 lines
						
					
					
						
							6.4 KiB
						
					
					
				
								using System;
							 | 
						|
								using System.Collections.Generic;
							 | 
						|
								using System.Diagnostics;
							 | 
						|
								using System.Web;
							 | 
						|
								
							 | 
						|
								namespace Apewer.Web
							 | 
						|
								{
							 | 
						|
								
							 | 
						|
								    /// <summary>选项。</summary>
							 | 
						|
								    public sealed class ApiOptions
							 | 
						|
								    {
							 | 
						|
								
							 | 
						|
								        /// <summary>允许压缩。</summary>
							 | 
						|
								        /// <remarks>默认值:不允许。</remarks>
							 | 
						|
								        public bool AllowCompression { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>设置 Access-Control-Max-Age 的值。</summary>
							 | 
						|
								        /// <remarks>默认值:60。</remarks>
							 | 
						|
								        public int AccessControlMaxAge { get; set; } = 60;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许枚举输出 Applications 或 Functions。</summary>
							 | 
						|
								        /// <remarks>默认值:不允许,不输出列表。</remarks>
							 | 
						|
								        public bool AllowEnumerate { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许解析 favicon.ico 请求。</summary>
							 | 
						|
								        /// <remarks>默认值:不允许,响应空。</remarks>
							 | 
						|
								        public bool AllowFavIcon { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许解析 OPTIONS 请求。</summary>
							 | 
						|
								        /// <remarks>默认值:不允许,直接返回 0 字节的 text/plain 内容。</remarks>
							 | 
						|
								        public bool AllowOptions { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许解析 robots.txt 请求。</summary>
							 | 
						|
								        /// <remarks>默认值:不允许,拒绝搜索引擎收录根目录。</remarks>
							 | 
						|
								        public bool AllowRobots { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        // /// <summary>允许同步 IO。</summary>
							 | 
						|
								        // /// <remarks>
							 | 
						|
								        // /// <para>默认值:允许。</para>
							 | 
						|
								        // /// <para>允许:使用同步方法写入 Response.Body,可能会导致线程不足而崩溃。</para>
							 | 
						|
								        // /// <para>不允许:必须用异步方法写入 Response.Body。</para>
							 | 
						|
								        // /// </remarks>
							 | 
						|
								        // public bool AllowSynchronousIO { get; set; } = true;
							 | 
						|
								
							 | 
						|
								        /// <summary>默认的结果渲染器。</summary>
							 | 
						|
								        /// <remarks>默认值:NULL</remarks>
							 | 
						|
								        public Action<ApiContext, object> DefaultRenderer { get; set; }
							 | 
						|
								
							 | 
						|
								        /// <summary>对输出的 Json 对象缩进。</summary>
							 | 
						|
								        /// <remarks>默认值:不缩进。</remarks>
							 | 
						|
								        public bool JsonIndent { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>默认的结果渲染器。</summary>
							 | 
						|
								        /// <remarks>默认值:NULL</remarks>
							 | 
						|
								        public Action<ApiContext, Json> JsonRenderer { get; set; }
							 | 
						|
								
							 | 
						|
								        /// <summary>限制最大请求的字节数。</summary>
							 | 
						|
								        /// <remarks>默认值:-1,不使用 ApiOptions 限制。</remarks>
							 | 
						|
								        public long MaxRequestBody { get; set; } = -1;
							 | 
						|
								
							 | 
						|
								        /// <summary>输出前的检查。</summary>
							 | 
						|
								        public ApiPreOutput PreOutput { get; set; }
							 | 
						|
								
							 | 
						|
								        /// <summary>默认的结果渲染器。</summary>
							 | 
						|
								        /// <remarks>默认值:NULL(返回值不为空时视为错误信息)</remarks>
							 | 
						|
								        public Action<ApiContext, string> TextRenderer { get; set; }
							 | 
						|
								
							 | 
						|
								        /// <summary>在响应头中设置 Content-Security-Policy,要求浏览器升级资源链接,使用 HTTPS。</summary>
							 | 
						|
								        /// <remarks>默认值:不要求。在 HTTPS 页面中,不自动升级 HTTP 资源。</remarks>
							 | 
						|
								        public bool UpgradeHttps { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>使用反射。</summary>
							 | 
						|
								        /// <remarks>默认值:使用</remarks>
							 | 
						|
								        public bool UseReflection { get; set; } = true;
							 | 
						|
								
							 | 
						|
								        /// <summary>使用路由。</summary>
							 | 
						|
								        /// <remarks>默认值:使用</remarks>
							 | 
						|
								        public bool UseRoute { get; set; } = true;
							 | 
						|
								
							 | 
						|
								        /// <summary>在响应中包含 Access-Control 属性。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithAccessControl { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>在响应中包含时间属性。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithClock { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许响应标头中包含 X-Content-Type-Options: nosiff。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。当设置默认控制器时自动启用此属性。</remarks>
							 | 
						|
								        public bool WithContentTypeOptions { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>在响应中包含执行 API 的持续时间。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithDuration { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许响应中包含 Exception 对象的属性。</summary>
							 | 
						|
								        /// <remarks>默认值:允许。</remarks>
							 | 
						|
								        public bool WithException { get; set; } = true;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许输出 Application 列表时包含模块名称。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithModuleName { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许在枚举 Function 时包含参数信息。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithParameters { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>在响应中包含 Application 和 Function 属性。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithTarget { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        /// <summary>允许输出 Application 列表时包含类型名称。</summary>
							 | 
						|
								        /// <remarks>默认值:不包含。</remarks>
							 | 
						|
								        public bool WithTypeName { get; set; } = false;
							 | 
						|
								
							 | 
						|
								        #region 默认控制器,可用于静态控制器。
							 | 
						|
								
							 | 
						|
								        private Type _default = null;
							 | 
						|
								
							 | 
						|
								        /// <summary>获取已设置的控制器类型,当未匹配到 Application 或 Function 时候,使用此类型创建控制器。</summary>
							 | 
						|
								        public Type Default { get => _default; }
							 | 
						|
								
							 | 
						|
								        /// <summary>设置控制器类型,当未匹配到 Application 或 Function 时候,使用此类型创建控制器。</summary>
							 | 
						|
								        public void SetDefault<T>() where T : ApiController, new() => _default = typeof(T);
							 | 
						|
								
							 | 
						|
								        /// <summary>取消默认控制器类型。</summary>
							 | 
						|
								        public void SetDefault() => _default = null;
							 | 
						|
								
							 | 
						|
								        #endregion
							 | 
						|
								
							 | 
						|
								        /// <summary>创建默认选项。</summary>
							 | 
						|
								        /// <remarks>
							 | 
						|
								        /// 在 Debug 模式中默认设置以下选项
							 | 
						|
								        /// <br />JsonIndent =  TRUE
							 | 
						|
								        /// <br />WithException =  TRUE
							 | 
						|
								        /// <br />WithDuration =  TRUE
							 | 
						|
								        /// <br />WithParameters =  TRUE
							 | 
						|
								        /// </remarks>
							 | 
						|
								        public ApiOptions()
							 | 
						|
								        {
							 | 
						|
								            Debug();
							 | 
						|
								        }
							 | 
						|
								
							 | 
						|
								        [Conditional("DEBUG")]
							 | 
						|
								        void Debug()
							 | 
						|
								        {
							 | 
						|
								            JsonIndent = true;
							 | 
						|
								            WithException = true;
							 | 
						|
								            WithDuration = true;
							 | 
						|
								            WithParameters = true;
							 | 
						|
								        }
							 | 
						|
								
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								}
							 | 
						|
								
							 |