SimpleWorkerRequest.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. public override void EndOfRequest ()
  105. {
  106. }
  107. public override void FlushResponse (bool finalFlush)
  108. {
  109. }
  110. public override string GetAppPath ()
  111. {
  112. return app_virtual_dir;
  113. }
  114. public override string GetAppPathTranslated ()
  115. {
  116. if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
  117. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
  118. }
  119. return app_physical_dir;
  120. }
  121. public override string GetFilePath ()
  122. {
  123. return Path.Combine (app_virtual_dir, page);
  124. }
  125. public override string GetFilePathTranslated ()
  126. {
  127. string local_page;
  128. if (Path.DirectorySeparatorChar == '\\')
  129. local_page = page.Replace ('/', '\\');
  130. else
  131. local_page = page;
  132. string path = Path.Combine (app_physical_dir, local_page);
  133. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  134. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  135. }
  136. return path;
  137. }
  138. public override string GetHttpVerbName ()
  139. {
  140. return "GET";
  141. }
  142. public override string GetHttpVersion ()
  143. {
  144. return "HTTP/1.0";
  145. }
  146. public override string GetLocalAddress ()
  147. {
  148. return "127.0.0.1";
  149. }
  150. public override int GetLocalPort ()
  151. {
  152. return 80;
  153. }
  154. public override string GetPathInfo ()
  155. {
  156. if (path_info == null) {
  157. int idx = page.IndexOf ('/');
  158. if (idx >= 0) {
  159. path_info = page.Substring (idx);
  160. } else {
  161. path_info = "";
  162. }
  163. }
  164. return path_info;
  165. }
  166. public override string GetQueryString ()
  167. {
  168. return query;
  169. }
  170. public override string GetRawUrl ()
  171. {
  172. if (raw_url == null){
  173. string q = ((query == null || query == "") ? "" : "?" + query);
  174. raw_url = Path.Combine (app_virtual_dir, page) + q;
  175. }
  176. return raw_url;
  177. }
  178. public override string GetRemoteAddress ()
  179. {
  180. return "127.0.0.1";
  181. }
  182. public override int GetRemotePort ()
  183. {
  184. return 0;
  185. }
  186. public override string GetServerVariable (string name)
  187. {
  188. return "";
  189. }
  190. public override string GetUriPath ()
  191. {
  192. if (app_virtual_dir == "/")
  193. return app_virtual_dir + page;
  194. return app_virtual_dir + "/" + page;
  195. }
  196. public override IntPtr GetUserToken ()
  197. {
  198. return IntPtr.Zero;
  199. }
  200. public override string MapPath (string path)
  201. {
  202. if (!hosted)
  203. return null;
  204. if (!path.StartsWith (app_virtual_dir))
  205. throw new ArgumentNullException ("path is not rooted in the virtual directory");
  206. string rest = path.Substring (app_virtual_dir.Length);
  207. if (rest.Length > 0 && rest [0] == '/')
  208. rest = rest.Substring (1);
  209. return Path.Combine (app_physical_dir, rest);
  210. }
  211. public override void SendKnownResponseHeader (int index, string value)
  212. {
  213. }
  214. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  215. {
  216. }
  217. public override void SendResponseFromFile (string filename, long offset, long length)
  218. {
  219. }
  220. public override void SendResponseFromMemory (byte [] data, int length)
  221. {
  222. output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
  223. }
  224. public override void SendStatus (int statusCode, string statusDescription)
  225. {
  226. }
  227. public override void SendUnknownResponseHeader (string name, string value)
  228. {
  229. }
  230. }
  231. }