2 // System.Web.UI.PageParser
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
8 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Globalization;
33 using System.Security.Permissions;
35 using System.Web.Compilation;
36 using System.Web.Configuration;
37 using System.Web.Hosting;
38 using System.Web.Util;
41 namespace System.Web.UI
43 // CAS - no InheritanceDemand here as the class is sealed
44 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 public sealed class PageParser : TemplateControlParser
47 static Type defaultPageBaseType;
48 static Type defaultApplicationBaseType;
49 static Type defaultPageParserFilterType;
50 static Type defaultUserControlBaseType;
51 static bool enableLongStringsAsResources = true;
52 PagesEnableSessionState enableSessionState = PagesEnableSessionState.True;
53 bool enableViewStateMac;
54 bool enableViewStateMacSet;
59 TraceMode tracemode = TraceMode.Default;
61 MainDirectiveAttribute <int> codepage;
62 MainDirectiveAttribute <string> responseEncoding;
63 MainDirectiveAttribute <int> lcid;
64 MainDirectiveAttribute <string> clientTarget;
65 MainDirectiveAttribute <string> masterPage;
66 MainDirectiveAttribute <string> title;
67 MainDirectiveAttribute <string> theme;
68 MainDirectiveAttribute <string> metaDescription;
69 MainDirectiveAttribute <string> metaKeywords;
75 int asyncTimeout = -1;
77 string masterVirtualPath;
78 string styleSheetTheme;
79 bool enable_event_validation;
80 bool maintainScrollPositionOnPostBack;
81 int maxPageStateFieldLength = -1;
82 Type previousPageType;
83 string previousPageVirtualPath;
84 public static bool EnableLongStringsAsResources {
85 get { return enableLongStringsAsResources; }
87 BuildManager.AssertPreStartMethodsRunning ();
88 enableLongStringsAsResources = value;
92 public static Type DefaultPageBaseType {
93 get { return defaultPageBaseType; }
95 BuildManager.AssertPreStartMethodsRunning ();
96 if (value != null && !typeof (Page).IsAssignableFrom (value))
97 throw new ArgumentException (String.Format ("The value assigned to property '{0}' is invalid.", "DefaultPageBaseType"));
99 defaultPageBaseType = value;
103 public static Type DefaultApplicationBaseType {
104 get { return defaultApplicationBaseType; }
106 BuildManager.AssertPreStartMethodsRunning ();
107 if (value != null && !typeof (HttpApplication).IsAssignableFrom (value))
108 throw new ArgumentException (String.Format ("The value assigned to property '{0}' is invalid.", "DefaultApplicationBaseType"));
109 defaultApplicationBaseType = value;
113 public static Type DefaultPageParserFilterType {
114 get { return defaultPageParserFilterType; }
116 BuildManager.AssertPreStartMethodsRunning ();
117 if (value != null && !typeof (PageParserFilter).IsAssignableFrom (value))
118 throw new ArgumentException (String.Format ("The value assigned to property '{0}' is invalid.", "DefaultPageParserFilterType"));
119 defaultPageParserFilterType = value;
123 public static Type DefaultUserControlBaseType {
124 get { return defaultUserControlBaseType; }
126 if (value != null && !typeof (UserControl).IsAssignableFrom (value))
127 throw new ArgumentException (String.Format ("The value assigned to property '{0}' is invalid.", "DefaultUserControlBaseType"));
128 BuildManager.AssertPreStartMethodsRunning ();
129 defaultUserControlBaseType = value;
134 LoadConfigDefaults ();
137 internal PageParser (string virtualPath, string inputFile, HttpContext context)
139 this.VirtualPath = new VirtualPath (virtualPath);
141 BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
142 InputFile = inputFile;
144 AddApplicationAssembly ();
145 LoadConfigDefaults ();
148 internal PageParser (VirtualPath virtualPath, TextReader reader, HttpContext context)
149 : this (virtualPath, null, reader, context)
153 internal PageParser (VirtualPath virtualPath, string inputFile, TextReader reader, HttpContext context)
155 this.VirtualPath = virtualPath;
157 BaseVirtualDir = virtualPath.DirectoryNoNormalize;
159 if (String.IsNullOrEmpty (inputFile))
160 InputFile = virtualPath.PhysicalPath;
162 InputFile = inputFile;
164 AddApplicationAssembly ();
165 LoadConfigDefaults ();
168 internal override void LoadConfigDefaults ()
170 base.LoadConfigDefaults ();
171 PagesSection ps = PagesConfig;
173 notBuffer = !ps.Buffer;
174 enableSessionState = ps.EnableSessionState;
175 enableViewStateMac = ps.EnableViewStateMac;
176 smartNavigation = ps.SmartNavigation;
177 validateRequest = ps.ValidateRequest;
179 string value = ps.MasterPageFile;
180 if (value.Length > 0)
181 masterPage = new MainDirectiveAttribute <string> (value, true);
183 enable_event_validation = ps.EnableEventValidation;
184 maxPageStateFieldLength = ps.MaxPageStateFieldLength;
186 if (value.Length > 0)
187 theme = new MainDirectiveAttribute <string> (value, true);
189 styleSheetTheme = ps.StyleSheetTheme;
190 if (styleSheetTheme.Length == 0)
191 styleSheetTheme = null;
192 maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
195 public static IHttpHandler GetCompiledPageInstance (string virtualPath, string inputFile, HttpContext context)
199 if (!String.IsNullOrEmpty (inputFile))
200 isFake = !inputFile.StartsWith (HttpRuntime.AppDomainAppPath);
202 return BuildManager.CreateInstanceFromVirtualPath (new VirtualPath (virtualPath, inputFile, isFake), typeof (IHttpHandler)) as IHttpHandler;
205 internal override void ProcessMainAttributes (IDictionary atts)
207 // note: the 'enableSessionState' configuration property is
208 // processed in a case-sensitive manner while the page-level
209 // attribute is processed case-insensitive
210 string enabless = GetString (atts, "EnableSessionState", null);
211 if (enabless != null) {
212 if (String.Compare (enabless, "readonly", true, Helpers.InvariantCulture) == 0)
213 enableSessionState = PagesEnableSessionState.ReadOnly;
214 else if (String.Compare (enabless, "true", true, Helpers.InvariantCulture) == 0)
215 enableSessionState = PagesEnableSessionState.True;
216 else if (String.Compare (enabless, "false", true, Helpers.InvariantCulture) == 0)
217 enableSessionState = PagesEnableSessionState.False;
219 ThrowParseException ("Invalid value for enableSessionState: " + enabless);
222 string value = GetString (atts, "CodePage", null);
224 if (responseEncoding != null)
225 ThrowParseException ("CodePage and ResponseEncoding are mutually exclusive.");
227 if (!BaseParser.IsExpression (value)) {
231 cpval = (int) UInt32.Parse (value);
233 ThrowParseException ("Invalid value for CodePage: " + value);
237 Encoding.GetEncoding (cpval);
239 ThrowParseException ("Unsupported codepage: " + value);
241 codepage = new MainDirectiveAttribute <int> (cpval, true);
243 codepage = new MainDirectiveAttribute <int> (value);
246 value = GetString (atts, "ResponseEncoding", null);
248 if (codepage != null)
249 ThrowParseException ("CodePage and ResponseEncoding are mutually exclusive.");
251 if (!BaseParser.IsExpression (value)) {
253 Encoding.GetEncoding (value);
255 ThrowParseException ("Unsupported encoding: " + value);
257 responseEncoding = new MainDirectiveAttribute <string> (value, true);
259 responseEncoding = new MainDirectiveAttribute <string> (value);
262 contentType = GetString (atts, "ContentType", null);
264 value = GetString (atts, "LCID", null);
266 if (!BaseParser.IsExpression (value)) {
269 parsedLcid = (int) UInt32.Parse (value);
271 ThrowParseException ("Invalid value for LCID: " + value);
274 CultureInfo ci = null;
276 ci = new CultureInfo (parsedLcid);
278 ThrowParseException ("Unsupported LCID: " + value);
281 if (ci.IsNeutralCulture) {
282 string suggestedCulture = SuggestCulture (ci.Name);
283 string fmt = "LCID attribute must be set to a non-neutral Culture.";
284 if (suggestedCulture != null) {
285 ThrowParseException (fmt + " Please try one of these: " +
288 ThrowParseException (fmt);
291 lcid = new MainDirectiveAttribute <int> (parsedLcid, true);
293 lcid = new MainDirectiveAttribute <int> (value);
296 culture = GetString (atts, "Culture", null);
297 if (culture != null) {
299 ThrowParseException ("Culture and LCID are mutually exclusive.");
301 CultureInfo ci = null;
303 if (!culture.StartsWith ("auto"))
304 ci = new CultureInfo (culture);
306 ThrowParseException ("Unsupported Culture: " + culture);
309 if (ci != null && ci.IsNeutralCulture) {
310 string suggestedCulture = SuggestCulture (culture);
311 string fmt = "Culture attribute must be set to a non-neutral Culture.";
312 if (suggestedCulture != null)
313 ThrowParseException (fmt +
314 " Please try one of these: " + suggestedCulture);
316 ThrowParseException (fmt);
320 uiculture = GetString (atts, "UICulture", null);
321 if (uiculture != null) {
322 CultureInfo ci = null;
324 if (!uiculture.StartsWith ("auto"))
325 ci = new CultureInfo (uiculture);
327 ThrowParseException ("Unsupported Culture: " + uiculture);
330 if (ci != null && ci.IsNeutralCulture) {
331 string suggestedCulture = SuggestCulture (uiculture);
332 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
333 if (suggestedCulture != null)
334 ThrowParseException (fmt +
335 " Please try one of these: " + suggestedCulture);
337 ThrowParseException (fmt);
341 string tracestr = GetString (atts, "Trace", null);
342 if (tracestr != null) {
344 atts ["Trace"] = tracestr;
345 trace = GetBool (atts, "Trace", false);
348 string tracemodes = GetString (atts, "TraceMode", null);
349 if (tracemodes != null) {
352 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
357 if (!valid || tracemode == TraceMode.Default)
358 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
359 "one of the following values: SortByTime, SortByCategory.");
362 errorPage = GetString (atts, "ErrorPage", null);
363 validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
364 value = GetString (atts, "ClientTarget", null);
366 if (!BaseParser.IsExpression (value)) {
367 value = value.Trim ();
369 ClientTargetSection sec = GetConfigSection <ClientTargetSection> ("system.web/clientTarget");
370 ClientTarget ct = null;
372 if ((ct = sec.ClientTargets [value]) == null)
373 value = value.ToLowerInvariant ();
375 if (ct == null && (ct = sec.ClientTargets [value]) == null) {
376 ThrowParseException (String.Format (
377 "ClientTarget '{0}' is an invalid alias. See the " +
378 "documentation for <clientTarget> config. section.",
381 value = ct.UserAgent;
382 clientTarget = new MainDirectiveAttribute <string> (value, true);
384 clientTarget = new MainDirectiveAttribute <string> (value);
387 notBuffer = !GetBool (atts, "Buffer", true);
388 async = GetBool (atts, "Async", false);
389 string asyncTimeoutVal = GetString (atts, "AsyncTimeout", null);
390 if (asyncTimeoutVal != null) {
392 asyncTimeout = Int32.Parse (asyncTimeoutVal);
393 } catch (Exception) {
394 ThrowParseException ("AsyncTimeout must be an integer value");
398 value = GetString (atts, "MasterPageFile", masterPage != null ? masterPage.Value : null);
399 if (!String.IsNullOrEmpty (value)) {
400 if (!BaseParser.IsExpression (value)) {
401 value = System.Web.VirtualPathUtility.Combine(BaseVirtualDir, value);
402 var vpp = HostingEnvironment.VirtualPathProvider;
403 if (!vpp.FileExists (value))
404 ThrowParseFileNotFound (value);
405 value = vpp.CombineVirtualPaths (VirtualPath.Absolute, VirtualPathUtility.ToAbsolute (value));
406 AddDependency (value, false);
407 masterPage = new MainDirectiveAttribute <string> (value, true);
409 masterPage = new MainDirectiveAttribute <string> (value);
412 value = GetString(atts, "Title", null);
414 if (!BaseParser.IsExpression (value))
415 title = new MainDirectiveAttribute <string> (value, true);
417 title = new MainDirectiveAttribute <string> (value);
420 value = GetString (atts, "Theme", theme != null ? theme.Value : null);
422 if (!BaseParser.IsExpression (value))
423 theme = new MainDirectiveAttribute <string> (value, true);
425 theme = new MainDirectiveAttribute <string> (value);
428 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
429 enable_event_validation = GetBool (atts, "EnableEventValidation", enable_event_validation);
430 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
432 if (atts.Contains ("EnableViewStateMac")) {
433 enableViewStateMac = GetBool (atts, "EnableViewStateMac", enableViewStateMac);
434 enableViewStateMacSet = true;
436 value = GetString (atts, "MetaDescription", null);
438 if (!BaseParser.IsExpression (value))
439 metaDescription = new MainDirectiveAttribute <string> (value, true);
441 metaDescription = new MainDirectiveAttribute <string> (value);
444 value = GetString (atts, "MetaKeywords", null);
446 if (!BaseParser.IsExpression (value))
447 metaKeywords = new MainDirectiveAttribute <string> (value, true);
449 metaKeywords = new MainDirectiveAttribute <string> (value);
452 GetString (atts, "SmartNavigation", null);
454 base.ProcessMainAttributes (atts);
457 internal override void AddDirective (string directive, IDictionary atts)
459 bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
460 bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
461 StringComparison.OrdinalIgnoreCase) == 0;
463 string typeName = null;
464 string virtualPath = null;
467 if (isMasterType || isPreviousPageType) {
468 PageParserFilter pfilter = PageParserFilter;
470 pfilter.PreprocessDirective (directive.ToLowerInvariant (), atts);
472 typeName = GetString (atts, "TypeName", null);
473 virtualPath = GetString (atts, "VirtualPath", null);
475 if (typeName != null && virtualPath != null)
476 ThrowParseException (
477 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
478 if (typeName != null) {
479 type = LoadType (typeName);
481 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
485 previousPageType = type;
486 } else if (!String.IsNullOrEmpty (virtualPath)) {
487 if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
488 ThrowParseFileNotFound (virtualPath);
490 AddDependency (virtualPath, true);
492 masterVirtualPath = virtualPath;
494 previousPageVirtualPath = virtualPath;
496 ThrowParseException (String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
499 AddAssembly (type.Assembly, true);
501 base.AddDirective (directive, atts);
504 static string SuggestCulture (string culture)
506 string retval = null;
507 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
508 if (ci.Name.StartsWith (culture))
509 retval += ci.Name + " ";
514 internal Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
516 return BuildManager.GetCompiledType (virtualPath);
519 internal override Type CompileIntoType ()
521 AspGenerator generator = new AspGenerator (this);
522 return generator.GetCompiledType ();
525 internal bool EnableSessionState {
527 return enableSessionState == PagesEnableSessionState.True ||
528 ReadOnlySessionState;
532 internal bool EnableViewStateMac {
533 get { return enableViewStateMac; }
536 internal bool EnableViewStateMacSet {
537 get { return enableViewStateMacSet; }
540 internal bool SmartNavigation {
541 get { return smartNavigation; }
544 internal bool ReadOnlySessionState {
546 return enableSessionState == PagesEnableSessionState.ReadOnly;
550 internal bool HaveTrace {
551 get { return haveTrace; }
554 internal bool Trace {
555 get { return trace; }
558 internal TraceMode TraceMode {
559 get { return tracemode; }
561 internal override Type DefaultBaseType {
563 Type ret = DefaultPageBaseType;
565 return base.DefaultBaseType;
570 internal override string DefaultBaseTypeName {
571 get { return PagesConfig.PageBaseType; }
574 internal override string DefaultDirectiveName {
575 get { return "page"; }
578 internal string ContentType {
579 get { return contentType; }
582 internal MainDirectiveAttribute <string> ResponseEncoding {
583 get { return responseEncoding; }
586 internal MainDirectiveAttribute <int> CodePage {
587 get { return codepage; }
590 internal MainDirectiveAttribute <int> LCID {
594 internal MainDirectiveAttribute <string> ClientTarget {
595 get { return clientTarget; }
598 internal MainDirectiveAttribute <string> MasterPageFile {
599 get { return masterPage; }
602 internal MainDirectiveAttribute <string> Title {
603 get { return title; }
606 internal MainDirectiveAttribute <string> Theme {
607 get { return theme; }
609 internal MainDirectiveAttribute <string> MetaDescription {
610 get { return metaDescription; }
613 internal MainDirectiveAttribute <string> MetaKeywords {
614 get { return metaKeywords; }
616 internal string Culture {
617 get { return culture; }
620 internal string UICulture {
621 get { return uiculture; }
624 internal string ErrorPage {
625 get { return errorPage; }
628 internal bool ValidateRequest {
629 get { return validateRequest; }
632 internal bool NotBuffer {
633 get { return notBuffer; }
636 internal bool Async {
637 get { return async; }
640 internal int AsyncTimeout {
641 get { return asyncTimeout; }
644 internal string StyleSheetTheme {
645 get { return styleSheetTheme; }
648 internal Type MasterType {
650 if (masterType == null && !String.IsNullOrEmpty (masterVirtualPath))
651 masterType = BuildManager.GetCompiledType (masterVirtualPath);
657 internal bool EnableEventValidation {
658 get { return enable_event_validation; }
661 internal bool MaintainScrollPositionOnPostBack {
662 get { return maintainScrollPositionOnPostBack; }
665 internal int MaxPageStateFieldLength {
666 get { return maxPageStateFieldLength; }
669 internal Type PreviousPageType {
671 if (previousPageType == null && !String.IsNullOrEmpty (previousPageVirtualPath)) {
672 string mappedPath = MapPath (previousPageVirtualPath);
673 previousPageType = GetCompiledPageType (previousPageVirtualPath, mappedPath, HttpContext.Current);
676 return previousPageType;