SimpleWorkerRequest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. public override string RootWebConfigPath {
  119. get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
  120. }
  121. public override void EndOfRequest ()
  122. {
  123. }
  124. public override void FlushResponse (bool finalFlush)
  125. {
  126. }
  127. public override string GetAppPath ()
  128. {
  129. return app_virtual_dir;
  130. }
  131. public override string GetAppPathTranslated ()
  132. {
  133. if (SecurityManager.SecurityEnabled && (app_physical_dir != null) && (app_physical_dir.Length > 0)) {
  134. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, app_physical_dir).Demand ();
  135. }
  136. return app_physical_dir;
  137. }
  138. public override string GetFilePath ()
  139. {
  140. string result = UrlUtils.Combine (app_virtual_dir, page);
  141. if (result == "")
  142. return app_virtual_dir == "/" ? app_virtual_dir : app_virtual_dir + "/";
  143. return result;
  144. }
  145. public override string GetFilePathTranslated ()
  146. {
  147. string local_page;
  148. if (Path.DirectorySeparatorChar == '\\')
  149. local_page = page.Replace ('/', '\\');
  150. else
  151. local_page = page;
  152. string path = Path.Combine (app_physical_dir, local_page);
  153. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  154. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  155. }
  156. return path;
  157. }
  158. public override string GetHttpVerbName ()
  159. {
  160. return "GET";
  161. }
  162. public override string GetHttpVersion ()
  163. {
  164. return "HTTP/1.0";
  165. }
  166. public override string GetLocalAddress ()
  167. {
  168. return "127.0.0.1";
  169. }
  170. public override int GetLocalPort ()
  171. {
  172. return 80;
  173. }
  174. public override string GetPathInfo ()
  175. {
  176. return path_info;
  177. }
  178. public override string GetQueryString ()
  179. {
  180. return query;
  181. }
  182. public override string GetRawUrl ()
  183. {
  184. if (raw_url == null){
  185. string q = ((query == null || query == "") ? "" : "?" + query);
  186. raw_url = UrlUtils.Combine (app_virtual_dir, page);
  187. if (path_info != "") {
  188. raw_url += "/" + path_info + q;
  189. } else {
  190. raw_url += q;
  191. }
  192. }
  193. return raw_url;
  194. }
  195. public override string GetRemoteAddress ()
  196. {
  197. return "127.0.0.1";
  198. }
  199. public override int GetRemotePort ()
  200. {
  201. return 0;
  202. }
  203. public override string GetServerVariable (string name)
  204. {
  205. return "";
  206. }
  207. public override string GetUriPath ()
  208. {
  209. if (app_virtual_dir == "/")
  210. return app_virtual_dir + page + path_info;
  211. return app_virtual_dir + "/" + page + path_info;
  212. }
  213. public override IntPtr GetUserToken ()
  214. {
  215. return IntPtr.Zero;
  216. }
  217. public override string MapPath (string path)
  218. {
  219. if (!hosted)
  220. return null;
  221. if (path != null && path.Length == 0)
  222. return app_physical_dir;
  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. if (Path.DirectorySeparatorChar != '/') // for windows suport
  229. rest = rest.Replace ('/', Path.DirectorySeparatorChar);
  230. return Path.Combine (app_physical_dir, rest);
  231. }
  232. public override void SendKnownResponseHeader (int index, string value)
  233. {
  234. }
  235. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  236. {
  237. }
  238. public override void SendResponseFromFile (string filename, long offset, long length)
  239. {
  240. }
  241. public override void SendResponseFromMemory (byte [] data, int length)
  242. {
  243. output.Write (System.Text.Encoding.Default.GetChars (data, 0, length));
  244. }
  245. public override void SendStatus (int statusCode, string statusDescription)
  246. {
  247. }
  248. public override void SendUnknownResponseHeader (string name, string value)
  249. {
  250. }
  251. }
  252. }