1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
//------------------------------------------------------------------------------
// <copyright file="ErrorHandlerModule.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.IO;
using System.Web;
using System.Diagnostics;
using System.Collections;
using System.Text;
using System.Security.Permissions;
using System.Globalization;
namespace System.Web.Mobile
{
/*
* Error Handler Module
* An Http Module that traps errors, and formats them for the appropriate
* device.
*
* Copyright (c) 2000 Microsoft Corporation
*/
/// <include file='doc\ErrorHandlerModule.uex' path='docs/doc[@for="ErrorHandlerModule"]/*' />
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
public class ErrorHandlerModule : IHttpModule
{
/// <include file='doc\ErrorHandlerModule.uex' path='docs/doc[@for="ErrorHandlerModule.IHttpModule.Init"]/*' />
/// <internalonly/>
void IHttpModule.Init(HttpApplication application)
{
// application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.Error += (new EventHandler(this.Application_Error));
// application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
/* obsolete
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (context != null)
{
// Some device/gateway combination sends postdata's charset
// in a separate header rather than in Content-Type.
SetCharsetInRequestHeader(context);
}
}
private void SetCharsetInRequestHeader(HttpContext context)
{
String userAgent = context.Request.UserAgent;
if (userAgent != null && CultureInfo.InvariantCulture.CompareInfo.IsPrefix(userAgent, "UP"))
{
String postDataCharset = context.Request.Headers["x-up-devcap-post-charset"];
if (postDataCharset != null && postDataCharset.Length > 0)
{
try
{
context.Request.ContentEncoding = Encoding.GetEncoding(postDataCharset);
}
catch
{
// Exception may be thrown when charset is not valid.
// In this case, do nothing, and let the framework
// use the configured RequestEncoding setting.
}
}
}
}
*/
/* Obsolete
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (context != null)
{
MobileRedirect.CheckForInvalidRedirection(context);
}
}
*/
private void Application_Error(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = null;
bool useAdaptiveErrorReporting = false;
try
{
context = application.Context;
if(context.IsCustomErrorEnabled)
{
return;
}
Exception error = context.Server.GetLastError();
if ((error == null) || (!RequiresAdaptiveErrorReporting(context, error)))
{
return;
}
useAdaptiveErrorReporting = true;
MobileErrorInfo errorInfo = new MobileErrorInfo(error);
context.Items[MobileErrorInfo.ContextKey] = errorInfo;
context.Response.Clear();
IHttpHandler errorHandler = CreateErrorFormatter(context);
errorHandler.ProcessRequest(context);
}
catch(Exception e2)
{
if (useAdaptiveErrorReporting && context != null)
{
// Failed to format error. Let it continue through
// default processing.
context.Response.Write(e2.ToString());
context.Server.ClearError();
return;
}
else
{
return;
}
}
context.Server.ClearError();
}
/// <include file='doc\ErrorHandlerModule.uex' path='docs/doc[@for="ErrorHandlerModule.IHttpModule.Dispose"]/*' />
/// <internalonly/>
void IHttpModule.Dispose()
{
}
private bool RequiresAdaptiveErrorReporting(HttpContext context, Exception error)
{
// Check if the error message is a non-500 error.
HttpException httpError = error as HttpException;
if (httpError != null && httpError.GetHttpCode() != 500)
{
return false;
}
bool b;
// Checks whether custom error formatting is required for the
// given device.
MobileCapabilities caps = context.Request.Browser as MobileCapabilities;
if (caps == null)
{
b = false;
}
else if (caps.PreferredRenderingMime != "text/html")
{
b = true;
}
else
{
b = caps.RequiresHtmlAdaptiveErrorReporting;
}
return b;
}
private IHttpHandler CreateErrorFormatter(HttpContext context)
{
//
return new System.Web.UI.MobileControls.ErrorFormatterPage();
}
}
}
|