2
0

SimpleWebHandlerParser.cs 12 KB

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