New test.
[mono.git] / mcs / class / System.Web / System.Web / HttpResponse.cs
index 8e41fcbe5caf4ebd91ebb41408230b8415c6992e..890078fb4a179bffa4567190965a291a6c06bc51 100644 (file)
@@ -61,7 +61,7 @@ namespace System.Web {
                string charset;
                bool charset_set;
                CachedRawResponse cached_response;
-               string cache_control = "private";
+               string user_cache_control = "private";
                string redirect_location;
                
                //
@@ -236,6 +236,9 @@ namespace System.Web {
 
                public Stream Filter {
                        get {
+                               if (WorkerRequest == null)
+                                       return null;
+
                                return output_stream.Filter;
                        }
 
@@ -424,7 +427,7 @@ namespace System.Web {
                                throw new HttpException ("headers have been already sent");
                        
                        if (String.Compare (name, "content-length", true, CultureInfo.InvariantCulture) == 0){
-                               content_length = Int64.Parse (value);
+                               content_length = (long) UInt64.Parse (value);
                                use_chunked = false;
                                return;
                        }
@@ -441,7 +444,7 @@ namespace System.Web {
                        }
 
                        if (String.Compare (name, "cache-control", true, CultureInfo.InvariantCulture) == 0){
-                               cache_control = value;
+                               user_cache_control = value;
                                return;
                        }
 
@@ -511,7 +514,7 @@ namespace System.Web {
                        content_length = -1;
                        content_type = "text/html";
                        transfer_encoding = null;
-                       cache_control = "private";
+                       user_cache_control = null;
                        headers.Clear ();
                }
 
@@ -536,7 +539,7 @@ namespace System.Web {
                                Thread.CurrentThread.Abort (FlagEnd);
                        } else {
                                // If this is called from an async event, signal the completion
-                               // but don't thow.
+                               // but don't throw.
                                context.ApplicationInstance.CompleteRequest ();
                        }
                }
@@ -546,23 +549,8 @@ namespace System.Web {
                //   Content-Type
                //   Transfer-Encoding (chunked)
                //   Cache-Control
-               internal void WriteHeaders (bool final_flush)
+               void AddHeadersNoCache (ArrayList write_headers, bool final_flush)
                {
-                       if (headers_sent)
-                               return;
-
-                       if (WorkerRequest != null)
-                               WorkerRequest.SendStatus (status_code, StatusDescription);
-
-                       if (cached_response != null)
-                               cached_response.SetHeaders (headers);
-
-                       // If this page is cached use the cached headers
-                       // instead of the standard headers      
-                       ArrayList write_headers = headers;
-                       if (cached_headers != null)
-                               write_headers = cached_headers;
-
                        //
                        // Transfer-Encoding
                        //
@@ -602,7 +590,9 @@ namespace System.Web {
                                        // If we are not chunked, we need to set "Connection: close".
                                        //
                                        if (use_chunked){
+#if DEBUG
                                                Console.WriteLine ("Setting to close2");
+#endif
                                                write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderConnection, "close"));
                                        }
                                }
@@ -612,7 +602,9 @@ namespace System.Web {
                                // close at the end.
                                //
                                if (use_chunked){
+#if DEBUG
                                        Console.WriteLine ("Setting to close");
+#endif
                                        write_headers.Add (new KnownResponseHeader (HttpWorkerRequest.HeaderConnection, "close"));
                                }
                        }
@@ -623,7 +615,7 @@ namespace System.Web {
                        if (cache_policy != null)
                                cache_policy.SetHeaders (this, headers);
                        else
-                               write_headers.Add (new UnknownResponseHeader ("Cache-Control", cache_control));
+                               write_headers.Add (new UnknownResponseHeader ("Cache-Control", CacheControl));
                        
                        //
                        // Content-Type
@@ -648,6 +640,27 @@ namespace System.Web {
                                        write_headers.Add (cookies.Get (i).GetCookieHeader ());
                        }
                        
+               }
+
+               internal void WriteHeaders (bool final_flush)
+               {
+                       if (headers_sent)
+                               return;
+
+                       if (WorkerRequest != null)
+                               WorkerRequest.SendStatus (status_code, StatusDescription);
+
+                       if (cached_response != null)
+                               cached_response.SetHeaders (headers);
+
+                       // If this page is cached use the cached headers
+                       // instead of the standard headers      
+                       ArrayList write_headers = headers;
+                       if (cached_headers != null)
+                               write_headers = cached_headers;
+                       else
+                               AddHeadersNoCache (write_headers, final_flush);
+
                        //
                        // Flush
                        //
@@ -666,7 +679,7 @@ namespace System.Web {
 
                internal void DoFilter (bool close)
                {
-                       if (output_stream.Filter != null && context != null && context.Error == null)
+                       if (output_stream.HaveFilter && context != null && context.Error == null)
                                output_stream.ApplyFilter (close);
                }
 
@@ -824,7 +837,11 @@ namespace System.Web {
                        Flush ();
                }
 
-#if !TARGET_JVM
+#if TARGET_JVM
+               public void WriteFile (IntPtr fileHandle, long offset, long size) {
+                       throw new NotSupportedException("IntPtr not supported");
+               }
+#else
                public void WriteFile (IntPtr fileHandle, long offset, long size)
                {
                        if (offset < 0)
@@ -889,6 +906,7 @@ namespace System.Web {
                internal void TransmitFile (string filename, bool final_flush)
                {
                        FileInfo fi = new FileInfo (filename);
+                       using (Stream s = fi.OpenRead ()); // Just check if we can read.
                        output_stream.WriteFile (filename, 0, fi.Length);
                        output_stream.ApplyFilter (final_flush);
                        Flush (final_flush);
@@ -946,37 +964,25 @@ namespace System.Web {
                //
                public string CacheControl {
                        set {
-                               if (String.Compare (value, "public", true, CultureInfo.InvariantCulture) == 0)
+                               if (value == null || value == "") {
+                                       Cache.SetCacheability (HttpCacheability.NoCache);
+                                       user_cache_control = null;
+                               } else if (String.Compare (value, "public", true, CultureInfo.InvariantCulture) == 0) {
                                        Cache.SetCacheability (HttpCacheability.Public);
-                               else if (String.Compare (value, "private", true, CultureInfo.InvariantCulture) == 0)
+                                       user_cache_control = "public";
+                               } else if (String.Compare (value, "private", true, CultureInfo.InvariantCulture) == 0) {
                                        Cache.SetCacheability (HttpCacheability.Private);
-                               else if (String.Compare (value, "no-cache", true, CultureInfo.InvariantCulture) == 0)
+                                       user_cache_control = "private";
+                               } else if (String.Compare (value, "no-cache", true, CultureInfo.InvariantCulture) == 0) {
                                        Cache.SetCacheability (HttpCacheability.NoCache);
-                               else
+                                       user_cache_control = "no-cache";
+                               } else
                                        throw new ArgumentException ("CacheControl property only allows `public', " +
                                                                     "`private' or no-cache, for different uses, use " +
                                                                     "Response.AppendHeader");
-                               cache_control = value;
                        }
 
-                       get {
-                               if ((cache_control == null) && (cache_policy != null)) {
-                                       switch (Cache.Cacheability) {
-                                       case (HttpCacheability)0:
-                                       case HttpCacheability.NoCache:
-                                               return "no-cache";
-                                       case HttpCacheability.Private: 
-                                       case HttpCacheability.Server:
-                                       case HttpCacheability.ServerAndPrivate:
-                                               return "private";
-                                       case HttpCacheability.Public:
-                                               return "public";
-                                       default:
-                                               throw new Exception ("Unknown internal state: " + Cache.Cacheability);
-                                       }
-                               }
-                               return cache_control;
-                       }
+                       get { return (user_cache_control != null) ? user_cache_control : "private"; }
                }
 #endregion