-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathGlobalizationSection.cs
344 lines (313 loc) · 14.2 KB
/
GlobalizationSection.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//------------------------------------------------------------------------------
// <copyright file="GlobalizationSection.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.Threading;
using System.Globalization;
using System.Web.Util;
using System.Security.Permissions;
/*
<!--
globalization Attributes:
requestEncoding="[Encoding value]" - Encoding to use for request
responseEncoding="[Encoding value]" - Encoding to use for response
enableBestFitResponseEncoding="[true|false]" - Enable best fit character encoding for response
responseHeaderEncoding="[Encoding value]" - Encoding to use for response headers (default is utf-8)
fileEncoding="[Encoding value]" - Encoding to use for files
culture="[Culture]" - default Thread.CurrentCulture
uiCulture="[Culture]" - default Thread.CurrentUICulture
resourceProviderFactoryType="[type]"
-->
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
/>
*/
public sealed class GlobalizationSection : ConfigurationSection {
private static ConfigurationPropertyCollection _properties;
private static readonly ConfigurationProperty _propRequestEncoding =
new ConfigurationProperty("requestEncoding", typeof(string), Encoding.UTF8.WebName, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propResponseEncoding =
new ConfigurationProperty("responseEncoding", typeof(string), Encoding.UTF8.WebName, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propFileEncoding =
new ConfigurationProperty("fileEncoding", typeof(string), String.Empty, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propCulture =
new ConfigurationProperty("culture", typeof(string), String.Empty, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propUICulture =
new ConfigurationProperty("uiCulture", typeof(string), String.Empty, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propEnableClientBasedCulture =
new ConfigurationProperty("enableClientBasedCulture", typeof(bool), false, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propResponseHeaderEncoding =
new ConfigurationProperty("responseHeaderEncoding", typeof(string), Encoding.UTF8.WebName, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propResourceProviderFactoryType =
new ConfigurationProperty("resourceProviderFactoryType", typeof(string), String.Empty, ConfigurationPropertyOptions.None);
private static readonly ConfigurationProperty _propEnableBestFitResponseEncoding =
new ConfigurationProperty("enableBestFitResponseEncoding", typeof(bool), false, ConfigurationPropertyOptions.None);
private Encoding responseEncodingCache = null;
private Encoding responseHeaderEncodingCache = null;
private Encoding requestEncodingCache = null;
private Encoding fileEncodingCache = null;
private String cultureCache = null;
private String uiCultureCache = null;
private Type _resourceProviderFactoryType;
static GlobalizationSection() {
// Property initialization
_properties = new ConfigurationPropertyCollection();
_properties.Add(_propRequestEncoding);
_properties.Add(_propResponseEncoding);
_properties.Add(_propFileEncoding);
_properties.Add(_propCulture);
_properties.Add(_propUICulture);
_properties.Add(_propEnableClientBasedCulture);
_properties.Add(_propResponseHeaderEncoding);
_properties.Add(_propResourceProviderFactoryType);
_properties.Add(_propEnableBestFitResponseEncoding);
}
public GlobalizationSection() {
}
protected override ConfigurationPropertyCollection Properties {
get {
return _properties;
}
}
[ConfigurationProperty("requestEncoding", DefaultValue = "utf-8")]
public Encoding RequestEncoding {
get {
if (requestEncodingCache == null) {
requestEncodingCache = Encoding.UTF8;
}
return requestEncodingCache;
}
set {
if (value != null) {
base[_propRequestEncoding] = value.WebName;
requestEncodingCache = value;
}
else {
base[_propRequestEncoding] = value;
requestEncodingCache = Encoding.UTF8;
}
}
}
[ConfigurationProperty("responseEncoding", DefaultValue = "utf-8")]
public Encoding ResponseEncoding {
get {
if (responseEncodingCache == null)
responseEncodingCache = Encoding.UTF8;
return responseEncodingCache;
}
set {
if (value != null) {
base[_propResponseEncoding] = value.WebName;
responseEncodingCache = value;
}
else {
base[_propResponseEncoding] = value;
responseEncodingCache = Encoding.UTF8;
}
}
}
[ConfigurationProperty("responseHeaderEncoding", DefaultValue = "utf-8")]
public Encoding ResponseHeaderEncoding {
get {
if (responseHeaderEncodingCache == null) {
responseHeaderEncodingCache = Encoding.UTF8;
}
return responseHeaderEncodingCache;
}
set {
if (value != null) {
base[_propResponseHeaderEncoding] = value.WebName;
responseHeaderEncodingCache = value;
}
else {
base[_propResponseHeaderEncoding] = value;
responseHeaderEncodingCache = Encoding.UTF8;
}
}
}
[ConfigurationProperty("fileEncoding")]
public Encoding FileEncoding {
get {
if (fileEncodingCache == null) {
fileEncodingCache = Encoding.Default;
}
return fileEncodingCache;
}
set {
if (value != null) {
base[_propFileEncoding] = value.WebName;
fileEncodingCache = value;
}
else {
base[_propFileEncoding] = value;
fileEncodingCache = Encoding.Default;
}
}
}
[ConfigurationProperty("culture", DefaultValue = "")]
public string Culture {
get {
if (cultureCache == null) {
cultureCache = (string)base[_propCulture];
}
return cultureCache;
}
set {
base[_propCulture] = value;
cultureCache = value;
}
}
[ConfigurationProperty("uiCulture", DefaultValue = "")]
public string UICulture {
get {
if (uiCultureCache == null) {
uiCultureCache = (string)base[_propUICulture];
}
return uiCultureCache;
}
set {
base[_propUICulture] = value;
uiCultureCache = value;
}
}
[ConfigurationProperty("enableClientBasedCulture", DefaultValue = false)]
public bool EnableClientBasedCulture {
get {
return (bool)base[_propEnableClientBasedCulture];
}
set {
base[_propEnableClientBasedCulture] = value;
}
}
[ConfigurationProperty("resourceProviderFactoryType", DefaultValue = "")]
public string ResourceProviderFactoryType {
get {
return (string)base[_propResourceProviderFactoryType];
}
set {
base[_propResourceProviderFactoryType] = value;
}
}
[ConfigurationProperty("enableBestFitResponseEncoding", DefaultValue = false)]
public bool EnableBestFitResponseEncoding {
get {
return (bool)base[_propEnableBestFitResponseEncoding];
}
set {
base[_propEnableBestFitResponseEncoding] = value;
}
}
internal Type ResourceProviderFactoryTypeInternal {
get {
if (_resourceProviderFactoryType == null && !String.IsNullOrEmpty(ResourceProviderFactoryType)) {
lock (this) {
if (_resourceProviderFactoryType == null) {
Type resourceProviderFactoryType = ConfigUtil.GetType(ResourceProviderFactoryType, "resourceProviderFactoryType", this);
ConfigUtil.CheckBaseType(typeof(System.Web.Compilation.ResourceProviderFactory), resourceProviderFactoryType, "resourceProviderFactoryType", this);
_resourceProviderFactoryType = resourceProviderFactoryType;
}
}
}
return _resourceProviderFactoryType;
}
}
private void CheckCulture(string configCulture) {
if (StringUtil.EqualsIgnoreCase(configCulture, HttpApplication.AutoCulture)) {
return;
}
else if (StringUtil.StringStartsWithIgnoreCase(configCulture, HttpApplication.AutoCulture)) {
// This will throw if bad
CultureInfo dummyCultureInfo = new CultureInfo(configCulture.Substring(5));
return;
}
// This will throw if bad
new CultureInfo(configCulture);
}
protected override void PreSerialize(XmlWriter writer) {
PostDeserialize();
}
protected override void PostDeserialize() {
ConfigurationPropertyCollection props = Properties;
// Need to check that the encodings provided are valid here
ConfigurationProperty errorProperty = null;
int errorLine = Int32.MaxValue;
try {
if (!String.IsNullOrEmpty((string)base[_propResponseEncoding]))
responseEncodingCache = (Encoding)Encoding.GetEncoding((string)base[_propResponseEncoding]);
}
catch {
errorProperty = _propResponseEncoding;
errorLine = ElementInformation.Properties[errorProperty.Name].LineNumber;
}
try {
if (!String.IsNullOrEmpty((string)base[_propResponseHeaderEncoding])) {
responseHeaderEncodingCache = (Encoding)Encoding.GetEncoding((string)base[_propResponseHeaderEncoding]);
}
}
catch {
if (errorLine > ElementInformation.Properties[_propResponseHeaderEncoding.Name].LineNumber) {
errorProperty = _propResponseHeaderEncoding;
errorLine = ElementInformation.Properties[errorProperty.Name].LineNumber;
}
}
try {
if (!String.IsNullOrEmpty((string)base[_propRequestEncoding])) {
requestEncodingCache = (Encoding)Encoding.GetEncoding((string)base[_propRequestEncoding]);
}
}
catch {
if (errorLine > ElementInformation.Properties[_propRequestEncoding.Name].LineNumber) {
errorProperty = _propRequestEncoding;
errorLine = ElementInformation.Properties[errorProperty.Name].LineNumber;
}
}
try {
if (!String.IsNullOrEmpty((string)base[_propFileEncoding])) {
fileEncodingCache = (Encoding)Encoding.GetEncoding((string)base[_propFileEncoding]);
}
}
catch {
if (errorLine > ElementInformation.Properties[_propFileEncoding.Name].LineNumber) {
errorProperty = _propFileEncoding;
errorLine = ElementInformation.Properties[errorProperty.Name].LineNumber;
}
}
try {
if (!String.IsNullOrEmpty((string)base[_propCulture])) {
CheckCulture((string)base[_propCulture]);
}
}
catch {
if (errorLine > ElementInformation.Properties[_propCulture.Name].LineNumber) {
errorProperty = _propCulture;
errorLine = ElementInformation.Properties[_propCulture.Name].LineNumber;
}
}
try {
if (!String.IsNullOrEmpty((string)base[_propUICulture])) {
CheckCulture((string)base[_propUICulture]);
}
}
catch {
if (errorLine > ElementInformation.Properties[_propUICulture.Name].LineNumber) {
errorProperty = _propUICulture;
errorLine = ElementInformation.Properties[_propUICulture.Name].LineNumber;
}
}
if (errorProperty != null) {
throw new ConfigurationErrorsException(SR.GetString(SR.Invalid_value_for_globalization_attr, errorProperty.Name),
ElementInformation.Properties[errorProperty.Name].Source, ElementInformation.Properties[errorProperty.Name].LineNumber);
}
}
}
}