SimpleWorkerRequest.cs 7.1 KB

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