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 PagesEnableSessionState enableSessionState = PagesEnableSessionState.True;
48 bool enableViewStateMac;
49 bool enableViewStateMacSet;
54 TraceMode tracemode = TraceMode.Default;
56 MainDirectiveAttribute <int> codepage;
57 MainDirectiveAttribute <string> responseEncoding;
58 MainDirectiveAttribute <int> lcid;
59 MainDirectiveAttribute <string> clientTarget;
60 MainDirectiveAttribute <string> masterPage;
61 MainDirectiveAttribute <string> title;
62 MainDirectiveAttribute <string> theme;
68 int asyncTimeout = -1;
70 string masterVirtualPath;
71 string styleSheetTheme;
72 bool enable_event_validation;
73 bool maintainScrollPositionOnPostBack;
74 int maxPageStateFieldLength = -1;
75 Type previousPageType;
76 string previousPageVirtualPath;
80 LoadConfigDefaults ();
83 internal PageParser (string virtualPath, string inputFile, HttpContext context)
85 this.VirtualPath = new VirtualPath (virtualPath);
87 BaseVirtualDir = VirtualPathUtility.GetDirectory (virtualPath, false);
88 InputFile = inputFile;
90 AddApplicationAssembly ();
91 LoadConfigDefaults ();
94 internal PageParser (VirtualPath virtualPath, TextReader reader, HttpContext context)
95 : this (virtualPath, null, reader, context)
99 internal PageParser (VirtualPath virtualPath, string inputFile, TextReader reader, HttpContext context)
101 this.VirtualPath = virtualPath;
103 BaseVirtualDir = virtualPath.DirectoryNoNormalize;
105 if (String.IsNullOrEmpty (inputFile))
106 InputFile = virtualPath.PhysicalPath;
108 InputFile = inputFile;
110 AddApplicationAssembly ();
111 LoadConfigDefaults ();
114 internal override void LoadConfigDefaults ()
116 base.LoadConfigDefaults ();
117 PagesSection ps = PagesConfig;
119 notBuffer = !ps.Buffer;
120 enableSessionState = ps.EnableSessionState;
121 enableViewStateMac = ps.EnableViewStateMac;
122 smartNavigation = ps.SmartNavigation;
123 validateRequest = ps.ValidateRequest;
125 string value = ps.MasterPageFile;
126 if (value.Length > 0)
127 masterPage = new MainDirectiveAttribute <string> (value, true);
129 enable_event_validation = ps.EnableEventValidation;
130 maxPageStateFieldLength = ps.MaxPageStateFieldLength;
132 if (value.Length > 0)
133 theme = new MainDirectiveAttribute <string> (value, true);
135 styleSheetTheme = ps.StyleSheetTheme;
136 if (styleSheetTheme.Length == 0)
137 styleSheetTheme = null;
138 maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
141 public static IHttpHandler GetCompiledPageInstance (string virtualPath, string inputFile, HttpContext context)
145 if (!String.IsNullOrEmpty (inputFile))
146 isFake = !inputFile.StartsWith (HttpRuntime.AppDomainAppPath);
148 return BuildManager.CreateInstanceFromVirtualPath (new VirtualPath (virtualPath, inputFile, isFake), typeof (IHttpHandler)) as IHttpHandler;
151 internal override void ProcessMainAttributes (IDictionary atts)
153 // note: the 'enableSessionState' configuration property is
154 // processed in a case-sensitive manner while the page-level
155 // attribute is processed case-insensitive
156 string enabless = GetString (atts, "EnableSessionState", null);
157 if (enabless != null) {
158 if (String.Compare (enabless, "readonly", true, Helpers.InvariantCulture) == 0)
159 enableSessionState = PagesEnableSessionState.ReadOnly;
160 else if (String.Compare (enabless, "true", true, Helpers.InvariantCulture) == 0)
161 enableSessionState = PagesEnableSessionState.True;
162 else if (String.Compare (enabless, "false", true, Helpers.InvariantCulture) == 0)
163 enableSessionState = PagesEnableSessionState.False;
165 ThrowParseException ("Invalid value for enableSessionState: " + enabless);
168 string value = GetString (atts, "CodePage", null);
170 if (responseEncoding != null)
171 ThrowParseException ("CodePage and ResponseEncoding are mutually exclusive.");
173 if (!BaseParser.IsExpression (value)) {
177 cpval = (int) UInt32.Parse (value);
179 ThrowParseException ("Invalid value for CodePage: " + value);
183 Encoding.GetEncoding (cpval);
185 ThrowParseException ("Unsupported codepage: " + value);
187 codepage = new MainDirectiveAttribute <int> (cpval, true);
189 codepage = new MainDirectiveAttribute <int> (value);
192 value = GetString (atts, "ResponseEncoding", null);
194 if (codepage != null)
195 ThrowParseException ("CodePage and ResponseEncoding are mutually exclusive.");
197 if (!BaseParser.IsExpression (value)) {
199 Encoding.GetEncoding (value);
201 ThrowParseException ("Unsupported encoding: " + value);
203 responseEncoding = new MainDirectiveAttribute <string> (value, true);
205 responseEncoding = new MainDirectiveAttribute <string> (value);
208 contentType = GetString (atts, "ContentType", null);
210 value = GetString (atts, "LCID", null);
212 if (!BaseParser.IsExpression (value)) {
215 parsedLcid = (int) UInt32.Parse (value);
217 ThrowParseException ("Invalid value for LCID: " + value);
220 CultureInfo ci = null;
222 ci = new CultureInfo (parsedLcid);
224 ThrowParseException ("Unsupported LCID: " + value);
227 if (ci.IsNeutralCulture) {
228 string suggestedCulture = SuggestCulture (ci.Name);
229 string fmt = "LCID attribute must be set to a non-neutral Culture.";
230 if (suggestedCulture != null) {
231 ThrowParseException (fmt + " Please try one of these: " +
234 ThrowParseException (fmt);
237 lcid = new MainDirectiveAttribute <int> (parsedLcid, true);
239 lcid = new MainDirectiveAttribute <int> (value);
242 culture = GetString (atts, "Culture", null);
243 if (culture != null) {
245 ThrowParseException ("Culture and LCID are mutually exclusive.");
247 CultureInfo ci = null;
249 if (!culture.StartsWith ("auto"))
250 ci = new CultureInfo (culture);
252 ThrowParseException ("Unsupported Culture: " + culture);
255 if (ci != null && ci.IsNeutralCulture) {
256 string suggestedCulture = SuggestCulture (culture);
257 string fmt = "Culture attribute must be set to a non-neutral Culture.";
258 if (suggestedCulture != null)
259 ThrowParseException (fmt +
260 " Please try one of these: " + suggestedCulture);
262 ThrowParseException (fmt);
266 uiculture = GetString (atts, "UICulture", null);
267 if (uiculture != null) {
268 CultureInfo ci = null;
270 if (!uiculture.StartsWith ("auto"))
271 ci = new CultureInfo (uiculture);
273 ThrowParseException ("Unsupported Culture: " + uiculture);
276 if (ci != null && ci.IsNeutralCulture) {
277 string suggestedCulture = SuggestCulture (uiculture);
278 string fmt = "UICulture attribute must be set to a non-neutral Culture.";
279 if (suggestedCulture != null)
280 ThrowParseException (fmt +
281 " Please try one of these: " + suggestedCulture);
283 ThrowParseException (fmt);
287 string tracestr = GetString (atts, "Trace", null);
288 if (tracestr != null) {
290 atts ["Trace"] = tracestr;
291 trace = GetBool (atts, "Trace", false);
294 string tracemodes = GetString (atts, "TraceMode", null);
295 if (tracemodes != null) {
298 tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
303 if (!valid || tracemode == TraceMode.Default)
304 ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
305 "one of the following values: SortByTime, SortByCategory.");
308 errorPage = GetString (atts, "ErrorPage", null);
309 validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
310 value = GetString (atts, "ClientTarget", null);
312 if (!BaseParser.IsExpression (value)) {
313 value = value.Trim ();
315 ClientTargetSection sec = GetConfigSection <ClientTargetSection> ("system.web/clientTarget");
316 ClientTarget ct = null;
318 if ((ct = sec.ClientTargets [value]) == null)
319 value = value.ToLowerInvariant ();
321 if (ct == null && (ct = sec.ClientTargets [value]) == null) {
322 ThrowParseException (String.Format (
323 "ClientTarget '{0}' is an invalid alias. See the " +
324 "documentation for <clientTarget> config. section.",
327 value = ct.UserAgent;
328 clientTarget = new MainDirectiveAttribute <string> (value, true);
330 clientTarget = new MainDirectiveAttribute <string> (value);
333 notBuffer = !GetBool (atts, "Buffer", true);
334 async = GetBool (atts, "Async", false);
335 string asyncTimeoutVal = GetString (atts, "AsyncTimeout", null);
336 if (asyncTimeoutVal != null) {
338 asyncTimeout = Int32.Parse (asyncTimeoutVal);
339 } catch (Exception) {
340 ThrowParseException ("AsyncTimeout must be an integer value");
344 value = GetString (atts, "MasterPageFile", masterPage != null ? masterPage.Value : null);
345 if (!String.IsNullOrEmpty (value)) {
346 if (!BaseParser.IsExpression (value)) {
347 if (!HostingEnvironment.VirtualPathProvider.FileExists (value))
348 ThrowParseFileNotFound (value);
349 AddDependency (value);
350 masterPage = new MainDirectiveAttribute <string> (value, true);
352 masterPage = new MainDirectiveAttribute <string> (value);
355 value = GetString(atts, "Title", null);
357 if (!BaseParser.IsExpression (value))
358 title = new MainDirectiveAttribute <string> (value, true);
360 title = new MainDirectiveAttribute <string> (value);
363 value = GetString (atts, "Theme", theme != null ? theme.Value : null);
365 if (!BaseParser.IsExpression (value))
366 theme = new MainDirectiveAttribute <string> (value, true);
368 theme = new MainDirectiveAttribute <string> (value);
371 styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
372 enable_event_validation = GetBool (atts, "EnableEventValidation", enable_event_validation);
373 maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
375 if (atts.Contains ("EnableViewState")) {
376 enableViewStateMac = GetBool (atts, "EnableViewStateMac", enableViewStateMac);
377 enableViewStateMacSet = true;
381 GetString (atts, "SmartNavigation", null);
383 base.ProcessMainAttributes (atts);
386 internal override void AddDirective (string directive, IDictionary atts)
388 bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
389 bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
390 StringComparison.OrdinalIgnoreCase) == 0;
392 string typeName = null;
393 string virtualPath = null;
396 if (isMasterType || isPreviousPageType) {
397 PageParserFilter pfilter = PageParserFilter;
399 pfilter.PreprocessDirective (directive.ToLowerInvariant (), atts);
401 typeName = GetString (atts, "TypeName", null);
402 virtualPath = GetString (atts, "VirtualPath", null);
404 if (typeName != null && virtualPath != null)
405 ThrowParseException (
406 String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
407 if (typeName != null) {
408 type = LoadType (typeName);
410 ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
414 previousPageType = type;
415 } else if (!String.IsNullOrEmpty (virtualPath)) {
416 if (!HostingEnvironment.VirtualPathProvider.FileExists (virtualPath))
417 ThrowParseFileNotFound (virtualPath);
419 AddDependency (virtualPath);
421 masterVirtualPath = virtualPath;
423 previousPageVirtualPath = virtualPath;
425 ThrowParseException (String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
428 AddAssembly (type.Assembly, true);
430 base.AddDirective (directive, atts);
433 static string SuggestCulture (string culture)
435 string retval = null;
436 foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
437 if (ci.Name.StartsWith (culture))
438 retval += ci.Name + " ";
443 public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
445 return BuildManager.GetCompiledType (virtualPath);
448 protected override Type CompileIntoType ()
450 AspGenerator generator = new AspGenerator (this);
451 return generator.GetCompiledType ();
454 internal bool EnableSessionState {
456 return enableSessionState == PagesEnableSessionState.True ||
457 ReadOnlySessionState;
461 internal bool EnableViewStateMac {
462 get { return enableViewStateMac; }
465 internal bool EnableViewStateMacSet {
466 get { return enableViewStateMacSet; }
469 internal bool SmartNavigation {
470 get { return smartNavigation; }
473 internal bool ReadOnlySessionState {
475 return enableSessionState == PagesEnableSessionState.ReadOnly;
479 internal bool HaveTrace {
480 get { return haveTrace; }
483 internal bool Trace {
484 get { return trace; }
487 internal TraceMode TraceMode {
488 get { return tracemode; }
491 internal override string DefaultBaseTypeName {
492 get { return PagesConfig.PageBaseType; }
495 internal override string DefaultDirectiveName {
496 get { return "page"; }
499 internal string ContentType {
500 get { return contentType; }
503 internal MainDirectiveAttribute <string> ResponseEncoding {
504 get { return responseEncoding; }
507 internal MainDirectiveAttribute <int> CodePage {
508 get { return codepage; }
511 internal MainDirectiveAttribute <int> LCID {
515 internal MainDirectiveAttribute <string> ClientTarget {
516 get { return clientTarget; }
519 internal MainDirectiveAttribute <string> MasterPageFile {
520 get { return masterPage; }
523 internal MainDirectiveAttribute <string> Title {
524 get { return title; }
527 internal MainDirectiveAttribute <string> Theme {
528 get { return theme; }
531 internal string Culture {
532 get { return culture; }
535 internal string UICulture {
536 get { return uiculture; }
539 internal string ErrorPage {
540 get { return errorPage; }
543 internal bool ValidateRequest {
544 get { return validateRequest; }
547 internal bool NotBuffer {
548 get { return notBuffer; }
551 internal bool Async {
552 get { return async; }
555 internal int AsyncTimeout {
556 get { return asyncTimeout; }
559 internal string StyleSheetTheme {
560 get { return styleSheetTheme; }
563 internal Type MasterType {
565 if (masterType == null && !String.IsNullOrEmpty (masterVirtualPath))
566 masterType = BuildManager.GetCompiledType (masterVirtualPath);
572 internal bool EnableEventValidation {
573 get { return enable_event_validation; }
576 internal bool MaintainScrollPositionOnPostBack {
577 get { return maintainScrollPositionOnPostBack; }
580 internal int MaxPageStateFieldLength {
581 get { return maxPageStateFieldLength; }
584 internal Type PreviousPageType {
586 if (previousPageType == null && !String.IsNullOrEmpty (previousPageVirtualPath)) {
587 string mappedPath = MapPath (previousPageVirtualPath);
588 previousPageType = GetCompiledPageType (previousPageVirtualPath, mappedPath, HttpContext.Current);
591 return previousPageType;