changing Request.InputStream

Discussion in 'ASP .Net' started by karahan celikel, Mar 2, 2004.

  1. Is there a way to modify the request stream?

    I tried to do it using Request.Filter, but no success so far.

    Thanks.

    Karahan Celikel
     
    karahan celikel, Mar 2, 2004
    #1
    1. Advertising

  2. Hi Karahan,

    Thanks for posting in the community!
    From your description, you're wanting to do some certain modification on
    the Request's InputStream and also you found using the Request Filter
    HttpModule not worked, yes?
    If there is anything I misunderstood, please feel free to let me know.

    Based on my experience, generally if we'd like to do some general
    operations on the Request's InputStream, the only way is just the Filter
    httpMoudle. But this way may be somewhat complex because that means we need
    to directly deal with the raw byte stream of the Request. Here are some
    tech articles on using the HTTPMODULE to generate Fitler:


    #Intercepting Filter
    http://msdn.microsoft.com/library/en-us/dnpatterns/html/DesInterceptingFilte
    r.asp?frame=true

    #Implementing Intercepting Filter in ASP.NET Using HTTP Module
    http://msdn.microsoft.com/library/en-us/dnpatterns/html/ImpInterceptingFilte
    rInASP.asp?frame=true

    #Filtering HTTP Requests with .NET
    http://www.ondotnet.com/pub/a/dotnet/2003/10/20/httpfilter.html

    #HttpRequest.Filter Property
    http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebHttpRequest
    ClassFilterTopic.asp?frame=true

    #Build an ASP.NET HttpRequest Log Filter
    http://www.eggheadcafe.com/articles/20030813.asp

    In addition, since you mentioned that you failed to get it work, would you
    please provide some further infos on your code logics or what kind of
    detailed problems you met? Thanks.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Mar 3, 2004
    #2
    1. Advertising

  3. Hi Steven,

    I have an HttpModule and I hook into Application's BeginRequest event here

    *******************************************************************

    private void BeginRequest(object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;
    app.Context.Request.Filter = new
    MyRequestFilter(app.Context.Request.Filter);
    }
    *******************************************************************

    MyRequest filter is a subclass of Stream. Following is that class. I won't
    show the other stream overrides for the sake of readibility.

    *******************************************************************

    public class MyRequestFilter:Stream
    {
    private Stream _sink;
    public MyRequestFilter(Stream sink)
    {
    _sink = sink;
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
    int c = _sink.Read(buffer, offset, count);
    Here I want to change the buffer
    return c;
    }

    *******************************************************************

    In the Read method I have a buffer array representing the request string. I
    can change the values in it but obviously I can't resize it.

    I want to find the occurances of string X and replace it with string Y in
    the request stream without any size limitations.

    Thanks
     
    Karahan Celikel, Mar 3, 2004
    #3
  4. Hi Karahan,

    Thanks for your response. Yes, as you've found that the Filter HttpModule
    can help modifing the Request's inputstream. However, this modification is
    very constrained that we can only do simple replacement but not change the
    stream's size. Also, do you think it possbile that we implement your former
    requirement via other means rather than modifying the input stream? Any
    way, I'll consult some further experts to see whether there are any other
    ideas.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
     
    Steven Cheng[MSFT], Mar 4, 2004
    #4
  5. Hi Steven,

    Actually Request.InputStream is readonly. As far as I understand it is the
    Filter that can be writable. If there is a filter of the request then
    Application uses this filter instead of Request.InputStream.
    Yesterday , I found a workaround to the problem. But it is not an elegant
    solution.

    Before the following Read method of the filter is called

    public override int Read(byte[] buffer, int offset, int count)
    {
    int c = _sink.Read(buffer, offset, count);
    Here I want to change the buffer
    return c;
    }

    size of the buffer array and count parameters are determined by the the
    Length property of the Filter itself. So we can deceive the caller here by
    manipulating the Length property. Here we can generate the new request
    string and return the length of it

    public override long Length
    {
    get
    {
    if(this.newRequestStr!="")
    return this.newRequestStr.Length;
    else
    {
    if(_sink.Length!=0)
    {
    this.newRequestStr = ProcessRequest();
    return this.newRequestStr.Length;
    }
    else
    return 0;
    }
    }
    }

    and we need to change the Read method

    public override int Read(byte[] buffer, int offset, int count)
    {
    byte[] newBuffer =
    System.Text.Encoding.UTF8.GetBytes(this.newRequestStr);

    for(int i=0;i<count;i++)
    buffer = newBuffer;

    return newRequestStr.Length;
    }


    But as you can see, generating the new request string in the getter of the
    Length property is something like a hack rather than an elegant solution.

    I would appreciate if you can investigate it further. There is not much
    documentation about this topic.

    Thanks

    Karahan Celikel
     
    Karahan Celikel, Mar 4, 2004
    #5
    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. Brian Birtle
    Replies:
    2
    Views:
    2,048
    John Saunders
    Oct 16, 2003
  2. steven shingler

    Email attachment from InputStream?

    steven shingler, Jan 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    382
    Shiv Kumar
    Jan 20, 2004
  3. R
    Replies:
    5
    Views:
    2,117
    Kevin McMurtrie
    Mar 13, 2005
  4. Ben
    Replies:
    1
    Views:
    11,957
    Finomosec
    Feb 16, 2006
  5. deepak

    Request.InputStream

    deepak, Feb 13, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    798
    deepak
    Feb 13, 2008
Loading...

Share This Page