ServiceHostParser.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // ServiceHostParser.cs
  3. //
  4. // Author:
  5. // Ankit Jain ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // Copyright (C) 2006 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.IO;
  32. using System.Text;
  33. using System.Web;
  34. using System.Web.Caching;
  35. using System.Web.Configuration;
  36. namespace System.ServiceModel.Channels {
  37. class ServiceHostParser
  38. {
  39. string file;
  40. string url;
  41. string type_name;
  42. string language;
  43. string factory;
  44. bool debug;
  45. bool got_default_directive;
  46. string program; // If program == null, we have to get the requested 'type_name' from the assemblies in bin
  47. ArrayList assemblies;
  48. HttpContext context;
  49. public ServiceHostParser (string file, string url, HttpContext context)
  50. {
  51. this.file = file;
  52. this.url = url;
  53. assemblies = new ArrayList ();
  54. assemblies.Add ("System.ServiceModel");
  55. this.context = context;
  56. CompilationSection section = (CompilationSection) context.GetSection ("system.web/compilation");
  57. language = section.DefaultLanguage;
  58. }
  59. public HttpContext Context {
  60. get { return context; }
  61. }
  62. public string Filename {
  63. get { return file; }
  64. }
  65. public string TypeName {
  66. get { return type_name; }
  67. }
  68. public bool Debug {
  69. get { return debug; }
  70. }
  71. public string Program {
  72. get { return program; }
  73. }
  74. public ArrayList Assemblies {
  75. get { return assemblies; }
  76. }
  77. public string Factory {
  78. get { return factory; }
  79. }
  80. public string Language {
  81. get { return language; }
  82. }
  83. public void Parse ()
  84. {
  85. using (StreamReader reader = new StreamReader (file)) {
  86. string line;
  87. bool directive_found = false;
  88. StringBuilder content = new StringBuilder ();
  89. while ((line = reader.ReadLine ()) != null) {
  90. string trimmed = line.Trim ();
  91. if (!directive_found && trimmed == String.Empty)
  92. continue;
  93. if (trimmed.StartsWith ("<%@")) {
  94. ParseDirective (trimmed);
  95. directive_found = true;
  96. continue;
  97. }
  98. content.Append (line + "\n");
  99. content.Append (reader.ReadToEnd ());
  100. }
  101. if (!got_default_directive)
  102. throw new Exception ("No @ServiceHost directive found");
  103. this.program = content.ToString ().Trim ();
  104. if (this.program.Trim () == "")
  105. this.program = null;
  106. }
  107. if (String.IsNullOrEmpty (Language))
  108. throw new Exception ("Language not specified.");
  109. }
  110. void ParseDirective (string line)
  111. {
  112. StringDictionary attributes = Split (line);
  113. //Directive
  114. if (String.Compare (attributes ["directive"], "ServiceHost", true) == 0) {
  115. got_default_directive = true;
  116. if (!attributes.ContainsKey ("SERVICE"))
  117. throw new Exception ("Service attribute not present in @ServiceHost directive.");
  118. else
  119. type_name = attributes ["SERVICE"];
  120. if (attributes.ContainsKey ("LANGUAGE"))
  121. language = attributes ["LANGUAGE"];
  122. if (attributes.ContainsKey ("FACTORY"))
  123. factory = attributes ["FACTORY"];
  124. if (attributes.ContainsKey ("DEBUG")) {
  125. if (String.Compare (attributes ["DEBUG"], "TRUE", true) == 0)
  126. debug = true;
  127. else if (String.Compare (attributes ["DEBUG"], "FALSE", true) == 0)
  128. debug = false;
  129. else
  130. throw new Exception (String.Format (
  131. "Invalid value for debug attribute : '{0}'", attributes ["DEBUG"]));
  132. }
  133. //FIXME: Other attributes,
  134. return;
  135. }
  136. //FIXME: Other directives? Documentation doesn't mention any other
  137. throw new Exception (String.Format ("Cannot handle directive : '{0}'", attributes ["directive"]));
  138. }
  139. StringDictionary Split (string line)
  140. {
  141. line.Trim ();
  142. int end_pos = line.LastIndexOf ("%>");
  143. if (end_pos < 0)
  144. throw new Exception ("Directive must end with '%>'");
  145. StringDictionary table = new StringDictionary ();
  146. string content = line.Substring (3, end_pos - 3).Trim ();
  147. if (content.Length == 0)
  148. throw new Exception ("No directive found");
  149. int len = content.Length;
  150. int pos = 0;
  151. while (pos < len && content [pos] != ' ')
  152. pos ++;
  153. if (pos >= len) {
  154. table ["directive"] = content;
  155. return table;
  156. }
  157. table ["directive"] = content.Substring (0, pos);
  158. content = content.Substring (pos);
  159. len = content.Length;
  160. pos = 0;
  161. while (pos < len) {
  162. //skip spaces
  163. while (content [pos] == ' ' && pos < len)
  164. pos ++;
  165. int eq_pos = content.IndexOf ('=', pos);
  166. string key = content.Substring (pos, eq_pos - pos).Trim ();
  167. pos = eq_pos + 1;
  168. int start_quote = content.IndexOf ('"', pos);
  169. int end_quote = content.IndexOf ('"', start_quote + 1);
  170. string val = content.Substring (start_quote + 1, end_quote - start_quote - 1).Trim ();
  171. pos = end_quote + 1;
  172. table [key.ToUpper ()] = val;
  173. }
  174. return table;
  175. }
  176. }
  177. }