X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fclass%2FSystem.Web%2FSystem.Web%2FHttpException.cs;h=6385ffe7cb325273c38e77e17cc1d297dcaada43;hb=60f847468e0e5740947aea5e0df987bc6b052e77;hp=e64185a785f3e2d622db222de4f8ae5627c1b00c;hpb=5bbfa8860b090e465a3aa45edeb9c94481ef1a22;p=mono.git diff --git a/mcs/class/System.Web/System.Web/HttpException.cs b/mcs/class/System.Web/System.Web/HttpException.cs index e64185a785f..6385ffe7cb3 100644 --- a/mcs/class/System.Web/System.Web/HttpException.cs +++ b/mcs/class/System.Web/System.Web/HttpException.cs @@ -36,6 +36,7 @@ using System.Security.Permissions; using System.Text; using System.Web.Util; using System.Web.Compilation; +using System.Collections.Specialized; namespace System.Web { @@ -47,8 +48,15 @@ namespace System.Web #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 http_code = 500; - + string resource_name; + string description; + + const string errorStyleFonts = "\"Verdana\",\"DejaVu Sans\",sans-serif"; + public HttpException () { } @@ -68,13 +76,28 @@ namespace System.Web http_code = httpCode; } + internal HttpException (int httpCode, string message, string resourceName) : this (httpCode, message) + { + resource_name = resourceName; + } + + internal HttpException (int httpCode, string message, string resourceName, string description) : this (httpCode, message, resourceName) + { + this.description = description; + } + #if NET_2_0 - protected HttpException (SerializationInfo info, StreamingContext context) + protected +#else + internal +#endif + HttpException (SerializationInfo info, StreamingContext context) : base (info, context) { http_code = info.GetInt32 ("_httpCode"); } +#if NET_2_0 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)] public override void GetObjectData (SerializationInfo info, StreamingContext context) { @@ -100,57 +123,213 @@ namespace System.Web http_code = httpCode; } + internal HttpException (int httpCode, string message, Exception innerException, string resourceName) + : this (httpCode, message, innerException) + { + resource_name = resourceName; + } + public string GetHtmlErrorMessage () { - if (!(this.InnerException is HtmlizedException)) - return GetDefaultErrorMessage (); - - return GetHtmlizedErrorMessage (); + try { + HttpContext ctx = HttpContext.Current; + if (ctx != null && ctx.IsCustomErrorEnabled) { + if (http_code != 404 && http_code != 403) + return GetCustomErrorDefaultMessage (); + else + return GetDefaultErrorMessage (false); + } + + if (!(this.InnerException is HtmlizedException)) + return GetDefaultErrorMessage (true); + + return GetHtmlizedErrorMessage (); + } catch (Exception ex) { + Console.WriteLine (ex); + + // we need the try/catch block in case the + // problem was with MapPath, which will cause + // IsCustomErrorEnabled to throw an exception + return GetCustomErrorDefaultMessage (); + } } internal virtual string Description { - get { return "Error processing request."; } + get { + if (description != null) + return description; + + return DEFAULT_DESCRIPTION_TEXT; + } + + set { + if (value != null && value.Length > 0) + description = value; + else + description = DEFAULT_DESCRIPTION_TEXT; + } } - - string GetDefaultErrorMessage () - { - StringBuilder builder = new StringBuilder ("\r\n"); - builder.Append ("Error"); - if (http_code != 0) - builder.Append (" " + http_code); - builder.AppendFormat ("" + - "

Server error in '{0}' " + - "application


\r\n", - HtmlEncode (HttpRuntime.AppDomainAppVirtualPath)); + void WriteFileTop (StringBuilder builder, string title) + { +#if TARGET_J2EE + builder.AppendFormat ("{0}

Server Error in '{0}' Application


", + HtmlEncode (HttpRuntime.AppDomainAppVirtualPath)); + } + + void WriteFileBottom (StringBuilder builder, bool showTrace) + { + if (showTrace) { + builder.Append ("
"); + builder.AppendFormat ("Version information: Mono Version: {0}; ASP.NET Version: {0}\r\n", Environment.Version); + + string trace, message; + bool haveTrace; + Exception ex = this; + + builder.Append ("\r\n"); + } else + builder.Append ("\r\n"); + } - builder.AppendFormat ("Description: {0}\r\n

\r\n", Description); - builder.Append ("Error Message: "); + string GetCustomErrorDefaultMessage () + { + StringBuilder builder = new StringBuilder (); + WriteFileTop (builder, "Runtime Error"); +#if TARGET_J2EE //on portal we cannot know if we run locally + if (!HttpContext.Current.IsServletRequest) + builder.Append ( +@"

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed (for security reasons).

+

Details: To enable the details of this specific error message to be viewable, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

+
+
+<!-- Web.Config Configuration File -->
+
+<configuration>
+    <system.web>
+
+        <customErrors mode="Off"/>
+    </system.web>
+</configuration>
+

+"); + else +#endif + builder.Append (@"

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons)." + ( + " It could, however, be viewed by browsers running on the local server machine.") + + @"

+

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

+
+
+<!-- Web.Config Configuration File -->
+
+<configuration>
+    <system.web>
+
+        <customErrors mode="Off"/>
+    </system.web>
+</configuration>
+

+

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

+
+<!-- Web.Config Configuration File -->
+
+<configuration>
+    <system.web>
+        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
+
+    </system.web>
+</configuration>
"); + WriteFileBottom (builder, false); + return builder.ToString (); + } + + string GetDefaultErrorMessage (bool showTrace) + { + Exception ex, baseEx; + ex = baseEx = GetBaseException (); + if (ex == null) + ex = this; + + StringBuilder builder = new StringBuilder (); + WriteFileTop (builder, String.Format ("Error{0}", http_code != 0 ? " " + http_code : String.Empty)); + builder.Append ("

"); + if (http_code == 404) + builder.Append ("The resource cannot be found."); + else + builder.Append (HtmlEncode (ex.Message)); + builder.Append ("

\r\n

Description: "); + if (http_code != 0) builder.AppendFormat ("HTTP {0}. ", http_code); + builder.Append (http_code == 404 ? ERROR_404_DESCRIPTION : HtmlEncode (Description)); + builder.Append ("

\r\n"); - builder.AppendFormat ("{0}\r\n

\r\n", HtmlEncode (this.Message)); - - if (InnerException != null) { - builder.AppendFormat ("Stack Trace: "); - builder.Append ("\r\n\r\n
"); - WriteTextAsCode (builder, InnerException.ToString ()); -#if TARGET_J2EE //Required, because toString of Java doesn't print stackTrace - WriteTextAsCode (builder, InnerException.StackTrace); -#endif - builder.Append ("
\r\n

\r\n"); + if (resource_name != null && resource_name.Length > 0) + builder.AppendFormat ("

Requested URL: {0}

\r\n", resource_name); + + if (showTrace && baseEx != null && http_code != 404 && http_code != 403) { + builder.Append ("

Stack Trace:

"); + builder.Append ("\r\n\r\n
"); + WriteTextAsCode (builder, baseEx.ToString ()); + builder.Append ("
\r\n"); } - - builder.Append ("
\r\n\r\n\r\n"); - builder.AppendFormat ("\r\n", HttpUtility.HtmlEncode (this.ToString ())); -#if TARGET_J2EE //Required, because toString of Java doesn't print stackTrace - builder.AppendFormat ("\r\n", HttpUtility.HtmlEncode (this.StackTrace)); -#endif - + WriteFileBottom (builder, showTrace); + return builder.ToString (); } @@ -165,84 +344,150 @@ namespace System.Web string GetHtmlizedErrorMessage () { - StringBuilder builder = new StringBuilder ("\r\n"); + StringBuilder builder = new StringBuilder (); HtmlizedException exc = (HtmlizedException) this.InnerException; - builder.Append (exc.Title); - builder.AppendFormat ("" + - "

Server Error in '{0}' " + - "Application


\r\n", - HttpRuntime.AppDomainAppVirtualPath); - - builder.AppendFormat ("

{0}

\r\n", exc.Title); - builder.AppendFormat ("Description: {0}\r\n

\r\n", HtmlEncode (exc.Description)); - string errorMessage = "
" + HtmlEncode (exc.ErrorMessage).Replace ("\n", "
"); - builder.AppendFormat ("Error message: {0}\r\n

\r\n", errorMessage); - - if (exc.FileName != null) - builder.AppendFormat ("File name: {0}", HtmlEncode (exc.FileName)); +#if TARGET_J2EE + bool isParseException = false; + bool isCompileException = false; +#else + bool isParseException = exc is ParseException; + bool isCompileException = (!isParseException && exc is CompilationException); +#endif + + WriteFileTop (builder, exc.Title); + builder.AppendFormat ("

{0}

\r\n", exc.Title); + builder.AppendFormat ("

Description: {0}\r\n

\r\n", HtmlEncode (exc.Description)); + string errorMessage = HtmlEncode (exc.ErrorMessage); + + builder.Append ("

"); + if (isParseException) + builder.Append ("Parser "); + else if (isCompileException) + builder.Append ("Compiler "); + + builder.Append ("Error Message: "); +#if NET_2_0 + builder.AppendFormat ("{0}

", errorMessage); +#else + builder.AppendFormat ("
{0}

", errorMessage); +#endif + StringBuilder longCodeVersion = null; + if (exc.FileText != null) { - if (exc.SourceFile != exc.FileName) - builder.AppendFormat ("

Source File: {0}", exc.SourceFile); - - if (exc is ParseException) { - builder.Append ("    Line: "); - builder.Append (exc.ErrorLines [0]); + if (isParseException || isCompileException) { + builder.Append ("

Source Error:

\r\n"); + builder.Append ("\r\n\r\n
"); + + if (isCompileException) + longCodeVersion = new StringBuilder (); + WriteSource (builder, longCodeVersion, exc); + builder.Append ("
\r\n"); + } else { + builder.Append ("\r\n\r\n
"); + WriteSource (builder, null, exc); + builder.Append ("
\r\n"); } - builder.Append ("\r\n

\r\n"); + builder.Append ("

Source File: "); + if (exc.SourceFile != exc.FileName) + builder.Append (exc.SourceFile); + else + builder.Append (exc.FileName); + + if (isParseException || isCompileException) { + int[] errorLines = exc.ErrorLines; + int numErrors = errorLines != null ? errorLines.Length : 0; + if (numErrors > 0) { + builder.AppendFormat ("  Line{0}: ", numErrors > 1 ? "s" : String.Empty); + for (int i = 0; i < numErrors; i++) { + if (i > 0) + builder.Append (", "); + builder.Append (exc.ErrorLines [i]); + } + } + } + builder.Append ("

"); + } else if (exc.FileName != null) + builder.AppendFormat ("{0}

", HtmlEncode (exc.FileName)); - if (exc is ParseException) { - builder.Append ("Source Error: \r\n"); - builder.Append ("\r\n\r\n
"); - WriteSource (builder, exc); - builder.Append ("
\r\n

\r\n"); - } else { - builder.Append ("\r\n\r\n
"); - WriteSource (builder, exc); - builder.Append ("
\r\n

\r\n"); + bool needToggleJS = false; + +#if !TARGET_J2EE + if (isCompileException) { + CompilationException cex = exc as CompilationException; + StringCollection output = cex.CompilerOutput; + + if (output != null && output.Count > 0) { + needToggleJS = true; + StringBuilder sb = new StringBuilder (); + foreach (string s in output) + sb.Append (s + "\r\n"); + WriteExpandableBlock (builder, "compilerOutput", "Show Detailed Compiler Output", sb.ToString ()); } } - - builder.Append ("


\r\n\r\n\r\n"); - builder.AppendFormat ("\r\n", HtmlEncode (exc.ToString ())); -#if TARGET_JVM - builder.AppendFormat ("\r\n", HtmlEncode (exc.StackTrace)); #endif + + if (longCodeVersion != null && longCodeVersion.Length > 0) { + WriteExpandableBlock (builder, "fullCode", "Show Complete Compilation Source", longCodeVersion.ToString ()); + needToggleJS = true; + } + + if (needToggleJS) + builder.Append ("\r\n"); + + WriteFileBottom (builder, true); + return builder.ToString (); } + static void WriteExpandableBlock (StringBuilder builder, string id, string title, string contents) + { + builder.AppendFormat ("
{0}:

" + + "
" + + "
\r\n", title, id);
+			builder.Append (contents);
+			builder.Append ("
"); + } + static void WriteTextAsCode (StringBuilder builder, string text) { - builder.Append ("
\r\n");
-			builder.AppendFormat ("{0}", HtmlEncode (text));
-			builder.Append ("
\r\n"); + builder.AppendFormat ("
{0}
", HtmlEncode (text)); } #if TARGET_J2EE - static void WriteSource (StringBuilder builder, HtmlizedException e) + static void WriteSource (StringBuilder builder, StringBuilder longVersion, HtmlizedException e) { builder.Append ("
");
 			WritePageSource (builder, e);
-			builder.Append ("
\r\n"); + builder.Append ("\r\n"); } #else - static void WriteSource (StringBuilder builder, HtmlizedException e) + static void WriteSource (StringBuilder builder, StringBuilder longVersion, HtmlizedException e) { builder.Append ("
");
 			if (e is CompilationException)
-				WriteCompilationSource (builder, e);
+				WriteCompilationSource (builder, longVersion, e);
 			else
 				WritePageSource (builder, e);
 
-			builder.Append ("
\r\n"); + builder.Append ("\r\n"); } #endif - static void WriteCompilationSource (StringBuilder builder, HtmlizedException e) + static void WriteCompilationSource (StringBuilder builder, StringBuilder longVersion, HtmlizedException e) { int [] a = e.ErrorLines; string s; @@ -252,21 +497,42 @@ namespace System.Web if (a != null && a.Length > 0) errline = a [0]; - - TextReader reader = new StringReader (e.FileText); - while ((s = reader.ReadLine ()) != null) { - line++; - - if (errline == line) - builder.Append (""); - builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s)); - - if (line == errline) { - builder.Append (""); - errline = (++index < a.Length) ? a [index] : 0; + int begin = errline - 2; + int end = errline + 2; + + if (begin < 0) + begin = 0; + + string tmp; + using (TextReader reader = new StringReader (e.FileText)) { + while ((s = reader.ReadLine ()) != null) { + line++; + if (line < begin || line > end) { + if (longVersion != null) + longVersion.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s)); + continue; + } + + if (errline == line) { + if (longVersion != null) + longVersion.Append (""); + builder.Append (""); + } + + tmp = String.Format ("Line {0}: {1}\r\n", line, HtmlEncode (s)); + builder.Append (tmp); + if (longVersion != null) + longVersion.Append (tmp); + + if (line == errline) { + builder.Append (""); + if (longVersion != null) + longVersion.Append (""); + errline = (++index < a.Length) ? a [index] : 0; + } } - } + } } static void WritePageSource (StringBuilder builder, HtmlizedException e) @@ -275,8 +541,8 @@ namespace System.Web int line = 0; int beginerror = e.ErrorLines [0]; int enderror = e.ErrorLines [1]; - int begin = beginerror - 3; - int end = enderror + 3; + int begin = beginerror - 2; + int end = enderror + 2; if (begin <= 0) begin = 1; @@ -292,7 +558,7 @@ namespace System.Web if (beginerror == line) builder.Append (""); - builder.AppendFormat ("{0}\r\n", HtmlEncode (s)); + builder.AppendFormat ("Line {0}: {1}\r\n", line, HtmlEncode (s)); if (enderror <= line) { builder.Append ("");