SimpleWorkerRequest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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. _AppInstallPath = o.ToString ();
  42. o = current.GetData (".hostingInstallDir");
  43. if (o == null)
  44. throw new HttpException ("Cannot get .hostingInstallDir");
  45. _AppVirtualPath = CheckAndAddSlash (o.ToString ());
  46. _Output = Output;
  47. if (_AppPhysicalPath == null)
  48. throw new HttpException ("Invalid app domain");
  49. _HasInstallInfo = true;
  50. }
  51. public SimpleWorkerRequest (string AppVirtualPath,
  52. string AppPhysicalPath,
  53. string Page,
  54. string Query,
  55. TextWriter Output)
  56. {
  57. if (AppDomain.CurrentDomain.GetData (".appPath") == null)
  58. throw new HttpException ("Invalid app domain");
  59. _Page = Page;
  60. ParsePathInfo ();
  61. _Query = Query;
  62. _AppVirtualPath = AppVirtualPath;
  63. _AppPhysicalPath = CheckAndAddSlash (AppPhysicalPath);
  64. _Output = Output;
  65. _HasInstallInfo = false;
  66. }
  67. [MonoTODO("Implement security")]
  68. public override string MachineInstallDirectory
  69. {
  70. get {
  71. if (_HasInstallInfo)
  72. return _AppInstallPath;
  73. return null;
  74. }
  75. }
  76. [MonoTODO("Get config path from Web.Config class")]
  77. public override string MachineConfigPath
  78. {
  79. get {
  80. return "MachineConfigPath"; //FIXME
  81. }
  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. if (Path.DirectorySeparatorChar != '/')
  104. return _AppPhysicalPath + _Page.Replace ('/', Path.DirectorySeparatorChar);
  105. return _AppPhysicalPath + _Page;
  106. }
  107. public override string GetHttpVerbName ()
  108. {
  109. return "GET";
  110. }
  111. public override string GetHttpVersion ()
  112. {
  113. return "HTTP/1.0";
  114. }
  115. public override string GetLocalAddress ()
  116. {
  117. return "127.0.0.1";
  118. }
  119. public override int GetLocalPort ()
  120. {
  121. return 80;
  122. }
  123. public override string GetPathInfo ()
  124. {
  125. return (null != _PathInfo) ? _PathInfo : String.Empty;
  126. }
  127. public override string GetQueryString ()
  128. {
  129. return _Query;
  130. }
  131. public override string GetRawUrl ()
  132. {
  133. string path = CreatePath (true);
  134. if (null != _Query && _Query.Length > 0)
  135. return path + "?" + _Query;
  136. return path;
  137. }
  138. public override string GetRemoteAddress()
  139. {
  140. return "127.0.0.1";
  141. }
  142. public override int GetRemotePort()
  143. {
  144. return 0;
  145. }
  146. public override string GetServerVariable(string name)
  147. {
  148. return String.Empty;
  149. }
  150. public override string GetUriPath()
  151. {
  152. return CreatePath (true);
  153. }
  154. public override IntPtr GetUserToken()
  155. {
  156. return IntPtr.Zero;
  157. }
  158. public override string MapPath (string path)
  159. {
  160. string sPath = _AppPhysicalPath.Substring (0, _AppPhysicalPath.Length - 1);
  161. if (path != null && path.Length > 0 && path [0] != '/')
  162. return sPath;
  163. char sep = Path.DirectorySeparatorChar;
  164. if (path.StartsWith(_AppVirtualPath)) {
  165. if (sep == '/')
  166. return sPath + path.Substring (_AppVirtualPath.Length);
  167. else
  168. return sPath + path.Substring (_AppVirtualPath.Length).Replace ('/', '\\');
  169. }
  170. return null;
  171. }
  172. public override void SendKnownResponseHeader (int index, string value)
  173. {
  174. }
  175. public override void SendResponseFromFile (IntPtr handle, long offset, long length)
  176. {
  177. }
  178. public override void SendResponseFromFile (string filename, long offset, long length)
  179. {
  180. }
  181. public override void SendResponseFromMemory (byte [] data, int length)
  182. {
  183. _Output.Write (Encoding.Default.GetChars (data, 0, length));
  184. }
  185. public override void SendStatus(int statusCode, string statusDescription)
  186. {
  187. }
  188. public override void SendUnknownResponseHeader(string name, string value)
  189. {
  190. }
  191. // Create's a path string
  192. private string CheckAndAddSlash(string sPath)
  193. {
  194. if (null == sPath)
  195. return null;
  196. if (!sPath.EndsWith ("" + Path.DirectorySeparatorChar))
  197. return sPath + Path.DirectorySeparatorChar;
  198. return sPath;
  199. }
  200. // Create's a path string
  201. private string CreatePath (bool bIncludePathInfo)
  202. {
  203. string sPath;
  204. if ("/" != _AppVirtualPath)
  205. sPath = "/" + _Page;
  206. else
  207. sPath = String.Empty;
  208. if (bIncludePathInfo && null != _PathInfo)
  209. return sPath + _PathInfo;
  210. return sPath;
  211. }
  212. // Parses out the string after / known as the "path info"
  213. private void ParsePathInfo ()
  214. {
  215. /* int iPos = _Page.LastIndexOf("/");
  216. if (iPos >= 0) {
  217. _PathInfo = _Page.Substring (iPos);
  218. _Page = _Page.Substring (0, iPos);
  219. }*/
  220. }
  221. }
  222. }