Call SetEndOfSendNotification
[mono.git] / mcs / class / System.Web / System.Web / HttpException.cs
index 3616a099a24e776c254acb93e604698d34dfca9f..006da9ed8320b33e8b84c2a83c7b9d06366bc775 100644 (file)
@@ -7,7 +7,7 @@
 //
 // (c) 2002 Patrik Torstensson
 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -36,6 +36,7 @@ using System.Security.Permissions;
 using System.Text;
 using System.Web.Util;
 using System.Web.Compilation;
+using System.Web.Management;
 using System.Collections.Specialized;
 
 namespace System.Web
@@ -43,19 +44,28 @@ namespace System.Web
        // CAS
        [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
-#if NET_2_0
        [Serializable]
-#endif
        public class HttpException : ExternalException
        {
                const string DEFAULT_DESCRIPTION_TEXT = "Error processing request.";
                const string ERROR_404_DESCRIPTION = "The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.";
-               
+
+               int webEventCode = WebEventCodes.UndefinedEventCode;
                int http_code = 500;
                string resource_name;
                string description;
                
                const string errorStyleFonts = "\"Verdana\",\"DejaVu Sans\",sans-serif";
+
+#if NET_4_0
+               public
+#else
+               internal
+#endif
+               int WebEventCode 
+               {
+                       get { return webEventCode; }
+               }
                
                public HttpException ()
                {
@@ -86,11 +96,11 @@ namespace System.Web
                        this.description = description;
                }
                
-#if NET_2_0
                protected HttpException (SerializationInfo info, StreamingContext context)
                        : base (info, context)
                {
                        http_code = info.GetInt32 ("_httpCode");
+                       webEventCode = info.GetInt32 ("_webEventCode");
                }
 
                [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
@@ -98,8 +108,8 @@ namespace System.Web
                {
                        base.GetObjectData (info, context);
                        info.AddValue ("_httpCode", http_code);
+                       info.AddValue ("_webEventCode", webEventCode);
                }
-#endif
 
                public HttpException (int httpCode, string message, int hr) 
                        : base (message, hr)
@@ -128,13 +138,22 @@ namespace System.Web
                {
                        try {
                                HttpContext ctx = HttpContext.Current;
-                               if (ctx != null && ctx.IsCustomErrorEnabled)
-                                       return GetCustomErrorDefaultMessage ();
+                               if (ctx != null && ctx.IsCustomErrorEnabled) {
+                                       if (http_code != 404 && http_code != 403)
+                                               return GetCustomErrorDefaultMessage ();
+                                       else
+                                               return GetDefaultErrorMessage (false, null);
+                               }
+                               
+                               Exception ex = GetBaseException ();
+                               if (ex == null)
+                                       ex = this;
                                
-                               if (!(this.InnerException is HtmlizedException))
-                                       return GetDefaultErrorMessage ();
+                               HtmlizedException htmlException = ex  as HtmlizedException;
+                               if (htmlException == null)
+                                       return GetDefaultErrorMessage (true, ex);
                                
-                               return GetHtmlizedErrorMessage ();
+                               return GetHtmlizedErrorMessage (htmlException);
                        } catch (Exception ex) {
                                Console.WriteLine (ex);
                                
@@ -161,6 +180,59 @@ namespace System.Web
                        }
                }
 
+               internal static HttpException NewWithCode (string message, int webEventCode)
+               {
+                       var ret = new HttpException (message);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+
+               internal static HttpException NewWithCode (string message, Exception innerException, int webEventCode)
+               {
+                       var ret = new HttpException (message, innerException);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+
+               internal static HttpException NewWithCode (int httpCode, string message, int webEventCode)
+               {
+                       var ret = new HttpException (httpCode, message);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+               
+               internal static HttpException NewWithCode (int httpCode, string message, Exception innerException, string resourceName, int webEventCode)
+               {
+                       var ret = new HttpException (httpCode, message, innerException, resourceName);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+
+               internal static HttpException NewWithCode (int httpCode, string message, string resourceName, int webEventCode)
+               {
+                       var ret = new HttpException (httpCode, message, resourceName);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+
+               internal static HttpException NewWithCode (int httpCode, string message, Exception innerException, int webEventCode)
+               {
+                       var ret = new HttpException (httpCode, message, innerException);
+                       ret.SetWebEventCode (webEventCode);
+
+                       return ret;
+               }
+               
+               internal void SetWebEventCode (int webEventCode)
+               {
+                       this.webEventCode = webEventCode;
+               }
+               
                void WriteFileTop (StringBuilder builder, string title)
                {
 #if TARGET_J2EE
@@ -206,33 +278,36 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
                
                void WriteFileBottom (StringBuilder builder, bool showTrace)
                {
-                       builder.Append ("<hr style=\"color: silver\"/>");
-                       builder.AppendFormat ("<strong>Version information: </strong> Mono Version: {0}; ASP.NET Version: {0}</body></html>\r\n<!--", Environment.Version);
-                       if (!showTrace)
-                               return;
-                       
-                       string trace, message;
-                       bool haveTrace;
-                       Exception ex = this;
+                       if (showTrace) {
+                               builder.Append ("<hr style=\"color: silver\"/>");
+                               builder.AppendFormat ("<strong>Version information: </strong> Mono Runtime Version: <tt>{0}</tt>; ASP.NET Version: <tt>{1}</tt></body></html>\r\n",
+                                                     RuntimeHelpers.MonoVersion, Environment.Version);
                        
-                       while (ex != null) {
-                               trace = ex.StackTrace;
-                               message = ex.Message;
-                               haveTrace = (trace != null && trace.Length > 0);
+                               string trace, message;
+                               bool haveTrace;
+                               Exception ex = this;
+
+                               builder.Append ("\r\n<!--");
+                               while (ex != null) {
+                                       trace = ex.StackTrace;
+                                       message = ex.Message;
+                                       haveTrace = (trace != null && trace.Length > 0);
                                
-                               if (!haveTrace && (message == null || message.Length == 0)) {
-                                       ex = ex.InnerException;
-                                       continue;
-                               }
+                                       if (!haveTrace && (message == null || message.Length == 0)) {
+                                               ex = ex.InnerException;
+                                               continue;
+                                       }
 
-                               builder.Append ("\r\n[" + ex.GetType () + "]: " + HtmlEncode (message) + "\r\n");
-                               if (haveTrace)
-                                       builder.Append (ex.StackTrace);
+                                       builder.Append ("\r\n[" + ex.GetType () + "]: " + HtmlEncode (message) + "\r\n");
+                                       if (haveTrace)
+                                               builder.Append (ex.StackTrace);
                                
-                               ex = ex.InnerException;
-                       }
+                                       ex = ex.InnerException;
+                               }
                        
-                       builder.Append ("\r\n-->");
+                               builder.Append ("\r\n-->");
+                       } else
+                               builder.Append ("</body></html>\r\n");
                }
 
                string GetCustomErrorDefaultMessage ()
@@ -287,10 +362,10 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
                        return builder.ToString ();
                }
                
-               string GetDefaultErrorMessage ()
+               string GetDefaultErrorMessage (bool showTrace, Exception baseEx)
                {
-                       Exception ex, baseEx;
-                       ex = baseEx = GetBaseException ();
+                       Exception ex;
+                       ex = baseEx;
                        if (ex == null)
                                ex = this;
 
@@ -309,15 +384,15 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
                        builder.Append ("</p>\r\n");
 
                        if (resource_name != null && resource_name.Length > 0)
-                               builder.AppendFormat ("<p><strong>Resource URL: </strong>{0}</p>\r\n", resource_name);
+                               builder.AppendFormat ("<p><strong>Requested URL: </strong>{0}</p>\r\n", resource_name);
                        
-                       if (baseEx != null && http_code != 404) {
+                       if (showTrace && baseEx != null && http_code != 404 && http_code != 403) {
                                builder.Append ("<p><strong>Stack Trace: </strong></p>");
                                builder.Append ("<table summary=\"Stack Trace\" class=\"sampleCode\">\r\n<tr><td>");
                                WriteTextAsCode (builder, baseEx.ToString ());
                                builder.Append ("</td></tr>\r\n</table>\r\n");
                        }
-                       WriteFileBottom (builder, true);
+                       WriteFileBottom (builder, showTrace);
                        
                        return builder.ToString ();
                }
@@ -331,10 +406,20 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
                        return res.Replace ("\r\n", "<br />");
                }
 
-               string GetHtmlizedErrorMessage ()
+               string FormatSourceFile (string filename)
+               {
+                       if (filename == null || filename.Length == 0)
+                               return String.Empty;
+
+                       if (filename.StartsWith ("@@"))
+                               return "[internal] <!-- " + filename + " -->";
+
+                       return filename;
+               }
+               
+               string GetHtmlizedErrorMessage (HtmlizedException exc)
                {
                        StringBuilder builder = new StringBuilder ();
-                       HtmlizedException exc = (HtmlizedException) this.InnerException;
 #if TARGET_J2EE
                        bool isParseException = false;
                        bool isCompileException = false;
@@ -355,11 +440,7 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
                                builder.Append ("Compiler ");
                        
                        builder.Append ("Error Message: </strong>");
-#if NET_2_0
                        builder.AppendFormat ("<code>{0}</code></p>", errorMessage);
-#else
-                       builder.AppendFormat ("<blockquote><pre>{0}</pre></blockquote></p>", errorMessage);
-#endif
 
                        StringBuilder longCodeVersion = null;
                        
@@ -380,9 +461,9 @@ table.sampleCode {{width: 100%; background-color: #ffffcc; }}
 
                                builder.Append ("<br/><p><strong>Source File: </strong>");
                                if (exc.SourceFile != exc.FileName)
-                                       builder.Append (exc.SourceFile);
+                                       builder.Append (FormatSourceFile (exc.SourceFile));
                                else
-                                       builder.Append (exc.FileName);
+                                       builder.Append (FormatSourceFile (exc.FileName));
 
                                if (isParseException || isCompileException) {
                                        int[] errorLines = exc.ErrorLines;