PageParser.cs 16 KB

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