Relationship and difference between Output and OutputStream and Filter of HttpResponse

Source: Internet
Author: User

Such code is often seen on the Internet

HttpResponse response = HttpContext. Current. Response;
Response. Filter = new PageFilter (response. Filter );

To intercept the output stream, and make something similar, such as asp.net's js merge compression. Now I also want to talk about what these things are, we need to be familiar with the lifecycle of asp.net. If you are not familiar with the lifecycle, we suggest you first look at ASP. NET Request Processing Process ASP. NET pipelines and application Lifecycle

First, let's take a look at the source code of these three attributes:

Copy codeThe Code is as follows: public TextWriter Output
{
Get
{
Return this. _ writer;
}
Set
{
This. _ writer = value;
}
}

Public Stream OutputStream
{
Get
{
If (! This. UsingHttpWriter)
{
Throw new HttpException (SR. GetString ("OutputStream_NotAvail "));
}
Return this. _ httpWriter. OutputStream;
}
}

Copy codeThe Code is as follows: public Stream Filter
{
Get
{
If (this. UsingHttpWriter)
{
Return this. _ httpWriter. GetCurrentFilter ();
}
Return null;
}
Set
{
If (! This. UsingHttpWriter)
{
Throw new HttpException (SR. GetString ("Filtering_not_allowed "));
}
This. _ httpWriter. InstallFilter (value );
IIS7WorkerRequest request = this. _ wr as IIS7WorkerRequest;
If (request! = Null)
{
Request. ResponseFilterInstalled ();
}
}
}

We can see that both Filter and OutputStream use the UsingHttpWriter attribute. How is this attribute defined?Copy codeThe Code is as follows: private bool UsingHttpWriter
{
Get
{
Return (this. _ httpWriter! = Null) & (this. _ writer = this. _ httpWriter ));
}
}

From this attribute, we can know that _ writer and _ httpWriter are actually the same stuff. Their types are HttpWriter, while HttpWriter inherits from TextWriter. Now we can explain that Output is _ httpWriter, while OutputStream is the OutputStream attribute of _ httpWriter. The main code for the HttpWriter class is as follows:Copy codeThe Code is as follows: public Stream OutputStream
{
Get
{
Return this. _ stream;
}
}

Internal HttpWriter (HttpResponse response): base (null)
{
This. _ response = response;
This. _ stream = new HttpResponseStream (this );
This. _ buffers = new ArrayList ();
This. _ lastBuffer = null;
This. _ charBuffer = (char []) s_Allocator.GetBuffer ();
This. _ charBufferLength = this. _ charBuffer. Length;
This. _ charBufferFree = this. _ charBufferLength;
This. UpdateResponseBuffering ();
}

Internal HttpResponseStream (HttpWriter writer)
{
This. _ writer = writer;
}

HttpResponse calls the InstallFilter method of the HttpWriter class in the Filter attribute setting, and obtains the GetCurrentFilter that calls the class.Copy codeThe Code is as follows: internal void InstallFilter (Stream filter)
{
If (this. _ filterSink = null)
{
Throw new HttpException (SR. GetString ("Invalid_response_filter "));
}
This. _ installedFilter = filter;
}

Internal Stream GetCurrentFilter ()
{
If (this. _ installedFilter! = Null)
{
Return this. _ installedFilter;
}
If (this. _ filterSink = null)
{
This. _ filterSink = new HttpResponseStreamFilterSink (this );
}
Return this. _ filterSink;
}

From the above code, we can know that the Output stream of HttpResponse is the stream set by the Filter attribute, that is, the Output stream of the Output and OutputStream attributes of HttpResponse are all from the stream in the Filter. Let's see when _ writer and _ httpWriter initialize them? There is a method in HttpResonseCopy codeThe Code is as follows: internal void InitResponseWriter ()
{
If (this. _ httpWriter = null)
{
This. _ httpWriter = new HttpWriter (this );
This. _ writer = this. _ httpWriter;
}
}

This method is called by ProcessRequestInternal of HttpRuntime.Copy codeThe Code is as follows: private void ProcessRequestInternal (HttpWorkerRequest wr)
{
HttpContext context;
Try
{
Context = new HttpContext (wr, false );
}
Catch
{
Wr. SendStatus (400, "Bad Request ");
Wr. SendKnownResponseHeader (12, "text/html; charset = UTF-8 ");
Byte [] bytes = Encoding. ASCII. GetBytes ("Wr. SendResponseFromMemory (bytes, bytes. Length );
Wr. FlushResponse (true );
Wr. EndOfRequest ();
Return;
}
Wr. SetEndOfSendNotification (this. _ asyncEndOfSendCallback, context );
Interlocked. Increment (ref this. _ activeRequestCount );
HostingEnvironment. IncrementBusyCount ();
Try
{
Try
{
This. EnsureFirstRequestInit (context );
}
Catch
{
If (! Context. Request. IsDebuggingRequest)
{
Throw;
}
}
Context. Response. InitResponseWriter ();
IHttpHandler applicationInstance = HttpApplicationFactory. GetApplicationInstance (context );
If (applicationInstance = null)
{
Throw new HttpException (SR. GetString ("Unable_create_app_object "));
}
If (EtwTrace. IsTraceEnabled (5, 1 ))
{
EtwTrace. Trace (EtwTraceType. ETW_TYPE_START_HANDLER, context. WorkerRequest, applicationInstance. GetType (). FullName, "Start ");
}
If (applicationInstance is IHttpAsyncHandler)
{
IHttpAsyncHandler handler2 = (IHttpAsyncHandler) applicationInstance;
Context. AsyncAppHandler = handler2;
Handler2.BeginProcessRequest (context, this. _ handlerCompletionCallback, context );
}
Else
{
ApplicationInstance. ProcessRequest (context );
This. FinishRequest (context. WorkerRequest, context, null );
}
}
Catch (Exception exception)
{
Context. Response. InitResponseWriter ();
This. FinishRequest (wr, context, exception );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.