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 { diff --git a/AspNetServer/Program.cs b/AspNetServer/Program.cs index 5b3cade..4aabee8 100644 --- a/AspNetServer/Program.cs +++ b/AspNetServer/Program.cs @@ -1,9 +1,12 @@ -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.Threading; using System.Web.Hosting; namespace AspNetServer @@ -16,7 +19,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 +28,14 @@ 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应用程序域 @@ -39,5 +50,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); + } + } } }