+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
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;
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;
}
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;
}
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;
}
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)
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;
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 ());
}
}
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
);
}
}
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);
}
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;
}
return RedirectErrorPage (redirect);
}
catch (Exception ex) {
- httpEx = new HttpException (500, "", ex);
+ httpEx = HttpException.NewWithCode (500, String.Empty, ex, WebEventCodes.WebErrorOtherError);
return false;
}
}
using System.Text;
using System.Web.Util;
using System.Web.Compilation;
+using System.Web.Management;
using System.Collections.Specialized;
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 ()
{
: base (info, context)
{
http_code = info.GetInt32 ("_httpCode");
+ webEventCode = info.GetInt32 ("_webEventCode");
}
[SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
{
base.GetObjectData (info, context);
info.AddValue ("_httpCode", http_code);
+ info.AddValue ("_webEventCode", webEventCode);
}
public HttpException (int httpCode, string message, int hr)
}
}
+ 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
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;
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=");
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;
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);
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.
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
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 = "~";
}
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;
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);
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
SetupOfflineWatch ();
firstRun = false;
if (initialException != null) {
- FinishWithException (req, new HttpException ("Initial exception", initialException));
+ FinishWithException (req, HttpException.NewWithCode ("Initial exception", initialException, WebEventCodes.RuntimeErrorRequestAbort));
error = true;
}
}
try {
app = HttpApplicationFactory.GetApplication (context);
} catch (Exception e) {
- FinishWithException (req, new HttpException ("", e));
+ FinishWithException (req, HttpException.NewWithCode (String.Empty, e, WebEventCodes.RuntimeErrorRequestAbort));
error = true;
}
}
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
--- /dev/null
+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
+ }
+}