SimpleWorkerRequest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // System.Web.Hosting.SimpleWorkerRequest.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 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.IO;
  31. using System.Runtime.InteropServices;
  32. using System.Security;
  33. using System.Security.Permissions;
  34. using System.Web.UI;
  35. using System.Web.Util;
  36. namespace System.Web.Hosting {
  37. // CAS
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. // attributes
  41. [ComVisible (false)]
  42. public class SimpleWorkerRequest : HttpWorkerRequest {
  43. string page;
  44. string query;
  45. string app_virtual_dir;
  46. string app_physical_dir;
  47. string path_info;
  48. TextWriter output;
  49. bool hosted;
  50. // computed
  51. string raw_url;
  52. //
  53. // Constructor used when the target application domain
  54. // was created with ApplicationHost.CreateApplicationHost
  55. //
  56. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  57. public SimpleWorkerRequest (string page, string query, TextWriter output)
  58. {
  59. this.page = page;
  60. this.query = query;
  61. this.output = output;
  62. app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
  63. app_physical_dir = HttpRuntime.AppDomainAppPath;
  64. hosted = true;
  65. }
  66. //
  67. // Creates a SimpleWorkerRequest that can be used from any AppDomain
  68. //
  69. // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
  70. //
  71. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  72. public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
  73. {
  74. this.page = page;
  75. this.query = query;
  76. this.output = output;
  77. app_virtual_dir = appVirtualDir;
  78. app_physical_dir = appPhysicalDir;
  79. }
  80. public override string MachineConfigPath {
  81. get {
  82. if (hosted) {
  83. string path = ICalls.GetMachineConfigPath ();
  84. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  85. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  86. }
  87. return path;
  88. }
  89. return null;
  90. }
  91. }
  92. public override string MachineInstallDirectory {
  93. get {
  94. if (hosted) {
  95. string path = ICalls.GetMachineInstallDirectory ();
  96. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  97. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  98. }
  99. return path;
  100. }
  101. return null;
  102. }
  103. }
  104. #if NET_2_0
  105. [MonoTODO ("Gets the virtual path to the root web.config")]
  106. public override string RootWebConfigPath {
  107. get { return null; }
  108. }
  109. #endif
  110. public override void EndOfRequest ()
  111. {
  112. }
  113. public override void FlushResponse (bool finalFlush)
  114. {
  115. }
  116. public override string GetAppPath ()
  117. {
  118. return app_virtual_dir;
  119. }
  120. public override string GetAppPathTranslated ()
  121. {
  122. if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
  123. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
  124. }
  125. return app_physical_dir;
  126. }
  127. public override string GetFilePath ()
  128. {
  129. return Path.Combine (app_virtual_dir, page);
  130. }
  131. public override string GetFilePathTranslated ()
  132. {
  133. string local_page;
  134. if (Path.DirectorySeparatorChar == '\\')
  135. local_page = page.Replace ('/', '\\');
  136. else
  137. local_page = page;
  138. string path = Path.Combine (app_physical_dir, local_page);
  139. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  140. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  141. }
  142. return path;
  143. }
  144. public override string GetHttpVerbName ()
  145. {
  146. return "GET";
  147. }
  148. public override string GetHttpVersion ()
  149. {
  150. return "HTTP/1.0";
  151. }
  152. public override string GetLocalAddress ()
  153. {
  154. return "127.0.0.1";
  155. }
  156. public override int GetLocalPort ()
  157. {
  158. return 80;
  159. }
  160. public override string GetPathInfo ()
  161. {
  162. if (path_info == null) {
  163. int idx = page.IndexOf ('/');
  164. if (idx >= 0) {
  165. path_info = page.Substring (idx);
  166. } else {
  167. path_info = "";
  168. }
  169. }
  170. return path_info;
  171. }
  172. public override string GetQueryString ()
  173. {
  174. return query;
  175. }
  176. public override string GetRawUrl ()
  177. {
  178. if (raw_url == null){
  179. string q = ((query == null || query == "") ? "" : "?" + query);
  180. raw_url = Path.Combine (app_virtual_dir, page) + q;
  181. }
  182. return raw_url;
  183. }
  184. public override string GetRemoteAddress ()
  185. {
  186. return "127.0.0.1";
  187. }
  188. public override int GetRemotePort ()
  189. {
  190. return 0;
  191. }
  192. public override string GetServerVariable (string name)
  193. {
  194. return "";
  195. }
  196. public override string GetUriPath ()
  197. {
  198. if (app_virtual_dir == "/")
  199. return app_virtual_dir + page;
  200. return app_virtual_dir + "/" + page;
  201. }
  202. public override IntPtr GetUserToken ()
  203. {
  204. return IntPtr.Zero;
  205. }
  206. public override string MapPath (string path)
  207. {
  208. if (!hosted)
  209. return null;
  210. if (!path.StartsWith (app_virtual_dir))
  211. throw new ArgumentNullException ("path is not rooted in the virtual directory");
  212. string rest = path.Substring (app_virtual_dir.Length);
  213. if (rest.Length > 0 && rest [0] == '/')
  214. rest = rest.Substring (1);
  215. return Path.Combine (app_physical_dir, rest);
  216. }
  217. public override void SendKnownResponseHeader (int index, string value)
  218. {
  219. }
  220. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  221. {
  222. }
  223. public override void SendResponseFromFile (string filename, long offset, long length)
  224. {
  225. }
  226. public override void SendResponseFromMemory (byte [] data, int length)
  227. {
  228. output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
  229. }
  230. public override void SendStatus (int statusCode, string statusDescription)
  231. {
  232. }
  233. public override void SendUnknownResponseHeader (string name, string value)
  234. {
  235. }
  236. }
  237. }