PageParser.cs 11 KB

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