2009-06-04 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpResponse.cs
index 5591a0784769e2e2367dfd040e06e4d926d329ba..7ed591b59ee7c4bd7f11d7f83c6a41ab26fa822c 100644 (file)
@@ -68,8 +68,8 @@ namespace System.Web {
                CachedRawResponse cached_response;
                string user_cache_control = "private";
                string redirect_location;
-               
-               static UnknownResponseHeader version_header;
+               string version_header;
+               bool version_header_checked;
                
                //
                // Negative Content-Length means we auto-compute the size of content-length
@@ -81,9 +81,10 @@ namespace System.Web {
                // The list of the headers that we will send back to the client, except
                // the headers that we compute here.
                //
-               ArrayList headers = new ArrayList ();
+
+               NameValueCollection headers;
                bool headers_sent;
-               ArrayList cached_headers;
+               NameValueCollection cached_headers;
 
                //
                // Transfer encoding state
@@ -104,19 +105,6 @@ namespace System.Web {
                Encoding headerEncoding;
 #endif
 
-               static HttpResponse ()
-               {
-#if NET_2_0
-                       HttpRuntimeSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime") as HttpRuntimeSection;
-#else
-                       HttpRuntimeConfig config = HttpContext.GetAppConfig ("system.web/httpRuntime") as HttpRuntimeConfig;
-#endif
-                       if (config != null && config.EnableVersionHeader) {
-                               string version = Environment.Version.ToString (3);
-                               version_header = new UnknownResponseHeader ("X-AspNet-Version", version);
-                       }
-               }
-               
                internal HttpResponse ()
                {
                        output_stream = new HttpResponseStream (this);
@@ -145,6 +133,23 @@ namespace System.Web {
                        return prev;
                }
 
+               internal string VersionHeader {
+                       get {
+                               if (!version_header_checked && version_header == null) {
+                                       version_header_checked = true;
+#if NET_2_0
+                                       HttpRuntimeSection config = WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime") as HttpRuntimeSection;
+#else
+                                       HttpRuntimeConfig config = HttpContext.GetAppConfig ("system.web/httpRuntime") as HttpRuntimeConfig;
+#endif
+                                       if (config != null && config.EnableVersionHeader)
+                                               version_header = Environment.Version.ToString (3);
+                               }
+
+                               return version_header;
+                       }
+               }
+               
                internal string[] FileDependencies {
                        get {
                                if (fileDependencies == null || fileDependencies.Count == 0)
@@ -308,7 +313,21 @@ namespace System.Web {
                                headerEncoding = value;
                        }
                }
+
+               public
+#else
+               internal
 #endif
+               NameValueCollection Headers {
+                       get {
+                               if (headers == null)
+                                       headers = new NameValueCollection ();
+
+                               return headers;
+                       }
+               }
+
+               
                public bool IsClientConnected {
                        get {
                                if (WorkerRequest == null)
@@ -373,6 +392,21 @@ namespace System.Web {
                        }
                }
 
+#if NET_2_0
+               // We ignore the two properties on Mono as they are for use with IIS7, but there is
+               // no point in throwing PlatformNotSupportedException. We might find a use for them
+               // some day.
+               public int SubStatusCode {
+                       get;
+                       set;
+               }
+
+               public bool TrySkipIisCustomErrors {
+                       get;
+                       set;
+               }
+#endif
+               
                public int StatusCode {
                        get {
                                return status_code;
@@ -506,7 +540,7 @@ namespace System.Web {
                                return;
                        }
 
-                       headers.Add (new UnknownResponseHeader (name, value));
+                       Headers.Add (name, value);
                }
 
                [AspNetHostingPermission (SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Medium)]
@@ -521,7 +555,7 @@ namespace System.Web {
                        if (virtualPath == null)
                                return null;
                
-                       if (virtualPath == "")
+                       if (virtualPath.Length == 0)
                                return context.Request.RootVirtualDir;
 
                        if (UrlUtils.IsRelativeUrl (virtualPath)) {
@@ -580,8 +614,12 @@ namespace System.Web {
                        content_length = -1;
                        content_type = "text/html";
                        transfer_encoding = null;
-                       user_cache_control = null;
-                       headers.Clear ();
+                       user_cache_control = "private";
+                       if (cache_policy != null)
+                               cache_policy.Cacheability = HttpCacheability.Private;
+
+                       if (headers != null)
+                               headers.Clear ();
                }
 
                internal bool HeadersSent {
@@ -628,30 +666,31 @@ namespace System.Web {
                //   Transfer-Encoding (chunked)
                //   Cache-Control
                //   X-AspNet-Version
-               void AddHeadersNoCache (ArrayList write_headers, bool final_flush)
+               void AddHeadersNoCache (NameValueCollection write_headers, bool final_flush)
                {
 #if !TARGET_J2EE
                        //
                        // Transfer-Encoding
                        //
                        if (use_chunked)
-                               write_headers.Add (new UnknownResponseHeader ("Transfer-Encoding", "chunked"));
+                               write_headers.Add ("Transfer-Encoding", "chunked");
                        else if (transfer_encoding != null)
-                               write_headers.Add (new UnknownResponseHeader ("Transfer-Encoding", transfer_encoding));
+                               write_headers.Add ("Transfer-Encoding", transfer_encoding);
 #endif
                        if (redirect_location != null)
-                               write_headers.Add (new UnknownResponseHeader ("Location", redirect_location));
+                               write_headers.Add ("Location", redirect_location);
                        
 #if !TARGET_J2EE
-                       if (version_header != null)
-                               write_headers.Add (version_header);
+                       string vh = VersionHeader;
+                       if (vh != null)
+                               write_headers.Add ("X-AspNet-Version", vh);
 
                        //
                        // If Content-Length is set.
                        //
                        if (content_length >= 0) {
-                               write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderContentLength,
-                                                                     content_length.ToString (CultureInfo.InvariantCulture)));
+                               write_headers.Add (HttpWorkerRequest.GetKnownResponseHeaderName (HttpWorkerRequest.HeaderContentLength),
+                                                  content_length.ToString (CultureInfo.InvariantCulture));
                        } else if (BufferOutput) {
                                if (final_flush) {                                      
                                        //
@@ -659,15 +698,15 @@ namespace System.Web {
                                        // we know the content-length.
                                        //
                                        content_length = output_stream.total;
-                                       write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderContentLength,
-                                                                             content_length.ToString (CultureInfo.InvariantCulture)));
+                                       write_headers.Add (HttpWorkerRequest.GetKnownResponseHeaderName (HttpWorkerRequest.HeaderContentLength),
+                                                          content_length.ToString (CultureInfo.InvariantCulture));
                                } else {
                                        //
                                        // We are buffering, and this is a flush in the middle.
                                        // If we are not chunked, we need to set "Connection: close".
                                        //
                                        if (use_chunked){
-                                               write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderConnection, "close"));
+                                               write_headers.Add (HttpWorkerRequest.GetKnownResponseHeaderName (HttpWorkerRequest.HeaderConnection), "close");
                                        }
                                }
                        } else {
@@ -676,7 +715,7 @@ namespace System.Web {
                                // close at the end.
                                //
                                if (use_chunked){
-                                       write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderConnection, "close"));
+                                       write_headers.Add (HttpWorkerRequest.GetKnownResponseHeaderName (HttpWorkerRequest.HeaderConnection), "close");
                                }
                        }
 #endif
@@ -687,7 +726,7 @@ namespace System.Web {
                        if (cache_policy != null)
                                cache_policy.SetHeaders (this, headers);
                        else
-                               write_headers.Add (new UnknownResponseHeader ("Cache-Control", CacheControl));
+                               write_headers.Add ("Cache-Control", CacheControl);
                        
                        //
                        // Content-Type
@@ -703,13 +742,13 @@ namespace System.Web {
                                        }
                                }
                                
-                               write_headers.Add (new UnknownResponseHeader ("Content-Type", header));
+                               write_headers.Add ("Content-Type", header);
                        }
 
                        if (cookies != null && cookies.Count != 0){
                                int n = cookies.Count;
                                for (int i = 0; i < n; i++)
-                                       write_headers.Add (cookies.Get (i).GetCookieHeader ());
+                                       write_headers.Add ("Set-Cookie", cookies.Get (i).GetCookieHeaderValue ());
 #if TARGET_J2EE
                                // For J2EE Portal support emulate cookies by storing them in the session.
                                context.Request.SetSessionCookiesForPortal (cookies);
@@ -738,18 +777,35 @@ namespace System.Web {
 
                        // If this page is cached use the cached headers
                        // instead of the standard headers      
-                       ArrayList write_headers = headers;
+                       NameValueCollection write_headers;
                        if (cached_headers != null)
                                write_headers = cached_headers;
-                       else
+                       else {
+                               write_headers = Headers;
                                AddHeadersNoCache (write_headers, final_flush);
-
+                       }
+                       
                        if (WorkerRequest != null)
                                WorkerRequest.SendStatus (status_code, StatusDescription);
 
                        if (WorkerRequest != null) {
-                               foreach (BaseResponseHeader header in write_headers){
-                                       header.SendContent (WorkerRequest);
+                               string header_name;
+                               string[] values;
+                               int header_index;
+                               
+                               for (int i = 0; i < write_headers.Count; i++) {
+                                       header_name = write_headers.GetKey (i);
+                                       header_index = HttpWorkerRequest.GetKnownResponseHeaderIndex (header_name);
+                                       values = write_headers.GetValues (i);
+                                       if (values == null)
+                                               continue;
+                                       
+                                       foreach (string val in values) {
+                                               if (header_index > -1)
+                                                       WorkerRequest.SendKnownResponseHeader (header_index, val);
+                                               else
+                                                       WorkerRequest.SendUnknownResponseHeader (header_name, val);
+                                       }
                                }
                        }
                }
@@ -1041,9 +1097,10 @@ namespace System.Web {
 #endregion
                
 #region Cache Support
-               internal void SetCachedHeaders (ArrayList headers)
+               internal void SetCachedHeaders (NameValueCollection headers)
                {
                        cached_headers = headers;
+                       
                }
 
                internal bool IsCached {