Can I change the content of an incoming HTTP request using a HTTP Module?RSS

1 reply

Last post Feb 20, 2014 06:20 AM by SenneVL

  • Can I change the content of an incoming HTTP request using a HTTP Module?

    Nov 28, 2013 02:53 AM|Nitramk|LINK

    I'm trying to extend my REST service (built using WCF/webHttpBinding) so that clients can upload gzipped data. I'm not sure what the best way to accomplish this but I thought it would be fairly easy by adding a HTTP module which will decompress the data if Content-Encoding for the incoming request is set to gzip.

    So I created an class deriving from IHttpModule with the following implementation:  

    private void OnBeginRequest(object sender, EventArgs e)
      {
         var app = (HttpApplication) sender;
         var context = app.Context;
    
         var contentEncoding = context.Request.Headers["Content-Encoding"];
    
         if (contentEncoding == "gzip")
         {
            // some debug code:
            var decompressedStream = new GZipStream(context.Request.InputStream, CompressionMode.Decompress);
            var memoryStream = new MemoryStream();
            decompressedStream.CopyTo(memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
    
            var streamReader = new StreamReader(memoryStream);
            string msg = streamReader.ReadToEnd();
    
            context.Request.InputStream.Seek(0, SeekOrigin.Begin);
    
            app.Request.Filter = //new TestFilterStream(app.Request.Filter);
                        new System.IO.Compression.GZipStream(
                        app.Request.Filter, CompressionMode.Decompress);
         }
    
      }


     

    The issue I'm seeing is that the GZipStream decompression is never actually performed. I've confirmed that the incoming data is in fact gzip'd (the msg-variable contains the proper data). I've also tried creating my own stream class (TestFilterStream) above and assign that to app.Request.Filter and I've confirmed that no members on the stream class is actually called by ASP.NET. So it seems like while it's possible to specify a filter, that filter isn't actually used.

    Isn't HttpApplication.Request.Filter actually used?

    httpmodule filter request

  • Re: Can I change the content of an incoming HTTP request using a HTTP Module?

    Feb 20, 2014 06:20 AM|SenneVL|LINK

    Hi,

    as far as I know, IIS has everything in place to do gzip deflate!

    http://weblog.west-wind.com/posts/2011/May/05/Builtin-GZipDeflate-Compression-on-IIS-7x

    httpmodule filter request