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