//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://touchsocket.net/ // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ using System; using System.Threading.Tasks; using TouchSocket.Core; namespace TouchSocket.Http { /// /// Http静态内容插件 /// [PluginOption(Singleton = false)] public class HttpStaticPagePlugin : PluginBase { /// /// 构造函数 /// public HttpStaticPagePlugin() { this.FileCache = new FileCachePool(); this.SetNavigateAction(request => { return request.RelativeURL; }); } /// protected override void Loaded(IPluginManager pluginManager) { pluginManager.Add(nameof(IHttpPlugin.OnHttpRequest), this.OnHttpRequest); base.Loaded(pluginManager); } /// /// 提供文件扩展名和MIME类型之间的映射。 /// public IContentTypeProvider ContentTypeProvider { get; set; } /// /// 静态文件缓存。 /// public FileCachePool FileCache { get; private set; } /// /// 重新导航 /// public Func> NavigateAction { get; set; } /// /// 在响应之前调用。 /// public Func ResponseAction { get; set; } /// /// 添加静态 /// /// Static content path /// Cache prefix (default is "/") /// Cache filter (default is "*.*") /// Refresh cache millisecondsTimeout (default is 1 hour) public void AddFolder(string path, string prefix = "/", string filter = "*.*", TimeSpan? millisecondsTimeout = null) { millisecondsTimeout ??= TimeSpan.FromHours(1); this.FileCache.InsertPath(path, prefix, filter, millisecondsTimeout.Value, null); } /// /// Clear static content cache /// public void ClearFolder() { this.FileCache.Clear(); } private async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e) { var url = await this.NavigateAction.Invoke(e.Context.Request); if (this.FileCache.Find(url, out var data)) { var response = e.Context.Response; response.SetStatus(); string result = String.Empty; if (this.ContentTypeProvider?.TryGetContentType(url, out result) != true) { result = HttpTools.GetContentTypeFromExtension(url); } response.ContentType = result; response.SetContent(data); if (this.ResponseAction != null) { await this.ResponseAction.Invoke(e.Context); } await response.AnswerAsync(); e.Handled = true; } else { await e.InvokeNext(); } } /// /// Remove static content cache /// /// Static content path public void RemoveFolder(string path) { this.FileCache.RemovePath(path); } /// /// 设置提供文件扩展名和MIME类型之间的映射。 /// /// /// public HttpStaticPagePlugin SetContentTypeProvider(IContentTypeProvider provider) { this.ContentTypeProvider = provider ?? throw new ArgumentNullException(nameof(provider)); return this; } /// /// 设定重新导航 /// /// /// public HttpStaticPagePlugin SetNavigateAction(Func> func) { this.NavigateAction = func; return this; } /// /// 设定重新导航 /// /// /// public HttpStaticPagePlugin SetNavigateAction(Func func) { this.NavigateAction = (request) => { return Task.FromResult(func(request)); }; return this; } /// /// 在响应之前调用。 /// /// /// public HttpStaticPagePlugin SetResponseAction(Func func) { this.ResponseAction = func; return this; } /// /// 在响应之前调用。 /// /// /// public HttpStaticPagePlugin SetResponseAction(Action action) { this.ResponseAction = (response) => { action.Invoke(response); return EasyTask.CompletedTask; }; return this; } } }