-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
在 `ConfigFileHelper.cs` 中支持可空类型,改进了 `Get` 和 `Set` 方法。 在 `PekSysSetting.cs` 中添加了 `PekSysSetting` 类及其属性。 在 `HttpResponseExtensions.cs` 中添加了 JSON 和 HTML 写入方法及缓存设置方法。 在 `SessionExtensions.cs` 中添加了会话数据的 `Set` 和 `Get` 方法。 在 `DHWebHelper.cs` 中添加了多个用于处理请求和 URL 的方法。 添加了 `RequestHelper` 类,用于处理 HTTP 请求的辅助方法。 添加了 `UserAgentParser` 类,用于解析 UserAgent 字符串并判断设备类型。
- Loading branch information
猿人易
committed
Dec 17, 2024
1 parent
a6f5467
commit 4d53d1f
Showing
7 changed files
with
1,069 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.ComponentModel; | ||
|
||
using NewLife.Configuration; | ||
|
||
namespace Pek.Configs; | ||
|
||
/// <summary>系统通用配置</summary> | ||
[DisplayName("系统通用配置")] | ||
[Config("PekSys")] | ||
public class PekSysSetting : Config<PekSysSetting> | ||
{ | ||
/// <summary>机器人错误码。设置后拦截各种爬虫并返回相应错误,如404/500,默认0不拦截</summary> | ||
[Description("机器人错误码。设置后拦截各种爬虫并返回相应错误,如404/500,默认0不拦截")] | ||
[Category("通用")] | ||
public Int32 RobotError { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.Text; | ||
|
||
using NewLife.Serialization; | ||
|
||
namespace Pek; | ||
|
||
/// <summary> | ||
/// Http响应(<see cref="HttpResponse"/>) 扩展 | ||
/// </summary> | ||
public static class HttpResponseExtensions | ||
{ | ||
#region WriteJsonAsync(写入Json) | ||
|
||
/// <summary> | ||
/// 写入Json | ||
/// </summary> | ||
/// <param name="response">Http响应</param> | ||
/// <param name="obj">对象</param> | ||
public static async Task WriteJsonAsync(this HttpResponse response, Object obj) | ||
{ | ||
var json = JsonHelper.ToJson(obj); | ||
await response.WriteJsonAsync(json).ConfigureAwait(false); | ||
} | ||
|
||
/// <summary> | ||
/// 写入Json | ||
/// </summary> | ||
/// <param name="response">Http响应</param> | ||
/// <param name="json">Json字符串</param> | ||
public static async Task WriteJsonAsync(this HttpResponse response, String json) | ||
{ | ||
response.ContentType = "application/json; charset=utf-8"; | ||
await response.WriteAsync(json).ConfigureAwait(false); | ||
} | ||
|
||
#endregion | ||
|
||
#region SetCache(设置缓存头) | ||
|
||
/// <summary> | ||
/// 设置缓存头 | ||
/// </summary> | ||
/// <param name="response">Http响应</param> | ||
/// <param name="maxAge">最大有效期</param> | ||
public static void SetCache(this HttpResponse response, Int32 maxAge) | ||
{ | ||
if (maxAge == 0) | ||
{ | ||
SetNoCache(response); | ||
} | ||
else if (maxAge > 0) | ||
{ | ||
if (!response.Headers.ContainsKey("Cache-Control")) | ||
{ | ||
response.Headers.Append("Cache-Control", $"max-age={maxAge}"); | ||
} | ||
} | ||
} | ||
|
||
#endregion | ||
|
||
#region SetNoCache(设置无缓存) | ||
|
||
/// <summary> | ||
/// 设置无缓存 | ||
/// </summary> | ||
/// <param name="response">Http响应</param> | ||
public static void SetNoCache(this HttpResponse response) | ||
{ | ||
if (!response.Headers.ContainsKey("Cache-Control")) | ||
response.Headers.Append("Cache-Control", "no-store, no-cache, max-age=0"); | ||
if (!response.Headers.ContainsKey("Pragma")) | ||
response.Headers.Append("Pragma", "no-cache"); | ||
} | ||
|
||
#endregion | ||
|
||
#region WriteHtmlAsync(写入Html) | ||
|
||
/// <summary> | ||
/// 写入Html | ||
/// </summary> | ||
/// <param name="response">Http响应</param> | ||
/// <param name="html">Html字符串</param> | ||
public static async Task WriteHtmlAsync(this HttpResponse response, String html) | ||
{ | ||
response.ContentType = "text/html; charset=utf-8"; | ||
await response.WriteAsync(html, Encoding.UTF8).ConfigureAwait(false); | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using NewLife.Serialization; | ||
|
||
namespace Pek; | ||
|
||
/// <summary> | ||
/// 会话(<see cref="ISession"/>) 扩展 | ||
/// </summary> | ||
public static class SessionExtensions | ||
{ | ||
/// <summary> | ||
/// 设置会话 | ||
/// </summary> | ||
/// <typeparam name="T">对象类型</typeparam> | ||
/// <param name="session">会话</param> | ||
/// <param name="key">键</param> | ||
/// <param name="value">值</param> | ||
public static void Set<T>(this ISession session, String key, T value) | ||
{ | ||
if (String.IsNullOrWhiteSpace(key)) | ||
{ | ||
return; | ||
} | ||
session.Set(key, JsonHelper.ToJson(value!)); | ||
} | ||
|
||
/// <summary> | ||
/// 获取指定键名的值 | ||
/// </summary> | ||
/// <typeparam name="T">对象类型</typeparam> | ||
/// <param name="session">会话</param> | ||
/// <param name="key">键名</param> | ||
/// <returns></returns> | ||
public static T? Get<T>(this ISession session, String key) | ||
{ | ||
var value = session.GetString(key); | ||
return String.IsNullOrWhiteSpace(value) ? default : JsonHelper.ToJsonEntity<T>(value); | ||
} | ||
} |
Oops, something went wrong.