diff --git a/Mozi.HttpEmbedded/AccessManager.cs b/Mozi.HttpEmbedded/AccessManager.cs index 05f2f62035e224436ee6c15bc7d1caa710217ea7..f9e262332f10552ea2fdf73a5fc02911ca079b0f 100644 --- a/Mozi.HttpEmbedded/AccessManager.cs +++ b/Mozi.HttpEmbedded/AccessManager.cs @@ -9,7 +9,7 @@ namespace Mozi.HttpEmbedded { public static AccessManager _access; - private List _blacklist = new List(); + private readonly List _blacklist = new List(); public static AccessManager Instance { diff --git a/Mozi.HttpEmbedded/Docment/DefaultHome.html b/Mozi.HttpEmbedded/Document/DefaultHome.html similarity index 75% rename from Mozi.HttpEmbedded/Docment/DefaultHome.html rename to Mozi.HttpEmbedded/Document/DefaultHome.html index f36bbd78eca597f5c11c98b2929e1ff64f2c3725..42b2cc0ba403b060cd5bbe5656cbdfe8538ac9aa 100644 --- a/Mozi.HttpEmbedded/Docment/DefaultHome.html +++ b/Mozi.HttpEmbedded/Document/DefaultHome.html @@ -1,7 +1,7 @@  - 模板示例页面 + HomePage @@ -39,10 +39,13 @@ h4 { font-size: 0.67rem; } + p, p > * { + font-size: 0.78rem; + } -

欢迎使用Mozi.HttpEmbedded${Info.VersionName}

-
+

欢迎使用 Mozi.HttpEmbedded${Info.VersionName}

+
\ No newline at end of file diff --git a/Mozi.HttpEmbedded/Document/DocLoader.cs b/Mozi.HttpEmbedded/Document/DocLoader.cs new file mode 100644 index 0000000000000000000000000000000000000000..441b8c807edf975f0cc6e5ae65dc920a967d87d3 --- /dev/null +++ b/Mozi.HttpEmbedded/Document/DocLoader.cs @@ -0,0 +1,19 @@ +using Mozi.HttpEmbedded.Encode; +using System.Reflection; + +namespace Mozi.HttpEmbedded.Docment +{ + public class DocLoader + { + public static string Load(string docName) + { + Assembly ass = Assembly.GetExecutingAssembly(); + var resName = ass.GetName().Name + ".Document." + docName; + var stream=ass.GetManifestResourceStream(resName); + byte[] buffer = new byte[stream.Length]; + stream.Read(buffer, 0, buffer.Length); + stream.Close(); + return StringEncoder.Decode(buffer); + } + } +} diff --git a/Mozi.HttpEmbedded/Docment/Error.html b/Mozi.HttpEmbedded/Document/Error.html similarity index 90% rename from Mozi.HttpEmbedded/Docment/Error.html rename to Mozi.HttpEmbedded/Document/Error.html index f1880b2aedae0c8ba1fa0430157221a4629ee57c..8baf399d432ac3361de85e1e2c5ce54aa2627513 100644 --- a/Mozi.HttpEmbedded/Docment/Error.html +++ b/Mozi.HttpEmbedded/Document/Error.html @@ -36,11 +36,14 @@ h4 { font-size: 0.67rem; } + p, p > * { + font-size: 0.78rem; + }

${Error.Code}-${Error.Title}

-
+

时间:${Error.Time}

内容:${Error.Description}

源:${Error.Source}

diff --git a/Mozi.HttpEmbedded/Docment/ExamplePage.html b/Mozi.HttpEmbedded/Document/ExamplePage.html similarity index 100% rename from Mozi.HttpEmbedded/Docment/ExamplePage.html rename to Mozi.HttpEmbedded/Document/ExamplePage.html diff --git a/Mozi.HttpEmbedded/HttpRequest.cs b/Mozi.HttpEmbedded/HttpRequest.cs index 51d5ff3ed3f42cfb98e14c3c2017ebb80332116e..edb41fb00ad273c4a4534eeef2a4cdcd891b277d 100644 --- a/Mozi.HttpEmbedded/HttpRequest.cs +++ b/Mozi.HttpEmbedded/HttpRequest.cs @@ -206,6 +206,7 @@ namespace Mozi.HttpEmbedded { req.Post=UrlEncoder.ParseQuery(StringEncoder.Decode(data)); } + //TODO ļӦд뻺 /// /// multipart/form-data /// diff --git a/Mozi.HttpEmbedded/HttpServer.cs b/Mozi.HttpEmbedded/HttpServer.cs index 6ababa7626561bd9fdd430fc75143bff52e61d77..2e3295ad365df1bbbd39cadedeaa53f617cdcc7d 100644 --- a/Mozi.HttpEmbedded/HttpServer.cs +++ b/Mozi.HttpEmbedded/HttpServer.cs @@ -4,6 +4,7 @@ using Mozi.HttpEmbedded.Auth; using Mozi.HttpEmbedded.Cert; using Mozi.HttpEmbedded.Common; using Mozi.HttpEmbedded.Compress; +using Mozi.HttpEmbedded.Docment; using Mozi.HttpEmbedded.Page; using Mozi.HttpEmbedded.Source; @@ -161,7 +162,8 @@ namespace Mozi.HttpEmbedded { context.Request = HttpRequest.Parse(args.Data); //TODO HTTP/1.1 通过Connection控制连接 服务器同时对连接进行监测 保证服务器效率 - //TODO 此处应判断Content-Length然后继续读流 + //DONE 此处应判断Content-Length然后继续读流 + //TODO 如何解决文件传输内存占用过大的问题 long contentLength = -1; if (context.Request.Headers.Contains(HeaderProperty.ContentLength.PropertyName)){ @@ -190,6 +192,14 @@ namespace Mozi.HttpEmbedded } catch (Exception ex) { + #region 测试片段,模板引擎开发好以后注释掉 + string doc = DocLoader.Load("Error.html"); + doc = doc.Replace("${Error.Code}", StatusCode.InternalServerError.Code.ToString()); + doc=doc.Replace("${Error.Description}", ex.StackTrace ?? ex.StackTrace.ToString()); + #endregion + + context.Response.Write(doc); + context.Response.Headers.Add(HeaderProperty.ContentType, Mime.GetContentType("html")); sc = StatusCode.InternalServerError; Log.Error(ex.Message+":"+ex.StackTrace??""); } @@ -198,7 +208,7 @@ namespace Mozi.HttpEmbedded } //最后响应数据 - //if (args.Socket != null && args.Socket.Connected) + if (args.Socket != null && args.Socket.Connected) { context.Response.AddHeader(HeaderProperty.Server, ServerName); context.Response.SetStatus(sc); @@ -248,8 +258,14 @@ namespace Mozi.HttpEmbedded //判断资源类型 bool isStatic = st.IsStatic(fileext); context.Response.Headers.Add(HeaderProperty.ContentType, contenttype); + if (context.Request.Path == "/") + { + context.Response.Write(DocLoader.Load("DefaultHome.html")); + context.Response.Headers.Add(HeaderProperty.ContentType, Mime.GetContentType("html")); + return StatusCode.Success; + } //静态文件处理 - if (st.Enabled && isStatic) + else if (st.Enabled && isStatic) { //响应静态文件 if (st.Exists(path, "")) @@ -340,12 +356,7 @@ namespace Mozi.HttpEmbedded if (router.Match(context.Request.Path) != null) { object result=null; - try - { - result = router.Invoke(context); - }catch(Exception ex){ - - } + result = router.Invoke(context); if (result != null) { context.Response.Write(result.ToString()); diff --git a/Mozi.HttpEmbedded/Mozi.HttpEmbedded.csproj b/Mozi.HttpEmbedded/Mozi.HttpEmbedded.csproj index 66e8e9ba9693eb39d36789c580cb2afdb95a74c1..4fb522eab55e4cf85d1d614b74ae0dcc32b5e6b1 100644 --- a/Mozi.HttpEmbedded/Mozi.HttpEmbedded.csproj +++ b/Mozi.HttpEmbedded/Mozi.HttpEmbedded.csproj @@ -70,6 +70,7 @@ + @@ -152,9 +153,9 @@ - - - + + + diff --git a/Mozi.HttpEmbedded/Page/AbsPage.cs b/Mozi.HttpEmbedded/Page/AbsPage.cs index 3bf1082cd2397bba485ccbf6b37b90cd336f950d..ec31e556a82d0ed22396859efeb214a790a15f58 100644 --- a/Mozi.HttpEmbedded/Page/AbsPage.cs +++ b/Mozi.HttpEmbedded/Page/AbsPage.cs @@ -10,6 +10,11 @@ protected HttpResponse Response { get; set; } protected HttpContext Context { get; set; } + /// + /// 重定向 + /// + /// + public abstract void RedirectTo(string url); public abstract void Get(); diff --git a/Mozi.HttpEmbedded/Page/Global.cs b/Mozi.HttpEmbedded/Page/Global.cs index 896c21e49811743260273459f9a3c96668aef104..da61fa5bae0d51d8a672cca7ddad3849fdcc1f3c 100644 --- a/Mozi.HttpEmbedded/Page/Global.cs +++ b/Mozi.HttpEmbedded/Page/Global.cs @@ -8,11 +8,51 @@ namespace Mozi.HttpEmbedded.Page /// public class Global { - public Dictionary _data=new Dictionary(); - - public Global Set() + private static Global _global; + + private Dictionary _data = new Dictionary(); + + /// + /// 设置键值 + /// + /// + /// + /// + public Global Set(string key, object value) { + if (_data.ContainsKey(key)) + { + _data[key] = value; + } + else + { + _data.Add(key, value); + } return this; } + /// + /// 获取键值 + /// + /// + /// + public object Get(string key) + { + return _data[key]; + } + /// + /// 索引器 + /// + /// + /// + public object this[string key]{ + get + { + return Get(key); + } + set + { + Set(key, value); + } + } } } diff --git a/Mozi.HttpEmbedded/Page/PageCreator.cs b/Mozi.HttpEmbedded/Page/PageCreator.cs index 5f69003aaf1a5e0bcde03809863522b653fd8fb0..e656e47aba79be516a22012e18cace66a262df8d 100644 --- a/Mozi.HttpEmbedded/Page/PageCreator.cs +++ b/Mozi.HttpEmbedded/Page/PageCreator.cs @@ -21,6 +21,13 @@ namespace Mozi.HttpEmbedded.Page public PageCreator LoadFromStream(Stream stream) { + Prepare(); + return this; + } + public PageCreator Prepare() + { + InjectGlobal(); + InjectValues(); return this; } /// @@ -39,5 +46,15 @@ namespace Mozi.HttpEmbedded.Page { return null; } + + public byte[] GetBuffer() + { + return new byte[]{ }; + } + + public string GetPage() + { + return ""; + } } }