SimpleWebHandlerParser.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // System.Web.UI.SimpleWebHandlerParser
  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.CodeDom.Compiler;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Reflection;
  33. using System.Security.Permissions;
  34. using System.Text;
  35. using System.Web.Compilation;
  36. using System.Web.Configuration;
  37. using System.Web.Util;
  38. namespace System.Web.UI
  39. {
  40. // CAS
  41. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. public abstract class SimpleWebHandlerParser
  44. {
  45. HttpContext context;
  46. string vPath;
  47. string physPath;
  48. string className;
  49. bool debug;
  50. string language;
  51. string program;
  52. bool gotDefault;
  53. ArrayList assemblies;
  54. ArrayList dependencies;
  55. Hashtable anames;
  56. string privateBinPath;
  57. string baseDir;
  58. string baseVDir;
  59. #if !NET_2_0
  60. CompilationConfiguration compilationConfig;
  61. #else
  62. TextReader reader;
  63. #endif
  64. int appAssemblyIndex = -1;
  65. Type cachedType;
  66. protected SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath)
  67. {
  68. cachedType = CachingCompiler.GetTypeFromCache (physicalPath);
  69. if (cachedType != null)
  70. return; // We don't need anything else.
  71. this.context = context;
  72. this.vPath = virtualPath;
  73. this.physPath = physicalPath;
  74. AddDependency (physicalPath);
  75. assemblies = new ArrayList ();
  76. string location = Context.ApplicationInstance.AssemblyLocation;
  77. if (location != typeof (TemplateParser).Assembly.Location)
  78. appAssemblyIndex = assemblies.Add (location);
  79. #if NET_2_0
  80. bool addAssembliesInBin = false;
  81. foreach (AssemblyInfo info in CompilationConfig.Assemblies) {
  82. if (info.Assembly == "*")
  83. addAssembliesInBin = true;
  84. else
  85. AddAssemblyByName (info.Assembly, null);
  86. }
  87. if (addAssembliesInBin)
  88. AddAssembliesInBin ();
  89. #else
  90. assemblies.AddRange (CompilationConfig.Assemblies);
  91. if (CompilationConfig.AssembliesInBin)
  92. AddAssembliesInBin ();
  93. #endif
  94. language = CompilationConfig.DefaultLanguage;
  95. GetDirectivesAndContent ();
  96. }
  97. #if NET_2_0
  98. internal SimpleWebHandlerParser (HttpContext context, string virtualPath, string physicalPath, TextReader reader)
  99. : this (context, virtualPath, physicalPath)
  100. {
  101. this.reader = reader;
  102. }
  103. #endif
  104. protected Type GetCompiledTypeFromCache ()
  105. {
  106. return cachedType;
  107. }
  108. void GetDirectivesAndContent ()
  109. {
  110. string line;
  111. bool directiveFound = false;
  112. bool inDirective = false;
  113. StringBuilder directive = null;
  114. StringBuilder content = new StringBuilder ();
  115. int idxStart, idxEnd, length;
  116. using (StreamReader reader = new StreamReader (File.OpenRead (physPath))) {
  117. while ((line = reader.ReadLine ()) != null && cachedType == null) {
  118. length = line.Length;
  119. if (length == 0) {
  120. content.Append ("\n");
  121. continue;
  122. }
  123. idxStart = line.IndexOf ("<%");
  124. if (idxStart > -1) {
  125. idxEnd = line.IndexOf ("%>");
  126. if (idxStart > 0)
  127. content.Append (line.Substring (0, idxStart));
  128. if (directive == null)
  129. directive = new StringBuilder ();
  130. else
  131. directive.Length = 0;
  132. if (idxEnd > -1) {
  133. directiveFound = true;
  134. inDirective = false;
  135. directive.Append (line.Substring (idxStart, idxEnd - idxStart + 2));
  136. if (idxEnd < length - 2)
  137. content.Append (line.Substring (idxEnd + 2, length - idxEnd - 2));
  138. } else {
  139. inDirective = true;
  140. directiveFound = false;
  141. directive.Append (line.Substring (idxStart));
  142. continue;
  143. }
  144. }
  145. if (inDirective) {
  146. int idx = line.IndexOf ("%>");
  147. if (idx > -1) {
  148. directive.Append (line.Substring (0, idx + 2));
  149. if (idx < length)
  150. content.Append (line.Substring (idx + 2) + "\n");
  151. inDirective = false;
  152. directiveFound = true;
  153. } else {
  154. directive.Append (line);
  155. continue;
  156. }
  157. }
  158. if (directiveFound) {
  159. ParseDirective (directive.ToString ());
  160. directiveFound = false;
  161. if (gotDefault) {
  162. cachedType = CachingCompiler.GetTypeFromCache (physPath);
  163. if (cachedType != null)
  164. break;
  165. }
  166. continue;
  167. }
  168. content.Append (line + "\n");
  169. }
  170. directive = null;
  171. }
  172. if (!gotDefault)
  173. throw new ParseException (null, "No @" + DefaultDirectiveName +
  174. " directive found");
  175. if (cachedType == null)
  176. this.program = content.ToString ();
  177. }
  178. void TagParsed (ILocation location, System.Web.Compilation.TagType tagtype, string tagid, TagAttributes attributes)
  179. {
  180. if (tagtype != System.Web.Compilation.TagType.Directive)
  181. throw new ParseException (location, "Unexpected tag");
  182. if (String.Compare (tagid, DefaultDirectiveName, true) == 0) {
  183. AddDefaultDirective (location, attributes);
  184. } else if (String.Compare (tagid, "Assembly", true) == 0) {
  185. AddAssemblyDirective (location, attributes);
  186. } else {
  187. throw new ParseException (location, "Unexpected directive: " + tagid);
  188. }
  189. }
  190. void TextParsed (ILocation location, string text)
  191. {
  192. if (text.Trim () != "")
  193. throw new ParseException (location, "Text not allowed here");
  194. }
  195. void ParseError (ILocation location, string message)
  196. {
  197. throw new ParseException (location, message);
  198. }
  199. static string GetAndRemove (Hashtable table, string key)
  200. {
  201. string o = table [key] as string;
  202. table.Remove (key);
  203. return o;
  204. }
  205. void ParseDirective (string line)
  206. {
  207. AspParser parser = new AspParser (physPath, new StringReader (line));
  208. parser.Error += new ParseErrorHandler (ParseError);
  209. parser.TagParsed += new TagParsedHandler (TagParsed);
  210. parser.TextParsed += new TextParsedHandler (TextParsed);
  211. parser.Parse ();
  212. }
  213. internal virtual void AddDefaultDirective (ILocation location, TagAttributes attrs)
  214. {
  215. if (gotDefault)
  216. throw new ParseException (location, "duplicate " + DefaultDirectiveName + " directive");
  217. gotDefault = true;
  218. Hashtable attributes = attrs.GetDictionary (null);
  219. className = GetAndRemove (attributes, "class");
  220. if (className == null)
  221. throw new ParseException (null, "No Class attribute found.");
  222. string d = GetAndRemove (attributes, "debug");
  223. if (d != null) {
  224. debug = (String.Compare (d, "true", true) == 0);
  225. if (debug == false && String.Compare (d, "false", true) != 0)
  226. throw new ParseException (null, "Invalid value for Debug attribute");
  227. }
  228. language = GetAndRemove (attributes, "language");
  229. if (language == null)
  230. language = CompilationConfig.DefaultLanguage;
  231. GetAndRemove (attributes, "codebehind");
  232. if (attributes.Count > 0)
  233. throw new ParseException (location, "Unrecognized attribute in " +
  234. DefaultDirectiveName + " directive");
  235. }
  236. internal virtual void AddAssemblyDirective (ILocation location, TagAttributes attrs)
  237. {
  238. Hashtable tbl = attrs.GetDictionary (null);
  239. string name = GetAndRemove (tbl, "Name");
  240. string src = GetAndRemove (tbl, "Src");
  241. if (name == null && src == null)
  242. throw new ParseException (location, "You gotta specify Src or Name");
  243. if (name != null && src != null)
  244. throw new ParseException (location, "Src and Name cannot be used together");
  245. if (name != null) {
  246. AddAssemblyByName (name, location);
  247. } else {
  248. GetAssemblyFromSource (src, location);
  249. }
  250. if (tbl.Count > 0)
  251. throw new ParseException (location, "Unrecognized attribute in Assembly directive");
  252. }
  253. internal virtual void AddAssembly (Assembly assembly, bool fullPath)
  254. {
  255. if (assembly == null)
  256. throw new ArgumentNullException ("assembly");
  257. if (anames == null)
  258. anames = new Hashtable ();
  259. string name = assembly.GetName ().Name;
  260. string loc = assembly.Location;
  261. if (fullPath) {
  262. if (!assemblies.Contains (loc)) {
  263. assemblies.Add (loc);
  264. }
  265. anames [name] = loc;
  266. anames [loc] = assembly;
  267. } else {
  268. if (!assemblies.Contains (name)) {
  269. assemblies.Add (name);
  270. }
  271. anames [name] = assembly;
  272. }
  273. }
  274. internal virtual Assembly AddAssemblyByName (string name, ILocation location)
  275. {
  276. if (anames == null)
  277. anames = new Hashtable ();
  278. if (anames.Contains (name)) {
  279. object o = anames [name];
  280. if (o is string)
  281. o = anames [o];
  282. return (Assembly) o;
  283. }
  284. Assembly assembly = LoadAssemblyFromBin (name);
  285. if (assembly != null) {
  286. AddAssembly (assembly, true);
  287. return assembly;
  288. }
  289. Exception ex = null;
  290. try {
  291. assembly = Assembly.LoadWithPartialName (name);
  292. } catch (Exception e) {
  293. ex = null;
  294. assembly = null;
  295. }
  296. if (assembly == null)
  297. throw new ParseException (location, String.Format ("Assembly '{0}' not found", name), ex);
  298. AddAssembly (assembly, true);
  299. return assembly;
  300. }
  301. void AddAssembliesInBin ()
  302. {
  303. foreach (string s in HttpApplication.BinDirectoryAssemblies) {
  304. try {
  305. Assembly assembly = Assembly.LoadFrom (s);
  306. AddAssembly (assembly, true);
  307. } catch (Exception e) {
  308. throw new Exception ("Error while loading " + s, e);
  309. }
  310. }
  311. }
  312. Assembly LoadAssemblyFromBin (string name)
  313. {
  314. Assembly assembly = null;
  315. foreach (string dll in HttpApplication.BinDirectoryAssemblies) {
  316. string fn = Path.GetFileName (dll);
  317. fn = Path.ChangeExtension (fn, null);
  318. if (fn != name)
  319. continue;
  320. assembly = Assembly.LoadFrom (dll);
  321. return assembly;
  322. }
  323. return null;
  324. }
  325. Assembly GetAssemblyFromSource (string vpath, ILocation location)
  326. {
  327. vpath = UrlUtils.Combine (BaseVirtualDir, vpath);
  328. string realPath = context.Request.MapPath (vpath);
  329. if (!File.Exists (realPath))
  330. throw new ParseException (location, "File " + vpath + " not found");
  331. AddDependency (realPath);
  332. CompilerResults result = CachingCompiler.Compile (language, realPath, realPath, assemblies);
  333. if (result.NativeCompilerReturnValue != 0) {
  334. StreamReader reader = new StreamReader (realPath);
  335. throw new CompilationException (realPath, result.Errors, reader.ReadToEnd ());
  336. }
  337. AddAssembly (result.CompiledAssembly, true);
  338. return result.CompiledAssembly;
  339. }
  340. internal Type GetTypeFromBin (string typeName)
  341. {
  342. Type result = null;
  343. #if NET_2_0
  344. IList toplevelAssemblies = BuildManager.TopLevelAssemblies;
  345. if (toplevelAssemblies != null && toplevelAssemblies.Count > 0) {
  346. foreach (Assembly asm in toplevelAssemblies) {
  347. Type type = asm.GetType (typeName, false);
  348. if (type != null) {
  349. if (result != null)
  350. throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
  351. result = type;
  352. }
  353. }
  354. }
  355. #endif
  356. foreach (string dll in HttpApplication.BinDirectoryAssemblies) {
  357. Assembly assembly = Assembly.LoadFrom (dll);
  358. Type type = assembly.GetType (typeName, false);
  359. if (type != null) {
  360. if (result != null)
  361. throw new HttpException (String.Format ("Type {0} is not unique.", typeName));
  362. result = type;
  363. }
  364. }
  365. if (result == null)
  366. throw new HttpException (String.Format ("Type {0} not found.", typeName));
  367. return result;
  368. }
  369. internal virtual void AddDependency (string filename)
  370. {
  371. if (dependencies == null)
  372. dependencies = new ArrayList ();
  373. if (!dependencies.Contains (filename))
  374. dependencies.Add (filename);
  375. }
  376. // Properties
  377. protected abstract string DefaultDirectiveName { get; }
  378. internal HttpContext Context {
  379. get { return context; }
  380. }
  381. internal string VirtualPath {
  382. get { return vPath; }
  383. }
  384. internal string PhysicalPath {
  385. get { return physPath; }
  386. }
  387. internal string ClassName {
  388. get { return className; }
  389. }
  390. internal bool Debug {
  391. get { return debug; }
  392. }
  393. internal string Language {
  394. get { return language; }
  395. }
  396. internal string Program {
  397. get { return program; }
  398. }
  399. internal ArrayList Assemblies {
  400. get {
  401. if (appAssemblyIndex != -1) {
  402. object o = assemblies [appAssemblyIndex];
  403. assemblies.RemoveAt (appAssemblyIndex);
  404. assemblies.Add (o);
  405. appAssemblyIndex = -1;
  406. }
  407. return assemblies;
  408. }
  409. }
  410. internal ArrayList Dependencies {
  411. get { return dependencies; }
  412. }
  413. internal string BaseDir {
  414. get {
  415. if (baseDir == null)
  416. baseDir = context.Request.MapPath (BaseVirtualDir);
  417. return baseDir;
  418. }
  419. }
  420. internal virtual string BaseVirtualDir {
  421. get {
  422. if (baseVDir == null)
  423. baseVDir = UrlUtils.GetDirectory (context.Request.FilePath);
  424. return baseVDir;
  425. }
  426. }
  427. #if NET_2_0
  428. CompilationSection CompilationConfig {
  429. get {
  430. return (CompilationSection)WebConfigurationManager.GetSection ("system.web/compilation");
  431. }
  432. }
  433. internal TextReader Reader {
  434. get { return reader; }
  435. set { reader = value; }
  436. }
  437. #else
  438. internal CompilationConfiguration CompilationConfig {
  439. get {
  440. if (compilationConfig == null)
  441. compilationConfig = CompilationConfiguration.GetInstance (context);
  442. return compilationConfig;
  443. }
  444. }
  445. #endif
  446. }
  447. }