程式的生命周期往往比我们想像中的长,通常年纪愈大的程式包袱愈重,后面接手的人肩负的压力也愈重,如果我们知道有一个字串叫http://www.xxx.com,现在因为政策的关系必须改成https://www.xxx.com,偏偏http://www.xxx.com被到处写死在资料库及程式原始码里面,除了把写死的那些找出来改之外,我们还可以怎么做?
在ASP.NET有一个古老的东西叫 Response.Filter
,下面简述ASP.NET页面传到Client端的流程。
ASP.NET 页面透过HttpWriter 写到串流物件,最终传到使用者的电脑上呈现,而我们可以用Response.Filter 在HttpWriter 与串流物件中间插入一个我们自订的串流物件,来拦截页面内容趁机加工。
建立一个自订的串流物件
我假设我自订的串流物叫ReplacerStream
继承Stream
类别,其中在Write 方法中将要准备写入的页面资料置换成我们想要的特定字串。
public class ReplacerStream : Stream
{
private readonly Stream responseFilter;
private readonly Encoding contentEncoding;
public ReplacerStream(Stream responseFilter, Encoding contentEncoding)
{
this.responseFilter = responseFilter;
this.contentEncoding = contentEncoding;
}
...省略
public override void Write(byte[] buffer, int offset, int count)
{
var content = this.contentEncoding.GetString(buffer);
var contentBytes = this.contentEncoding.GetBytes(
Regex.Replace(content, "http://www.xxx.com", "https://www.xxx.com", RegexOptions.IgnoreCase));
this.responseFilter.Write(contentBytes, 0, contentBytes.Length);
}
}
建立一个HttpModule
自订串流物件建立好后,接着就要用它来取代Response.Filter
,我们需要在ASP.NET 管线中的其中一个Event 去做这件事情,至于要选择在哪一个Event 来做还满重要的,因为当页面已完成输出之后Response.Filter
基本上就啥用处了,因此我们必须要在输出页面前来替换掉Response.Filter
。
我选择在PostRequestHandlerExecute这个Event里面将Response.Filter取代为自订的串流物件。
public class ReplacerModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute += this.OnPostRequestHandlerExecute;
}
public void Dispose()
{
}
private void OnPostRequestHandlerExecute(object sender, EventArgs eventArgs)
{
var context = ((HttpApplication)sender).Context;
if (context.Response.ContentType.Equals("text/html"))
{
context.Response.Filter = new ReplacerStream(context.Response.Filter, context.Response.ContentEncoding);
}
}
}
在Web.Config 中新增HttpModule
写好HttpModule后,在Web.Config中<system.webServer>\<modules>
的位置加进去就行了。
原本的HTML 页面内容是http 字串,在经过自订的Response.Filter 之后就都被置换成https 了。