changing Request.InputStream

  • Thread starter karahan celikel
  • Start date
K

karahan celikel

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
 
S

Steven Cheng[MSFT]

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
 
K

Karahan Celikel

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
 
S

Steven Cheng[MSFT]

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
 
K

Karahan Celikel

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
 
S

Steven Cheng[MSFT]

Hi Karahan,

After further researching and consulting some other experts. Currently it
seems that the Filter is the only way to do some genreic simple
modification. But I did not found any other means on change the In stream's
size. The only thing found is that we can completely modify the Request
Stream via SoapExtension in ASP.NET webservice(soap message). But since it
based on soap request which is quite different from our situation, we still
haven't any better solution.


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
 
Joined
Jun 7, 2012
Messages
1
Reaction score
0
I think that this is the good point of the community, some questions can live for a long time. :)
Now, I also have to modify the content of the files is uploaded by the multiple upload functon of SharePoint portal (it is using PUT method).

And this is my issue: the Filter's Read method isn't called. It works great for the asp.net website I created. But doesn't work for SharePoint.

Thank you very much.
Huy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top