ignore case and use invariant culture on comparison
[mono.git] / mcs / class / System.Web / System.Web.UI / PageParser.cs
1 //      
2 // System.Web.UI.PageParser
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class PageParser : TemplateControlParser
45         {
46                 bool enableSessionState = true;
47                 bool enableViewStateMac = true;
48                 bool smartNavigation = false;
49                 bool haveTrace;
50                 bool trace;
51                 bool notBuffer;
52                 TraceMode tracemode;
53                 bool readonlySessionState;
54                 string responseEncoding;
55                 string contentType;
56                 int codepage = -1;
57                 int lcid = -1;
58                 string culture;
59                 string uiculture;
60                 string errorPage;
61                 bool validateRequest;
62                 string clientTarget;
63                 Type baseType = typeof (Page);
64
65 #if NET_2_0
66                 string masterPage;
67                 Type masterType;
68                 string title;
69                 string theme;
70                 string styleSheetTheme;
71                 bool enable_event_validation;
72                 bool maintainScrollPositionOnPostBack;
73                 int maxPageStateFieldLength = -1;
74                 string pageParserFilter = String.Empty;
75 #endif
76
77                 public PageParser ()
78                 {
79                         LoadPagesConfigDefaults ();
80                 }
81                 
82                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
83                 {
84                         Context = context;
85                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
86                         InputFile = inputFile;
87                         SetBaseType (PagesConfig.PageBaseType);
88                         AddApplicationAssembly ();
89                         LoadPagesConfigDefaults ();
90                 }
91
92 #if NET_2_0
93                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
94                 {
95                         Context = context;
96                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
97                         Reader = reader;
98                         SetBaseType (PagesConfig.PageBaseType);
99                         AddApplicationAssembly ();
100                         LoadPagesConfigDefaults ();
101                 }
102 #endif
103
104                 internal override void LoadPagesConfigDefaults ()
105                 {
106                         base.LoadPagesConfigDefaults ();
107 #if NET_2_0
108                         PagesSection ps = PagesConfig;
109 #else
110                         PagesConfiguration ps = PagesConfig;
111 #endif
112
113                         notBuffer = !ps.Buffer;
114 #if NET_2_0
115                         switch (ps.EnableSessionState) {
116                                 case PagesEnableSessionState.True:
117                                 case PagesEnableSessionState.ReadOnly:
118                                         enableSessionState = true;
119                                         break;
120
121                                 default:
122                                         enableSessionState = false;
123                                         break;
124                         }
125 #else
126                         if (String.Compare (ps.EnableSessionState, "true", true, CultureInfo.InvariantCulture) == 0)
127                                 enableSessionState = true;
128                         else
129                                 enableSessionState = false;
130 #endif
131                         
132                         enableViewStateMac = ps.EnableViewStateMac;
133                         smartNavigation = ps.SmartNavigation;
134                         validateRequest = ps.ValidateRequest;
135 #if NET_2_0
136                         masterPage = ps.MasterPageFile;
137                         if (masterPage.Length == 0)
138                                 masterPage = null;
139                         
140                         enable_event_validation = ps.EnableEventValidation;
141                         maxPageStateFieldLength = ps.MaxPageStateFieldLength;
142                         pageParserFilter = ps.PageParserFilterType;
143                         theme = ps.Theme;
144                         if (theme.Length == 0)
145                                 theme = null;
146                         styleSheetTheme = ps.StyleSheetTheme;
147                         if (styleSheetTheme.Length == 0)
148                                 styleSheetTheme = null;
149                         maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
150 #endif
151                 }
152                 
153                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
154                                                                     string inputFile, 
155                                                                     HttpContext context)
156                 {
157                         PageParser pp = new PageParser (virtualPath, inputFile, context);
158                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
159                         return h;
160                 }
161                 
162                 internal override void ProcessMainAttributes (Hashtable atts)
163                 {
164                         // note: the 'enableSessionState' configuration property is
165                         // processed in a case-sensitive manner while the page-level
166                         // attribute is processed case-insensitive
167                         string enabless = GetString (atts, "EnableSessionState", enableSessionState.ToString ());
168                         if (enabless != null) {
169                                 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
170                                 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
171                                         enableSessionState = true;
172                                 } else if (String.Compare (enabless, "false", true) == 0) {
173                                         enableSessionState = false;
174                                 } else {
175                                         ThrowParseException ("Invalid value for enableSessionState: " + enabless);
176                                 }
177                         }
178
179                         string cp = GetString (atts, "CodePage", null);
180                         if (cp != null) {
181                                 if (responseEncoding != null)
182                                         ThrowParseException ("CodePage and ResponseEncoding are " +
183                                                              "mutually exclusive.");
184
185                                 int codepage = 0;
186                                 try {
187                                         codepage = (int) UInt32.Parse (cp);
188                                 } catch {
189                                         ThrowParseException ("Invalid value for CodePage: " + cp);
190                                 }
191
192                                 try {
193                                         Encoding.GetEncoding (codepage);
194                                 } catch {
195                                         ThrowParseException ("Unsupported codepage: " + cp);
196                                 }
197                         }
198                         
199                         responseEncoding = GetString (atts, "ResponseEncoding", null);
200                         if (responseEncoding != null) {
201                                 if (codepage != -1)
202                                         ThrowParseException ("CodePage and ResponseEncoding are " +
203                                                              "mutually exclusive.");
204
205                                 try {
206                                         Encoding.GetEncoding (responseEncoding);
207                                 } catch {
208                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
209                                 }
210                         }
211                         
212                         contentType = GetString (atts, "ContentType", null);
213
214                         string lcidStr = GetString (atts, "LCID", null);
215                         if (lcidStr != null) {
216                                 try {
217                                         lcid = (int) UInt32.Parse (lcidStr);
218                                 } catch {
219                                         ThrowParseException ("Invalid value for LCID: " + lcid);
220                                 }
221
222                                 CultureInfo ci = null;
223                                 try {
224                                         ci = new CultureInfo (lcid);
225                                 } catch {
226                                         ThrowParseException ("Unsupported LCID: " + lcid);
227                                 }
228
229                                 if (ci.IsNeutralCulture) {
230                                         string suggestedCulture = SuggestCulture (ci.Name);
231                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
232                                         if (suggestedCulture != null) {
233                                                 ThrowParseException (fmt + " Please try one of these: " +
234                                                                      suggestedCulture);
235                                         } else {
236                                                 ThrowParseException (fmt);
237                                         }
238                                 }
239                         }
240
241                         culture = GetString (atts, "Culture", null);
242                         if (culture != null) {
243                                 if (lcidStr != null) 
244                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
245                                 
246                                 CultureInfo ci = null;
247                                 try {
248 #if NET_2_0
249                                         if (!culture.StartsWith ("auto"))
250 #endif
251                                                 ci = new CultureInfo (culture);
252                                 } catch {
253                                         ThrowParseException ("Unsupported Culture: " + culture);
254                                 }
255
256                                 if (ci != null && ci.IsNeutralCulture) {
257                                         string suggestedCulture = SuggestCulture (culture);
258                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
259                                         if (suggestedCulture != null)
260                                                 ThrowParseException (fmt +
261                                                                 " Please try one of these: " + suggestedCulture);
262                                         else
263                                                 ThrowParseException (fmt);
264                                 }
265                         }
266
267                         uiculture = GetString (atts, "UICulture", null);
268                         if (uiculture != null) {
269                                 CultureInfo ci = null;
270                                 try {
271 #if NET_2_0
272                                         if (!uiculture.StartsWith ("auto"))
273 #endif
274                                                 ci = new CultureInfo (uiculture);
275                                 } catch {
276                                         ThrowParseException ("Unsupported Culture: " + uiculture);
277                                 }
278
279                                 if (ci != null && ci.IsNeutralCulture) {
280                                         string suggestedCulture = SuggestCulture (uiculture);
281                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
282                                         if (suggestedCulture != null)
283                                                 ThrowParseException (fmt +
284                                                                 " Please try one of these: " + suggestedCulture);
285                                         else
286                                                 ThrowParseException (fmt);
287                                 }
288                         }
289
290                         string tracestr = GetString (atts, "Trace", null);
291                         if (tracestr != null) {
292                                 haveTrace = true;
293                                 atts ["Trace"] = tracestr;
294                                 trace = GetBool (atts, "Trace", false);
295                         }
296
297                         string tracemodes = GetString (atts, "TraceMode", null);
298                         if (tracemodes != null) {
299                                 bool valid = true;
300                                 try {
301                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
302                                 } catch {
303                                         valid = false;
304                                 }
305
306                                 if (!valid || tracemode == TraceMode.Default)
307                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
308                                                         "one of the following values: SortByTime, SortByCategory.");
309                         }
310
311                         errorPage = GetString (atts, "ErrorPage", null);
312                         validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
313                         clientTarget = GetString (atts, "ClientTarget", null);
314                         if (clientTarget != null) {
315 #if NET_2_0
316                                 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
317                                 if (sec.ClientTargets[clientTarget] == null) {
318                                         ThrowParseException (String.Format (
319                                                         "ClientTarget '{0}' is an invalid alias. See the " +
320                                                         "documentation for <clientTarget> config. section.",
321                                                         clientTarget));
322                                 }
323                                 clientTarget = sec.ClientTargets[clientTarget].UserAgent;
324 #else
325                                 NameValueCollection coll;
326                                 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
327                                 if (coll == null || coll [clientTarget] == null) {
328                                         ThrowParseException (String.Format (
329                                                         "ClientTarget '{0}' is an invalid alias. See the " +
330                                                         "documentation for <clientTarget> config. section.",
331                                                         clientTarget));
332                                 }
333                                 clientTarget = (string) coll [clientTarget];
334 #endif
335                         }
336
337                         notBuffer = !GetBool (atts, "Buffer", true);
338                         
339 #if NET_2_0
340                         masterPage = GetString (atts, "MasterPageFile", masterPage);
341                         
342                         // Make sure the page exists
343                         if (!String.IsNullOrEmpty (masterPage))
344                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
345
346                         title = GetString(atts, "Title", null);
347
348                         theme = GetString (atts, "Theme", theme);
349                         styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
350                         enable_event_validation = GetBool (atts, "EnableEventValidation", true);
351                         maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
352 #endif
353                         // Ignored by now
354                         GetString (atts, "EnableViewStateMac", null);
355                         GetString (atts, "SmartNavigation", null);
356
357                         base.ProcessMainAttributes (atts);
358                 }
359                 
360 #if NET_2_0
361                 internal override void AddDirective (string directive, Hashtable atts)
362                 {
363                         if (String.Compare ("MasterType", directive, true) == 0) {
364                                 string type = GetString (atts, "TypeName", null);
365                                 if (type != null) {
366                                         masterType = LoadType (type);
367                                         if (masterType == null)
368                                                 ThrowParseException ("Could not load type '" + type + "'.");
369                                 } else {
370                                         string path = GetString (atts, "VirtualPath", null);
371                                         if (path != null)
372                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
373                                         else
374                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
375                                 }
376                                 AddAssembly (masterType.Assembly, true);
377                         }
378                         else
379                                 base.AddDirective (directive, atts);
380                 }
381 #endif
382                 
383                 static string SuggestCulture (string culture)
384                 {
385                         string retval = null;
386                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
387                                 if (ci.Name.StartsWith (culture))
388                                         retval += ci.Name + " ";
389                         }
390                         return retval;
391                 }
392
393                 protected override Type CompileIntoType ()
394                 {
395                         AspGenerator generator = new AspGenerator (this);
396                         return generator.GetCompiledType ();
397                 }
398
399                 internal bool EnableSessionState {
400                         get { return enableSessionState; }
401                 }
402
403                 internal bool EnableViewStateMac {
404                         get { return enableViewStateMac; }
405                 }
406                 
407                 internal bool SmartNavigation {
408                         get { return smartNavigation; }
409                 }
410                 
411                 internal bool ReadOnlySessionState {
412                         get { return readonlySessionState; }
413                 }
414
415                 internal bool HaveTrace {
416                         get { return haveTrace; }
417                 }
418
419                 internal bool Trace {
420                         get { return trace; }
421                 }
422
423                 internal TraceMode TraceMode {
424                         get { return tracemode; }
425                 }
426                 
427                 internal override Type DefaultBaseType {
428                         get { return baseType; }
429                 }
430
431                 internal override string DefaultBaseTypeName {
432                         get { return "System.Web.UI.Page"; }
433                 }
434
435                 internal override string DefaultDirectiveName {
436                         get { return "page"; }
437                 }
438
439                 internal string ResponseEncoding {
440                         get { return responseEncoding; }
441                 }
442
443                 internal string ContentType {
444                         get { return contentType; }
445                 }
446
447                 internal int CodePage {
448                         get { return codepage; }
449                 }
450
451                 internal string Culture {
452                         get { return culture; }
453                 }
454
455                 internal string UICulture {
456                         get { return uiculture; }
457                 }
458
459                 internal int LCID {
460                         get { return lcid; }
461                 }
462
463                 internal string ErrorPage {
464                         get { return errorPage; }
465                 }
466
467                 internal bool ValidateRequest {
468                         get { return validateRequest; }
469                 }
470
471                 internal string ClientTarget {
472                         get { return clientTarget; }
473                 }
474
475                 internal bool NotBuffer {
476                         get { return notBuffer; }
477                 }
478
479 #if NET_2_0
480                 internal string Theme {
481                         get { return theme; }
482                 }
483
484                 internal string StyleSheetTheme {
485                         get { return styleSheetTheme; }
486                 }
487
488                 internal string MasterPageFile {
489                         get { return masterPage; }
490                 }
491                 
492                 internal Type MasterType {
493                         get { return masterType; }
494                 }
495
496                 internal string Title {
497                         get { return title; }
498                 }
499
500                 internal bool EnableEventValidation {
501                         get { return enable_event_validation; }
502                 }
503
504                 internal bool MaintainScrollPositionOnPostBack {
505                         get { return maintainScrollPositionOnPostBack; }
506                 }
507
508                 internal int MaxPageStateFieldLength {
509                         get { return maxPageStateFieldLength; }
510                 }
511
512                 internal string PageParserFilterType {
513                         get { return pageParserFilter; }
514                 }
515 #endif
516         }
517 }
518