PageParser.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //
  2. // System.Web.UI.PageParser
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  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. using System.Collections;
  30. using System.Collections.Specialized;
  31. using System.Globalization;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. using System.Web.Compilation;
  35. using System.Web.Configuration;
  36. using System.Web.Util;
  37. using System.IO;
  38. namespace System.Web.UI
  39. {
  40. // CAS - no InheritanceDemand here as the class is sealed
  41. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. public sealed class PageParser : TemplateControlParser
  43. {
  44. bool enableSessionState = true;
  45. bool enableViewStateMac = true;
  46. bool smartNavigation = false;
  47. bool haveTrace;
  48. bool trace;
  49. bool notBuffer;
  50. TraceMode tracemode;
  51. bool readonlySessionState = false;
  52. string responseEncoding;
  53. string contentType;
  54. int codepage = -1;
  55. int lcid = -1;
  56. string culture;
  57. string uiculture;
  58. string errorPage;
  59. bool validateRequest;
  60. string clientTarget;
  61. Type baseType = typeof (Page);
  62. #if NET_2_0
  63. string masterPage;
  64. Type masterType;
  65. string title;
  66. string theme;
  67. string styleSheetTheme;
  68. bool enable_event_validation;
  69. bool maintainScrollPositionOnPostBack;
  70. int maxPageStateFieldLength = -1;
  71. string pageParserFilter = String.Empty;
  72. Type previousPageType;
  73. #endif
  74. public PageParser ()
  75. {
  76. LoadPagesConfigDefaults ();
  77. }
  78. internal PageParser (string virtualPath, string inputFile, HttpContext context)
  79. {
  80. Context = context;
  81. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  82. InputFile = inputFile;
  83. SetBaseType (PagesConfig.PageBaseType);
  84. AddApplicationAssembly ();
  85. LoadPagesConfigDefaults ();
  86. }
  87. #if NET_2_0
  88. internal PageParser (string virtualPath, TextReader reader, HttpContext context)
  89. {
  90. Context = context;
  91. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  92. Reader = reader;
  93. SetBaseType (PagesConfig.PageBaseType);
  94. AddApplicationAssembly ();
  95. LoadPagesConfigDefaults ();
  96. }
  97. #endif
  98. internal override void LoadPagesConfigDefaults ()
  99. {
  100. base.LoadPagesConfigDefaults ();
  101. #if NET_2_0
  102. PagesSection ps = PagesConfig;
  103. #else
  104. PagesConfiguration ps = PagesConfig;
  105. #endif
  106. notBuffer = !ps.Buffer;
  107. #if NET_2_0
  108. switch (ps.EnableSessionState) {
  109. case PagesEnableSessionState.True:
  110. enableSessionState = true;
  111. break;
  112. case PagesEnableSessionState.ReadOnly:
  113. enableSessionState = true;
  114. readonlySessionState = true;
  115. break;
  116. default:
  117. enableSessionState = false;
  118. break;
  119. }
  120. #else
  121. if (String.Compare (ps.EnableSessionState, "true", true, CultureInfo.InvariantCulture) == 0)
  122. enableSessionState = true;
  123. else
  124. enableSessionState = false;
  125. #endif
  126. enableViewStateMac = ps.EnableViewStateMac;
  127. smartNavigation = ps.SmartNavigation;
  128. validateRequest = ps.ValidateRequest;
  129. #if NET_2_0
  130. masterPage = ps.MasterPageFile;
  131. if (masterPage.Length == 0)
  132. masterPage = null;
  133. enable_event_validation = ps.EnableEventValidation;
  134. maxPageStateFieldLength = ps.MaxPageStateFieldLength;
  135. pageParserFilter = ps.PageParserFilterType;
  136. theme = ps.Theme;
  137. if (theme.Length == 0)
  138. theme = null;
  139. styleSheetTheme = ps.StyleSheetTheme;
  140. if (styleSheetTheme.Length == 0)
  141. styleSheetTheme = null;
  142. maintainScrollPositionOnPostBack = ps.MaintainScrollPositionOnPostBack;
  143. #endif
  144. }
  145. public static IHttpHandler GetCompiledPageInstance (string virtualPath,
  146. string inputFile,
  147. HttpContext context)
  148. {
  149. PageParser pp = new PageParser (virtualPath, inputFile, context);
  150. IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
  151. return h;
  152. }
  153. internal override void ProcessMainAttributes (Hashtable atts)
  154. {
  155. // note: if EnableSessionState is not specified in page directive
  156. // then we must use LoadPagesConfigDefaults settings
  157. if (atts.ContainsKey("EnableSessionState")) {
  158. // note: the 'enableSessionState' configuration property is
  159. // processed in a case-sensitive manner while the page-level
  160. // attribute is processed case-insensitive
  161. string enabless = GetString (atts, "EnableSessionState", enableSessionState.ToString ());
  162. if (enabless != null) {
  163. readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
  164. if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
  165. enableSessionState = true;
  166. } else if (String.Compare (enabless, "false", true) == 0) {
  167. enableSessionState = false;
  168. } else {
  169. ThrowParseException ("Invalid value for enableSessionState: " + enabless);
  170. }
  171. }
  172. }
  173. string cp = GetString (atts, "CodePage", null);
  174. if (cp != null) {
  175. if (responseEncoding != null)
  176. ThrowParseException ("CodePage and ResponseEncoding are " +
  177. "mutually exclusive.");
  178. int codepage = 0;
  179. try {
  180. codepage = (int) UInt32.Parse (cp);
  181. } catch {
  182. ThrowParseException ("Invalid value for CodePage: " + cp);
  183. }
  184. try {
  185. Encoding.GetEncoding (codepage);
  186. } catch {
  187. ThrowParseException ("Unsupported codepage: " + cp);
  188. }
  189. }
  190. responseEncoding = GetString (atts, "ResponseEncoding", null);
  191. if (responseEncoding != null) {
  192. if (codepage != -1)
  193. ThrowParseException ("CodePage and ResponseEncoding are " +
  194. "mutually exclusive.");
  195. try {
  196. Encoding.GetEncoding (responseEncoding);
  197. } catch {
  198. ThrowParseException ("Unsupported encoding: " + responseEncoding);
  199. }
  200. }
  201. contentType = GetString (atts, "ContentType", null);
  202. string lcidStr = GetString (atts, "LCID", null);
  203. if (lcidStr != null) {
  204. try {
  205. lcid = (int) UInt32.Parse (lcidStr);
  206. } catch {
  207. ThrowParseException ("Invalid value for LCID: " + lcid);
  208. }
  209. CultureInfo ci = null;
  210. try {
  211. ci = new CultureInfo (lcid);
  212. } catch {
  213. ThrowParseException ("Unsupported LCID: " + lcid);
  214. }
  215. if (ci.IsNeutralCulture) {
  216. string suggestedCulture = SuggestCulture (ci.Name);
  217. string fmt = "LCID attribute must be set to a non-neutral Culture.";
  218. if (suggestedCulture != null) {
  219. ThrowParseException (fmt + " Please try one of these: " +
  220. suggestedCulture);
  221. } else {
  222. ThrowParseException (fmt);
  223. }
  224. }
  225. }
  226. culture = GetString (atts, "Culture", null);
  227. if (culture != null) {
  228. if (lcidStr != null)
  229. ThrowParseException ("Culture and LCID are mutually exclusive.");
  230. CultureInfo ci = null;
  231. try {
  232. #if NET_2_0
  233. if (!culture.StartsWith ("auto"))
  234. #endif
  235. ci = new CultureInfo (culture);
  236. } catch {
  237. ThrowParseException ("Unsupported Culture: " + culture);
  238. }
  239. if (ci != null && ci.IsNeutralCulture) {
  240. string suggestedCulture = SuggestCulture (culture);
  241. string fmt = "Culture attribute must be set to a non-neutral Culture.";
  242. if (suggestedCulture != null)
  243. ThrowParseException (fmt +
  244. " Please try one of these: " + suggestedCulture);
  245. else
  246. ThrowParseException (fmt);
  247. }
  248. }
  249. uiculture = GetString (atts, "UICulture", null);
  250. if (uiculture != null) {
  251. CultureInfo ci = null;
  252. try {
  253. #if NET_2_0
  254. if (!uiculture.StartsWith ("auto"))
  255. #endif
  256. ci = new CultureInfo (uiculture);
  257. } catch {
  258. ThrowParseException ("Unsupported Culture: " + uiculture);
  259. }
  260. if (ci != null && ci.IsNeutralCulture) {
  261. string suggestedCulture = SuggestCulture (uiculture);
  262. string fmt = "UICulture 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. string tracestr = GetString (atts, "Trace", null);
  271. if (tracestr != null) {
  272. haveTrace = true;
  273. atts ["Trace"] = tracestr;
  274. trace = GetBool (atts, "Trace", false);
  275. }
  276. string tracemodes = GetString (atts, "TraceMode", null);
  277. if (tracemodes != null) {
  278. bool valid = true;
  279. try {
  280. tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
  281. } catch {
  282. valid = false;
  283. }
  284. if (!valid || tracemode == TraceMode.Default)
  285. ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
  286. "one of the following values: SortByTime, SortByCategory.");
  287. }
  288. errorPage = GetString (atts, "ErrorPage", null);
  289. validateRequest = GetBool (atts, "ValidateRequest", validateRequest);
  290. clientTarget = GetString (atts, "ClientTarget", null);
  291. if (clientTarget != null) {
  292. clientTarget = clientTarget.Trim ();
  293. #if NET_2_0
  294. ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
  295. ClientTarget ct = null;
  296. if ((ct = sec.ClientTargets [clientTarget]) == null)
  297. clientTarget = clientTarget.ToLower (CultureInfo.InvariantCulture);
  298. if (ct == null && (ct = sec.ClientTargets [clientTarget]) == null) {
  299. ThrowParseException (String.Format (
  300. "ClientTarget '{0}' is an invalid alias. See the " +
  301. "documentation for <clientTarget> config. section.",
  302. clientTarget));
  303. }
  304. clientTarget = ct.UserAgent;
  305. #else
  306. NameValueCollection coll;
  307. coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
  308. object ct = null;
  309. if (coll != null) {
  310. ct = coll [clientTarget];
  311. if (ct == null)
  312. ct = coll [clientTarget.ToLower ()];
  313. }
  314. if (ct == null) {
  315. ThrowParseException (String.Format (
  316. "ClientTarget '{0}' is an invalid alias. See the " +
  317. "documentation for <clientTarget> config. section.",
  318. clientTarget));
  319. }
  320. clientTarget = (string) ct;
  321. #endif
  322. }
  323. notBuffer = !GetBool (atts, "Buffer", true);
  324. #if NET_2_0
  325. masterPage = GetString (atts, "MasterPageFile", masterPage);
  326. // Make sure the page exists
  327. if (!String.IsNullOrEmpty (masterPage))
  328. MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
  329. title = GetString(atts, "Title", null);
  330. theme = GetString (atts, "Theme", theme);
  331. styleSheetTheme = GetString (atts, "StyleSheetTheme", styleSheetTheme);
  332. enable_event_validation = GetBool (atts, "EnableEventValidation", true);
  333. maintainScrollPositionOnPostBack = GetBool (atts, "MaintainScrollPositionOnPostBack", maintainScrollPositionOnPostBack);
  334. #endif
  335. // Ignored by now
  336. GetString (atts, "EnableViewStateMac", null);
  337. GetString (atts, "SmartNavigation", null);
  338. base.ProcessMainAttributes (atts);
  339. }
  340. #if NET_2_0
  341. internal override void AddDirective (string directive, Hashtable atts)
  342. {
  343. bool isMasterType = String.Compare ("MasterType", directive, StringComparison.OrdinalIgnoreCase) == 0;
  344. bool isPreviousPageType = isMasterType ? false : String.Compare ("PreviousPageType", directive,
  345. StringComparison.OrdinalIgnoreCase) == 0;
  346. string typeName = null;
  347. string virtualPath = null;
  348. Type type = null;
  349. if (isMasterType || isPreviousPageType) {
  350. typeName = GetString (atts, "TypeName", null);
  351. virtualPath = GetString (atts, "VirtualPath", null);
  352. if (typeName != null && virtualPath != null)
  353. ThrowParseException (
  354. String.Format ("The '{0}' directive must have exactly one attribute: TypeName or VirtualPath", directive));
  355. if (typeName != null) {
  356. type = LoadType (typeName);
  357. if (type == null)
  358. ThrowParseException (String.Format ("Could not load type '{0}'.", typeName));
  359. } else if (virtualPath != null) {
  360. string mappedPath = MapPath (virtualPath);
  361. if (isMasterType)
  362. type = masterType = MasterPageParser.GetCompiledMasterType (virtualPath,
  363. mappedPath,
  364. HttpContext.Current);
  365. else
  366. type = previousPageType = GetCompiledPageType (virtualPath, mappedPath,
  367. HttpContext.Current);
  368. } else
  369. ThrowParseException (
  370. String.Format ("The {0} directive must have either a TypeName or a VirtualPath attribute.", directive));
  371. AddAssembly (type.Assembly, true);
  372. } else
  373. base.AddDirective (directive, atts);
  374. }
  375. #endif
  376. static string SuggestCulture (string culture)
  377. {
  378. string retval = null;
  379. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
  380. if (ci.Name.StartsWith (culture))
  381. retval += ci.Name + " ";
  382. }
  383. return retval;
  384. }
  385. public static Type GetCompiledPageType (string virtualPath, string inputFile, HttpContext context)
  386. {
  387. PageParser pp = new PageParser (virtualPath, inputFile, context);
  388. return pp.CompileIntoType ();
  389. }
  390. protected override Type CompileIntoType ()
  391. {
  392. AspGenerator generator = new AspGenerator (this);
  393. return generator.GetCompiledType ();
  394. }
  395. internal bool EnableSessionState {
  396. get { return enableSessionState; }
  397. }
  398. internal bool EnableViewStateMac {
  399. get { return enableViewStateMac; }
  400. }
  401. internal bool SmartNavigation {
  402. get { return smartNavigation; }
  403. }
  404. internal bool ReadOnlySessionState {
  405. get { return readonlySessionState; }
  406. }
  407. internal bool HaveTrace {
  408. get { return haveTrace; }
  409. }
  410. internal bool Trace {
  411. get { return trace; }
  412. }
  413. internal TraceMode TraceMode {
  414. get { return tracemode; }
  415. }
  416. internal override Type DefaultBaseType {
  417. get { return baseType; }
  418. }
  419. internal override string DefaultBaseTypeName {
  420. get { return "System.Web.UI.Page"; }
  421. }
  422. internal override string DefaultDirectiveName {
  423. get { return "page"; }
  424. }
  425. internal string ResponseEncoding {
  426. get { return responseEncoding; }
  427. }
  428. internal string ContentType {
  429. get { return contentType; }
  430. }
  431. internal int CodePage {
  432. get { return codepage; }
  433. }
  434. internal string Culture {
  435. get { return culture; }
  436. }
  437. internal string UICulture {
  438. get { return uiculture; }
  439. }
  440. internal int LCID {
  441. get { return lcid; }
  442. }
  443. internal string ErrorPage {
  444. get { return errorPage; }
  445. }
  446. internal bool ValidateRequest {
  447. get { return validateRequest; }
  448. }
  449. internal string ClientTarget {
  450. get { return clientTarget; }
  451. }
  452. internal bool NotBuffer {
  453. get { return notBuffer; }
  454. }
  455. #if NET_2_0
  456. internal string Theme {
  457. get { return theme; }
  458. }
  459. internal string StyleSheetTheme {
  460. get { return styleSheetTheme; }
  461. }
  462. internal string MasterPageFile {
  463. get { return masterPage; }
  464. }
  465. internal Type MasterType {
  466. get { return masterType; }
  467. }
  468. internal string Title {
  469. get { return title; }
  470. }
  471. internal bool EnableEventValidation {
  472. get { return enable_event_validation; }
  473. }
  474. internal bool MaintainScrollPositionOnPostBack {
  475. get { return maintainScrollPositionOnPostBack; }
  476. }
  477. internal int MaxPageStateFieldLength {
  478. get { return maxPageStateFieldLength; }
  479. }
  480. internal string PageParserFilterType {
  481. get { return pageParserFilter; }
  482. }
  483. internal Type PreviousPageType {
  484. get { return previousPageType; }
  485. }
  486. #endif
  487. }
  488. }