2
0

SimpleWorkerRequest.cs 7.0 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.Configuration;
  35. using System.Web.UI;
  36. using System.Web.Util;
  37. namespace System.Web.Hosting {
  38. // CAS
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  41. // attributes
  42. [ComVisible (false)]
  43. public class SimpleWorkerRequest : HttpWorkerRequest {
  44. string page;
  45. string query;
  46. string app_virtual_dir;
  47. string app_physical_dir;
  48. string path_info;
  49. TextWriter output;
  50. bool hosted;
  51. // computed
  52. string raw_url;
  53. //
  54. // Constructor used when the target application domain
  55. // was created with ApplicationHost.CreateApplicationHost
  56. //
  57. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  58. public SimpleWorkerRequest (string page, string query, TextWriter output)
  59. {
  60. this.page = page;
  61. this.query = query;
  62. this.output = output;
  63. app_virtual_dir = HttpRuntime.AppDomainAppVirtualPath;
  64. app_physical_dir = HttpRuntime.AppDomainAppPath;
  65. hosted = true;
  66. }
  67. //
  68. // Creates a SimpleWorkerRequest that can be used from any AppDomain
  69. //
  70. // This is used for user instantiates HttpContext (my_SimpleWorkerRequest)
  71. //
  72. [SecurityPermission (SecurityAction.Demand, UnmanagedCode = true)]
  73. public SimpleWorkerRequest (string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
  74. {
  75. this.page = page;
  76. this.query = query;
  77. this.output = output;
  78. app_virtual_dir = appVirtualDir;
  79. app_physical_dir = appPhysicalDir;
  80. }
  81. public override string MachineConfigPath {
  82. get {
  83. if (hosted) {
  84. string path = ICalls.GetMachineConfigPath ();
  85. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  86. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  87. }
  88. return path;
  89. }
  90. return null;
  91. }
  92. }
  93. public override string MachineInstallDirectory {
  94. get {
  95. if (hosted) {
  96. string path = ICalls.GetMachineInstallDirectory ();
  97. if (SecurityManager.SecurityEnabled && (path != null) && (path.Length > 0)) {
  98. new FileIOPermission (FileIOPermissionAccess.PathDiscovery, path).Demand ();
  99. }
  100. return path;
  101. }
  102. return null;
  103. }
  104. }
  105. #if NET_2_0
  106. public override string RootWebConfigPath {
  107. get { return WebConfigurationManager.OpenWebConfiguration ("~").FilePath; }
  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. }