-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathCustomErrorsSection.cs
252 lines (214 loc) · 8.83 KB
/
CustomErrorsSection.cs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//------------------------------------------------------------------------------
// <copyright file="CustomErrorsSection.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.Configuration {
using System;
using System.Xml;
using System.Configuration;
using System.Collections.Specialized;
using System.Collections;
using System.IO;
using System.Text;
using System.Globalization;
using System.Web.Util;
using System.Web.Configuration;
using System.Security.Permissions;
/* From Machine.Config
<!--
customErrors Attributes:
mode="[On|Off|RemoteOnly]"
On: Always display custom errors
Off: Always display ASP.NET error pages
RemoteOnly: Display custom errors to remote clients and ASP.NET errors to localhost
redirectMode="[ResponseRedirect|ResponseRewrite]"
ResponseRedirect: Use redirection to display the custom error page, the URL changes
ResponseRewrite: Send the custom error page without changing the URL
defaultRedirect="url" - Url to redirect client to when an error occurs
-->
<customErrors mode="RemoteOnly" />
*/
public sealed class CustomErrorsSection : ConfigurationSection {
private static ConfigurationPropertyCollection _properties;
private static readonly ConfigurationProperty _propAllowNestedErrors =
new ConfigurationProperty("allowNestedErrors",
typeof(bool),
false /* defaultValue */,
ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propDefaultRedirect =
new ConfigurationProperty("defaultRedirect",
typeof(string),
null,
ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propRedirectMode =
new ConfigurationProperty("redirectMode",
typeof(CustomErrorsRedirectMode),
CustomErrorsRedirectMode.ResponseRedirect,
ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propMode =
new ConfigurationProperty("mode",
typeof(CustomErrorsMode),
CustomErrorsMode.RemoteOnly,
ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propErrors =
new ConfigurationProperty(null,
typeof(CustomErrorCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection);
private string basepath = null;
private string _DefaultAbsolutePath = null;
private static CustomErrorsSection _default = null;
static CustomErrorsSection() {
// Property initialization
_properties = new ConfigurationPropertyCollection();
_properties.Add(_propAllowNestedErrors);
_properties.Add(_propDefaultRedirect);
_properties.Add(_propRedirectMode);
_properties.Add(_propMode);
_properties.Add(_propErrors);
}
public CustomErrorsSection() {
}
protected override ConfigurationPropertyCollection Properties {
get {
return _properties;
}
}
[ConfigurationProperty("allowNestedErrors", DefaultValue = false)]
public bool AllowNestedErrors {
get {
return (bool)base[_propAllowNestedErrors];
}
set {
base[_propAllowNestedErrors] = value;
}
}
[ConfigurationProperty("defaultRedirect")]
public string DefaultRedirect {
get {
return (string)base[_propDefaultRedirect];
}
set {
base[_propDefaultRedirect] = value;
}
}
[ConfigurationProperty("redirectMode", DefaultValue = CustomErrorsRedirectMode.ResponseRedirect)]
public CustomErrorsRedirectMode RedirectMode {
get {
return (CustomErrorsRedirectMode)base[_propRedirectMode];
}
set {
base[_propRedirectMode] = value;
}
}
[ConfigurationProperty("mode", DefaultValue = CustomErrorsMode.RemoteOnly)]
public CustomErrorsMode Mode {
get {
return (CustomErrorsMode)base[_propMode];
}
set {
base[_propMode] = value;
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public CustomErrorCollection Errors {
get {
return (CustomErrorCollection)base[_propErrors];
}
}
internal String DefaultAbsolutePath {
get {
if (_DefaultAbsolutePath == null) {
_DefaultAbsolutePath = GetAbsoluteRedirect(DefaultRedirect, basepath);
}
return _DefaultAbsolutePath;
}
}
internal String GetRedirectString(int code) {
String r = null;
if (Errors != null) {
CustomError ce = Errors[(string)code.ToString(CultureInfo.InvariantCulture)];
if (ce != null)
r = GetAbsoluteRedirect(ce.Redirect, basepath);
}
if (r == null) {
r = DefaultAbsolutePath;
}
return r;
}
protected override void Reset(ConfigurationElement parentElement) {
base.Reset(parentElement);
CustomErrorsSection parent = parentElement as CustomErrorsSection;
if (parent != null) {
basepath = parent.basepath;
}
}
protected override void DeserializeSection(XmlReader reader) {
WebContext context;
base.DeserializeSection(reader);
// Determine Web Context
context = EvaluationContext.HostingContext as WebContext;
if (context != null) {
basepath = UrlPath.AppendSlashToPathIfNeeded(context.Path);
}
}
//
// helper to create absolute redirect
//
internal static String GetAbsoluteRedirect(String path, String basePath) {
if (path != null && UrlPath.IsRelativeUrl(path)) {
if (String.IsNullOrEmpty(basePath))
basePath = "/";
path = UrlPath.Combine(basePath, path);
}
return path;
}
internal static CustomErrorsSection GetSettings(HttpContext context) {
return GetSettings(context, false);
}
internal static CustomErrorsSection GetSettings(HttpContext context, bool canThrow) {
CustomErrorsSection ce = null;
RuntimeConfig runtimeConfig = null;
if (canThrow) {
runtimeConfig = RuntimeConfig.GetConfig(context);
if (runtimeConfig != null) {
ce = runtimeConfig.CustomErrors;
}
}
else {
runtimeConfig = RuntimeConfig.GetLKGConfig(context);
if (runtimeConfig != null) {
ce = runtimeConfig.CustomErrors;
}
if (ce == null) {
if (_default == null) {
_default = new CustomErrorsSection();
}
ce = _default;
}
}
return ce;
}
internal bool CustomErrorsEnabled(HttpRequest request) {
// This could throw if the config file is malformed, but we don't want
// to throw from here, as it would mess up error handling
try {
// Always turn of custom errors in retail deployment mode (DevDiv 36396)
if (DeploymentSection.RetailInternal)
return true;
}
catch { }
switch (Mode) {
case CustomErrorsMode.Off:
return false;
case CustomErrorsMode.On:
return true;
case CustomErrorsMode.RemoteOnly:
return (!request.IsLocal);
default:
return false;
}
}
}
}