how to capture content markup within an httpmodule? [modified]
-
Hello all, I currently have an httpmodule that extracts various information regarding the response that is being sent to the client. One thing I want to implement is a count of how many external files (.js or .css) are being included in the page. I have a regex function for this, but the problem I'm having is I can't quite figure out how to access the markup that is being sent to the client. Here's most of what I have so far:
public class ResponseSizeHttpModule : IHttpModule
{public void Init(HttpApplication application) { if (Convert.ToBoolean(ConfigurationManager.AppSettings\["CheckResponseSize"\]) == true) { application.BeginRequest += new EventHandler(this.AddFilter); application.EndRequest += new EventHandler(this.ResponseMessage); } } private void AddFilter(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; application.Response.Filter = new CounterClass(application.Response.Filter); } private void ResponseMessage(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; long lPageSize = application.Response.Filter.Length; long lRespLimit = Convert.ToInt64(ConfigurationManager.AppSettings\["ResponseLimitBytes"\]); if (lPageSize > lRespLimit) { **string strResponse = ""; //get the content of the response here!** int intNumIncludes = CountIncludes(strResponse); //StringBuilder sbAlertMsg = new StringBuilder(); //build message here //HttpContext.Current.Response.Write(sbAlertMsg); } } private int CountIncludes(string strResponse) { Regex reScripts = new Regex(".js|.css", RegexOptions.IgnoreCase); return reScripts.Matches(strResponse).Count; } public void Dispose(){} }
The bolded part is where I'd like to try to do this. I'm pretty new to httpmodules and the like so any suggestions on more effective methods are welcome, as would any other help. Thanks in advance!
------------------- abort, retry, fail?
-
Hello all, I currently have an httpmodule that extracts various information regarding the response that is being sent to the client. One thing I want to implement is a count of how many external files (.js or .css) are being included in the page. I have a regex function for this, but the problem I'm having is I can't quite figure out how to access the markup that is being sent to the client. Here's most of what I have so far:
public class ResponseSizeHttpModule : IHttpModule
{public void Init(HttpApplication application) { if (Convert.ToBoolean(ConfigurationManager.AppSettings\["CheckResponseSize"\]) == true) { application.BeginRequest += new EventHandler(this.AddFilter); application.EndRequest += new EventHandler(this.ResponseMessage); } } private void AddFilter(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; application.Response.Filter = new CounterClass(application.Response.Filter); } private void ResponseMessage(Object source, EventArgs e) { HttpApplication application = (HttpApplication)source; long lPageSize = application.Response.Filter.Length; long lRespLimit = Convert.ToInt64(ConfigurationManager.AppSettings\["ResponseLimitBytes"\]); if (lPageSize > lRespLimit) { **string strResponse = ""; //get the content of the response here!** int intNumIncludes = CountIncludes(strResponse); //StringBuilder sbAlertMsg = new StringBuilder(); //build message here //HttpContext.Current.Response.Write(sbAlertMsg); } } private int CountIncludes(string strResponse) { Regex reScripts = new Regex(".js|.css", RegexOptions.IgnoreCase); return reScripts.Matches(strResponse).Count; } public void Dispose(){} }
The bolded part is where I'd like to try to do this. I'm pretty new to httpmodules and the like so any suggestions on more effective methods are welcome, as would any other help. Thanks in advance!
------------------- abort, retry, fail?
There's a technique used in this article[^] that is similar to what you're describing. See if that is useful to you.
-
There's a technique used in this article[^] that is similar to what you're describing. See if that is useful to you.