importing messaging-2008 branch to trunk, going on.
[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
63 #if NET_2_0
64                 bool async;
65                 int asyncTimeout = -1;
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                 Type previousPageType;
76 #endif
77
78                 public PageParser ()
79                 {
80                         LoadConfigDefaults ();
81                 }
82                 
83                 internal PageParser (string virtualPath, string inputFile, HttpContext context)
84                 {
85                         Context = context;
86                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
87                         InputFile = inputFile;
88                         SetBaseType (null);
89                         AddApplicationAssembly ();
90                         LoadConfigDefaults ();
91 #if NET_2_0
92                         this.VirtualPath = new VirtualPath (virtualPath);
93 #endif
94                 }
95
96 #if NET_2_0
97                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
98                         : this (virtualPath, null, reader, context)
99                 {
100                 }
101                 
102                 internal PageParser (string virtualPath, string inputFile, TextReader reader, HttpContext context)
103                 {
104                         Context = context;
105                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
106                         Reader = reader;
107                         if (String.IsNullOrEmpty (inputFile)) {
108                                 HttpRequest req = context != null ? context.Request : null;
109                                 if (req != null)
110                                         InputFile = req.MapPath (virtualPath);
111                         } else
112                                 InputFile = inputFile;
113                         SetBaseType (null);
114                         AddApplicationAssembly ();
115                         LoadConfigDefaults ();
116 #if NET_2_0
117                         this.VirtualPath = new VirtualPath (virtualPath);
118 #endif
119                 }
120 #endif
121
122                 internal override void LoadConfigDefaults ()
123                 {
124                         base.LoadConfigDefaults ();
125 #if NET_2_0
126                         PagesSection ps = PagesConfig;
127 #else
128                         PagesConfiguration ps = PagesConfig;
129 #endif                  
130
131                         notBuffer = !ps.Buffer;
132                         enableSessionState = ps.EnableSessionState;
133                         enableViewStateMac = ps.EnableViewStateMac;
134                         smartNavigation = ps.SmartNavigation;
135                         validateRequest = ps.ValidateRequest;
136 #if NET_2_0
137                         masterPage = ps.MasterPageFile;
138                         if (masterPage.Length == 0)
139                                 masterPage = null;
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 #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.GetSection ("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) Context.GetConfig ("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                         // Make sure the page exists
371                         if (!String.IsNullOrEmpty (masterPage)) {
372                                 BuildManager.GetCompiledType (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                                 typeName = GetString (atts, "TypeName", null);
403                                 virtualPath = GetString (atts, "VirtualPath", null);
404
405                                 if (typeName != null && virtualPath != null)
406                                         ThrowParseException (
407                                                 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
408                                 if (typeName != null) {
409                                         type = LoadType (typeName);
410                                         if (type == null)
411                                                 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
412                                 } else if (virtualPath != null) {
413                                         string mappedPath = MapPath (virtualPath);
414                                         if (isMasterType)
415                                                 type = masterType = BuildManager.GetCompiledType (virtualPath);
416                                         else
417                                                 type = previousPageType = GetCompiledPageType (virtualPath, mappedPath,
418                                                                                                HttpContext.Current);
419                                 } else
420                                         ThrowParseException (
421                                                 String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
422
423                                 AddAssembly (type.Assembly, true);
424                         } else
425                                 base.AddDirective (directive, atts);
426                 }
427 #endif
428                 
429                 static string SuggestCulture (string culture)
430                 {
431                         string retval = null;
432                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
433                                 if (ci.Name.StartsWith (culture))
434                                         retval += ci.Name + " ";
435                         }
436                         return retval;
437                 }
438
439                 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
440                 {
441 #if NET_2_0
442                         return BuildManager.GetCompiledType (virtualPath);
443 #else
444                         PageParser pp = new PageParser (virtualPath, inputFile, context);
445                         return pp.CompileIntoType ();
446 #endif
447                 }
448                 
449                 protected override Type CompileIntoType ()
450                 {
451                         AspGenerator generator = new AspGenerator (this);
452                         return generator.GetCompiledType ();
453                 }
454
455                 internal bool EnableSessionState {
456                         get {
457                                 return enableSessionState == PagesEnableSessionState.True ||
458                                         ReadOnlySessionState;
459                         }
460                 }
461
462                 internal bool EnableViewStateMac {
463                         get { return enableViewStateMac; }
464                 }
465                 
466                 internal bool SmartNavigation {
467                         get { return smartNavigation; }
468                 }
469                 
470                 internal bool ReadOnlySessionState {
471                         get {
472                                 return enableSessionState == PagesEnableSessionState.ReadOnly;
473                         }
474                 }
475
476                 internal bool HaveTrace {
477                         get { return haveTrace; }
478                 }
479
480                 internal bool Trace {
481                         get { return trace; }
482                 }
483
484                 internal TraceMode TraceMode {
485                         get { return tracemode; }
486                 }               
487
488 #if NET_2_0
489                 internal override string DefaultBaseTypeName {
490                         get { return PagesConfig.PageBaseType; }
491                 }
492 #else
493                 internal override string DefaultBaseTypeName {
494                         get { return "System.Web.UI.Page"; }
495                 }
496 #endif
497                 
498                 internal override string DefaultDirectiveName {
499                         get { return "page"; }
500                 }
501
502                 internal string ResponseEncoding {
503                         get { return responseEncoding; }
504                 }
505
506                 internal string ContentType {
507                         get { return contentType; }
508                 }
509
510                 internal int CodePage {
511                         get { return codepage; }
512                 }
513
514                 internal string Culture {
515                         get { return culture; }
516                 }
517
518                 internal string UICulture {
519                         get { return uiculture; }
520                 }
521
522                 internal int LCID {
523                         get { return lcid; }
524                 }
525
526                 internal string ErrorPage {
527                         get { return errorPage; }
528                 }
529
530                 internal bool ValidateRequest {
531                         get { return validateRequest; }
532                 }
533
534                 internal string ClientTarget {
535                         get { return clientTarget; }
536                 }
537
538                 internal bool NotBuffer {
539                         get { return notBuffer; }
540                 }
541
542 #if NET_2_0
543                 internal bool Async {
544                         get { return async; }
545                 }
546
547                 internal int AsyncTimeout {
548                         get { return asyncTimeout; }
549                 }
550                 
551                 internal string Theme {
552                         get { return theme; }
553                 }
554
555                 internal string StyleSheetTheme {
556                         get { return styleSheetTheme; }
557                 }
558
559                 internal string MasterPageFile {
560                         get { return masterPage; }
561                 }
562                 
563                 internal Type MasterType {
564                         get { return masterType; }
565                 }
566
567                 internal string Title {
568                         get { return title; }
569                 }
570
571                 internal bool EnableEventValidation {
572                         get { return enable_event_validation; }
573                 }
574
575                 internal bool MaintainScrollPositionOnPostBack {
576                         get { return maintainScrollPositionOnPostBack; }
577                 }
578
579                 internal int MaxPageStateFieldLength {
580                         get { return maxPageStateFieldLength; }
581                 }
582
583                 internal string PageParserFilterType {
584                         get { return pageParserFilter; }
585                 }
586
587                 internal Type PreviousPageType {
588                         get { return previousPageType; }
589                 }
590 #endif
591         }
592 }
593