2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Web / System.Web / HttpResponse.cs
index 404b2f14d5a8b31398db8021b01028dd86ced08c..734c2018db7a4cf2a6ec79ff1dfa8c58a900aabf 100644 (file)
@@ -7,6 +7,27 @@
 //
 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
 //
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
 using System;
 using System.Collections;
 using System.Globalization;
@@ -21,8 +42,8 @@ namespace System.Web
        public sealed class HttpResponse
        {
                // Chunked encoding static helpers
-               static byte [] s_arrChunkSuffix = { 10, 13 };
-               static byte [] s_arrChunkEnd = { 10 , 13 };
+               static byte [] s_arrChunkSuffix = {13, 10};
+               static byte [] s_arrChunkEnd = {48, 13, 10, 13, 10};
                static string s_sChunkedPrefix = "\r\n";
 
                ArrayList _Headers;
@@ -38,6 +59,11 @@ namespace System.Web
                bool filtered;
                long _lContentLength;
                int _iStatusCode;
+               
+               int _expiresInMinutes;
+               bool _expiresInMinutesSet;
+               DateTime _expiresAbsolute;
+               bool _expiresAbsoluteSet;
 
                bool _ClientDisconnected;
                bool closed;
@@ -143,10 +169,6 @@ namespace System.Web
                internal bool IsCached {
                        get { return cached_response != null; }
                }
-               
-               internal void CacheResponse (HttpRequest request) {
-                       cached_response = new CachedRawResponse (_CachePolicy);
-               }
 
                internal CachedRawResponse GetCachedResponse () {
                        cached_response.StatusCode = StatusCode;
@@ -159,7 +181,6 @@ namespace System.Web
                        cached_headers = headers;
                }
                
-               [MonoTODO("We need to add cache headers also")]
                private ArrayList GenerateHeaders ()
                {
                        ArrayList oHeaders = new ArrayList (_Headers.ToArray ());
@@ -169,8 +190,12 @@ namespace System.Web
                        CultureInfo oSavedInfo = Thread.CurrentThread.CurrentCulture;
                        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
 
-                       string date = DateTime.Now.ToUniversalTime ().ToString ("ddd, d MMM yyyy HH:mm:ss ");
-                       oHeaders.Add (new HttpResponseHeader ("Date", date + "GMT"));
+                       string date = DateTime.UtcNow.ToString ("ddd, d MMM yyyy HH:mm:ss ");
+                       HttpResponseHeader date_header = new HttpResponseHeader ("Date", date + "GMT");
+                       oHeaders.Add (date_header);
+                       
+                       if (IsCached)
+                               cached_response.DateHeader = date_header;
 
                        Thread.CurrentThread.CurrentCulture = oSavedInfo;
 
@@ -331,8 +356,15 @@ namespace System.Web
                                virtualPath = UrlUtils.Reduce (virtualPath);
                        }
 
-                       if (app_path_mod != null && virtualPath.IndexOf (app_path_mod) > 0)
-                               virtualPath = UrlUtils.Combine (app_path_mod, virtualPath);
+                       if (app_path_mod != null && virtualPath.IndexOf (app_path_mod) < 0) {
+                               string rvd = _Context.Request.RootVirtualDir;
+                               string basevd = rvd.Replace (app_path_mod, "");
+
+                               if (!virtualPath.StartsWith (basevd))
+                                       return virtualPath;
+
+                               virtualPath = UrlUtils.Combine (rvd, virtualPath.Substring (basevd.Length));
+                       }
 
                        return virtualPath;
                }
@@ -370,13 +402,24 @@ namespace System.Web
                public HttpCachePolicy Cache
                {
                        get {
-                               if (null == _CachePolicy)
+                               if (null == _CachePolicy) {
                                        _CachePolicy = new HttpCachePolicy ();
+                                       _CachePolicy.CacheabilityUpdated += new CacheabilityUpdatedCallback (
+                                               OnCacheabilityUpdated);
+                               }
 
                                return _CachePolicy;
                        }
                }
 
+               private void OnCacheabilityUpdated (object sender, CacheabilityUpdatedEventArgs e)
+               {
+                       if (e.Cacheability >= HttpCacheability.Server && !IsCached)
+                               cached_response = new CachedRawResponse (_CachePolicy);
+                       else if (e.Cacheability <= HttpCacheability.Private)
+                               cached_response = null;
+               }
+               
                [MonoTODO("Set status in the cache policy")]
                public string CacheControl
                {
@@ -453,27 +496,35 @@ namespace System.Web
                        }
                }
 
-               [MonoTODO("Set expires in the cache policy")]
                public int Expires
                {
                        get {
-                               throw new NotImplementedException ();
+                               return _expiresInMinutes;
                        }
 
                        set {
-                               throw new NotImplementedException ();
+                               if (!_expiresInMinutesSet || (value < _expiresInMinutes))
+                               {
+                                       _expiresInMinutes = value;
+                                       Cache.SetExpires(_Context.Timestamp.Add(new TimeSpan(0, _expiresInMinutes, 0)));
+                               }
+                               _expiresInMinutesSet = true;
                        }
                }
 
-               [MonoTODO("Set expiresabsolute in the cache policy")]
                public DateTime ExpiresAbsolute
                {
                        get {
-                               throw new NotImplementedException ();
+                               return _expiresAbsolute;
                        }
 
                        set {
-                               throw new NotImplementedException ();
+                               if (!_expiresAbsoluteSet || value.CompareTo(_expiresAbsolute)<0)
+                               {
+                                       _expiresAbsolute = value;
+                                       Cache.SetExpires(_expiresAbsolute); 
+                               }
+                               _expiresAbsoluteSet = true;
                        }
                }
 
@@ -574,9 +625,6 @@ namespace System.Web
                        }
                        
                        set {
-                               if (_bHeadersSent)
-                                       throw new HttpException ("Headers has been sent to the client");
-
                                _bSuppressContent = true;
                        }
                }
@@ -605,11 +653,7 @@ namespace System.Web
                                break;
                        case HttpWorkerRequest.HeaderTransferEncoding:
                                _sTransferEncoding = value;
-                               if (value.Equals ("chunked")) {
-                                       _bChunked = true;
-                               } else {
-                                       _bChunked = false;
-                               }
+                               _bChunked = (value == "chunked");
                                break;
                        case HttpWorkerRequest.HeaderPragma:
                                _sCacheControl = value;
@@ -634,11 +678,7 @@ namespace System.Web
                                break;
                        case "transfer-encoding":
                                _sTransferEncoding = value;
-                               if (value.Equals ("chunked")) {
-                                       _bChunked = true;
-                               } else {
-                                       _bChunked = false;
-                               }
+                               _bChunked = (value == "chunked");
                                break;
                        case "pragma":
                                _sCacheControl = value;
@@ -677,6 +717,11 @@ namespace System.Web
                        Clear();
                }
 
+               internal void SetHeadersSent (bool val)
+               {
+                       _bHeadersSent = val;
+               }
+               
                public void ClearHeaders ()
                {
                        if (_bHeadersSent)
@@ -772,9 +817,6 @@ namespace System.Web
                                                   _sTransferEncoding == null) {
                                                        // 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,
@@ -799,6 +841,11 @@ namespace System.Web
                                }
 
                                if (length == 0) {
+                                       if (bFinish && _bChunked) {
+                                               _WorkerRequest.SendResponseFromMemory (s_arrChunkEnd,
+                                                                               s_arrChunkEnd.Length);
+                                       }
+
                                        _WorkerRequest.FlushResponse (bFinish);
                                        if (!bFinish)
                                                _Writer.Clear ();
@@ -808,6 +855,9 @@ namespace System.Web
                                if (!_bSuppressContent && Request.HttpMethod == "HEAD")
                                        _bSuppressContent = true;
 
+                               if (_bSuppressContent)
+                                       _Writer.Clear ();
+
                                if (!_bSuppressContent) {
                                        _bClientDisconnected = false;
                                        if (_bChunked) {
@@ -907,10 +957,17 @@ namespace System.Web
                        _TextWriter.Write (buffer, index, count);
                }
 
-               [MonoTODO()]
                public static void RemoveOutputCacheItem (string path)
                {
-                       throw new NotImplementedException ();
+                       if (path == null)
+                               throw new ArgumentNullException ("path");
+                       
+                       if (!UrlUtils.IsRooted (path))
+                               throw new ArgumentException ("Invalid path for HttpResponse.RemoveOutputCacheItem '" +
+                                       path + "'. An absolute virtual path is expected.");
+
+                       Cache cache = HttpRuntime.Cache;
+                       cache.Remove (path);
                }
 
                public void SetCookie (HttpCookie cookie)