calculate AppRelativeTemplateSourceDirectory from HttpContext when control is not...
[mono.git] / mcs / class / System.Web / System.Web.UI / PageParser.cs
1 //      
2 // System.Web.UI.PageParser
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Util;
38 using System.IO;
39
40 namespace System.Web.UI
41 {
42         // CAS - no InheritanceDemand here as the class is sealed
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         public sealed class PageParser : TemplateControlParser
45         {
46                 bool enableSessionState = true;
47                 bool enableViewStateMac = true;
48                 bool smartNavigation = false;
49                 bool haveTrace;
50                 bool trace;
51                 bool notBuffer;
52                 TraceMode tracemode;
53                 bool readonlySessionState;
54                 string responseEncoding;
55                 string contentType;
56                 int codepage = -1;
57                 int lcid = -1;
58                 string culture;
59                 string uiculture;
60                 string errorPage;
61                 bool validateRequest;
62                 string clientTarget;
63                 Type baseType = typeof (Page);
64
65 #if NET_2_0
66                 string masterPage;
67                 Type masterType;
68                 string title;
69                 string theme;
70                 string styleSheetTheme;
71                 bool enable_event_validation;
72                 bool maintainScrollPositionOnPostBack;
73                 int maxPageStateFieldLength = -1;
74                 string pageParserFilter = String.Empty;
75                 Type previousPageType;
76 #endif
77
78                 public PageParser ()
79                 {
80                         LoadPagesConfigDefaults ();
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 (PagesConfig.PageBaseType);
89                         AddApplicationAssembly ();
90                         LoadPagesConfigDefaults ();
91                 }
92
93 #if NET_2_0
94                 internal PageParser (string virtualPath, TextReader reader, HttpContext context)
95                 {
96                         Context = context;
97                         BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
98                         Reader = reader;
99                         SetBaseType (PagesConfig.PageBaseType);
100                         AddApplicationAssembly ();
101                         LoadPagesConfigDefaults ();
102                 }
103 #endif
104
105                 internal override void LoadPagesConfigDefaults ()
106                 {
107                         base.LoadPagesConfigDefaults ();
108 #if NET_2_0
109                         PagesSection ps = PagesConfig;
110 #else
111                         PagesConfiguration ps = PagesConfig;
112 #endif
113
114                         notBuffer = !ps.Buffer;
115 #if NET_2_0
116                         switch (ps.EnableSessionState) {
117                                 case PagesEnableSessionState.True:
118                                 case PagesEnableSessionState.ReadOnly:
119                                         enableSessionState = true;
120                                         break;
121
122                                 default:
123                                         enableSessionState = false;
124                                         break;
125                         }
126 #else
127                         if (String.Compare (ps.EnableSessionState, "true", true, CultureInfo.InvariantCulture) == 0)
128                                 enableSessionState = true;
129                         else
130                                 enableSessionState = false;
131 #endif
132                         
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                         
141                         enable_event_validation = ps.EnableEventValidation;
142                         maxPageStateFieldLength = ps.MaxPageStateFieldLength;
143                         pageParserFilter = ps.PageParserFilterType;
144                         theme = ps.Theme;
145                         if (theme.Length == 0)
146                                 theme = null;
147                         styleSheetTheme = ps.StyleSheetTheme;
148                         if (styleSheetTheme.Length == 0)
149                                 styleSheetTheme = null;
150                         maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
151 #endif
152                 }
153                 
154                 public static IHttpHandler GetCompiledPageInstance (string virtualPath,
155                                                                     string inputFile, 
156                                                                     HttpContext context)
157                 {
158                         PageParser pp = new PageParser (virtualPath, inputFile, context);
159                         IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
160                         return h;
161                 }
162                 
163                 internal override void ProcessMainAttributes (Hashtable atts)
164                 {
165                         // note: the 'enableSessionState' configuration property is
166                         // processed in a case-sensitive manner while the page-level
167                         // attribute is processed case-insensitive
168                         string enabless = GetString (atts, "EnableSessionState", enableSessionState.ToString ());
169                         if (enabless != null) {
170                                 readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
171                                 if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
172                                         enableSessionState = true;
173                                 } else if (String.Compare (enabless, "false", true) == 0) {
174                                         enableSessionState = false;
175                                 } else {
176                                         ThrowParseException ("Invalid value for enableSessionState: " + enabless);
177                                 }
178                         }
179
180                         string cp = GetString (atts, "CodePage", null);
181                         if (cp != null) {
182                                 if (responseEncoding != null)
183                                         ThrowParseException ("CodePage and ResponseEncoding are " +
184                                                              "mutually exclusive.");
185
186                                 int codepage = 0;
187                                 try {
188                                         codepage = (int) UInt32.Parse (cp);
189                                 } catch {
190                                         ThrowParseException ("Invalid value for CodePage: " + cp);
191                                 }
192
193                                 try {
194                                         Encoding.GetEncoding (codepage);
195                                 } catch {
196                                         ThrowParseException ("Unsupported codepage: " + cp);
197                                 }
198                         }
199                         
200                         responseEncoding = GetString (atts, "ResponseEncoding", null);
201                         if (responseEncoding != null) {
202                                 if (codepage != -1)
203                                         ThrowParseException ("CodePage and ResponseEncoding are " +
204                                                              "mutually exclusive.");
205
206                                 try {
207                                         Encoding.GetEncoding (responseEncoding);
208                                 } catch {
209                                         ThrowParseException ("Unsupported encoding: " + responseEncoding);
210                                 }
211                         }
212                         
213                         contentType = GetString (atts, "ContentType", null);
214
215                         string lcidStr = GetString (atts, "LCID", null);
216                         if (lcidStr != null) {
217                                 try {
218                                         lcid = (int) UInt32.Parse (lcidStr);
219                                 } catch {
220                                         ThrowParseException ("Invalid value for LCID: " + lcid);
221                                 }
222
223                                 CultureInfo ci = null;
224                                 try {
225                                         ci = new CultureInfo (lcid);
226                                 } catch {
227                                         ThrowParseException ("Unsupported LCID: " + lcid);
228                                 }
229
230                                 if (ci.IsNeutralCulture) {
231                                         string suggestedCulture = SuggestCulture (ci.Name);
232                                         string fmt = "LCID attribute must be set to a non-neutral Culture.";
233                                         if (suggestedCulture != null) {
234                                                 ThrowParseException (fmt + " Please try one of these: " +
235                                                                      suggestedCulture);
236                                         } else {
237                                                 ThrowParseException (fmt);
238                                         }
239                                 }
240                         }
241
242                         culture = GetString (atts, "Culture", null);
243                         if (culture != null) {
244                                 if (lcidStr != null) 
245                                         ThrowParseException ("Culture and LCID are mutually exclusive.");
246                                 
247                                 CultureInfo ci = null;
248                                 try {
249 #if NET_2_0
250                                         if (!culture.StartsWith ("auto"))
251 #endif
252                                                 ci = new CultureInfo (culture);
253                                 } catch {
254                                         ThrowParseException ("Unsupported Culture: " + culture);
255                                 }
256
257                                 if (ci != null && ci.IsNeutralCulture) {
258                                         string suggestedCulture = SuggestCulture (culture);
259                                         string fmt = "Culture attribute must be set to a non-neutral Culture.";
260                                         if (suggestedCulture != null)
261                                                 ThrowParseException (fmt +
262                                                                 " Please try one of these: " + suggestedCulture);
263                                         else
264                                                 ThrowParseException (fmt);
265                                 }
266                         }
267
268                         uiculture = GetString (atts, "UICulture", null);
269                         if (uiculture != null) {
270                                 CultureInfo ci = null;
271                                 try {
272 #if NET_2_0
273                                         if (!uiculture.StartsWith ("auto"))
274 #endif
275                                                 ci = new CultureInfo (uiculture);
276                                 } catch {
277                                         ThrowParseException ("Unsupported Culture: " + uiculture);
278                                 }
279
280                                 if (ci != null && ci.IsNeutralCulture) {
281                                         string suggestedCulture = SuggestCulture (uiculture);
282                                         string fmt = "UICulture attribute must be set to a non-neutral Culture.";
283                                         if (suggestedCulture != null)
284                                                 ThrowParseException (fmt +
285                                                                 " Please try one of these: " + suggestedCulture);
286                                         else
287                                                 ThrowParseException (fmt);
288                                 }
289                         }
290
291                         string tracestr = GetString (atts, "Trace", null);
292                         if (tracestr != null) {
293                                 haveTrace = true;
294                                 atts ["Trace"] = tracestr;
295                                 trace = GetBool (atts, "Trace", false);
296                         }
297
298                         string tracemodes = GetString (atts, "TraceMode", null);
299                         if (tracemodes != null) {
300                                 bool valid = true;
301                                 try {
302                                         tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
303                                 } catch {
304                                         valid = false;
305                                 }
306
307                                 if (!valid || tracemode == TraceMode.Default)
308                                         ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
309                                                         "one of the following values: SortByTime, SortByCategory.");
310                         }
311
312                         errorPage = GetString (atts, "ErrorPage", null);
313                         validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
314                         clientTarget = GetString (atts, "ClientTarget", null);
315                         if (clientTarget != null) {
316                                 clientTarget = clientTarget.Trim ();
317 #if NET_2_0
318                                 ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
319                                 ClientTarget ct = null;
320                                 
321                                 if ((ct = sec.ClientTargets [clientTarget]) == null)
322                                         clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
323                                 
324                                 if (ct == null && (ct = sec.ClientTargets [clientTarget]) == 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 = ct.UserAgent;
331 #else
332                                 NameValueCollection coll;
333                                 coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
334                                 object ct = null;
335                                 
336                                 if (coll != null) {
337                                         ct = coll [clientTarget];
338                                         if (ct == null)
339                                                 ct = coll [clientTarget.ToLower ()];
340                                 }
341                                 
342                                 if (ct == null) {
343                                         ThrowParseException (String.Format (
344                                                         "ClientTarget '{0}' is an invalid alias. See the " +
345                                                         "documentation for <clientTarget> config. section.",
346                                                         clientTarget));
347                                 }
348                                 clientTarget = (string) ct;
349 #endif
350                         }
351
352                         notBuffer = !GetBool (atts, "Buffer", true);
353                         
354 #if NET_2_0
355                         masterPage = GetString (atts, "MasterPageFile", masterPage);
356                         
357                         // Make sure the page exists
358                         if (!String.IsNullOrEmpty (masterPage))
359                                 MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
360
361                         title = GetString(atts, "Title", null);
362
363                         theme = GetString (atts, "Theme", theme);
364                         styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
365                         enable_event_validation = GetBool (atts, "EnableEventValidation", true);
366                         maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
367 #endif
368                         // Ignored by now
369                         GetString (atts, "EnableViewStateMac", null);
370                         GetString (atts, "SmartNavigation", null);
371
372                         base.ProcessMainAttributes (atts);
373                 }
374                 
375 #if NET_2_0
376                 internal override void AddDirective (string directive, Hashtable atts)
377                 {                       
378                         bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
379                         bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
380                                                                                          StringComparison.OrdinalIgnoreCase) == 0;
381                         
382                         string typeName = null;
383                         string virtualPath = null;
384                         Type type = null;
385                         
386                         if (isMasterType || isPreviousPageType) {
387                                 typeName = GetString (atts, "TypeName", null);
388                                 virtualPath = GetString (atts, "VirtualPath", null);
389
390                                 if (typeName != null && virtualPath != null)
391                                         ThrowParseException (
392                                                 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
393                                 if (typeName != null) {
394                                         type = LoadType (typeName);
395                                         if (type == null)
396                                                 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
397                                 } else if (virtualPath != null) {
398                                         string mappedPath = MapPath (virtualPath);
399                                         if (isMasterType)
400                                                 type = masterType = MasterPageParser.GetCompiledMasterType (virtualPath,
401                                                                                                      mappedPath,
402                                                                                                      HttpContext.Current);
403                                         else
404                                                 type = previousPageType = GetCompiledPageType (virtualPath, mappedPath,
405                                                                                                HttpContext.Current);
406                                 } else
407                                         ThrowParseException (
408                                                 String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
409
410                                 AddAssembly (type.Assembly, true);
411                         } else
412                                 base.AddDirective (directive, atts);
413                 }
414 #endif
415                 
416                 static string SuggestCulture (string culture)
417                 {
418                         string retval = null;
419                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
420                                 if (ci.Name.StartsWith (culture))
421                                         retval += ci.Name + " ";
422                         }
423                         return retval;
424                 }
425
426                 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
427                 {
428                         PageParser pp = new PageParser (virtualPath, inputFile, context);
429                         return pp.CompileIntoType ();
430                 }
431                 
432                 protected override Type CompileIntoType ()
433                 {
434                         AspGenerator generator = new AspGenerator (this);
435                         return generator.GetCompiledType ();
436                 }
437
438                 internal bool EnableSessionState {
439                         get { return enableSessionState; }
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 { return readonlySessionState; }
452                 }
453
454                 internal bool HaveTrace {
455                         get { return haveTrace; }
456                 }
457
458                 internal bool Trace {
459                         get { return trace; }
460                 }
461
462                 internal TraceMode TraceMode {
463                         get { return tracemode; }
464                 }
465                 
466                 internal override Type DefaultBaseType {
467                         get { return baseType; }
468                 }
469
470                 internal override string DefaultBaseTypeName {
471                         get { return "System.Web.UI.Page"; }
472                 }
473
474                 internal override string DefaultDirectiveName {
475                         get { return "page"; }
476                 }
477
478                 internal string ResponseEncoding {
479                         get { return responseEncoding; }
480                 }
481
482                 internal string ContentType {
483                         get { return contentType; }
484                 }
485
486                 internal int CodePage {
487                         get { return codepage; }
488                 }
489
490                 internal string Culture {
491                         get { return culture; }
492                 }
493
494                 internal string UICulture {
495                         get { return uiculture; }
496                 }
497
498                 internal int LCID {
499                         get { return lcid; }
500                 }
501
502                 internal string ErrorPage {
503                         get { return errorPage; }
504                 }
505
506                 internal bool ValidateRequest {
507                         get { return validateRequest; }
508                 }
509
510                 internal string ClientTarget {
511                         get { return clientTarget; }
512                 }
513
514                 internal bool NotBuffer {
515                         get { return notBuffer; }
516                 }
517
518 #if NET_2_0
519                 internal string Theme {
520                         get { return theme; }
521                 }
522
523                 internal string StyleSheetTheme {
524                         get { return styleSheetTheme; }
525                 }
526
527                 internal string MasterPageFile {
528                         get { return masterPage; }
529                 }
530                 
531                 internal Type MasterType {
532                         get { return masterType; }
533                 }
534
535                 internal string Title {
536                         get { return title; }
537                 }
538
539                 internal bool EnableEventValidation {
540                         get { return enable_event_validation; }
541                 }
542
543                 internal bool MaintainScrollPositionOnPostBack {
544                         get { return maintainScrollPositionOnPostBack; }
545                 }
546
547                 internal int MaxPageStateFieldLength {
548                         get { return maxPageStateFieldLength; }
549                 }
550
551                 internal string PageParserFilterType {
552                         get { return pageParserFilter; }
553                 }
554
555                 internal Type PreviousPageType {
556                         get { return previousPageType; }
557                 }
558 #endif
559         }
560 }
561