PageParser.cs 12 KB

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