PageParser.cs 16 KB

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