IHttpModule Filter Html Response Sharepoint

Discussion in '.NET' started by Q1Q, Dec 29, 2008.

  1. Q1Q

    Q1Q

    Joined:
    Dec 29, 2008
    Messages:
    2
    Hi,

    I'm rather new to both ASP.net and Sharepoint and I need to do some filtering of the response output from a Sharepoint application.

    I've read about the possibility to intercept the response in ASP.net applications "just before" it is sent to the client. It succeeded to do that for asp-pages served by Sharepoint, but my problem is that I want to intercept the pure HTML files only.

    I do catch the response but I'm not able to get the call to the overridden write() in my filter. It only works for the asp-pages.

    See the code below. Could somebody please have a look and try to help me out =)

    public class MOSSCleanupModule : IHttpModule
    {
    public void Init(HttpApplication app)
    {
    app.ReleaseRequestState += new EventHandler(InstallResponseFilter);
    }

    private void InstallResponseFilter(object sender, EventArgs e)
    {
    HttpResponse response = HttpContext.Current.Response;
    HttpRequest request = HttpContext.Current.Request;
    if (response.ContentType == "text/html" && request.Url.AbsolutePath.EndsWith(".htm"))
    {
    Debug.Write("in InstallResponseFilter()");
    response.Filter = new ModifyHtmlContentFilter(response.Filter);
    }
    }

    public void Dispose()
    {}
    }

    public class ModifyHtmlContentFilter : Stream
    {
    private Stream responseStream;

    public ModifyHtmlContentFilter(Stream inputStream)>
    {
    Debug.Write("in ModifyHtmlContentFilter");
    this.responseStream = inputStream;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
    Debug.Write("in custom Write()");
    StringBuilder html = new StringBuilder(System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count));
    this.modifyHtml(html, "..");
    byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(html.ToString());
    responseStream.Write(data, 0, data.Length);
    }

    private void modifyHtml(StringBuilder html, string search)
    {
    Debug.Write("in modifyHtml()");
    // Modify the html
    }

    #region Filter overrides

    public override bool CanRead{get{return true;}

    public override bool CanSeek{get{return true;}}

    public override bool CanWrite{get{return true;}}

    public override void Close(){responseStream.Close();}

    public override void Flush(){responseStream.Flush();}

    public override long Length{get{return 0;}}

    public override long Position{get{return position;}set{position = value;}}

    public override long Seek(long offset, SeekOrigin origin){return responseStream.Seek(offset, origin);}

    public override void SetLength(long length){responseStream.SetLength(length);}

    public override int Read(byte[] buffer, int offset, int count){return responseStream.Read(buffer, offset, count);}

    #endregion

    }
    }
    Q1Q, Dec 29, 2008
    #1
    1. Advertising

  2. Q1Q

    Q1Q

    Joined:
    Dec 29, 2008
    Messages:
    2
    It started working for pure html file when I changed the EventHandler to BeginRequest to catch it earlier in the pipeline.
    Q1Q, Dec 30, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan Godwin
    Replies:
    0
    Views:
    3,776
    Duncan Godwin
    Jun 25, 2003
  2. Steven Livingstone

    http pipline, ihttpmodule and filtering responses

    Steven Livingstone, Oct 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    457
  3. Brad
    Replies:
    3
    Views:
    1,870
    Steven Cheng[MSFT]
    Feb 19, 2004
  4. =?Utf-8?B?QnJpYW4gTm9ybWFu?=

    HttpModule read Response without using Response.Filter

    =?Utf-8?B?QnJpYW4gTm9ybWFu?=, Jun 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    2,625
    John Timney \(ASP.NET MVP\)
    Jun 13, 2005
  5. zax75
    Replies:
    1
    Views:
    1,096
Loading...

Share This Page