2009-02-28 Gonzalo Paniagua Javier <gonzalo@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.Hosting;
38 using System.Web.Util;
39 using System.IO;
40
41 namespace System.Web.UI
42 {
43         // CAS - no InheritanceDemand here as the class is sealed
44         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         public sealed class PageParser : TemplateControlParser
46         {
47                 PagesEnableSessionState enableSessionState = PagesEnableSessionState.True;
48                 bool enableViewStateMac = true;
49                 bool smartNavigation;
50                 bool haveTrace;
51                 bool trace;
52                 bool notBuffer;
53                 TraceMode tracemode = TraceMode.Default;
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
64 #if NET_2_0
65                 bool async;
66                 int asyncTimeout = -1;
67                 string masterPage;
68                 Type masterType;
69                 string masterVirtualPath;
70                 string title;
71                 string theme;
72                 string styleSheetTheme;
73                 bool enable_event_validation;
74                 bool maintainScrollPositionOnPostBack;
75                 int maxPageStateFieldLength = -1;
76                 Type previousPageType;
77                 string previousPageVirtualPath;
78 #endif
79
80                 public PageParser ()
81                 {
82                         LoadConfigDefaults ();
83                 }
84                 
85                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
86                 {
87 #if NET_2_0
88                         this.VirtualPath = new VirtualPath (virtualPath);
89 #endif
90
91                         Context = context;
92                         BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
93                         InputFile = inputFile;
94                         SetBaseType (null);
95                         AddApplicationAssembly ();
96                         LoadConfigDefaults ();
97                 }
98
99 #if NET_2_0
100                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
101                         : this (virtualPath, null, reader, context)
102                 {
103                 }
104                 
105                 internal PageParser (string virtualPath, string inputFile, TextReader reader, HttpContext context)
106                 {
107                         this.VirtualPath = new VirtualPath (virtualPath);
108                         Context = context;
109                         BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
110                         Reader = reader;
111                         if (String.IsNullOrEmpty (inputFile)) {
112                                 HttpRequest req = context != null ? context.Request : null;
113                                 if (req != null)
114                                         InputFile = req.MapPath (virtualPath);
115                         } else
116                                 InputFile = inputFile;
117                         SetBaseType (null);
118                         AddApplicationAssembly ();
119                         LoadConfigDefaults ();
120                 }
121 #endif
122
123                 internal override void LoadConfigDefaults ()
124                 {
125                         base.LoadConfigDefaults ();
126 #if NET_2_0
127                         PagesSection ps = PagesConfig;
128 #else
129                         PagesConfiguration ps = PagesConfig;
130 #endif                  
131
132                         notBuffer = !ps.Buffer;
133                         enableSessionState = ps.EnableSessionState;
134                         enableViewStateMac = ps.EnableViewStateMac;
135                         smartNavigation = ps.SmartNavigation;
136                         validateRequest = ps.ValidateRequest;
137 #if NET_2_0
138                         masterPage = ps.MasterPageFile;
139                         if (masterPage.Length == 0)
140                                 masterPage = null;
141                         enable_event_validation = ps.EnableEventValidation;
142                         maxPageStateFieldLength = ps.MaxPageStateFieldLength;
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 #if NET_2_0
158                         return BuildManager.CreateInstanceFromVirtualPath (virtualPath, typeof (IHttpHandler)) as IHttpHandler;
159 #else
160                         PageParser pp = new PageParser (virtualPath, inputFile, context);
161                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
162                         return h;
163 #endif
164                 }
165                 
166                 internal override void ProcessMainAttributes (Hashtable atts)
167                 {
168                         // note: the 'enableSessionState' configuration property is
169                         // processed in a case-sensitive manner while the page-level
170                         // attribute is processed case-insensitive
171                         string enabless = GetString (atts, "EnableSessionState", null);
172                         if (enabless != null) {
173                                 if (String.Compare (enabless, "readonly", true) == 0)
174                                         enableSessionState = PagesEnableSessionState.ReadOnly;
175                                 else if (String.Compare (enabless, "true", true) == 0)
176                                         enableSessionState = PagesEnableSessionState.True;
177                                 else if (String.Compare (enabless, "false", true) == 0)
178                                         enableSessionState = PagesEnableSessionState.False;
179                                 else
180                                         ThrowParseException ("Invalid value for enableSessionState: " + enabless);
181                         }
182
183                         string cp = GetString (atts, "CodePage", null);
184                         if (cp != null) {
185                                 if (responseEncoding != null)
186                                         ThrowParseException ("CodePage and ResponseEncoding are " +
187                                                 "mutually exclusive.");
188
189                                 int codepage = 0;
190                                 try {
191                                         codepage = (int) UInt32.Parse (cp);
192                                 } catch {
193                                         ThrowParseException ("Invalid value for CodePage: " + cp);
194                                 }
195
196                                 try {
197                                         Encoding.GetEncoding (codepage);
198                                 } catch {
199                                         ThrowParseException ("Unsupported codepage: " + cp);
200                                 }
201                         }
202                         
203                         responseEncoding = GetString (atts, "ResponseEncoding", null);
204                         if (responseEncoding != null) {
205                                 if (codepage != -1)
206                                         ThrowParseException ("CodePage and ResponseEncoding are " +
207                                                              "mutually exclusive.");
208
209                                 try {
210                                         Encoding.GetEncoding (responseEncoding);
211                                 } catch {
212                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
213                                 }
214                         }
215                         
216                         contentType = GetString (atts, "ContentType", null);
217
218                         string lcidStr = GetString (atts, "LCID", null);
219                         if (lcidStr != null) {
220                                 try {
221                                         lcid = (int) UInt32.Parse (lcidStr);
222                                 } catch {
223                                         ThrowParseException ("Invalid value for LCID: " + lcid);
224                                 }
225
226                                 CultureInfo ci = null;
227                                 try {
228                                         ci = new CultureInfo (lcid);
229                                 } catch {
230                                         ThrowParseException ("Unsupported LCID: " + lcid);
231                                 }
232
233                                 if (ci.IsNeutralCulture) {
234                                         string suggestedCulture = SuggestCulture (ci.Name);
235                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
236                                         if (suggestedCulture != null) {
237                                                 ThrowParseException (fmt + " Please try one of these: " +
238                                                                      suggestedCulture);
239                                         } else {
240                                                 ThrowParseException (fmt);
241                                         }
242                                 }
243                         }
244
245                         culture = GetString (atts, "Culture", null);
246                         if (culture != null) {
247                                 if (lcidStr != null) 
248                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
249                                 
250                                 CultureInfo ci = null;
251                                 try {
252 #if NET_2_0
253                                         if (!culture.StartsWith ("auto"))
254 #endif
255                                                 ci = new CultureInfo (culture);
256                                 } catch {
257                                         ThrowParseException ("Unsupported Culture: " + culture);
258                                 }
259
260                                 if (ci != null && ci.IsNeutralCulture) {
261                                         string suggestedCulture = SuggestCulture (culture);
262                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
263                                         if (suggestedCulture != null)
264                                                 ThrowParseException (fmt +
265                                                                 " Please try one of these: " + suggestedCulture);
266                                         else
267                                                 ThrowParseException (fmt);
268                                 }
269                         }
270
271                         uiculture = GetString (atts, "UICulture", null);
272                         if (uiculture != null) {
273                                 CultureInfo ci = null;
274                                 try {
275 #if NET_2_0
276                                         if (!uiculture.StartsWith ("auto"))
277 #endif
278                                                 ci = new CultureInfo (uiculture);
279                                 } catch {
280                                         ThrowParseException ("Unsupported Culture: " + uiculture);
281                                 }
282
283                                 if (ci != null && ci.IsNeutralCulture) {
284                                         string suggestedCulture = SuggestCulture (uiculture);
285                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
286                                         if (suggestedCulture != null)
287                                                 ThrowParseException (fmt +
288                                                                 " Please try one of these: " + suggestedCulture);
289                                         else
290                                                 ThrowParseException (fmt);
291                                 }
292                         }
293
294                         string tracestr = GetString (atts, "Trace", null);
295                         if (tracestr != null) {
296                                 haveTrace = true;
297                                 atts ["Trace"] = tracestr;
298                                 trace = GetBool (atts, "Trace", false);
299                         }
300
301                         string tracemodes = GetString (atts, "TraceMode", null);
302                         if (tracemodes != null) {
303                                 bool valid = true;
304                                 try {
305                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
306                                 } catch {
307                                         valid = false;
308                                 }
309
310                                 if (!valid || tracemode == TraceMode.Default)
311                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
312                                                         "one of the following values: SortByTime, SortByCategory.");
313                         }
314
315                         errorPage = GetString (atts, "ErrorPage", null);
316                         validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
317                         clientTarget = GetString (atts, "ClientTarget", null);
318                         if (clientTarget != null) {
319                                 clientTarget = clientTarget.Trim ();
320 #if NET_2_0
321                                 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetWebApplicationSection ("system.web/clientTarget");
322                                 ClientTarget ct = null;
323                                 
324                                 if ((ct = sec.ClientTargets [clientTarget]) == null)
325                                         clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
326                                 
327                                 if (ct == null && (ct = sec.ClientTargets [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 = ct.UserAgent;
334 #else
335                                 NameValueCollection coll;
336                                 coll = (NameValueCollection) HttpContext.GetAppConfig ("system.web/clientTarget");
337                                 object ct = null;
338                                 
339                                 if (coll != null) {
340                                         ct = coll [clientTarget];
341                                         if (ct == null)
342                                                 ct = coll [clientTarget.ToLower ()];
343                                 }
344                                 
345                                 if (ct == null) {
346                                         ThrowParseException (String.Format (
347                                                         "ClientTarget '{0}' is an invalid alias. See the " +
348                                                         "documentation for <clientTarget> config. section.",
349                                                         clientTarget));
350                                 }
351                                 clientTarget = (string) ct;
352 #endif
353                         }
354
355                         notBuffer = !GetBool (atts, "Buffer", true);
356                         
357 #if NET_2_0
358                         async = GetBool (atts, "Async", false);
359                         string asyncTimeoutVal = GetString (atts, "AsyncTimeout", null);
360                         if (asyncTimeoutVal != null) {
361                                 try {
362                                         asyncTimeout = Int32.Parse (asyncTimeoutVal);
363                                 } catch (Exception) {
364                                         ThrowParseException ("AsyncTimeout must be an integer value");
365                                 }
366                         }
367                         
368                         masterPage = GetString (atts, "MasterPageFile", masterPage);
369                         
370                         if (!String.IsNullOrEmpty (masterPage)) {
371                                 if (!HostingEnvironment.VirtualPathProvider.FileExists (masterPage))
372                                         ThrowParseFileNotFound (masterPage);
373                                 AddDependency (masterPage);
374                         }
375                         
376                         title = GetString(atts, "Title", null);
377
378                         theme = GetString (atts, "Theme", theme);
379                         styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
380                         enable_event_validation = GetBool (atts, "EnableEventValidation", enable_event_validation);
381                         maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
382 #endif
383                         // Ignored by now
384                         GetString (atts, "EnableViewStateMac", null);
385                         GetString (atts, "SmartNavigation", null);
386
387                         base.ProcessMainAttributes (atts);
388                 }
389                 
390 #if NET_2_0
391                 internal override void AddDirective (string directive, Hashtable atts)
392                 {                       
393                         bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
394                         bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
395                                                                                          StringComparison.OrdinalIgnoreCase) == 0;
396                         
397                         string typeName = null;
398                         string virtualPath = null;
399                         Type type = null;
400                         
401                         if (isMasterType || isPreviousPageType) {
402                                 PageParserFilter pfilter = PageParserFilter;
403                                 if (pfilter != null)
404                                         pfilter.PreprocessDirective (directive.ToLower (CultureInfo.InvariantCulture), atts);
405                                 
406                                 typeName = GetString (atts, "TypeName", null);
407                                 virtualPath = GetString (atts, "VirtualPath", null);
408
409                                 if (typeName != null && virtualPath != null)
410                                         ThrowParseException (
411                                                 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
412                                 if (typeName != null) {
413                                         type = LoadType (typeName);
414                                         if (type == null)
415                                                 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
416                                         if (isMasterType)
417                                                 masterType = type;
418                                         else
419                                                 previousPageType = type;
420                                 } else if (!String.IsNullOrEmpty (virtualPath)) {
421                                         if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
422                                                 ThrowParseFileNotFound (virtualPath);
423
424                                         AddDependency (virtualPath);
425                                         if (isMasterType)
426                                                 masterVirtualPath = virtualPath;
427                                         else
428                                                 previousPageVirtualPath = virtualPath;
429                                 } else
430                                         ThrowParseException (String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
431
432                                 if (type != null)
433                                         AddAssembly (type.Assembly, true);
434                         } else
435                                 base.AddDirective (directive, atts);
436                 }
437 #endif
438                 
439                 static string SuggestCulture (string culture)
440                 {
441                         string retval = null;
442                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
443                                 if (ci.Name.StartsWith (culture))
444                                         retval += ci.Name + " ";
445                         }
446                         return retval;
447                 }
448
449                 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
450                 {
451 #if NET_2_0
452                         return BuildManager.GetCompiledType (virtualPath);
453 #else
454                         PageParser pp = new PageParser (virtualPath, inputFile, context);
455                         return pp.CompileIntoType ();
456 #endif
457                 }
458                 
459                 protected override Type CompileIntoType ()
460                 {
461                         AspGenerator generator = new AspGenerator (this);
462                         return generator.GetCompiledType ();
463                 }
464
465                 internal bool EnableSessionState {
466                         get {
467                                 return enableSessionState == PagesEnableSessionState.True ||
468                                         ReadOnlySessionState;
469                         }
470                 }
471
472                 internal bool EnableViewStateMac {
473                         get { return enableViewStateMac; }
474                 }
475                 
476                 internal bool SmartNavigation {
477                         get { return smartNavigation; }
478                 }
479                 
480                 internal bool ReadOnlySessionState {
481                         get {
482                                 return enableSessionState == PagesEnableSessionState.ReadOnly;
483                         }
484                 }
485
486                 internal bool HaveTrace {
487                         get { return haveTrace; }
488                 }
489
490                 internal bool Trace {
491                         get { return trace; }
492                 }
493
494                 internal TraceMode TraceMode {
495                         get { return tracemode; }
496                 }               
497
498 #if NET_2_0
499                 internal override string DefaultBaseTypeName {
500                         get { return PagesConfig.PageBaseType; }
501                 }
502 #else
503                 internal override string DefaultBaseTypeName {
504                         get { return "System.Web.UI.Page"; }
505                 }
506 #endif
507                 
508                 internal override string DefaultDirectiveName {
509                         get { return "page"; }
510                 }
511
512                 internal string ResponseEncoding {
513                         get { return responseEncoding; }
514                 }
515
516                 internal string ContentType {
517                         get { return contentType; }
518                 }
519
520                 internal int CodePage {
521                         get { return codepage; }
522                 }
523
524                 internal string Culture {
525                         get { return culture; }
526                 }
527
528                 internal string UICulture {
529                         get { return uiculture; }
530                 }
531
532                 internal int LCID {
533                         get { return lcid; }
534                 }
535
536                 internal string ErrorPage {
537                         get { return errorPage; }
538                 }
539
540                 internal bool ValidateRequest {
541                         get { return validateRequest; }
542                 }
543
544                 internal string ClientTarget {
545                         get { return clientTarget; }
546                 }
547
548                 internal bool NotBuffer {
549                         get { return notBuffer; }
550                 }
551
552 #if NET_2_0
553                 internal bool Async {
554                         get { return async; }
555                 }
556
557                 internal int AsyncTimeout {
558                         get { return asyncTimeout; }
559                 }
560                 
561                 internal string Theme {
562                         get { return theme; }
563                 }
564
565                 internal string StyleSheetTheme {
566                         get { return styleSheetTheme; }
567                 }
568
569                 internal string MasterPageFile {
570                         get { return masterPage; }
571                 }
572                 
573                 internal Type MasterType {
574                         get {
575                                 if (masterType == null && !String.IsNullOrEmpty (masterVirtualPath))
576                                         masterType = BuildManager.GetCompiledType (masterVirtualPath);
577                                 
578                                 return masterType;
579                         }
580                 }
581
582                 internal string Title {
583                         get { return title; }
584                 }
585
586                 internal bool EnableEventValidation {
587                         get { return enable_event_validation; }
588                 }
589
590                 internal bool MaintainScrollPositionOnPostBack {
591                         get { return maintainScrollPositionOnPostBack; }
592                 }
593
594                 internal int MaxPageStateFieldLength {
595                         get { return maxPageStateFieldLength; }
596                 }
597
598                 internal Type PreviousPageType {
599                         get {
600                                 if (previousPageType == null && !String.IsNullOrEmpty (previousPageVirtualPath)) {
601                                         string mappedPath = MapPath (previousPageVirtualPath);
602                                         previousPageType = GetCompiledPageType (previousPageVirtualPath, mappedPath, HttpContext.Current);
603                                 }
604                                 
605                                 return previousPageType;
606                         }
607                 }
608 #endif
609         }
610 }
611