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