SimpleWorkerRequest.cs 7.0 KB

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