SimpleWebHandlerParser.cs 12 KB

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