Разработка распределенных приложений в Microsoft.NET Framework


Пример II1


// SevaAspHost.cs using System; using System.IO; using System.NET; using System.Web; using System.Web.Hosting; using System.Text.RegularExpressions; namespace Seva.AspHost { public class AspHost: MarshalByRefObject { private HttpListener listener; private string virtualDir; private string hostingDir; public string VirtualDir { get {return virtualDir;} } public string HostingDir { get {return hostingDir;} } public static AspHost CreateHost(string[] prefixes, string aspVirtualDir, string aspHostingDir) { if (!HttpListener.IsSupported) { throw new NotSupportedException( "Требуется Windows XP SP2/Server 2003."); } AspHost host = (AspHost) ApplicationHost.CreateApplicationHost( typeof(AspHost), aspVirtualDir, aspHostingDir); host.Init(prefixes, aspVirtualDir, aspHostingDir); return host; } public void Init(string[] prefixes, string aspVirtualDir, string aspHostingDir) { virtualDir = aspVirtualDir; hostingDir = aspHostingDir; listener = new HttpListener(); foreach (string prefix in prefixes) listener.Prefixes.Add(prefix); } public void Start() { listener.Start(); } public void Stop() { listener.Stop(); } public void ProcessRequest() { HttpListenerContext context = listener.GetContext(); HttpListenerWorkerRequest workerRequest = new HttpListenerWorkerRequest(context, this); HttpRuntime.ProcessRequest(workerRequest); } } public class HttpListenerWorkerRequest: HttpWorkerRequest { private HttpListenerContext context; private AspHost host; public HttpListenerWorkerRequest(HttpListenerContext listenerContext, AspHost aspHost) { context = listenerContext; host = aspHost; } public override void EndOfRequest() { context.Response.OutputStream.Close(); context.Response.Close(); } public override void FlushResponse(bool finalFlush) { context.Response.OutputStream.Flush(); } public override string GetHttpVerbName() { return context.Request.HttpMethod; } public override string GetHttpVersion() { return string.Format("HTTP/{0}", context.Request.ProtocolVersion.ToString()); } public override string GetLocalAddress() { return context.Request.LocalEndPoint.Address.ToString(); } public override int GetLocalPort() { return context.Request.LocalEndPoint.Port; } public override string GetQueryString() { return context.Request.Url.Query.TrimStart(new char[]{'?'}); } public override string GetRawUrl() { return context.Request.RawUrl; } public override string GetRemoteAddress() { return context.Request.RemoteEndPoint.Address.ToString(); } public override int GetRemotePort() { return context.Request.RemoteEndPoint.Port; } public override string GetUriPath() { return context.Request.Url.LocalPath; } public override void SendKnownResponseHeader(int index, string value) { context.Response.Headers[GetKnownResponseHeaderName(index)] = value; } public override void SendResponseFromMemory(byte[] data, int length) { context.Response.OutputStream.Write(data, 0, length); } public override void SendStatus(int statusCode, string statusDescription) { context.Response.StatusCode = statusCode; context.Response.StatusDescription = statusDescription; } public override void SendUnknownResponseHeader(string name, string value) { context.Response.Headers[name] = value; } public override void SendResponseFromFile(IntPtr handle, long offset, long length) { } public override void SendResponseFromFile(string filename, long offset, long length) { } public override string GetAppPath() { return host.VirtualDir; } public override string GetAppPathTranslated() { return host.HostingDir; } public override string GetUnknownRequestHeader(string name) { return context.Request.Headers[name]; } public override string GetKnownRequestHeader(int index) { switch (index) { case HeaderUserAgent: return context.Request.UserAgent; default: return context.Request.Headers[GetKnownRequestHeaderName(index)]; } } public override string GetFilePath() { string s = context.Request.Url.LocalPath; Regex re = new Regex(@"^(.*\.as\wx)\/\w+$"); Match m = re.Match(s); if (m.Success) s = m.Groups[1].Value; return s; } public override string GetFilePathTranslated() { string s = GetFilePath().Substring(host.VirtualDir.Length); return host.HostingDir + s.Replace('/', '\\'); } public override string GetPathInfo() { return context.Request.Url.LocalPath.Substring(GetFilePath().Length); } public override int ReadEntityBody(byte[] buffer, int size) { return context.Request.InputStream.Read(buffer, 0, size); } } }
Листинг II.1.
Закрыть окно






Начало  Назад  Вперед



Книжный магазин