PageParser.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 haveTrace;
  46. bool trace;
  47. bool notBuffer;
  48. TraceMode tracemode;
  49. bool readonlySessionState;
  50. string responseEncoding;
  51. string contentType;
  52. int codepage = -1;
  53. int lcid = -1;
  54. string culture;
  55. string uiculture;
  56. string errorPage;
  57. bool validateRequest;
  58. string clientTarget;
  59. Type baseType = typeof (Page);
  60. #if NET_2_0
  61. string masterPage;
  62. Type masterType;
  63. string title;
  64. string theme;
  65. string styleSheetTheme;
  66. bool enable_event_validation;
  67. #endif
  68. public PageParser ()
  69. {
  70. }
  71. internal PageParser (string virtualPath, string inputFile, HttpContext context)
  72. {
  73. Context = context;
  74. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  75. InputFile = inputFile;
  76. SetBaseType (PagesConfig.PageBaseType);
  77. AddApplicationAssembly ();
  78. }
  79. #if NET_2_0
  80. internal PageParser (string virtualPath, TextReader reader, HttpContext context)
  81. {
  82. Context = context;
  83. BaseVirtualDir = UrlUtils.GetDirectory (virtualPath);
  84. Reader = reader;
  85. SetBaseType (PagesConfig.PageBaseType);
  86. AddApplicationAssembly ();
  87. }
  88. #endif
  89. public static IHttpHandler GetCompiledPageInstance (string virtualPath,
  90. string inputFile,
  91. HttpContext context)
  92. {
  93. PageParser pp = new PageParser (virtualPath, inputFile, context);
  94. IHttpHandler h = (IHttpHandler) pp.GetCompiledInstance ();
  95. return h;
  96. }
  97. internal override void ProcessMainAttributes (Hashtable atts)
  98. {
  99. string enabless = GetString (atts, "EnableSessionState",
  100. #if NET_2_0
  101. PagesConfig.EnableSessionState.ToString()
  102. #else
  103. PagesConfig.EnableSessionState
  104. #endif
  105. );
  106. if (enabless != null) {
  107. readonlySessionState = (String.Compare (enabless, "readonly", true) == 0);
  108. if (readonlySessionState == true || String.Compare (enabless, "true", true) == 0) {
  109. enableSessionState = true;
  110. } else if (String.Compare (enabless, "false", true) == 0) {
  111. enableSessionState = false;
  112. } else {
  113. ThrowParseException ("Invalid value for EnableSessionState: " + enabless);
  114. }
  115. }
  116. string cp = GetString (atts, "CodePage", null);
  117. if (cp != null) {
  118. if (responseEncoding != null)
  119. ThrowParseException ("CodePage and ResponseEncoding are " +
  120. "mutually exclusive.");
  121. int codepage = 0;
  122. try {
  123. codepage = (int) UInt32.Parse (cp);
  124. } catch {
  125. ThrowParseException ("Invalid value for CodePage: " + cp);
  126. }
  127. try {
  128. Encoding.GetEncoding (codepage);
  129. } catch {
  130. ThrowParseException ("Unsupported codepage: " + cp);
  131. }
  132. }
  133. responseEncoding = GetString (atts, "ResponseEncoding", null);
  134. if (responseEncoding != null) {
  135. if (codepage != -1)
  136. ThrowParseException ("CodePage and ResponseEncoding are " +
  137. "mutually exclusive.");
  138. try {
  139. Encoding.GetEncoding (responseEncoding);
  140. } catch {
  141. ThrowParseException ("Unsupported encoding: " + responseEncoding);
  142. }
  143. }
  144. contentType = GetString (atts, "ContentType", null);
  145. string lcidStr = GetString (atts, "LCID", null);
  146. if (lcidStr != null) {
  147. try {
  148. lcid = (int) UInt32.Parse (lcidStr);
  149. } catch {
  150. ThrowParseException ("Invalid value for LCID: " + lcid);
  151. }
  152. CultureInfo ci = null;
  153. try {
  154. ci = new CultureInfo (lcid);
  155. } catch {
  156. ThrowParseException ("Unsupported LCID: " + lcid);
  157. }
  158. if (ci.IsNeutralCulture) {
  159. string suggestedCulture = SuggestCulture (ci.Name);
  160. string fmt = "LCID attribute must be set to a non-neutral Culture.";
  161. if (suggestedCulture != null) {
  162. ThrowParseException (fmt + " Please try one of these: " +
  163. suggestedCulture);
  164. } else {
  165. ThrowParseException (fmt);
  166. }
  167. }
  168. }
  169. culture = GetString (atts, "Culture", null);
  170. if (culture != null) {
  171. if (lcidStr != null)
  172. ThrowParseException ("Culture and LCID are mutually exclusive.");
  173. CultureInfo ci = null;
  174. try {
  175. ci = new CultureInfo (culture);
  176. } catch {
  177. ThrowParseException ("Unsupported Culture: " + culture);
  178. }
  179. if (ci.IsNeutralCulture) {
  180. string suggestedCulture = SuggestCulture (culture);
  181. string fmt = "Culture attribute must be set to a non-neutral Culture.";
  182. if (suggestedCulture != null)
  183. ThrowParseException (fmt +
  184. " Please try one of these: " + suggestedCulture);
  185. else
  186. ThrowParseException (fmt);
  187. }
  188. }
  189. uiculture = GetString (atts, "UICulture", null);
  190. if (uiculture != null) {
  191. CultureInfo ci = null;
  192. try {
  193. ci = new CultureInfo (uiculture);
  194. } catch {
  195. ThrowParseException ("Unsupported Culture: " + uiculture);
  196. }
  197. if (ci.IsNeutralCulture) {
  198. string suggestedCulture = SuggestCulture (uiculture);
  199. string fmt = "UICulture attribute must be set to a non-neutral Culture.";
  200. if (suggestedCulture != null)
  201. ThrowParseException (fmt +
  202. " Please try one of these: " + suggestedCulture);
  203. else
  204. ThrowParseException (fmt);
  205. }
  206. }
  207. string tracestr = GetString (atts, "Trace", null);
  208. if (tracestr != null) {
  209. haveTrace = true;
  210. atts ["Trace"] = tracestr;
  211. trace = GetBool (atts, "Trace", false);
  212. }
  213. string tracemodes = GetString (atts, "TraceMode", null);
  214. if (tracemodes != null) {
  215. bool valid = true;
  216. try {
  217. tracemode = (TraceMode) Enum.Parse (typeof (TraceMode), tracemodes, false);
  218. } catch {
  219. valid = false;
  220. }
  221. if (!valid || tracemode == TraceMode.Default)
  222. ThrowParseException ("The 'tracemode' attribute is case sensitive and must be " +
  223. "one of the following values: SortByTime, SortByCategory.");
  224. }
  225. errorPage = GetString (atts, "ErrorPage", null);
  226. validateRequest = GetBool (atts, "ValidateRequest", PagesConfig.ValidateRequest);
  227. clientTarget = GetString (atts, "ClientTarget", null);
  228. if (clientTarget != null) {
  229. #if NET_2_0
  230. ClientTargetSection sec = (ClientTargetSection)WebConfigurationManager.GetSection ("system.web/clientTarget");
  231. if (sec.ClientTargets[clientTarget] == null) {
  232. ThrowParseException (String.Format (
  233. "ClientTarget '{0}' is an invalid alias. See the " +
  234. "documentation for <clientTarget> config. section.",
  235. clientTarget));
  236. }
  237. clientTarget = sec.ClientTargets[clientTarget].UserAgent;
  238. #else
  239. NameValueCollection coll;
  240. coll = (NameValueCollection) Context.GetConfig ("system.web/clientTarget");
  241. if (coll == null || coll [clientTarget] == null) {
  242. ThrowParseException (String.Format (
  243. "ClientTarget '{0}' is an invalid alias. See the " +
  244. "documentation for <clientTarget> config. section.",
  245. clientTarget));
  246. }
  247. clientTarget = (string) coll [clientTarget];
  248. #endif
  249. }
  250. notBuffer = !GetBool (atts, "Buffer", true);
  251. #if NET_2_0
  252. masterPage = GetString (atts, "MasterPageFile", null);
  253. // Make sure the page exists
  254. if (masterPage != null)
  255. MasterPageParser.GetCompiledMasterType (masterPage, MapPath (masterPage), HttpContext.Current);
  256. title = GetString(atts, "Title", null);
  257. theme = GetString (atts, "Theme", null);
  258. styleSheetTheme = GetString (atts, "StyleSheetTheme", null);
  259. enable_event_validation = GetBool (atts, "EnableEventValidation", true);
  260. #endif
  261. // Ignored by now
  262. GetString (atts, "EnableViewStateMac", null);
  263. GetString (atts, "SmartNavigation", null);
  264. base.ProcessMainAttributes (atts);
  265. }
  266. #if NET_2_0
  267. internal override void AddDirective (string directive, Hashtable atts)
  268. {
  269. if (String.Compare ("MasterType", directive, true) == 0) {
  270. string type = GetString (atts, "TypeName", null);
  271. if (type != null) {
  272. masterType = LoadType (type);
  273. if (masterType == null)
  274. ThrowParseException ("Could not load type '" + type + "'.");
  275. } else {
  276. string path = GetString (atts, "VirtualPath", null);
  277. if (path != null)
  278. masterType = MasterPageParser.GetCompiledMasterType (path, MapPath (path), HttpContext.Current);
  279. else
  280. ThrowParseException ("The MasterType directive must have either a TypeName or a VirtualPath attribute.");
  281. }
  282. AddAssembly (masterType.Assembly, true);
  283. }
  284. else
  285. base.AddDirective (directive, atts);
  286. }
  287. #endif
  288. static string SuggestCulture (string culture)
  289. {
  290. string retval = null;
  291. foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.SpecificCultures)) {
  292. if (ci.Name.StartsWith (culture))
  293. retval += ci.Name + " ";
  294. }
  295. return retval;
  296. }
  297. protected override Type CompileIntoType ()
  298. {
  299. AspGenerator generator = new AspGenerator (this);
  300. return generator.GetCompiledType ();
  301. }
  302. internal bool EnableSessionState {
  303. get { return enableSessionState; }
  304. }
  305. internal bool ReadOnlySessionState {
  306. get { return readonlySessionState; }
  307. }
  308. internal bool HaveTrace {
  309. get { return haveTrace; }
  310. }
  311. internal bool Trace {
  312. get { return trace; }
  313. }
  314. internal TraceMode TraceMode {
  315. get { return tracemode; }
  316. }
  317. internal override Type DefaultBaseType {
  318. get { return baseType; }
  319. }
  320. internal override string DefaultBaseTypeName {
  321. get { return "System.Web.UI.Page"; }
  322. }
  323. internal override string DefaultDirectiveName {
  324. get { return "page"; }
  325. }
  326. internal string ResponseEncoding {
  327. get { return responseEncoding; }
  328. }
  329. internal string ContentType {
  330. get { return contentType; }
  331. }
  332. internal int CodePage {
  333. get { return codepage; }
  334. }
  335. internal string Culture {
  336. get { return culture; }
  337. }
  338. internal string UICulture {
  339. get { return uiculture; }
  340. }
  341. internal int LCID {
  342. get { return lcid; }
  343. }
  344. internal string ErrorPage {
  345. get { return errorPage; }
  346. }
  347. internal bool ValidateRequest {
  348. get { return validateRequest; }
  349. }
  350. internal string ClientTarget {
  351. get { return clientTarget; }
  352. }
  353. internal bool NotBuffer {
  354. get { return notBuffer; }
  355. }
  356. #if NET_2_0
  357. internal string Theme {
  358. get { return theme; }
  359. }
  360. internal string StyleSheetTheme {
  361. get { return styleSheetTheme; }
  362. }
  363. internal string MasterPageFile {
  364. get { return masterPage; }
  365. }
  366. internal Type MasterType {
  367. get { return masterType; }
  368. }
  369. internal string Title {
  370. get { return title; }
  371. }
  372. internal bool EnableEventValidation {
  373. get { return enable_event_validation; }
  374. }
  375. #endif
  376. }
  377. }