From dc0a9da3c26abf985cfcc1cc9374173950156ff0 Mon Sep 17 00:00:00 2001 From: Jason Date: Tue, 13 Jul 2021 15:05:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mozi.HttpEmbedded.Test/Test.cs | 6 +- Mozi.HttpEmbedded/Document/DocLoader.cs | 3 + Mozi.HttpEmbedded/Document/Error.html | 65 +++-------------- Mozi.HttpEmbedded/Document/Home.html | 43 +++--------- Mozi.HttpEmbedded/Generic/AbsClassEnum.cs | 2 +- Mozi.HttpEmbedded/HttpServer.cs | 78 +++++++++++++++------ Mozi.HttpEmbedded/Middleware/IMiddleware.cs | 2 +- Mozi.HttpEmbedded/Page/BaseApi.cs | 4 +- Mozi.HttpEmbedded/Page/BasePage.cs | 11 ++- Mozi.HttpEmbedded/Page/Router.cs | 10 ++- Mozi.HttpEmbedded/Page/RuntimeApi.cs | 10 +-- Mozi.HttpEmbedded/Template/PageEngine.cs | 23 +++++- 12 files changed, 128 insertions(+), 129 deletions(-) diff --git a/Mozi.HttpEmbedded.Test/Test.cs b/Mozi.HttpEmbedded.Test/Test.cs index 38c7212..58122a8 100644 --- a/Mozi.HttpEmbedded.Test/Test.cs +++ b/Mozi.HttpEmbedded.Test/Test.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Mozi.HttpEmbedded.Page; +using Mozi.HttpEmbedded.Page; namespace Mozi.HttpEmbedded.Test { diff --git a/Mozi.HttpEmbedded/Document/DocLoader.cs b/Mozi.HttpEmbedded/Document/DocLoader.cs index ff5e261..fe2eb6c 100644 --- a/Mozi.HttpEmbedded/Document/DocLoader.cs +++ b/Mozi.HttpEmbedded/Document/DocLoader.cs @@ -3,6 +3,9 @@ using System.Reflection; namespace Mozi.HttpEmbedded.Docment { + /// + /// 文档载入器 + /// public class DocLoader { public static string Load(string docName) diff --git a/Mozi.HttpEmbedded/Document/Error.html b/Mozi.HttpEmbedded/Document/Error.html index 1ae60e0..573cdfd 100644 --- a/Mozi.HttpEmbedded/Document/Error.html +++ b/Mozi.HttpEmbedded/Document/Error.html @@ -7,60 +7,17 @@ diff --git a/Mozi.HttpEmbedded/Document/Home.html b/Mozi.HttpEmbedded/Document/Home.html index 15dce16..0ecf9cf 100644 --- a/Mozi.HttpEmbedded/Document/Home.html +++ b/Mozi.HttpEmbedded/Document/Home.html @@ -10,41 +10,14 @@ * { font-size: 18px;box-sizing: border-box;-webkit-box-sizing: border-box;color: #212121;-moz-box-sizing: border-box;-webkit-user-drag: none;} body { padding: 0; margin: 0;} ul { list-style-type: none;padding: 0;margin: 0; } - body { - padding: 0; - margin: 0; - } - - ul { - list-style-type: none; - padding: 0; - margin: 0; - } - - h1 { - font-size: 1.22rem; - } - - h2 { - font-size: 1rem; - } - - h3 { - font-size: 0.89rem; - } - - h4 { - font-size: 0.78rem; - } - - h5 { - font-size: 0.67rem; - } - - p, p > * { - font-size: 0.78rem; - line-height: 1.22rem; - } + body {padding: 0; margin: 0;} + ul { list-style-type: none;padding: 0;margin: 0; } + h1 { font-size: 1.22rem; } + h2 { font-size: 1rem; } + h3 { font-size: 0.89rem; } + h4 { font-size: 0.78rem; } + h5 { font-size: 0.67rem; } + p, p > * { font-size: 0.78rem; line-height: 1.22rem;} diff --git a/Mozi.HttpEmbedded/Generic/AbsClassEnum.cs b/Mozi.HttpEmbedded/Generic/AbsClassEnum.cs index 7ccbc19..5f967b0 100644 --- a/Mozi.HttpEmbedded/Generic/AbsClassEnum.cs +++ b/Mozi.HttpEmbedded/Generic/AbsClassEnum.cs @@ -4,7 +4,7 @@ using System.Reflection; namespace Mozi.HttpEmbedded.Generic { /// - /// ö + /// ö /// public abstract class AbsClassEnum { diff --git a/Mozi.HttpEmbedded/HttpServer.cs b/Mozi.HttpEmbedded/HttpServer.cs index bb44c1f..53b5538 100644 --- a/Mozi.HttpEmbedded/HttpServer.cs +++ b/Mozi.HttpEmbedded/HttpServer.cs @@ -60,11 +60,20 @@ namespace Mozi.HttpEmbedded private string _serverName = "HttpEmbedded"; //默认首页为index.html,index.htm - private string _indexPageMatchPattern = "index.html,index.htm"; + public string[] _indexPages = new string[] { "index.html","index.htm" }; - //允许和公开的方法 - private RequestMethod[] MethodAllow = new RequestMethod[] { RequestMethod.OPTIONS, RequestMethod.TRACE, RequestMethod.GET, RequestMethod.HEAD, RequestMethod.POST, RequestMethod.COPY, RequestMethod.PROPFIND, RequestMethod.LOCK, RequestMethod.UNLOCK }; + /// + /// 默认首页 + /// + public string IndexPages { get { return string.Join(",",_indexPages); } } + /// + /// 允许的方法 + /// + private RequestMethod[] MethodAllow = new RequestMethod[] { RequestMethod.OPTIONS, RequestMethod.TRACE, RequestMethod.GET, RequestMethod.HEAD, RequestMethod.POST, RequestMethod.COPY, RequestMethod.PROPFIND, RequestMethod.LOCK, RequestMethod.UNLOCK }; + /// + /// 公开的方法 + /// private RequestMethod[] MethodPublic = new RequestMethod[] { RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.HEAD, RequestMethod.PROPFIND, RequestMethod.PROPPATCH, RequestMethod.MKCOL, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.COPY, RequestMethod.MOVE, RequestMethod.LOCK, RequestMethod.UNLOCK }; //证书管理器 @@ -358,33 +367,60 @@ namespace Mozi.HttpEmbedded bool isStatic = st.IsStatic(fileext); context.Response.SetContentType(contenttype); - if (path == "/") + var pathReal = path; + if (pathReal == "/") { - var doc = DocLoader.Load("Home.html"); - PageEngine pc = new PageEngine(); - pc.LoadFromText(doc); - pc.Set("Info", new - { - VersionName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), - }); - pc.Prepare(); - - context.Response.Write(pc.GetBuffer()); - context.Response.SetContentType(Mime.GetContentType("html")); - return StatusCode.Success; + //var existsIndex = false; + //foreach(var r in _indexPages) + //{ + // if (st.Exists(path+r, "")) + // { + // string ifmodifiedsince = context.Request.Headers.GetValue(HeaderProperty.IfModifiedSince.PropertyName); + // if (st.CheckIfModified(path, ifmodifiedsince)) + // { + // DateTime dtModified = st.GetLastModifiedTime(path).ToUniversalTime(); + // context.Response.AddHeader(HeaderProperty.LastModified, dtModified.ToString("r")); + // context.Response.Write(st.Load(path, "")); + + // //ETag 仅测试 不具备判断缓存的能力 + // context.Response.AddHeader(HeaderProperty.ETag, String.Format("{0:x2}:{1:x2}", dtModified.ToUniversalTime().Ticks, context.Response.ContentLength)); + // return StatusCode.Success; + // } + // } + //} + //if (!existsIndex) + //{ + //优先加载 + var doc = DocLoader.Load("Home.html"); + PageEngine pc = new PageEngine(); + pc.LoadFromText(doc); + pc.Set("Info", new + { + VersionName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), + }); + pc.Prepare(); + + context.Response.Write(pc.GetBuffer()); + context.Response.SetContentType(Mime.GetContentType("html")); + return StatusCode.Success; + //} } //静态文件处理 else if (st.Enabled && isStatic) { + //if (pathReal.EndsWith("/")) + //{ + + //} //响应静态文件 - if (st.Exists(path, "")) + if (st.Exists(pathReal, "")) { string ifmodifiedsince = context.Request.Headers.GetValue(HeaderProperty.IfModifiedSince.PropertyName); - if (st.CheckIfModified(path, ifmodifiedsince)) + if (st.CheckIfModified(pathReal, ifmodifiedsince)) { - DateTime dtModified = st.GetLastModifiedTime(path).ToUniversalTime(); + DateTime dtModified = st.GetLastModifiedTime(pathReal).ToUniversalTime(); context.Response.AddHeader(HeaderProperty.LastModified, dtModified.ToString("r")); - context.Response.Write(st.Load(path, "")); + context.Response.Write(st.Load(pathReal, "")); //ETag 仅测试 不具备判断缓存的能力 context.Response.AddHeader(HeaderProperty.ETag, String.Format("{0:x2}:{1:x2}", dtModified.ToUniversalTime().Ticks, context.Response.ContentLength)); @@ -688,7 +724,7 @@ namespace Mozi.HttpEmbedded /// public void SetIndexPage(string pattern) { - _indexPageMatchPattern = pattern; + _indexPages = pattern.Split(new char[] { ',' }); } /// /// 关闭服务器 diff --git a/Mozi.HttpEmbedded/Middleware/IMiddleware.cs b/Mozi.HttpEmbedded/Middleware/IMiddleware.cs index 30c2b54..e94bd01 100644 --- a/Mozi.HttpEmbedded/Middleware/IMiddleware.cs +++ b/Mozi.HttpEmbedded/Middleware/IMiddleware.cs @@ -3,7 +3,7 @@ /// /// 中间件 接入多层次逻辑处理 /// - public interface IMiddleware + internal interface IMiddleware { void Invoke(); } diff --git a/Mozi.HttpEmbedded/Page/BaseApi.cs b/Mozi.HttpEmbedded/Page/BaseApi.cs index d28d807..004e1ae 100644 --- a/Mozi.HttpEmbedded/Page/BaseApi.cs +++ b/Mozi.HttpEmbedded/Page/BaseApi.cs @@ -5,7 +5,9 @@ /// public abstract class BaseApi { + /// + /// 绑定的上下文对象 + /// public HttpContext Context { get; internal set; } - } } diff --git a/Mozi.HttpEmbedded/Page/BasePage.cs b/Mozi.HttpEmbedded/Page/BasePage.cs index 432e1f1..c282234 100644 --- a/Mozi.HttpEmbedded/Page/BasePage.cs +++ b/Mozi.HttpEmbedded/Page/BasePage.cs @@ -5,15 +5,22 @@ /// public abstract class BasePage { + /// + /// 上下文对象 + /// protected HttpContext Context { get; set; } /// /// 重定向 /// /// public abstract void RedirectTo(string url); - + /// + /// GET方法 + /// public abstract void Get(); - + /// + /// POST方法 + /// public abstract void Post(); } diff --git a/Mozi.HttpEmbedded/Page/Router.cs b/Mozi.HttpEmbedded/Page/Router.cs index 15746b2..2eec59f 100644 --- a/Mozi.HttpEmbedded/Page/Router.cs +++ b/Mozi.HttpEmbedded/Page/Router.cs @@ -10,7 +10,7 @@ namespace Mozi.HttpEmbedded.Page { //TODO 增加API下载的功能,允许客户端提取所有API,同时加入鉴权机制 /// - /// 全局路由 + /// 全局路由 单例模式 /// /// /// 实例化Router时会自动扫描此程序集内部的API @@ -321,14 +321,18 @@ namespace Mozi.HttpEmbedded.Page return ap; } } - + /// + /// 访问域 + /// public class AccessPoint { public string Domain { get; set; } public string Controller { get; set; } public string Action { get; set; } } - + /// + /// 访问对象 + /// public class AccessObject { public Type Target { get; set; } diff --git a/Mozi.HttpEmbedded/Page/RuntimeApi.cs b/Mozi.HttpEmbedded/Page/RuntimeApi.cs index b47e9f0..4396601 100644 --- a/Mozi.HttpEmbedded/Page/RuntimeApi.cs +++ b/Mozi.HttpEmbedded/Page/RuntimeApi.cs @@ -33,12 +33,12 @@ namespace Mozi.HttpEmbedded.Page public RuntimeInfo Info() { RuntimeInfo info = new RuntimeInfo(); - var exeAssembly = Assembly.GetExecutingAssembly(); - info.Name = exeAssembly.GetName().Name; - info.VersionName = exeAssembly.GetName().Version.ToString(); - info.PlatformName =exeAssembly.ImageRuntimeVersion; + var ass = Assembly.GetExecutingAssembly(); + info.Name = ass.GetName().Name; + info.VersionName = ass.GetName().Version.ToString(); + info.PlatformName =ass.ImageRuntimeVersion; info.StartupTime = Context.Server.StartTime.ToUniversalTime().ToString("r"); - Module[] modules = exeAssembly.GetLoadedModules(); + Module[] modules = ass.GetLoadedModules(); foreach(var m in modules) { info.LoadedModules.Add(new LoadedModuleInfo() diff --git a/Mozi.HttpEmbedded/Template/PageEngine.cs b/Mozi.HttpEmbedded/Template/PageEngine.cs index 4e8c1e2..39f7504 100644 --- a/Mozi.HttpEmbedded/Template/PageEngine.cs +++ b/Mozi.HttpEmbedded/Template/PageEngine.cs @@ -144,12 +144,33 @@ namespace Mozi.HttpEmbedded.Template } /// /// $define表达式 + /// + /// $define 用于定义常量 + /// /// /// private PageEngine InflateStatementDefine() { throw new NotImplementedException(); } + /// + /// $undef 表达式 + /// + /// $undef 用于删除常量定义 + /// + /// + /// + private PageEngine InflateStatementUndef() + { + throw new NotImplementedException(); + } + /// + /// $set表达式 + /// + /// $set 用于定义变量 + /// + /// + /// private PageEngine InflateStatementSet() { throw new NotImplementedException(); @@ -222,7 +243,7 @@ namespace Mozi.HttpEmbedded.Template return this; } /// - /// == != <> && || + /// == != <> > < >= <=, && || ,+ - * / % /// /// private PageEngine ParseOperator() -- GitLab