Browse Source

Web:为允许解析 OPTIONS 方法提供选项,默认不解析,直接返回 0 字节 text/plain 内容。

master
王厅 1 day ago
parent
commit
215e45cf88
  1. 4
      Apewer/Web/ApiOptions.cs
  2. 18
      Apewer/Web/ApiProcessor.cs

4
Apewer/Web/ApiOptions.cs

@ -26,6 +26,10 @@ namespace Apewer.Web
/// <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;

18
Apewer/Web/ApiProcessor.cs

@ -92,9 +92,16 @@ namespace Apewer.Web
url = _context.Provider.GetUrl();
if (url == null) return "URL 无效。";
// Method
method = _context.Provider.GetMethod();
if (method == HttpMethod.NULL) return "HTTP 方法无效。";
if (method == HttpMethod.OPTIONS) return null;
switch (method)
{
case HttpMethod.NULL:
return "HTTP 方法无效。";
case HttpMethod.OPTIONS:
if (!_context.Options.AllowOptions) return null;
break;
}
// favicon.ico
var lowerPath = TextUtility.AssureStarts(TextUtility.Lower(url.AbsolutePath), "/");
@ -127,8 +134,11 @@ namespace Apewer.Web
// OPTIONS
if (_context.Request.Method == HttpMethod.OPTIONS)
{
_context.Response.Model = new ApiTextModel("");
return;
if (!_context.Options.AllowOptions)
{
_context.Response.Model = new ApiTextModel("");
return;
}
}
// 路由

Loading…
Cancel
Save