2007-05-01 Marek Habersack <mhabersack@novell.com>
[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                                 clientTarget = clientTarget.Trim ();
316 #if NET_2_0
317                                 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
318                                 ClientTarget ct = null;
319                                 
320                                 if ((ct = sec.ClientTargets [clientTarget]) == null)
321                                         clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
322                                 
323                                 if (ct == null && (ct = sec.ClientTargets [clientTarget]) == null) {
324                                         ThrowParseException (String.Format (
325                                                         "ClientTarget '{0}' is an invalid alias. See the " +
326                                                         "documentation for <clientTarget> config. section.",
327                                                         clientTarget));
328                                 }
329                                 clientTarget = ct.UserAgent;
330 #else
331                                 NameValueCollection coll;
332                                 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
333                                 object ct = null;
334                                 
335                                 if (coll != null) {
336                                         ct = coll [clientTarget];
337                                         if (ct == null)
338                                                 ct = coll [clientTarget.ToLower ()];
339                                 }
340                                 
341                                 if (ct == null) {
342                                         ThrowParseException (String.Format (
343                                                         "ClientTarget '{0}' is an invalid alias. See the " +
344                                                         "documentation for <clientTarget> config. section.",
345                                                         clientTarget));
346                                 }
347                                 clientTarget = (string) ct;
348 #endif
349                         }
350
351                         notBuffer = !GetBool (atts, "Buffer", true);
352                         
353 #if NET_2_0
354                         masterPage = GetString (atts, "MasterPageFile", masterPage);
355                         
356                         // Make sure the page exists
357                         if (!String.IsNullOrEmpty (masterPage))
358                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
359
360                         title = GetString(atts, "Title", null);
361
362                         theme = GetString (atts, "Theme", theme);
363                         styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
364                         enable_event_validation = GetBool (atts, "EnableEventValidation", true);
365                         maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
366 #endif
367                         // Ignored by now
368                         GetString (atts, "EnableViewStateMac", null);
369                         GetString (atts, "SmartNavigation", null);
370
371                         base.ProcessMainAttributes (atts);
372                 }
373                 
374 #if NET_2_0
375                 internal override void AddDirective (string directive, Hashtable atts)
376                 {
377                         if (String.Compare ("MasterType", directive, true) == 0) {
378                                 string type = GetString (atts, "TypeName", null);
379                                 if (type != null) {
380                                         masterType = LoadType (type);
381                                         if (masterType == null)
382                                                 ThrowParseException ("Could not load type '" + type + "'.");
383                                 } else {
384                                         string path = GetString (atts, "VirtualPath", null);
385                                         if (path != null)
386                                                 masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
387                                         else
388                                                 ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
389                                 }
390                                 AddAssembly (masterType.Assembly, true);
391                         }
392                         else
393                                 base.AddDirective (directive, atts);
394                 }
395 #endif
396                 
397                 static string SuggestCulture (string culture)
398                 {
399                         string retval = null;
400                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
401                                 if (ci.Name.StartsWith (culture))
402                                         retval += ci.Name + " ";
403                         }
404                         return retval;
405                 }
406
407                 protected override Type CompileIntoType ()
408                 {
409                         AspGenerator generator = new AspGenerator (this);
410                         return generator.GetCompiledType ();
411                 }
412
413                 internal bool EnableSessionState {
414                         get { return enableSessionState; }
415                 }
416
417                 internal bool EnableViewStateMac {
418                         get { return enableViewStateMac; }
419                 }
420                 
421                 internal bool SmartNavigation {
422                         get { return smartNavigation; }
423                 }
424                 
425                 internal bool ReadOnlySessionState {
426                         get { return readonlySessionState; }
427                 }
428
429                 internal bool HaveTrace {
430                         get { return haveTrace; }
431                 }
432
433                 internal bool Trace {
434                         get { return trace; }
435                 }
436
437                 internal TraceMode TraceMode {
438                         get { return tracemode; }
439                 }
440                 
441                 internal override Type DefaultBaseType {
442                         get { return baseType; }
443                 }
444
445                 internal override string DefaultBaseTypeName {
446                         get { return "System.Web.UI.Page"; }
447                 }
448
449                 internal override string DefaultDirectiveName {
450                         get { return "page"; }
451                 }
452
453                 internal string ResponseEncoding {
454                         get { return responseEncoding; }
455                 }
456
457                 internal string ContentType {
458                         get { return contentType; }
459                 }
460
461                 internal int CodePage {
462                         get { return codepage; }
463                 }
464
465                 internal string Culture {
466                         get { return culture; }
467                 }
468
469                 internal string UICulture {
470                         get { return uiculture; }
471                 }
472
473                 internal int LCID {
474                         get { return lcid; }
475                 }
476
477                 internal string ErrorPage {
478                         get { return errorPage; }
479                 }
480
481                 internal bool ValidateRequest {
482                         get { return validateRequest; }
483                 }
484
485                 internal string ClientTarget {
486                         get { return clientTarget; }
487                 }
488
489                 internal bool NotBuffer {
490                         get { return notBuffer; }
491                 }
492
493 #if NET_2_0
494                 internal string Theme {
495                         get { return theme; }
496                 }
497
498                 internal string StyleSheetTheme {
499                         get { return styleSheetTheme; }
500                 }
501
502                 internal string MasterPageFile {
503                         get { return masterPage; }
504                 }
505                 
506                 internal Type MasterType {
507                         get { return masterType; }
508                 }
509
510                 internal string Title {
511                         get { return title; }
512                 }
513
514                 internal bool EnableEventValidation {
515                         get { return enable_event_validation; }
516                 }
517
518                 internal bool MaintainScrollPositionOnPostBack {
519                         get { return maintainScrollPositionOnPostBack; }
520                 }
521
522                 internal int MaxPageStateFieldLength {
523                         get { return maxPageStateFieldLength; }
524                 }
525
526                 internal string PageParserFilterType {
527                         get { return pageParserFilter; }
528                 }
529 #endif
530         }
531 }
532