HttpServerUtility.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /**
  2. * Namespace: System.Web
  3. * Class: HttpServerUtility
  4. *
  5. * Authosr:
  6. * Wictor Wilén ([email protected])
  7. * Patrik Torstensson ([email protected])
  8. * Gonzalo Paniagua Javier ([email protected])
  9. *
  10. * (c) Wictor Wilén (2002)
  11. * (c) 2002 Patrik Torstensson
  12. * (c) 2003 Ximian, Inc. (http://www.ximian.com)
  13. *
  14. * (This log is no longer maintained)
  15. * ---------------------------------------
  16. * 2002-03-27 Wictor Started implementation
  17. * 2002-04-09 Patrik Added HttpContext constructor
  18. * 2002-04-10 Patrik Moved encoding to HttpUtility and
  19. * fixed all functions that used
  20. * HttpContext
  21. *
  22. */
  23. using System;
  24. using System.Collections.Specialized;
  25. using System.IO;
  26. using System.Text;
  27. using System.Web.Hosting;
  28. namespace System.Web
  29. {
  30. public sealed class HttpServerUtility
  31. {
  32. private static string _name;
  33. private HttpContext _Context;
  34. private HttpApplication _Application;
  35. internal HttpServerUtility (HttpContext Context)
  36. {
  37. _Context = Context;
  38. }
  39. internal HttpServerUtility (HttpApplication app)
  40. {
  41. _Application = app;
  42. }
  43. // Properties
  44. /// <summary>
  45. /// Gets the server's computer name.
  46. /// </summary>
  47. public string MachineName {
  48. get {
  49. if(_name == null)
  50. _name = Environment.MachineName;
  51. return _name;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets and sets the request time-out in seconds.
  56. /// </summary>
  57. [MonoTODO()]
  58. public int ScriptTimeout {
  59. get {
  60. throw new NotImplementedException ();
  61. }
  62. set {
  63. throw new NotImplementedException ();
  64. }
  65. }
  66. // Methods
  67. /// <summary>
  68. /// Clears the previous exception.
  69. /// </summary>
  70. public void ClearError ()
  71. {
  72. if (null != _Context) {
  73. _Context.ClearError ();
  74. return;
  75. }
  76. if (null != _Application) {
  77. _Application.ClearError ();
  78. }
  79. }
  80. /// <summary>
  81. /// Creates a server instance of a COM object identified by the object's Programmatic Identifier (ProgID).
  82. /// </summary>
  83. /// <param name="progID">The class or type of object to be instantiated. </param>
  84. /// <returns>The new object.</returns>
  85. public object CreateObject (string progID)
  86. {
  87. return CreateObject (Type.GetTypeFromProgID (progID));
  88. }
  89. /// <summary>
  90. /// Creates a server instance of a COM object identified by the object's type.
  91. /// </summary>
  92. /// <param name="type">A Type representing the object to create. </param>
  93. /// <returns>The new object.</returns>
  94. [MonoTODO()]
  95. public object CreateObject (Type type)
  96. {
  97. Object o;
  98. o = Activator.CreateInstance (type);
  99. // TODO: Call OnStartPage()
  100. return o;
  101. }
  102. /// <summary>
  103. /// Creates a server instance of a COM object identified by the object's class identifier (CLSID).
  104. /// </summary>
  105. /// <param name="clsid">The class identifier of the object to be instantiated. </param>
  106. /// <returns>The new object.</returns>
  107. public object CreateObjectFromClsid (string clsid)
  108. {
  109. Guid guid = new Guid (clsid);
  110. return CreateObject (Type.GetTypeFromCLSID (guid));
  111. }
  112. /// <summary>
  113. /// Executes a request to another page using the specified URL path to the page.
  114. /// </summary>
  115. /// <param name="path">The URL path of the new request. </param>
  116. public void Execute (string path)
  117. {
  118. Execute (path, null);
  119. }
  120. /// <summary>
  121. /// Executes a request to another page using the specified URL path to the page.
  122. /// A TextWriter captures output from the page.
  123. /// </summary>
  124. /// <param name="path">The URL path of the new request. </param>
  125. /// <param name="writer">The TextWriter to capture the output. </param>
  126. public void Execute (string path, TextWriter writer)
  127. {
  128. Execute (path, writer, false);
  129. }
  130. public void Execute (string path, TextWriter writer, bool preserveQuery)
  131. {
  132. if (path == null)
  133. throw new ArgumentNullException ("path");
  134. if (_Context == null)
  135. throw new HttpException ("No context available.");
  136. if (path.IndexOf (':') != -1)
  137. throw new ArgumentException ("Invalid path.");
  138. int qmark = path.IndexOf ('?');
  139. string query;
  140. if (qmark != -1) {
  141. query = path.Substring (qmark + 1);
  142. path = path.Substring (0, qmark);
  143. } else {
  144. query = "";
  145. }
  146. string filePath = _Context.Request.MapPath (path);
  147. HttpResponse response = _Context.Response;
  148. TextWriter output = writer;
  149. if (output == null)
  150. output = response.Output;
  151. HttpRequest request = _Context.Request;
  152. string oldFilePath = request.FilePath;
  153. request.SetFilePath (path);
  154. string oldQuery = request.QueryStringRaw;
  155. if (!preserveQuery) request.QueryStringRaw = query;
  156. IHttpHandler handler = _Context.ApplicationInstance.CreateHttpHandler (_Context,
  157. request.RequestType,
  158. path,
  159. filePath);
  160. TextWriter previous = null;
  161. try {
  162. previous = response.SetTextWriter (output);
  163. if (!(handler is IHttpAsyncHandler)) {
  164. handler.ProcessRequest (_Context);
  165. } else {
  166. IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
  167. IAsyncResult ar = asyncHandler.BeginProcessRequest (_Context, null, null);
  168. ar.AsyncWaitHandle.WaitOne ();
  169. asyncHandler.EndProcessRequest (ar);
  170. }
  171. } finally {
  172. request.SetFilePath (oldFilePath);
  173. request.QueryStringRaw = oldQuery;
  174. response.SetTextWriter (previous);
  175. }
  176. }
  177. /// <summary>
  178. /// Returns the previous exception.
  179. /// </summary>
  180. /// <returns>The previous exception that was thrown.</returns>
  181. public Exception GetLastError ()
  182. {
  183. if (_Context == null)
  184. return null;
  185. return _Context.Error;
  186. }
  187. /// <summary>
  188. /// Decodes an HTML-encoded string and returns the decoded string.
  189. /// </summary>
  190. /// <param name="s">The HTML string to decode. </param>
  191. /// <returns>The decoded text.</returns>
  192. public string HtmlDecode (string s)
  193. {
  194. return HttpUtility.HtmlDecode (s);
  195. }
  196. /// <summary>
  197. /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
  198. /// </summary>
  199. /// <param name="s">The HTML string to decode</param>
  200. /// <param name="output">The TextWriter output stream containing the decoded string. </param>
  201. public void HtmlDecode (string s, TextWriter output)
  202. {
  203. output.Write (HttpUtility.HtmlDecode (s));
  204. }
  205. /// <summary>
  206. /// HTML-encodes a string and returns the encoded string.
  207. /// </summary>
  208. /// <param name="s">The text string to encode. </param>
  209. /// <returns>The HTML-encoded text.</returns>
  210. public string HtmlEncode (string s)
  211. {
  212. return HttpUtility.HtmlEncode (s);
  213. }
  214. /// <summary>
  215. /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
  216. /// </summary>
  217. /// <param name="s">The string to encode. </param>
  218. /// <param name="output">The TextWriter output stream containing the encoded string. </param>
  219. public void HtmlEncode (string s, TextWriter output)
  220. {
  221. output.Write (HtmlEncode (s));
  222. }
  223. /// <summary>
  224. /// Returns the physical file path that corresponds to the specified virtual path on the Web server.
  225. /// </summary>
  226. /// <param name="path">The virtual path on the Web server. </param>
  227. /// <returns>The physical file path that corresponds to path.</returns>
  228. public string MapPath (string path)
  229. {
  230. if (null == _Context)
  231. throw new HttpException ("MapPath is not available");
  232. return _Context.Request.MapPath (path);
  233. }
  234. /// <summary>
  235. /// Terminates execution of the current page and begins execution of a new page using the specified
  236. /// URL path to the page.
  237. /// </summary>
  238. /// <param name="path">The URL path of the new page on the server to execute. </param>
  239. public void Transfer (string path)
  240. {
  241. Transfer (path, true);
  242. }
  243. /// <summary>
  244. /// Terminates execution of the current page and begins execution of a new page using the specified
  245. /// URL path to the page. Specifies whether to clear the QueryString and Form collections.
  246. /// </summary>
  247. /// <param name="path">The URL path of the new page on the server to execute. </param>
  248. /// <param name="preserveForm">If true, the QueryString and Form collections are preserved. If false,
  249. /// they are cleared. The default is false. </param>
  250. public void Transfer (string path, bool preserveForm)
  251. {
  252. HttpValueCollection oldForm = null;
  253. if (!preserveForm) {
  254. oldForm = _Context.Request.Form as HttpValueCollection;
  255. _Context.Request.SetForm (new HttpValueCollection ());
  256. }
  257. Execute (path, null, preserveForm);
  258. if (!preserveForm)
  259. _Context.Request.SetForm (oldForm);
  260. _Context.Response.End ();
  261. }
  262. /// <summary>
  263. /// URL-decodes a string and returns the decoded string.
  264. /// </summary>
  265. /// <param name="s">The text string to decode. </param>
  266. /// <returns>The decoded text.</returns>
  267. /// <remarks>
  268. /// Post/html encoding @ ftp://ftp.isi.edu/in-notes/rfc1866.txt
  269. /// Uncomment the line marked with RFC1738 to get pure RFC1738
  270. /// and it will also consider the RFC1866 (ftp://ftp.isi.edu/in-notes/rfc1866.txt)
  271. /// `application/x-www-form-urlencoded' format
  272. /// </remarks>
  273. public string UrlDecode (string s)
  274. {
  275. return HttpUtility.UrlDecode (s);
  276. }
  277. /// <summary>
  278. /// Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.
  279. /// </summary>
  280. /// <param name="s"></param>
  281. /// <param name="output"></param>
  282. public void UrlDecode (string s, TextWriter output)
  283. {
  284. output.Write (UrlDecode (s));
  285. }
  286. /// <summary>
  287. /// URL-encodes a string and returns the encoded string.
  288. /// </summary>
  289. /// <param name="s">The text to URL-encode. </param>
  290. /// <returns>The URL encoded text.</returns>
  291. public string UrlEncode (string s)
  292. {
  293. return HttpUtility.UrlEncode (s);
  294. }
  295. /// <summary>
  296. /// URL encodes a string and sends the resulting output to a TextWriter output stream.
  297. /// </summary>
  298. /// <param name="s">The text string to encode. </param>
  299. /// <param name="output">The TextWriter output stream containing the encoded string. </param>
  300. public void UrlEncode (string s, TextWriter output)
  301. {
  302. output.Write (UrlEncode (s));
  303. }
  304. /// <summary>
  305. /// URL-encodes the path portion of a URL string and returns the encoded string.
  306. /// </summary>
  307. /// <param name="s">The text to URL-encode.</param>
  308. /// <returns>The URL encoded text.</returns>
  309. /// <remarks>Does not do any browser specific adjustments, just encode everything</remarks>
  310. public string UrlPathEncode (string s)
  311. {
  312. // find the path portion (?)
  313. int idx = s.IndexOf ("?");
  314. string s2 = s.Substring (0, idx-1);
  315. s2 = UrlEncode (s2);
  316. s2 += s.Substring (idx);
  317. return s2;
  318. }
  319. }
  320. }