views:

22

answers:

1

Hi,

I need to modify the content of certain HttpRequests (SSAS connection strings) in IIS. Basically, I need to add an element to the SOAP contained in the request.

My approach so far has been to add a Filter to the HttpRequest, and perform the change in the filter's Read method. As far as I can tell, though, Read is never being executed.

My understanding of the Request.Filter is that it gets read from when IIS processes the request, so IIS should see my modified Request.

Is what I'm trying to do actually possible using an HttpModule and is my Filter approach correct?

If so, what would cause Read to not be hit?

Here's a simplified version of my code:


public class CustomHttpModule : IHttpModule {
    private HttpApplication app;
    public string ModuleName {
        get { return "CustomHttpModule"; }
    }
    public void Init(HttpApplication context) {
        app = context;
        context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);
    }
    void context_PreRequestHandlerExecute(object sender, EventArgs e) {
        var request = app.Context.Request;
        request.Filter = new CustomHttpFilter(request.Filter);
    }
}

public class CustomHttpFilter : Stream {
    private Stream outputStream;
    public CustomHttpFilter(Stream outputFilter) {
        outputStream = outputFilter;
    }
    public override int Read(byte[] buffer, int offset, int count) {
        // read and make the necessary changes
    }
}
A: 

I do not believe it is possible to modify the request with an http module, however it is possible to modify the response. The HttpRequest object is mostly read-only, so it is generally immutable in any context, not just from a module.

If you were really desperate, you might try using reflection to get access to more of the http request object. To access, modify, or invoke non-public members, however, your process would need full trust permissions, which is very risky in a web environment.

jrista