2010-04-27 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Mon, 26 Apr 2010 23:38:29 +0000 (23:38 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Mon, 26 Apr 2010 23:38:29 +0000 (23:38 -0000)
* HttpException.cs: implemented 4.0 property WebEventCode, also
made it available in the 2.0 profile as internal.
Added a method with several overloads, NewWithCode, which creates
an instance of HttpException and sets the web error code.

* HttpApplication.cs, HttpRequest.cs, HttpRuntime.cs: use
HttpException.NewWithCode for some exceptions.

svn path=/trunk/mcs/; revision=156150

mcs/class/System.Web/System.Web/ChangeLog
mcs/class/System.Web/System.Web/HttpApplication.cs
mcs/class/System.Web/System.Web/HttpException.cs
mcs/class/System.Web/System.Web/HttpRequest.cs
mcs/class/System.Web/System.Web/HttpRuntime.cs
mcs/class/System.Web/System.Web_test.dll.sources
mcs/class/System.Web/Test/System.Web/HttpExceptionTest.cs [new file with mode: 0644]

index 8cc0c03e41efd4144f33559d3ce02226b342cf67..8cc7339ca81a81f79a71aaf191524080568b7fd0 100644 (file)
@@ -1,3 +1,13 @@
+2010-04-27  Marek Habersack  <mhabersack@novell.com>
+
+       * HttpException.cs: implemented 4.0 property WebEventCode, also
+       made it available in the 2.0 profile as internal.
+       Added a method with several overloads, NewWithCode, which creates
+       an instance of HttpException and sets the web error code.
+
+       * HttpApplication.cs, HttpRequest.cs, HttpRuntime.cs: use HttpException.NewWithCode for some
+       exceptions.
+
 2010-04-21  Marek Habersack  <mhabersack@novell.com>
 
        * HttpApplication.cs: update to fix for bug #572469 - ProcessError
index 4b55fdaeb5512901a3b9f641faeeea738ebbc7ab..d9972ceddec874d60ed40d8dafba25186e78fad8 100644 (file)
@@ -75,6 +75,7 @@ using System.Threading;
 using System.Web.Caching;
 using System.Web.Compilation;
 using System.Web.Configuration;
+using System.Web.Management;
 using System.Web.SessionState;
 using System.Web.UI;
 using System.Web.Util;
@@ -277,10 +278,10 @@ namespace System.Web
                public HttpRequest Request {
                        get {
                                if (context == null)
-                                       throw new HttpException (Locale.GetText ("No context is available."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("No context is available."), WebEventCodes.RuntimeErrorRequestAbort);
 
                                if (false == HttpApplicationFactory.ContextAvailable)
-                                       throw new HttpException (Locale.GetText ("Request is not available in this context."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("Request is not available in this context."), WebEventCodes.RuntimeErrorRequestAbort);
 
                                return context.Request;
                        }
@@ -291,10 +292,10 @@ namespace System.Web
                public HttpResponse Response {
                        get {
                                if (context == null)
-                                       throw new HttpException (Locale.GetText ("No context is available."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("No context is available."), WebEventCodes.RuntimeErrorRequestAbort);
 
                                if (false == HttpApplicationFactory.ContextAvailable)
-                                       throw new HttpException (Locale.GetText ("Response is not available in this context."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("Response is not available in this context."), WebEventCodes.RuntimeErrorRequestAbort);
 
                                return context.Response;
                        }
@@ -324,11 +325,11 @@ namespace System.Web
                                        return session;
 
                                if (context == null)
-                                       throw new HttpException (Locale.GetText ("No context is available."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("No context is available."), WebEventCodes.RuntimeErrorRequestAbort);
 
                                HttpSessionState ret = context.Session;
                                if (ret == null)
-                                       throw new HttpException (Locale.GetText ("Session state is not available in the context."));
+                                       throw HttpException.NewWithCode (Locale.GetText ("Session state is not available in the context."), WebEventCodes.RuntimeErrorRequestAbort);
                                
                                return ret;
                        }
@@ -933,7 +934,7 @@ namespace System.Web
                                object obj = taex.ExceptionState;
                                Thread.ResetAbort ();
                                if (obj is StepTimeout)
-                                       ProcessError (new HttpException ("The request timed out."));
+                                       ProcessError (HttpException.NewWithCode ("The request timed out.", WebEventCodes.RequestTransactionAbort));
                                else {
                                        context.ClearError ();
                                        if (FlagEnd.Value != obj && !HttpRuntime.DomainUnloading)
@@ -1063,7 +1064,7 @@ namespace System.Web
                                        if (error is HttpException){
                                                response.StatusCode = ((HttpException)error).GetHttpCode ();
                                        } else {
-                                               error = new HttpException ("", error);
+                                               error = HttpException.NewWithCode (String.Empty, error, WebEventCodes.WebErrorOtherError);
                                                response.StatusCode = 500;
                                        }
                                        HttpException httpEx = (HttpException) error;
@@ -1073,7 +1074,7 @@ namespace System.Web
                                                response.Flush (true);
                                } else {
                                        if (!(error is HttpException))
-                                               error = new HttpException ("", error);
+                                               error = HttpException.NewWithCode (String.Empty, error, WebEventCodes.WebErrorOtherError);
                                        FinalErrorWrite (response, ((HttpException) error).GetHtmlErrorMessage ());
                                }
                        }
@@ -1182,8 +1183,9 @@ namespace System.Web
                                        string path = req.PathNoValidation;
                                        int idx = path != null ? path.IndexOfAny (invalidChars) : -1;
                                        if (idx != -1)
-                                               throw new HttpException (
-                                                       String.Format ("A potentially dangerous Request.Path value was detected from the client ({0}).", path [idx])
+                                               throw HttpException.NewWithCode (
+                                                       String.Format ("A potentially dangerous Request.Path value was detected from the client ({0}).", path [idx]),
+                                                       WebEventCodes.RuntimeErrorValidationFailure
                                                );
                                }
                        }
@@ -1267,13 +1269,20 @@ namespace System.Web
                                Console.WriteLine (fnf.ToString ());
 #endif
                                if (context.Request.IsLocal)
-                                       ProcessError (new HttpException (404, String.Format ("File not found {0}", fnf.FileName), fnf, context.Request.FilePath));
+                                       ProcessError (HttpException.NewWithCode (404,
+                                                                                String.Format ("File not found {0}", fnf.FileName),
+                                                                                fnf,
+                                                                                context.Request.FilePath,
+                                                                                WebEventCodes.RuntimeErrorRequestAbort));
                                else
-                                       ProcessError (new HttpException (404, "File not found: " + Path.GetFileName (fnf.FileName), context.Request.FilePath));
+                                       ProcessError (HttpException.NewWithCode (404,
+                                                                                "File not found: " + Path.GetFileName (fnf.FileName),
+                                                                                context.Request.FilePath,
+                                                                                WebEventCodes.RuntimeErrorRequestAbort));
                        } catch (DirectoryNotFoundException dnf){
                                if (!context.Request.IsLocal)
                                        dnf = null; // Do not "leak" real path information
-                               ProcessError (new HttpException (404, "Directory not found", dnf));
+                               ProcessError (HttpException.NewWithCode (404, "Directory not found", dnf, WebEventCodes.RuntimeErrorRequestAbort));
                        } catch (Exception e) {
                                ProcessError (e);
                        }
@@ -1534,7 +1543,7 @@ namespace System.Web
                                InitOnce (true);
                        } catch (Exception e) {
                                initialization_exception = e;
-                               FinalErrorWrite (context.Response, new HttpException ("", e).GetHtmlErrorMessage ());
+                               FinalErrorWrite (context.Response, HttpException.NewWithCode (String.Empty, e, WebEventCodes.RuntimeErrorRequestAbort).GetHtmlErrorMessage ());
                                PipelineDone ();
                                return;
                        }
@@ -1725,7 +1734,7 @@ namespace System.Web
                        return RedirectErrorPage (redirect);
                        }
                        catch (Exception ex) {
-                               httpEx = new HttpException (500, "", ex);
+                               httpEx = HttpException.NewWithCode (500, String.Empty, ex, WebEventCodes.WebErrorOtherError);
                                return false;
                        }
                }
index 138e69841e83ac8e163f22356d8c930916466a2d..006da9ed8320b33e8b84c2a83c7b9d06366bc775 100644 (file)
@@ -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
@@ -48,12 +49,23 @@ namespace System.Web
        {
                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 ()
                {
@@ -88,6 +100,7 @@ namespace System.Web
                        : base (info, context)
                {
                        http_code = info.GetInt32 ("_httpCode");
+                       webEventCode = info.GetInt32 ("_webEventCode");
                }
 
                [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
@@ -95,6 +108,7 @@ namespace System.Web
                {
                        base.GetObjectData (info, context);
                        info.AddValue ("_httpCode", http_code);
+                       info.AddValue ("_webEventCode", webEventCode);
                }
 
                public HttpException (int httpCode, string message, int hr) 
@@ -166,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
index 7405ae20f4b8d79a7054c6cefdab365e00780c8b..ded9e9e8e55859d08ccec241f9af83043fb435f7 100644 (file)
@@ -39,6 +39,7 @@ using System.Security;
 using System.Security.Permissions;
 using System.Security.Principal;
 using System.Web.Configuration;
+using System.Web.Management;
 using System.Web.UI;
 using System.Web.Util;
 using System.Globalization;
@@ -335,7 +336,7 @@ namespace System.Web
                        get {
                                if (encoding == null){
                                        if (worker_request == null)
-                                               throw new HttpException ("No HttpWorkerRequest");
+                                               throw HttpException.NewWithCode ("No HttpWorkerRequest", WebEventCodes.RuntimeErrorRequestAbort);
                                        
                                        string content_type = ContentType;
                                        string parameter = GetParameter (content_type, "; charset=");
@@ -786,7 +787,7 @@ namespace System.Web
                        int content_length_kb = content_length / 1024;
                        HttpRuntimeSection config = (HttpRuntimeSection) WebConfigurationManager.GetWebApplicationSection ("system.web/httpRuntime");
                        if (content_length_kb > config.MaxRequestLength)
-                               throw new HttpException (400, "Upload size exceeds httpRuntime limit.");
+                               throw HttpException.NewWithCode (400, "Upload size exceeds httpRuntime limit.", WebEventCodes.RuntimeErrorPostTooLarge);
 
                        int total = 0;
                        byte [] buffer;
@@ -833,8 +834,9 @@ namespace System.Web
                                total = Math.Min (content_length, total);
                                IntPtr content = Marshal.AllocHGlobal (content_length);
                                if (content == (IntPtr) 0)
-                                       throw new HttpException (String.Format ("Not enough memory to allocate {0} bytes.",
-                                                                       content_length));
+                                       throw HttpException.NewWithCode (
+                                               String.Format ("Not enough memory to allocate {0} bytes.", content_length),
+                                               WebEventCodes.WebErrorOtherError);
 
                                if (total > 0)
                                        Marshal.Copy (buffer, 0, content, total);
@@ -870,7 +872,7 @@ namespace System.Web
                                                break;
                                        total += n;
                                        if (total < 0 || total > maxlength)
-                                               throw new HttpException (400, "Upload size exceeds httpRuntime limit.");
+                                               throw HttpException.NewWithCode (400, "Upload size exceeds httpRuntime limit.", WebEventCodes.RuntimeErrorPostTooLarge);
 
                                        if (ms != null && total > disk_th) {
                                                // Swith to on-disk file.
@@ -892,7 +894,7 @@ namespace System.Web
                        DoFilter (buffer);
 
                        if (total < content_length)
-                               throw new HttpException (411, "The request body is incomplete.");
+                               throw HttpException.NewWithCode (411, "The request body is incomplete.", WebEventCodes.WebErrorOtherError);
                }
 #endif
 
@@ -1290,7 +1292,7 @@ namespace System.Web
                public string MapPath (string virtualPath, string baseVirtualDir, bool allowCrossAppMapping)
                {
                        if (worker_request == null)
-                               throw new HttpException ("No HttpWorkerRequest");
+                               throw HttpException.NewWithCode ("No HttpWorkerRequest", WebEventCodes.RuntimeErrorRequestAbort);
 
                        if (virtualPath == null)
                                virtualPath = "~";
@@ -1301,7 +1303,7 @@ namespace System.Web
                        }
 
                        if (!VirtualPathUtility.IsValidVirtualPath (virtualPath))
-                               throw new HttpException (String.Format ("'{0}' is not a valid virtual path.", virtualPath));
+                               throw HttpException.NewWithCode (String.Format ("'{0}' is not a valid virtual path.", virtualPath), WebEventCodes.RuntimeErrorRequestAbort);
 
                        string appVirtualPath = HttpRuntime.AppDomainAppVirtualPath;
 
@@ -1314,9 +1316,9 @@ namespace System.Web
                        
                        if (!allowCrossAppMapping){
                                if (!StrUtils.StartsWith (virtualPath, appVirtualPath, true))
-                                       throw new HttpException ("MapPath: Mapping across applications not allowed");
+                                       throw HttpException.NewWithCode ("MapPath: Mapping across applications not allowed", WebEventCodes.RuntimeErrorRequestAbort);
                                if (appVirtualPath.Length > 1 && virtualPath.Length > 1 && virtualPath [0] != '/')
-                                       throw new HttpException ("MapPath: Mapping across applications not allowed");
+                                       throw HttpException.NewWithCode ("MapPath: Mapping across applications not allowed", WebEventCodes.RuntimeErrorRequestAbort);
                        }
 #if TARGET_JVM
                        return worker_request.MapPath (virtualPath);
index d2bd1447f7bd533d9b2111c36d94d7f64b6f1731..0ca52c6e21552a0481c4c8764edfe2a7c760ae4b 100644 (file)
@@ -40,6 +40,7 @@ using System.Security;
 using System.Security.Permissions;
 using System.Web.Caching;
 using System.Web.Configuration;
+using System.Web.Management;
 using System.Web.UI;
 using System.Web.Util;
 #if MONOWEB_DEP
@@ -453,7 +454,7 @@ namespace System.Web
                                SetupOfflineWatch ();
                                firstRun = false;
                                if (initialException != null) {
-                                       FinishWithException (req, new HttpException ("Initial exception", initialException));
+                                       FinishWithException (req, HttpException.NewWithCode ("Initial exception", initialException, WebEventCodes.RuntimeErrorRequestAbort));
                                        error = true;
                                }
                        }
@@ -470,7 +471,7 @@ namespace System.Web
                                try {
                                        app = HttpApplicationFactory.GetApplication (context);
                                } catch (Exception e) {
-                                       FinishWithException (req, new HttpException ("", e));
+                                       FinishWithException (req, HttpException.NewWithCode (String.Empty, e, WebEventCodes.RuntimeErrorRequestAbort));
                                        error = true;
                                }
                        }
index d271519838c5b4c064f94112f1612f8cccf01b55..714e98645a8720c3fb8067e38d279f4505fc4c4d 100644 (file)
@@ -319,6 +319,7 @@ System.Web/HttpContextCas.cs
 System.Web/HttpCookieCas.cs
 System.Web/HttpCookieCollectionCas.cs
 System.Web/HttpExceptionCas.cs
+System.Web/HttpExceptionTest.cs
 System.Web/HttpFileCollectionCas.cs
 System.Web/HttpModuleCollectionCas.cs
 System.Web/HttpParseExceptionCas.cs
diff --git a/mcs/class/System.Web/Test/System.Web/HttpExceptionTest.cs b/mcs/class/System.Web/Test/System.Web/HttpExceptionTest.cs
new file mode 100644 (file)
index 0000000..69a21a3
--- /dev/null
@@ -0,0 +1,23 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Web;
+using System.Web.Management;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Web
+{
+       [TestFixture]
+       public class HttpExceptionTest
+       {
+#if NET_4_0
+               [Test]
+               public void WebEventCode ()
+               {
+                       var ex = new HttpException ();
+                       Assert.AreEqual (WebEventCodes.UndefinedEventCode, ex.WebEventCode);
+               }
+#endif
+       }
+}