SimpleWorkerRequest.cs 6.5 KB

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