SimpleWorkerRequest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //
  55. // Constructor used when the target application domain
  56. // was created with ApplicationHost.CreateApplicationHost
  57. //
  58. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  59. public SimpleWorkerRequest (string page, string query, TextWriter output)
  60. {
  61. this.page = page;
  62. this.query = query;
  63. this.output = output;
  64. app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
  65. app_physical_dir = HttpRuntime.AppDomainAppPath;
  66. hosted = true;
  67. InitializePaths ();
  68. }
  69. //
  70. // Creates a SimpleWorkerRequest that can be used from any AppDomain
  71. //
  72. // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
  73. //
  74. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  75. public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
  76. {
  77. this.page = page;
  78. this.query = query;
  79. this.output = output;
  80. app_virtual_dir = appVirtualDir;
  81. app_physical_dir = appPhysicalDir;
  82. InitializePaths ();
  83. }
  84. void InitializePaths ()
  85. {
  86. int idx = page.IndexOf ('/');
  87. if (idx >= 0) {
  88. path_info = page.Substring (idx);
  89. page = page.Substring (0, idx);
  90. } else {
  91. path_info = "";
  92. }
  93. }
  94. public override string MachineConfigPath {
  95. get {
  96. if (hosted) {
  97. string path = ICalls.GetMachineConfigPath ();
  98. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  99. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  100. }
  101. return path;
  102. }
  103. return null;
  104. }
  105. }
  106. public override string MachineInstallDirectory {
  107. get {
  108. if (hosted) {
  109. string path = ICalls.GetMachineInstallDirectory ();
  110. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  111. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  112. }
  113. return path;
  114. }
  115. return null;
  116. }
  117. }
  118. #if NET_2_0
  119. public override string RootWebConfigPath {
  120. get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
  121. }
  122. #endif
  123. public override void EndOfRequest ()
  124. {
  125. }
  126. public override void FlushResponse (bool finalFlush)
  127. {
  128. }
  129. public override string GetAppPath ()
  130. {
  131. return app_virtual_dir;
  132. }
  133. public override string GetAppPathTranslated ()
  134. {
  135. if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
  136. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
  137. }
  138. return app_physical_dir;
  139. }
  140. public override string GetFilePath ()
  141. {
  142. string result = UrlUtils.Combine (app_virtual_dir, page);
  143. if (result == "")
  144. return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/";
  145. return result;
  146. }
  147. public override string GetFilePathTranslated ()
  148. {
  149. string local_page;
  150. if (Path.DirectorySeparatorChar == '\\')
  151. local_page = page.Replace ('/', '\\');
  152. else
  153. local_page = page;
  154. string path = Path.Combine (app_physical_dir, local_page);
  155. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  156. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  157. }
  158. return path;
  159. }
  160. public override string GetHttpVerbName ()
  161. {
  162. return "GET";
  163. }
  164. public override string GetHttpVersion ()
  165. {
  166. return "HTTP/1.0";
  167. }
  168. public override string GetLocalAddress ()
  169. {
  170. return "127.0.0.1";
  171. }
  172. public override int GetLocalPort ()
  173. {
  174. return 80;
  175. }
  176. public override string GetPathInfo ()
  177. {
  178. return path_info;
  179. }
  180. public override string GetQueryString ()
  181. {
  182. return query;
  183. }
  184. public override string GetRawUrl ()
  185. {
  186. if (raw_url == null){
  187. string q = ((query == null || query == "") ? "" : "?" + query);
  188. raw_url = UrlUtils.Combine (app_virtual_dir, page);
  189. if (path_info != "") {
  190. raw_url += "/" + path_info + q;
  191. } else {
  192. raw_url += q;
  193. }
  194. }
  195. return raw_url;
  196. }
  197. public override string GetRemoteAddress ()
  198. {
  199. return "127.0.0.1";
  200. }
  201. public override int GetRemotePort ()
  202. {
  203. return 0;
  204. }
  205. public override string GetServerVariable (string name)
  206. {
  207. return "";
  208. }
  209. public override string GetUriPath ()
  210. {
  211. if (app_virtual_dir == "/")
  212. return app_virtual_dir + page + path_info;
  213. return app_virtual_dir + "/" + page + path_info;
  214. }
  215. public override IntPtr GetUserToken ()
  216. {
  217. return IntPtr.Zero;
  218. }
  219. public override string MapPath (string path)
  220. {
  221. if (!hosted)
  222. return null;
  223. if (!path.StartsWith (app_virtual_dir))
  224. throw new ArgumentNullException ("path is not rooted in the virtual directory");
  225. string rest = path.Substring (app_virtual_dir.Length);
  226. if (rest.Length > 0 && rest [0] == '/')
  227. rest = rest.Substring (1);
  228. return Path.Combine (app_physical_dir, rest);
  229. }
  230. public override void SendKnownResponseHeader (int index, string value)
  231. {
  232. }
  233. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  234. {
  235. }
  236. public override void SendResponseFromFile (string filename, long offset, long length)
  237. {
  238. }
  239. public override void SendResponseFromMemory (byte [] data, int length)
  240. {
  241. output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
  242. }
  243. public override void SendStatus (int statusCode, string statusDescription)
  244. {
  245. }
  246. public override void SendUnknownResponseHeader (string name, string value)
  247. {
  248. }
  249. }
  250. }