SimpleWorkerRequest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. namespace System.Web.Hosting
  13. {
  14. [MonoTODO("Implement security demands on the path usage functions (and review)")]
  15. public class SimpleWorkerRequest : HttpWorkerRequest
  16. {
  17. private string _Page;
  18. private string _Query;
  19. private string _PathInfo;
  20. private string _AppVirtualPath;
  21. private string _AppPhysicalPath;
  22. private string _AppInstallPath;
  23. private TextWriter _Output;
  24. private bool _HasInstallInfo;
  25. private SimpleWorkerRequest ()
  26. {
  27. }
  28. public SimpleWorkerRequest (string Page, string Query, TextWriter Output)
  29. {
  30. _Page = Page;
  31. ParsePathInfo ();
  32. _Query = Query;
  33. AppDomain current = AppDomain.CurrentDomain;
  34. _AppVirtualPath = current.GetData (".appPath").ToString ();
  35. _AppInstallPath = current.GetData (".hostingVirtualPath").ToString ();
  36. _AppPhysicalPath = CheckAndAddSlash (current.GetData (".hostingInstallDir").ToString ());
  37. _Output = Output;
  38. if (_AppPhysicalPath == null)
  39. throw new HttpException ("Invalid app domain");
  40. _HasInstallInfo = true;
  41. }
  42. public SimpleWorkerRequest (string AppVirtualPath,
  43. string AppPhysicalPath,
  44. string Page,
  45. string Query,
  46. TextWriter Output)
  47. {
  48. if (AppDomain.CurrentDomain.GetData (".appPath") == null)
  49. throw new HttpException ("Invalid app domain");
  50. _Page = Page;
  51. ParsePathInfo ();
  52. _Query = Query;
  53. _AppVirtualPath = AppVirtualPath;
  54. _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
  55. _Output = Output;
  56. _HasInstallInfo = false;
  57. }
  58. [MonoTODO("Implement security")]
  59. public override string MachineInstallDirectory
  60. {
  61. get {
  62. if (_HasInstallInfo)
  63. return _AppInstallPath;
  64. return null;
  65. }
  66. }
  67. [MonoTODO("Get config path from Web.Config class")]
  68. public override string MachineConfigPath
  69. {
  70. get {
  71. return "MachineConfigPath"; //FIXME
  72. }
  73. }
  74. public override void EndOfRequest ()
  75. {
  76. }
  77. public override void FlushResponse (bool finalFlush)
  78. {
  79. }
  80. public override string GetAppPath ()
  81. {
  82. return _AppVirtualPath;
  83. }
  84. public override string GetAppPathTranslated ()
  85. {
  86. return _AppPhysicalPath;
  87. }
  88. public override string GetFilePath ()
  89. {
  90. return CreatePath (false);
  91. }
  92. public override string GetFilePathTranslated ()
  93. {
  94. if (Path.DirectorySeparatorChar != '/')
  95. return _AppPhysicalPath.Replace ('/', Path.DirectorySeparatorChar);
  96. return _AppPhysicalPath;
  97. }
  98. public override string GetHttpVerbName ()
  99. {
  100. return "GET";
  101. }
  102. public override string GetHttpVersion ()
  103. {
  104. return "HTTP/1.0";
  105. }
  106. public override string GetLocalAddress ()
  107. {
  108. return "127.0.0.1";
  109. }
  110. public override int GetLocalPort ()
  111. {
  112. return 80;
  113. }
  114. public override string GetPathInfo ()
  115. {
  116. return (null != _PathInfo) ? _PathInfo : String.Empty;
  117. }
  118. public override string GetQueryString ()
  119. {
  120. return _Query;
  121. }
  122. public override string GetRawUrl ()
  123. {
  124. string path = CreatePath (true);
  125. if (null != _Query && _Query.Length > 0)
  126. return path + "?" + _Query;
  127. return path;
  128. }
  129. public override string GetRemoteAddress()
  130. {
  131. return "127.0.0.1";
  132. }
  133. public override int GetRemotePort()
  134. {
  135. return 0;
  136. }
  137. public override string GetServerVariable(string name)
  138. {
  139. return String.Empty;
  140. }
  141. public override string GetUriPath()
  142. {
  143. return CreatePath (true);
  144. }
  145. public override IntPtr GetUserToken()
  146. {
  147. return IntPtr.Zero;
  148. }
  149. public override string MapPath (string path)
  150. {
  151. string sPath = _AppPhysicalPath.Substring (0, _AppPhysicalPath.Length - 1);
  152. if (path != null && path.Length > 0 && path [0] != '/')
  153. return sPath;
  154. char sep = Path.DirectorySeparatorChar;
  155. if (path.StartsWith(_AppVirtualPath)) {
  156. if (sep == '/')
  157. return sPath + path.Substring (_AppVirtualPath.Length);
  158. else
  159. return sPath + path.Substring (_AppVirtualPath.Length).Replace ('/', '\\');
  160. }
  161. return null;
  162. }
  163. public override void SendKnownResponseHeader (int index, string value)
  164. {
  165. }
  166. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  167. {
  168. }
  169. public override void SendResponseFromFile (string filename, long offset, long length)
  170. {
  171. }
  172. public override void SendResponseFromMemory (byte [] data, int length)
  173. {
  174. _Output.Write (Encoding.Default.GetChars (data, 0, length));
  175. }
  176. public override void SendStatus(int statusCode, string statusDescription)
  177. {
  178. }
  179. public override void SendUnknownResponseHeader(string name, string value)
  180. {
  181. }
  182. // Create's a path string
  183. private string CheckAndAddSlash(string sPath)
  184. {
  185. if (null == sPath)
  186. return null;
  187. if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
  188. return sPath + Path.DirectorySeparatorChar;
  189. return sPath;
  190. }
  191. // Create's a path string
  192. private string CreatePath (bool bIncludePathInfo)
  193. {
  194. string sPath;
  195. if ("/" == _AppVirtualPath) {
  196. sPath = _AppVirtualPath + "/" + _Page;
  197. } else {
  198. sPath = "/" + _Page;
  199. }
  200. if (bIncludePathInfo && null != _PathInfo)
  201. return sPath + _PathInfo;
  202. return sPath;
  203. }
  204. // Parses out the string after / know as the "path info"
  205. private void ParsePathInfo ()
  206. {
  207. int iPos = _Page.IndexOf("/");
  208. if (iPos >= 0) {
  209. _PathInfo = _Page.Substring (iPos);
  210. _Page = _Page.Substring (0, iPos);
  211. }
  212. }
  213. }
  214. }