From 215e45cf880ea413b1e922741a50583d757602ac Mon Sep 17 00:00:00 2001 From: Elivo Date: Wed, 6 Aug 2025 16:05:51 +0800 Subject: [PATCH] =?UTF-8?q?Web=EF=BC=9A=E4=B8=BA=E5=85=81=E8=AE=B8?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=20OPTIONS=20=E6=96=B9=E6=B3=95=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E9=80=89=E9=A1=B9=EF=BC=8C=E9=BB=98=E8=AE=A4=E4=B8=8D?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=EF=BC=8C=E7=9B=B4=E6=8E=A5=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=200=20=E5=AD=97=E8=8A=82=20text/plain=20=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Web/ApiOptions.cs | 4 ++++ Apewer/Web/ApiProcessor.cs | 18 ++++++++++++++---- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Apewer/Web/ApiOptions.cs b/Apewer/Web/ApiOptions.cs index 58f00fd..9fd1c25 100644 --- a/Apewer/Web/ApiOptions.cs +++ b/Apewer/Web/ApiOptions.cs @@ -26,6 +26,10 @@ namespace Apewer.Web /// 默认值:不允许,响应空。 public bool AllowFavIcon { get; set; } = false; + /// 允许解析 OPTIONS 请求。 + /// 默认值:不允许,直接返回 0 字节的 text/plain 内容。 + public bool AllowOptions { get; set; } = false; + /// 允许解析 robots.txt 请求。 /// 默认值:不允许,拒绝搜索引擎收录根目录。 public bool AllowRobots { get; set; } = false; diff --git a/Apewer/Web/ApiProcessor.cs b/Apewer/Web/ApiProcessor.cs index 5a34d5d..781a795 100644 --- a/Apewer/Web/ApiProcessor.cs +++ b/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; + } } // 路由