内置了Web Server/IRouter的http api请求处理类

This commit is contained in:
fengjiayi
2024-09-17 15:58:37 +08:00
parent 6d85390395
commit 86da5b3ba6
9 changed files with 123 additions and 27 deletions

View File

@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Serein.Library.Web
{
internal class QueryStringParser
{
public static Dictionary<string, string> ParseQueryString(string query)
{
var result = new Dictionary<string, string>();
if (string.IsNullOrEmpty(query))
return result;
// 如果字符串以'?'开头,移除它
if (query.StartsWith("?"))
query = query.Substring(1);
// 拆分键值对
var pairs = query.Split('&');
foreach (var pair in pairs)
{
// 忽略空的键值对
if (string.IsNullOrEmpty(pair)) continue;
// 用等号分隔键和值
var keyValue = pair.Split(new[] { '=' }, 2);
var key = Uri.UnescapeDataString(keyValue[0]); // 解码键
var value = keyValue.Length > 1 ? Uri.UnescapeDataString(keyValue[1]) : string.Empty; // 解码值
result[key] = value; // 添加到字典中
}
return result;
}
}
}

View File

@@ -137,8 +137,9 @@ namespace Serein.Library.Web
if (!controllerType.IsClass || controllerType.IsAbstract) return false; // 如果不是类或者是抽象类,则直接返回
var autoHostingAttribute = controllerType.GetCustomAttribute<AutoHostingAttribute>();
var methods = controllerType.GetMethods().Where(m => m.GetCustomAttribute<WebApiAttribute>() != null).ToArray();
foreach (var method in controllerType.GetMethods()) // 遍历控制器类型的所有方法
foreach (var method in methods) // 遍历控制器类型的所有方法
{
var routeAttribute = method.GetCustomAttribute<WebApiAttribute>(); // 获取方法上的 WebAPIAttribute 自定义属性
if (routeAttribute != null) // 如果存在 WebAPIAttribute 属性
@@ -354,6 +355,7 @@ namespace Serein.Library.Web
{
return value;
}
#pragma warning restore CS0168 // 声明了变量,但从未使用过
}
/// <summary>
@@ -507,12 +509,17 @@ namespace Serein.Library.Web
if (pathParts.Length > 1) // 如果包含查询字符串
{
var queryParams = HttpUtility.ParseQueryString(pathParts[1]); // 解析查询字符串
foreach (string key in queryParams) // 遍历查询字符串的键值对
//var queryParams = HttpUtility.ParseQueryString(pathParts[1]); // 解析查询字符串
//foreach (string key in queryParams) // 遍历查询字符串的键值对
//{
// if (key == null) continue;
// routeValues[key] = queryParams[key]; // 将键值对添加到路由参数字典中
//}
var parsedQuery = QueryStringParser.ParseQueryString(pathParts[1]);
foreach (var kvp in parsedQuery)
{
if (key == null) continue;
routeValues[key] = queryParams[key]; // 将键值对添加到路由参数字典中
//Console.WriteLine($"{kvp.Key}: {kvp.Value}");
routeValues[kvp.Key] = kvp.Value; // 将键值对添加到路由参数字典中
}
}

View File

@@ -1,4 +1,6 @@
using Serein.Library.Api;
using Serein.Library.Attributes;
using Serein.Library.Utils;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -13,9 +15,12 @@ namespace Serein.Library.Web
/// </summary>
public class WebServer
{
[AutoHosting]
[AutoInjection]
public IRouter Router { get; set; } // 路由器
[AutoInjection]
public NodeRunCts nodeRunCts { get; set; }
private HttpListener listener; // HTTP 监听器
private RequestLimiter requestLimiter; //接口防刷
@@ -38,6 +43,21 @@ namespace Serein.Library.Web
listener.Start(); // 开始监听
//_ = Task.Run(async () =>
//{
// while (true)
// {
// await Task.Delay(100);
// if (nodeRunCts.IsCancellationRequested)
// {
// }
// var context = await listener.GetContextAsync(); // 获取请求上下文
// ProcessRequestAsync(context); // 处理请求
// }
//});
Task.Run(async () =>
{
while (listener.IsListening)
@@ -46,6 +66,7 @@ namespace Serein.Library.Web
ProcessRequestAsync(context); // 处理请求
}
});
return this;
}