2
0

SimpleWorkerRequest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // System.Web.Hosting
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. // (class signature from Bob Smith <[email protected]> (C) )
  7. //
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. namespace System.Web.Hosting {
  12. [MonoTODO("Implement security demands on the path usage functions (and review)")]
  13. public class SimpleWorkerRequest : HttpWorkerRequest {
  14. private string _Page;
  15. private string _Query;
  16. private string _PathInfo;
  17. private string _AppVirtualPath;
  18. private string _AppPhysicalPath;
  19. private string _AppInstallPath;
  20. private TextWriter _Output;
  21. private bool _HasInstallInfo;
  22. private SimpleWorkerRequest() {
  23. }
  24. public SimpleWorkerRequest(string Page, string Query, TextWriter Output) {
  25. _Page = Page;
  26. _Query = Query;
  27. _AppVirtualPath = System.Threading.Thread.GetDomain().GetData(".ASP.Net.App.VirtualPath").ToString();
  28. _AppInstallPath = AppDomain.CurrentDomain.GetData(".ASP.Net.App.InstallPath").ToString();
  29. _AppPhysicalPath = CheckAndAddSlash(AppDomain.CurrentDomain.GetData(".ASP.Net.App.Path").ToString());
  30. _Output = Output;
  31. if (_AppPhysicalPath == null) {
  32. // needs to be in a initialized app domain
  33. throw new HttpException("Invalid app domain");
  34. }
  35. _HasInstallInfo = true;
  36. }
  37. public SimpleWorkerRequest(string AppVirtualPath, string AppPhysicalPath, string Page, string Query, TextWriter Output) {
  38. if (AppDomain.CurrentDomain.GetData(".ASP.Net.App.Path") == null) {
  39. // needs to be in a initialized app domain
  40. throw new HttpException("Invalid app domain");
  41. }
  42. _Page = Page;
  43. _Query = Query;
  44. _AppVirtualPath = AppVirtualPath;
  45. _AppPhysicalPath = CheckAndAddSlash(AppPhysicalPath);
  46. _Output = Output;
  47. _HasInstallInfo = true;
  48. }
  49. [MonoTODO("Implement security")]
  50. public override string MachineInstallDirectory {
  51. get {
  52. if (_HasInstallInfo) {
  53. return _AppInstallPath;
  54. }
  55. return null;
  56. }
  57. }
  58. [MonoTODO("Get config path from Web.Config class")]
  59. public override string MachineConfigPath {
  60. get {
  61. return MachineConfigPath;
  62. }
  63. }
  64. public override void EndOfRequest() {
  65. }
  66. public override void FlushResponse(bool finalFlush) {
  67. _Output.Flush();
  68. }
  69. public override string GetAppPath() {
  70. return _AppVirtualPath;
  71. }
  72. [MonoTODO("Implement security")]
  73. public override string GetAppPathTranslated() {
  74. return _AppPhysicalPath;
  75. }
  76. public override string GetFilePath() {
  77. return CreatePath(false);
  78. }
  79. [MonoTODO("Implement security")]
  80. public override string GetFilePathTranslated() {
  81. return _AppPhysicalPath + _Page.Replace('/', '\\');
  82. }
  83. public override string GetHttpVerbName() {
  84. return "GET";
  85. }
  86. public override string GetHttpVersion() {
  87. return "HTTP/1.0";
  88. }
  89. public override string GetLocalAddress() {
  90. return "127.0.0.1";
  91. }
  92. public override int GetLocalPort() {
  93. return 80;
  94. }
  95. [MonoTODO("Implement security")]
  96. public override string GetPathInfo() {
  97. if (null != _PathInfo) {
  98. return _PathInfo;
  99. }
  100. return System.String.Empty;
  101. }
  102. public override string GetQueryString() {
  103. return _Query;
  104. }
  105. public override string GetRawUrl() {
  106. if (null != _Query && _Query.Length > 0) {
  107. return CreatePath(true) + "?" + _Query;
  108. }
  109. return CreatePath(true);
  110. }
  111. public override string GetRemoteAddress() {
  112. return "127.0.0.1";
  113. }
  114. public override int GetRemotePort() {
  115. return 0;
  116. }
  117. public override string GetServerVariable(string name) {
  118. return System.String.Empty;
  119. }
  120. public override string GetUriPath() {
  121. return CreatePath(true);
  122. }
  123. public override IntPtr GetUserToken() {
  124. return System.IntPtr.Zero;
  125. }
  126. public override string MapPath(string path) {
  127. string sPath = _AppPhysicalPath.Substring(0, _AppPhysicalPath.Length - 1);
  128. if (path != null && path.Length > 0 && path != "/") {
  129. return sPath;
  130. }
  131. if (path.StartsWith(_AppVirtualPath)) {
  132. return sPath + path.Substring(_AppVirtualPath.Length).Replace('/', '\\');
  133. }
  134. return null;
  135. }
  136. public override void SendKnownResponseHeader(int index, string value) {
  137. }
  138. public override void SendResponseFromFile(IntPtr handle, long offset, long length) {
  139. }
  140. public override void SendResponseFromFile(string filename, long offset, long length) {
  141. }
  142. public override void SendResponseFromMemory(byte[] data, int length) {
  143. _Output.Write(Encoding.Default.GetChars(data, 0, length));
  144. }
  145. public override void SendStatus(int statusCode, string statusDescription) {
  146. }
  147. public override void SendUnknownResponseHeader(string name, string value) {
  148. }
  149. // Create's a path string
  150. private string CheckAndAddSlash(string sPath) {
  151. if (null == sPath) {
  152. return null;
  153. }
  154. if (!sPath.EndsWith("\\")) {
  155. return sPath + "\\";
  156. }
  157. return sPath;
  158. }
  159. // Create's a path string
  160. private string CreatePath(bool bIncludePathInfo) {
  161. string sPath;
  162. if ("/" == _AppVirtualPath) {
  163. sPath = _AppVirtualPath + "/" + _Page;
  164. } else {
  165. sPath = "/" + _Page;
  166. }
  167. if (bIncludePathInfo && null != _PathInfo) {
  168. return sPath + _PathInfo;
  169. }
  170. return sPath;
  171. }
  172. // Parses out the string after / know as the "path info"
  173. private void ParsePathInfo() {
  174. int iPos = _Page.IndexOf("/");
  175. if (iPos >= 0) {
  176. _PathInfo = _Page.Substring(iPos);
  177. _Page = _Page.Substring(0, iPos);
  178. }
  179. }
  180. }
  181. }