2003-10-06 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpResponse.cs
index f6b6182dd8a893f3c9984d7b2222502bca88a8dd..dcce865f49b14ff75f2dfdccc00f7615e356ac9f 100644 (file)
@@ -34,6 +34,7 @@ namespace System.Web
                bool _bBuffering;
                bool _bHeadersSent;
                bool _bFlushing;
+               bool filtered;
                long _lContentLength;
                int _iStatusCode;
 
@@ -122,10 +123,12 @@ namespace System.Web
                        Flush (true);
                }
 
-               internal void DoFilter ()
+               internal void DoFilter (bool really)
                {
-                       if (null != _Writer) 
+                       if (really && null != _Writer) 
                                _Writer.FilterData (true);
+
+                       filtered = true;
                }
 
                [MonoTODO("We need to add cache headers also")]
@@ -176,8 +179,9 @@ namespace System.Web
 
                        if (_Cookies != null) {
                                int length = _Cookies.Count;
-                               for (int i = 0; i < length; i++)
+                               for (int i = 0; i < length; i++) {
                                        oHeaders.Add (_Cookies.Get (i).GetCookieHeader ());
+                               }
                        }
 
                        return oHeaders;
@@ -269,10 +273,21 @@ namespace System.Web
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO()]
                public string ApplyAppPathModifier (string virtualPath)
                {
-                       throw new NotImplementedException ();
+                       if (virtualPath == null)
+                               return null;
+
+                       if (virtualPath == "")
+                               return _Context.Request.RootVirtualDir;
+
+                       if (UrlUtils.IsRelativeUrl (virtualPath)) {
+                               virtualPath = UrlUtils.Combine (_Context.Request.RootVirtualDir, virtualPath);
+                       } else if (UrlUtils.IsRooted (virtualPath)) {
+                               virtualPath = UrlUtils.Reduce (virtualPath);
+                       }
+
+                       return virtualPath;
                }
 
                public bool Buffer
@@ -507,7 +522,7 @@ namespace System.Web
                        }
                }
 
-               public HttpRequest Request
+               HttpRequest Request
                {
                        get {
                                if (_Context == null)
@@ -631,14 +646,14 @@ namespace System.Web
                        Flush (true);
                }
 
-               [MonoTODO("Check timeout and if we can cancel the thread...")]
                public void End ()
                {
-                       if (!_bEnded) {
-                               Flush ();
-                               _WorkerRequest.CloseConnection ();
-                               _bEnded = true;
-                       }
+                       if (_bEnded)
+                               return;
+
+                       Flush ();
+                       _bEnded = true;
+                       _Context.ApplicationInstance.CompleteRequest ();
                }
 
                public void Flush ()
@@ -660,10 +675,12 @@ namespace System.Web
                        }
 
                        try {
-                               long length;
-                               if (!_bHeadersSent && !_bSuppressHeaders && !_bClientDisconnected) {
+                               if (_bClientDisconnected)
+                                       return;
+
+                               long length = _Writer.BufferSize;
+                               if (!_bHeadersSent && !_bSuppressHeaders) {
                                        if (bFinish) {
-                                               length = _Writer.BufferSize;
                                                if (length == 0 && _lContentLength == 0)
                                                        _sContentType = null;
 
@@ -671,27 +688,44 @@ namespace System.Web
                                                length = _Writer.BufferSize;
                                                if (length != 0)
                                                        _WorkerRequest.SendCalculatedContentLength ((int) length);
-                                       } else if (_lContentLength == 0 && _iStatusCode == 200 &&
+                                       } else {
+                                               if (_lContentLength == 0 && _iStatusCode == 200 &&
                                                   _sTransferEncoding == null) {
-                                               // Check we are going todo chunked encoding
-                                               string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
-
-                                               if (sProto != null && sProto == "HTTP/1.1") {
-                                                       AppendHeader (
-                                                               HttpWorkerRequest.HeaderTransferEncoding,
-                                                               "chunked");
-                                               }  else {
-                                                       // Just in case, the old browsers sends a HTTP/1.0
-                                                       // request with Connection: Keep-Alive
-                                                       AppendHeader (
-                                                               HttpWorkerRequest.HeaderConnection,
-                                                               "Close");
+                                                       // Check we are going todo chunked encoding
+                                                       string sProto = Request.ServerVariables ["SERVER_PROTOCOL"];
+                                                       sProto = "HTTP/1.0"; // Remove this line when we support properly
+                                                                            // chunked content
+
+                                                       if (sProto != null && sProto == "HTTP/1.1") {
+                                                               AppendHeader (
+                                                                       HttpWorkerRequest.HeaderTransferEncoding,
+                                                                       "chunked");
+                                                       }  else {
+                                                               // Just in case, the old browsers send a HTTP/1.0
+                                                               // request with Connection: Keep-Alive
+                                                               AppendHeader (
+                                                                       HttpWorkerRequest.HeaderConnection,
+                                                                       "Close");
+                                                       }
                                                }
 
+                                               length = _Writer.BufferSize;
                                                SendHeaders ();
                                        }
                                }
 
+                               if (!filtered) {
+                                       _Writer.FilterData (false);
+                                       length = _Writer.BufferSize;
+                               }
+
+                               if (length == 0) {
+                                       _WorkerRequest.FlushResponse (bFinish);
+                                       if (!bFinish)
+                                               _Writer.Clear ();
+                                       return;
+                               }
+
                                if (!_bSuppressContent && Request.HttpMethod == "HEAD")
                                        _bSuppressContent = true;
 
@@ -747,6 +781,7 @@ namespace System.Web
 
                        Clear ();
 
+                       url = ApplyAppPathModifier (url);
                        StatusCode = 302;
                        AppendHeader (HttpWorkerRequest.HeaderLocation, url);