SimpleWorkerRequest.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // System.Web.Hosting.SimpleWorkerRequest.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. //
  9. // Copyright (C) 2005,2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.IO;
  32. using System.Runtime.InteropServices;
  33. using System.Security;
  34. using System.Security.Permissions;
  35. using System.Web.Configuration;
  36. using System.Web.UI;
  37. using System.Web.Util;
  38. namespace System.Web.Hosting {
  39. // CAS
  40. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  42. // attributes
  43. [ComVisible (false)]
  44. public class SimpleWorkerRequest : HttpWorkerRequest {
  45. string page;
  46. string query;
  47. string app_virtual_dir;
  48. string app_physical_dir;
  49. string path_info;
  50. TextWriter output;
  51. bool hosted;
  52. // computed
  53. string raw_url;
  54. #if NET_2_0
  55. static readonly string root_web_config_path;
  56. static SimpleWorkerRequest ()
  57. {
  58. root_web_config_path = WebConfigurationManager.OpenWebConfiguration ("~").FilePath;
  59. }
  60. #endif
  61. //
  62. // Constructor used when the target application domain
  63. // was created with ApplicationHost.CreateApplicationHost
  64. //
  65. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  66. public SimpleWorkerRequest (string page, string query, TextWriter output)
  67. {
  68. this.page = page;
  69. this.query = query;
  70. this.output = output;
  71. app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
  72. app_physical_dir = HttpRuntime.AppDomainAppPath;
  73. hosted = true;
  74. InitializePaths ();
  75. }
  76. //
  77. // Creates a SimpleWorkerRequest that can be used from any AppDomain
  78. //
  79. // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
  80. //
  81. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  82. public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
  83. {
  84. this.page = page;
  85. this.query = query;
  86. this.output = output;
  87. app_virtual_dir = appVirtualDir;
  88. app_physical_dir = appPhysicalDir;
  89. InitializePaths ();
  90. }
  91. void InitializePaths ()
  92. {
  93. int idx = page.IndexOf ('/');
  94. if (idx >= 0) {
  95. path_info = page.Substring (idx);
  96. page = page.Substring (0, idx);
  97. } else {
  98. path_info = "";
  99. }
  100. }
  101. public override string MachineConfigPath {
  102. get {
  103. if (hosted) {
  104. string path = ICalls.GetMachineConfigPath ();
  105. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  106. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  107. }
  108. return path;
  109. }
  110. return null;
  111. }
  112. }
  113. public override string MachineInstallDirectory {
  114. get {
  115. if (hosted) {
  116. string path = ICalls.GetMachineInstallDirectory ();
  117. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  118. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  119. }
  120. return path;
  121. }
  122. return null;
  123. }
  124. }
  125. #if NET_2_0
  126. public override string RootWebConfigPath {
  127. get { return root_web_config_path; }
  128. }
  129. #endif
  130. public override void EndOfRequest ()
  131. {
  132. }
  133. public override void FlushResponse (bool finalFlush)
  134. {
  135. }
  136. public override string GetAppPath ()
  137. {
  138. return app_virtual_dir;
  139. }
  140. public override string GetAppPathTranslated ()
  141. {
  142. if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
  143. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
  144. }
  145. return app_physical_dir;
  146. }
  147. public override string GetFilePath ()
  148. {
  149. string result = UrlUtils.Combine (app_virtual_dir, page);
  150. if (result == "")
  151. return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/";
  152. return result;
  153. }
  154. public override string GetFilePathTranslated ()
  155. {
  156. string local_page;
  157. if (Path.DirectorySeparatorChar == '\\')
  158. local_page = page.Replace ('/', '\\');
  159. else
  160. local_page = page;
  161. string path = Path.Combine (app_physical_dir, local_page);
  162. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  163. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  164. }
  165. return path;
  166. }
  167. public override string GetHttpVerbName ()
  168. {
  169. return "GET";
  170. }
  171. public override string GetHttpVersion ()
  172. {
  173. return "HTTP/1.0";
  174. }
  175. public override string GetLocalAddress ()
  176. {
  177. return "127.0.0.1";
  178. }
  179. public override int GetLocalPort ()
  180. {
  181. return 80;
  182. }
  183. public override string GetPathInfo ()
  184. {
  185. return path_info;
  186. }
  187. public override string GetQueryString ()
  188. {
  189. return query;
  190. }
  191. public override string GetRawUrl ()
  192. {
  193. if (raw_url == null){
  194. string q = ((query == null || query == "") ? "" : "?" + query);
  195. raw_url = UrlUtils.Combine (app_virtual_dir, page);
  196. if (path_info != "") {
  197. raw_url += "/" + path_info + q;
  198. } else {
  199. raw_url += q;
  200. }
  201. }
  202. return raw_url;
  203. }
  204. public override string GetRemoteAddress ()
  205. {
  206. return "127.0.0.1";
  207. }
  208. public override int GetRemotePort ()
  209. {
  210. return 0;
  211. }
  212. public override string GetServerVariable (string name)
  213. {
  214. return "";
  215. }
  216. public override string GetUriPath ()
  217. {
  218. if (app_virtual_dir == "/")
  219. return app_virtual_dir + page + path_info;
  220. return app_virtual_dir + "/" + page + path_info;
  221. }
  222. public override IntPtr GetUserToken ()
  223. {
  224. return IntPtr.Zero;
  225. }
  226. public override string MapPath (string path)
  227. {
  228. if (!hosted)
  229. return null;
  230. if (path != null && path.Length == 0)
  231. return app_physical_dir;
  232. if (!path.StartsWith (app_virtual_dir))
  233. throw new ArgumentNullException ("path is not rooted in the virtual directory");
  234. string rest = path.Substring (app_virtual_dir.Length);
  235. if (rest.Length > 0 && rest [0] == '/')
  236. rest = rest.Substring (1);
  237. if (Path.DirectorySeparatorChar != '/') // for windows suport
  238. rest = rest.Replace ('/', Path.DirectorySeparatorChar);
  239. return Path.Combine (app_physical_dir, rest);
  240. }
  241. public override void SendKnownResponseHeader (int index, string value)
  242. {
  243. }
  244. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  245. {
  246. }
  247. public override void SendResponseFromFile (string filename, long offset, long length)
  248. {
  249. }
  250. public override void SendResponseFromMemory (byte [] data, int length)
  251. {
  252. output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
  253. }
  254. public override void SendStatus (int statusCode, string statusDescription)
  255. {
  256. }
  257. public override void SendUnknownResponseHeader (string name, string value)
  258. {
  259. }
  260. }
  261. }