From 8104d18bfcbd48c856987059129c7a8bf1d1bef6 Mon Sep 17 00:00:00 2001 From: wxy Date: Fri, 8 Aug 2014 23:35:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=89=93=E5=BC=80=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AspNetServer/Program.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/AspNetServer/Program.cs b/AspNetServer/Program.cs index 5b3cade..ebb6984 100644 --- a/AspNetServer/Program.cs +++ b/AspNetServer/Program.cs @@ -1,9 +1,11 @@ -using System; +using Microsoft.Win32; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Web.Hosting; namespace AspNetServer @@ -16,7 +18,7 @@ static void Main(string[] args) string dir = Directory.GetCurrentDirectory(); if(args.Length==0 || !int.TryParse(args[0],out port)) { - port = 45758; + port = 80; } InitHostFile(dir); @@ -25,6 +27,7 @@ static void Main(string[] args) WebServer server = new WebServer(host, port); server.Start(); + OpenUrl("http://127.0.0.1/default.aspx"); } //需要拷贝执行文件 才能创建ASP.NET应用程序域 @@ -39,5 +42,21 @@ private static void InitHostFile(string dir) File.Delete(target); File.Copy(source, target); } + + public static void OpenUrl(string url = "") + { + RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\"); + string s = key.GetValue("").ToString(); + + Regex reg = new Regex("\"([^\"]+)\""); + MatchCollection matchs = reg.Matches(s); + + string filename = ""; + if (matchs.Count > 0) + { + filename = matchs[0].Groups[1].Value; + System.Diagnostics.Process.Start(filename, url); + } + } } } From 1ac209137a837c77e9c3a23918d84e0abc5c1544 Mon Sep 17 00:00:00 2001 From: wxy Date: Thu, 14 Aug 2014 13:15:28 +0800 Subject: [PATCH 2/3] =?UTF-8?q?#=20=E5=9B=9E=E8=BD=A6=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AspNetServer/Program.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/AspNetServer/Program.cs b/AspNetServer/Program.cs index ebb6984..4aabee8 100644 --- a/AspNetServer/Program.cs +++ b/AspNetServer/Program.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Web.Hosting; namespace AspNetServer @@ -28,6 +29,13 @@ static void Main(string[] args) WebServer server = new WebServer(host, port); server.Start(); OpenUrl("http://127.0.0.1/default.aspx"); + + var key = Console.ReadKey(); + while ( key.Key != ConsoleKey.Enter) + { + Thread.Sleep(1000); + } + Console.ReadKey(); } //需要拷贝执行文件 才能创建ASP.NET应用程序域 From 48b170cc7c8e34d74b96a71538651e914ed58a02 Mon Sep 17 00:00:00 2001 From: wxy Date: Thu, 14 Aug 2014 16:15:44 +0800 Subject: [PATCH 3/3] * WaitForRequestBytes this._socket.Poll(0x186a0, SelectMode.SelectRead); --- AspNetServer/HttpProcessor.cs | 58 ++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/AspNetServer/HttpProcessor.cs b/AspNetServer/HttpProcessor.cs index 3a55408..d6415bf 100644 --- a/AspNetServer/HttpProcessor.cs +++ b/AspNetServer/HttpProcessor.cs @@ -39,33 +39,61 @@ public HttpProcessor(SimpleHost host, Socket socket) _socket = socket; } - public void ProcessRequest() + internal int WaitForRequestBytes() { + int available = 0; try { - RequestInfo requestInfo = ParseRequest(); - if (requestInfo != null) + if (this._socket.Available == 0) { - string staticContentType = GetStaticContentType(requestInfo); - if (!string.IsNullOrEmpty(staticContentType)) + this._socket.Poll(0x186a0, SelectMode.SelectRead); + if ((this._socket.Available == 0) && this._socket.Connected) { - WriteFileResponse(requestInfo.FilePath, staticContentType); + this._socket.Poll(0x1c9c380, SelectMode.SelectRead); } - else if (requestInfo.FilePath.EndsWith("/")) + } + available = this._socket.Available; + } + catch + { + } + return available; + } + + public void ProcessRequest() + { + try + { + if (WaitForRequestBytes() == 0) + { + SendErrorResponse(400); + } + else + { + RequestInfo requestInfo = ParseRequest(); + if (requestInfo != null) { - WriteDirResponse(requestInfo.FilePath); + string staticContentType = GetStaticContentType(requestInfo); + if (!string.IsNullOrEmpty(staticContentType)) + { + WriteFileResponse(requestInfo.FilePath, staticContentType); + } + else if (requestInfo.FilePath.EndsWith("/")) + { + WriteDirResponse(requestInfo.FilePath); + } + else + { + _host.ProcessRequest(this, requestInfo); + //WorkerRequest workerRequest = new WorkerRequest(this, requestInfo); + //HttpRuntime.ProcessRequest(workerRequest); + } } else { - _host.ProcessRequest(this, requestInfo); - //WorkerRequest workerRequest = new WorkerRequest(this, requestInfo); - //HttpRuntime.ProcessRequest(workerRequest); + SendErrorResponse(400); } } - else - { - SendErrorResponse(400); - } } finally {