SimpleWorkerRequest.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // System.Web.Hosting.SimpleWorkerRequest.cs
  3. //
  4. // Authors:
  5. // Patrik Torstensson ([email protected])
  6. // (class signature from Bob Smith <[email protected]> (C) )
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  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;
  30. using System.IO;
  31. using System.Text;
  32. using System.Runtime.InteropServices;
  33. using System.Web.Util;
  34. namespace System.Web.Hosting
  35. {
  36. [MonoTODO("Implement security demands on the path usage functions (and review)")]
  37. [ComVisible (false)]
  38. public class SimpleWorkerRequest : HttpWorkerRequest
  39. {
  40. private string _Page;
  41. private string _Query;
  42. private string _PathInfo = String.Empty;
  43. private string _AppVirtualPath;
  44. private string _AppPhysicalPath;
  45. private string _AppInstallPath;
  46. private TextWriter _Output;
  47. private bool _HasInstallInfo;
  48. private SimpleWorkerRequest ()
  49. {
  50. }
  51. public SimpleWorkerRequest (string Page, string Query, TextWriter Output)
  52. {
  53. _Page = Page;
  54. _Query = Query;
  55. AppDomain current = AppDomain.CurrentDomain;
  56. object o = current.GetData (".appPath");
  57. if (o == null)
  58. throw new HttpException ("Cannot get .appPath");
  59. _AppPhysicalPath = o.ToString ();
  60. o = current.GetData (".hostingVirtualPath");
  61. if (o == null)
  62. throw new HttpException ("Cannot get .hostingVirtualPath");
  63. _AppVirtualPath = o.ToString ();
  64. o = current.GetData (".hostingInstallDir");
  65. if (o == null)
  66. throw new HttpException ("Cannot get .hostingInstallDir");
  67. _AppInstallPath = o.ToString ();
  68. _Output = Output;
  69. if (_AppPhysicalPath == null)
  70. throw new HttpException ("Invalid app domain");
  71. _HasInstallInfo = true;
  72. ExtractPagePathInfo();
  73. }
  74. public SimpleWorkerRequest (string AppVirtualPath,
  75. string AppPhysicalPath,
  76. string Page,
  77. string Query,
  78. TextWriter Output)
  79. {
  80. if (AppDomain.CurrentDomain.GetData (".appPath") == null)
  81. throw new HttpException ("Invalid app domain");
  82. _Page = Page;
  83. _Query = Query;
  84. _AppVirtualPath = AppVirtualPath;
  85. _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
  86. _Output = Output;
  87. _HasInstallInfo = false;
  88. ExtractPagePathInfo();
  89. }
  90. [MonoTODO("Implement security")]
  91. public override string MachineInstallDirectory
  92. {
  93. get {
  94. if (_HasInstallInfo)
  95. return _AppInstallPath;
  96. return ICalls.GetMachineInstallDirectory ();
  97. }
  98. }
  99. public override string MachineConfigPath
  100. {
  101. get { return ICalls.GetMachineConfigPath (); }
  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 _AppVirtualPath;
  112. }
  113. public override string GetAppPathTranslated ()
  114. {
  115. return _AppPhysicalPath;
  116. }
  117. public override string GetFilePath ()
  118. {
  119. return CreatePath (false);
  120. }
  121. public override string GetFilePathTranslated ()
  122. {
  123. string page = _Page;
  124. if (Path.DirectorySeparatorChar != '/')
  125. page = _Page.Replace ('/', Path.DirectorySeparatorChar);
  126. if (page [0] == Path.DirectorySeparatorChar)
  127. page = page.Substring (1);
  128. return (Path.Combine (_AppPhysicalPath, page));
  129. }
  130. public override string GetHttpVerbName ()
  131. {
  132. return "GET";
  133. }
  134. public override string GetHttpVersion ()
  135. {
  136. return "HTTP/1.0";
  137. }
  138. public override string GetLocalAddress ()
  139. {
  140. return "127.0.0.1";
  141. }
  142. public override int GetLocalPort ()
  143. {
  144. return 80;
  145. }
  146. public override string GetPathInfo ()
  147. {
  148. return _PathInfo;
  149. }
  150. public override string GetQueryString ()
  151. {
  152. return _Query;
  153. }
  154. public override string GetRawUrl ()
  155. {
  156. string path = CreatePath (true);
  157. if (null != _Query && _Query.Length > 0)
  158. return path + "?" + _Query;
  159. return path;
  160. }
  161. public override string GetRemoteAddress()
  162. {
  163. return "127.0.0.1";
  164. }
  165. public override int GetRemotePort()
  166. {
  167. return 0;
  168. }
  169. public override string GetServerVariable(string name)
  170. {
  171. return String.Empty;
  172. }
  173. public override string GetUriPath()
  174. {
  175. return CreatePath (true);
  176. }
  177. public override IntPtr GetUserToken()
  178. {
  179. return IntPtr.Zero;
  180. }
  181. public override string MapPath (string path)
  182. {
  183. string sPath = _AppPhysicalPath.Substring (0, _AppPhysicalPath.Length - 1);
  184. if (path != null && path.Length > 0 && path [0] != '/')
  185. return sPath;
  186. char sep = Path.DirectorySeparatorChar;
  187. if (path.StartsWith(_AppVirtualPath)) {
  188. if (sep == '/')
  189. return sPath + path.Substring (_AppVirtualPath.Length);
  190. else
  191. return sPath + path.Substring (_AppVirtualPath.Length).Replace ('/', sep);
  192. }
  193. return null;
  194. }
  195. public override void SendKnownResponseHeader (int index, string value)
  196. {
  197. }
  198. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  199. {
  200. }
  201. public override void SendResponseFromFile (string filename, long offset, long length)
  202. {
  203. }
  204. public override void SendResponseFromMemory (byte [] data, int length)
  205. {
  206. _Output.Write (Encoding.Default.GetChars (data, 0, length));
  207. }
  208. public override void SendStatus(int statusCode, string statusDescription)
  209. {
  210. }
  211. public override void SendUnknownResponseHeader(string name, string value)
  212. {
  213. }
  214. // Create's a path string
  215. private string CheckAndAddSlash(string sPath)
  216. {
  217. if (null == sPath)
  218. return null;
  219. if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
  220. return sPath + Path.DirectorySeparatorChar;
  221. return sPath;
  222. }
  223. // Create's a path string
  224. private string CreatePath (bool bIncludePathInfo)
  225. {
  226. string sPath = Path.Combine (_AppVirtualPath, _Page);
  227. if (bIncludePathInfo)
  228. {
  229. sPath += _PathInfo;
  230. }
  231. return sPath;
  232. }
  233. // "The extra path information, as given by the client. In
  234. // other words, scripts can be accessed by their virtual
  235. // pathname, followed by extra information at the end of this
  236. // path. The extra information is sent as PATH_INFO."
  237. private void ExtractPagePathInfo ()
  238. {
  239. if (_Page == null || _Page == String.Empty)
  240. return;
  241. string FullPath = GetFilePathTranslated ();
  242. int PathInfoLength = 0;
  243. string LastFile = String.Empty;
  244. while (PathInfoLength < _Page.Length) {
  245. if (LastFile.Length > 0) {
  246. // increase it by the length of the file plus
  247. // a "/"
  248. //
  249. PathInfoLength += LastFile.Length + 1;
  250. }
  251. if (File.Exists (FullPath) == true)
  252. break;
  253. if (Directory.Exists (FullPath) == true) {
  254. PathInfoLength -= (LastFile.Length + 1);
  255. break;
  256. }
  257. LastFile = Path.GetFileName (FullPath);
  258. FullPath = Path.GetDirectoryName (FullPath);
  259. }
  260. if (PathInfoLength <= 0 || PathInfoLength > _Page.Length)
  261. return;
  262. _PathInfo = _Page.Substring (_Page.Length - PathInfoLength);
  263. _Page = _Page.Substring (0, _Page.Length - PathInfoLength);
  264. }
  265. }
  266. }