[asp.net] Handle closed responses gracefully.
authorMarek Habersack <grendel@twistedcode.net>
Thu, 30 Sep 2010 21:45:59 +0000 (23:45 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Thu, 30 Sep 2010 21:52:27 +0000 (23:52 +0200)
If ReleaseResources or Flush (true) were called, a flag is set that causes
any subsequent Flush () calls to fail with an exception.
At the same time, ReleaseResources does not destroy the output stream completely -
it has to remain around to match .NET behavior.

mcs/class/System.Web/System.Web/HttpResponse.cs

index 9aa99d9244bba7f1fefdf3e5dd6a763be74ae5a0..71402a1fcbe64f795d8831280293737d2e40a430 100644 (file)
@@ -99,6 +99,7 @@ namespace System.Web
                internal bool use_chunked;
                
                bool closed;
+               bool completed;
                internal bool suppress_content;
 
                //
@@ -801,6 +802,9 @@ namespace System.Web
 
                internal void Flush (bool final_flush)
                {
+                       if (completed)
+                               throw new HttpException ("Server cannot flush a completed response");
+                       
                        DoFilter (final_flush);
                        if (!headers_sent){
                                if (final_flush || status_code != 200)
@@ -814,9 +818,11 @@ namespace System.Web
                                output_stream.Clear ();
                                if (WorkerRequest != null)
                                        output_stream.Flush (WorkerRequest, true); // ignore final_flush here.
+                               completed = true;
                                return;
                        }
-
+                       completed = final_flush;
+                       
                        if (!headers_sent)
                                WriteHeaders (final_flush);
 
@@ -1321,8 +1327,12 @@ namespace System.Web
 
                internal void ReleaseResources ()
                {
+                       if (completed)
+                               return;
+                       
                        output_stream.ReleaseResources (true);
-                       output_stream = null;
+                       Close ();
+                       completed = true;
                }
        }