文章出處

我們在上面對ASP.NET Core默認提供的具有跨平臺能力的KestrelServer進行了詳細介紹(《聊聊ASP.NET Core默認提供的這個跨平臺的服務器——KestrelServer》),為了讓讀者朋友們對管道中的Server具有更加深刻的認識,接下來我們采用實例演示的形式創建一個自定義的Server。這個自定義的Server直接利用HttpListener來完成針對請求的監聽、接收和響應,我們將其命名為HttpListenerServer。在正式介紹HttpListenerServer的設計和實現之前,我們先來顯示一下如何將它應用到 一個具體的Web應用中。

一、HttpListenerServer的使用

我們依然采用最簡單的Hello World應用來演示針對HttpListenerServer的應用,所以我們在Startup類的Configure方法中編寫如下的程序直接響應一個“Hello World”字符串。

   1: public class Startup
   2: {
   3:     public void Configure(IApplicationBuilder app)
   4:     {
   5:         app.Run(context => context.Response.WriteAsync("Hello World!"));
   6:     }
   7: }

在作為程序入口的Main方法中,我們直接創建一個WebHostBuilder對象并調用擴展方法UseHttpListener完成針對自定義HttpListenerServer的注冊。我們接下來調用UseStartup方法注冊上面定義的這個啟動類型,然后調用Build方法創建一個WebHost對象,最后調用Run方法運行這個作為宿主的WebHost。

   1: public class Program
   2: {
   3:     public static void Main()
   4:     {
   5:         new WebHostBuilder()
   6:             .UseHttpListener()
   7:             .UseStartup<Startup>()
   8:             .Build()
   9:             .Run();
  10:     }
  11: }
  12:  
  13: public static class WebHostBuilderExtensions
  14: {
  15:     public static IWebHostBuilder UseHttpListener(this IWebHostBuilder builder)
  16:     {
  17:         builder.ConfigureServices(services => services.AddSingleton<IServer, HttpListenerServer>());
  18:         return builder;
  19:     }
  20: }

我們自定義的擴展方法UseHttpListener的邏輯很簡單,它只是調用WebHostBuilder的ConfigureServices方法將我們自定義的HttpListenerServer類型以單例模式注冊到指定的ServiceCollection上而已。我們直接運行這個程序并利用瀏覽器訪問默認的監聽地址(http://localhost:5000),服務端響應的“Hello World”字符串會按照如下圖所示的形式顯示在瀏覽器上。

 

二、總體設計

接下來我們來介紹一下HttpListenerServer的大體涉及。除了HttpListenerServer這個實現了IServer的自定義Server類型之外,我們只定義了一個名為HttpListenerServerFeature的特性類型,下圖所示的UML基本上體現了HttpListenerServer的總體設計。

 

三、HttpListenerServerFeature

如果我們利用HttpListener來監聽請求,它會為接收到的每次請求創建一個屬于自己的上下文,具體來說這是一個類型為HttpListenerContext對象。我們可以利用這個HttpListenerContext對象獲取所有與請求相關的信息,針對請求的任何響應也都是利用它完成的。上面這個HttpListenerServerFeature實際上就是對這個作為原始上下文的HttpListenerContext對象的封裝,或者說它是管道使用的DefaultHttpContext與這個原始上下文之間溝通的中介。

如下所示的代碼片段展示了HttpListenerServerFeature類型的完整定義。簡單起見,我們并沒有實現上面提到過的所有特性接口,而只是選擇性地實現了IHttpRequestFeature和IHttpResponseFeature這兩個最為核心的特性接口。它的構造函數除了具有一個類型為HttpListenerContext的參數之外,還具有一個字符串的參數pathBase用來指定請求URL的基地址(對應IHttpRequestFeature的PathBase屬性),我們利用它來計算請求URL的相對地址(對應IHttpRequestFeature的Path屬性)。IHttpRequestFeature和IHttpResponseFeature中定義的屬性都可以直接利用HttpListenerContext對應的成員來實現,這方面并沒有什么特別之處。

   1: public class HttpListenerServerFeature : IHttpRequestFeature, IHttpResponseFeature
   2: {
   3:     private readonly HttpListenerContext     httpListenerContext;
   4:     private string            queryString;
   5:     private IHeaderDictionary         requestHeaders;
   6:     private IHeaderDictionary         responseHeaders;
   7:     private string            protocol;
   8:     private readonly string       pathBase;
   9:  
  10:     public HttpListenerServerFeature(HttpListenerContext httpListenerContext, string pathBase)
  11:     {
  12:         this.httpListenerContext     = httpListenerContext;
  13:         this.pathBase         = pathBase;
  14:     }
  15:  
  16:     #region IHttpRequestFeature
  17:  
  18:     Stream IHttpRequestFeature.Body
  19:     {
  20:         get { return httpListenerContext.Request.InputStream; }
  21:         set { throw new NotImplementedException(); }
  22:     }
  23:  
  24:     IHeaderDictionary IHttpRequestFeature.Headers
  25:     {
  26:         get { return requestHeaders 
  27:          ?? (requestHeaders = GetHttpHeaders(httpListenerContext.Request.Headers)); }
  28:         set { throw new NotImplementedException(); }
  29:     }
  30:  
  31:     string IHttpRequestFeature.Method
  32:     {
  33:         get { return httpListenerContext.Request.HttpMethod; }
  34:         set { throw new NotImplementedException(); }
  35:     }
  36:  
  37:     string IHttpRequestFeature.Path
  38:     {
  39:         get { return httpListenerContext.Request.RawUrl.Substring(pathBase.Length);}
  40:         set { throw new NotImplementedException(); }
  41:     }
  42:  
  43:     string IHttpRequestFeature.PathBase
  44:     {
  45:         get { return pathBase; }
  46:         set { throw new NotImplementedException(); }
  47:     }
  48:  
  49:     string IHttpRequestFeature.Protocol
  50:     {
  51:         get{ return protocol ?? (protocol = this.GetProtocol());}
  52:         set { throw new NotImplementedException(); }
  53:     }
  54:  
  55:     string IHttpRequestFeature.QueryString
  56:     {
  57:         Get { return queryString ?? (queryString = this.ResolveQueryString());}
  58:         set { throw new NotImplementedException(); }
  59:     }
  60:  
  61:     string IHttpRequestFeature.Scheme
  62:     {
  63:         get { return httpListenerContext.Request.IsWebSocketRequest ? "https" : "http"; }
  64:         set { throw new NotImplementedException(); }
  65:     }
  66:     #endregion
  67:  
  68:     #region IHttpResponseFeature
  69:     Stream IHttpResponseFeature.Body
  70:     {
  71:         get { return httpListenerContext.Response.OutputStream; }
  72:         set { throw new NotImplementedException(); }
  73:     }
  74:  
  75:     string IHttpResponseFeature.ReasonPhrase
  76:     {
  77:         get { return httpListenerContext.Response.StatusDescription; }
  78:         set { httpListenerContext.Response.StatusDescription = value; }
  79:     }
  80:  
  81:     bool IHttpResponseFeature.HasStarted
  82:     {
  83:         get { return httpListenerContext.Response.SendChunked; }
  84:     }
  85:  
  86:     IHeaderDictionary IHttpResponseFeature.Headers
  87:     {
  88:         get { return responseHeaders 
  89:             ?? (responseHeaders = GetHttpHeaders(httpListenerContext.Response.Headers)); }
  90:         set { throw new NotImplementedException(); }
  91:     }
  92:     int IHttpResponseFeature.StatusCode
  93:     {
  94:         get { return httpListenerContext.Response.StatusCode; }
  95:         set { httpListenerContext.Response.StatusCode = value; }
  96:     }
  97:  
  98:     void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
  99:     {
 100:         throw new NotImplementedException();
 101:     }
 102:  
 103:     void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
 104:     {
 105:         throw new NotImplementedException();
 106:     }
 107:     #endregion
 108:  
 109:     private string ResolveQueryString()
 110:     {
 111:         string queryString = "";
 112:         var collection = httpListenerContext.Request.QueryString;
 113:         for (int i = 0; i < collection.Count; i++)
 114:         {
 115:             queryString += $"{collection.GetKey(i)}={collection.Get(i)}&";
 116:         }
 117:         return queryString.TrimEnd('&');
 118:     }
 119:  
 120:     private IHeaderDictionary GetHttpHeaders(NameValueCollection headers)
 121:     {
 122:         HeaderDictionary dictionary = new HeaderDictionary();
 123:         foreach (string name in headers.Keys)
 124:         {
 125:             dictionary[name] = new StringValues(headers.GetValues(name));
 126:         }
 127:         return dictionary;
 128:     }
 129:  
 130:     private string GetProtocol()
 131:     {
 132:         HttpListenerRequest request = httpListenerContext.Request;
 133:         Version version = request.ProtocolVersion;
 134:         return string.Format("{0}/{1}.{2}", request.IsWebSocketRequest ? "HTTPS" : "HTTP", version.Major, version.Minor);
 135:     }
 136: }


四、HttpListenerServer

接下來我們來看看HttpListenerServer的定義。如下面的代碼片段所示,用來監聽請求的HttpListener在構造函數中被創建,與此同時,我們會創建一個用于獲取監聽地址的ServerAddressesFeature對象并將其添加到屬于自己的特性列表中。當HttpListenerServer隨著Start方法的調用而被啟動后,它將這個ServerAddressesFeature對象提取出來,然后利用它得到所有的地址并添加到HttpListener的Prefixes屬性表示的監聽地址列表中。接下來,HttpListener的Start方法被調用,并在一個無限循環中開啟請求的監聽與接收。

   1: public class HttpListenerServer : IServer
   2: {
   3:     private readonly HttpListener listener;
   4:  
   5:     public IFeatureCollection Features { get; } = new FeatureCollection();
   6:     
   7:     public HttpListenerServer()
   8:     {
   9:         listener = new HttpListener();
  10:         this.Features.Set<IServerAddressesFeature>(new ServerAddressesFeature());
  11:     }
  12:  
  13:     public void Dispose()
  14:     {
  15:         listener.Stop();
  16:     }
  17:  
  18:     public void Start<TContext>(IHttpApplication<TContext> application)
  19:     {
  20:         foreach (string address in this.Features.Get<IServerAddressesFeature>().Addresses)
  21:         {
  22:             listener.Prefixes.Add(address.TrimEnd('/') + "/");
  23:         }
  24:  
  25:         listener.Start();
  26:         while (true)
  27:         {
  28:             HttpListenerContext httpListenerContext = listener.GetContext();
  29:  
  30:             string listenUrl = this.Features.Get<IServerAddressesFeature>().Addresses.First(address => httpListenerContext.Request.Url.IsBaseOf(new Uri(address)));
  31:             string pathBase = new Uri(listenUrl).LocalPath.TrimEnd('/') ;
  32:             HttpListenerServerFeature feature = new HttpListenerServerFeature(httpListenerContext, pathBase);
  33:  
  34:             FeatureCollection features = new FeatureCollection();
  35:             features.Set<IHttpRequestFeature>(feature);
  36:             features.Set<IHttpResponseFeature>(feature);
  37:             TContext context = application.CreateContext(features);
  38:  
  39:             application.ProcessRequestAsync(context).ContinueWith(task =>
  40:             {
  41:                 httpListenerContext.Response.Close();
  42:                 application.DisposeContext(context, task.Exception);
  43:             });
  44:         }
  45:     }
  46: }

HttpListener的GetContext方法以同步的方式監聽請求,并利用接收到的請求創建返回的HttpListenerContext對象。我們利用它解析出當前請求的基地址,并進一步創建出描述當前原始上下文的HttpListenerServerFeature。接下來我們將這個對象分別采用特性接口IHttpRequestFeature和IHttpResponseFeature添加到創建的FeatureCollection對象中。然后我們將這個FeatureCollection作為參數調用HttpApplication的CreateContext創建出上下文對象,并將其作為參數調用HttpApplication的ProcessContext方法讓注冊的中間件來逐個地對請求進行處理。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()