3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web
24 * Class: HttpServerUtility
27 * Wictor Wilén (wictor@ibizkit.se)
28 * Patrik Torstensson (patrik.torstensson@labs2.com)
29 * Gonzalo Paniagua Javier (gonzalo@ximian.com)
31 * (c) Wictor Wilén (2002)
32 * (c) 2002 Patrik Torstensson
33 * (c) 2003 Ximian, Inc. (http://www.ximian.com)
35 * (This log is no longer maintained)
36 * ---------------------------------------
37 * 2002-03-27 Wictor Started implementation
38 * 2002-04-09 Patrik Added HttpContext constructor
39 * 2002-04-10 Patrik Moved encoding to HttpUtility and
40 * fixed all functions that used
45 using System.Collections.Specialized;
48 using System.Web.Hosting;
49 using System.Web.Util;
53 public sealed class HttpServerUtility
55 private static string _name;
57 private HttpContext _Context;
58 private HttpApplication _Application;
60 internal HttpServerUtility (HttpContext Context)
65 internal HttpServerUtility (HttpApplication app)
74 /// Gets the server's computer name.
76 public string MachineName {
79 _name = Environment.MachineName;
85 public int ScriptTimeout {
87 return (int) _Context.ConfigTimeout.TotalSeconds;
91 throw new ArgumentOutOfRangeException ("value");
93 _Context.ConfigTimeout = new TimeSpan (0, 0, value);
100 /// Clears the previous exception.
102 public void ClearError ()
104 if (null != _Context) {
105 _Context.ClearError ();
109 if (null != _Application) {
110 _Application.ClearError ();
116 /// Creates a server instance of a COM object identified by the object's Programmatic Identifier (ProgID).
118 /// <param name="progID">The class or type of object to be instantiated. </param>
119 /// <returns>The new object.</returns>
120 public object CreateObject (string progID)
122 return CreateObject (Type.GetTypeFromProgID (progID));
127 /// Creates a server instance of a COM object identified by the object's type.
129 /// <param name="type">A Type representing the object to create. </param>
130 /// <returns>The new object.</returns>
131 public object CreateObject (Type type)
133 return Activator.CreateInstance (type);
138 /// Creates a server instance of a COM object identified by the object's class identifier (CLSID).
140 /// <param name="clsid">The class identifier of the object to be instantiated. </param>
141 /// <returns>The new object.</returns>
142 public object CreateObjectFromClsid (string clsid)
144 Guid guid = new Guid (clsid);
145 return CreateObject (Type.GetTypeFromCLSID (guid));
150 /// Executes a request to another page using the specified URL path to the page.
152 /// <param name="path">The URL path of the new request. </param>
153 public void Execute (string path)
155 Execute (path, null);
160 /// Executes a request to another page using the specified URL path to the page.
161 /// A TextWriter captures output from the page.
163 /// <param name="path">The URL path of the new request. </param>
164 /// <param name="writer">The TextWriter to capture the output. </param>
165 public void Execute (string path, TextWriter writer)
167 Execute (path, writer, false);
175 void Execute (string path, TextWriter writer, bool preserveQuery)
178 throw new ArgumentNullException ("path");
180 if (_Context == null)
181 throw new HttpException ("No context available.");
183 if (path.IndexOf (':') != -1)
184 throw new ArgumentException ("Invalid path.");
186 int qmark = path.IndexOf ('?');
189 query = path.Substring (qmark + 1);
190 path = path.Substring (0, qmark);
195 string filePath = _Context.Request.MapPath (path);
196 HttpResponse response = _Context.Response;
197 TextWriter output = writer;
199 output = response.Output;
201 HttpRequest request = _Context.Request;
202 string oldFilePath = request.FilePath;
203 request.SetFilePath (UrlUtils.Combine (_Context.Request.BaseVirtualDir, path));
204 string oldQuery = request.QueryStringRaw;
206 if (!preserveQuery) request.QueryStringRaw = query;
207 IHttpHandler handler = _Context.ApplicationInstance.CreateHttpHandler (_Context,
211 TextWriter previous = null;
213 previous = response.SetTextWriter (output);
214 if (!(handler is IHttpAsyncHandler)) {
215 handler.ProcessRequest (_Context);
217 IHttpAsyncHandler asyncHandler = (IHttpAsyncHandler) handler;
218 IAsyncResult ar = asyncHandler.BeginProcessRequest (_Context, null, null);
219 ar.AsyncWaitHandle.WaitOne ();
220 asyncHandler.EndProcessRequest (ar);
223 request.SetFilePath (oldFilePath);
224 request.QueryStringRaw = oldQuery;
225 response.SetTextWriter (previous);
231 /// Returns the previous exception.
233 /// <returns>The previous exception that was thrown.</returns>
234 public Exception GetLastError ()
236 if (_Context == null)
239 return _Context.Error;
245 /// Decodes an HTML-encoded string and returns the decoded string.
247 /// <param name="s">The HTML string to decode. </param>
248 /// <returns>The decoded text.</returns>
249 public string HtmlDecode (string s)
251 return HttpUtility.HtmlDecode (s);
256 /// Decodes an HTML-encoded string and sends the resulting output to a TextWriter output stream.
258 /// <param name="s">The HTML string to decode</param>
259 /// <param name="output">The TextWriter output stream containing the decoded string. </param>
260 public void HtmlDecode (string s, TextWriter output)
262 output.Write (HttpUtility.HtmlDecode (s));
266 /// HTML-encodes a string and returns the encoded string.
268 /// <param name="s">The text string to encode. </param>
269 /// <returns>The HTML-encoded text.</returns>
270 public string HtmlEncode (string s)
272 return HttpUtility.HtmlEncode (s);
276 /// HTML-encodes a string and sends the resulting output to a TextWriter output stream.
278 /// <param name="s">The string to encode. </param>
279 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
280 public void HtmlEncode (string s, TextWriter output)
282 output.Write (HtmlEncode (s));
287 /// Returns the physical file path that corresponds to the specified virtual path on the Web server.
289 /// <param name="path">The virtual path on the Web server. </param>
290 /// <returns>The physical file path that corresponds to path.</returns>
291 public string MapPath (string path)
293 if (null == _Context)
294 throw new HttpException ("MapPath is not available");
296 return _Context.Request.MapPath (path);
300 /// Terminates execution of the current page and begins execution of a new page using the specified
301 /// URL path to the page.
303 /// <param name="path">The URL path of the new page on the server to execute. </param>
304 public void Transfer (string path)
306 Transfer (path, true);
310 /// Terminates execution of the current page and begins execution of a new page using the specified
311 /// URL path to the page. Specifies whether to clear the QueryString and Form collections.
313 /// <param name="path">The URL path of the new page on the server to execute. </param>
314 /// <param name="preserveForm">If true, the QueryString and Form collections are preserved. If false,
315 /// they are cleared. The default is false. </param>
316 public void Transfer (string path, bool preserveForm)
318 HttpValueCollection oldForm = null;
320 oldForm = _Context.Request.Form as HttpValueCollection;
321 _Context.Request.SetForm (new HttpValueCollection ());
324 Execute (path, null, preserveForm);
326 _Context.Request.SetForm (oldForm);
328 _Context.Response.End ();
332 /// URL-decodes a string and returns the decoded string.
334 /// <param name="s">The text string to decode. </param>
335 /// <returns>The decoded text.</returns>
337 /// Post/html encoding @ ftp://ftp.isi.edu/in-notes/rfc1866.txt
338 /// Uncomment the line marked with RFC1738 to get pure RFC1738
339 /// and it will also consider the RFC1866 (ftp://ftp.isi.edu/in-notes/rfc1866.txt)
340 /// `application/x-www-form-urlencoded' format
342 public string UrlDecode (string s)
344 return HttpUtility.UrlDecode (s);
348 /// Decodes an HTML string received in a URL and sends the resulting output to a TextWriter output stream.
350 /// <param name="s"></param>
351 /// <param name="output"></param>
352 public void UrlDecode (string s, TextWriter output)
355 output.Write (UrlDecode (s));
359 /// URL-encodes a string and returns the encoded string.
361 /// <param name="s">The text to URL-encode. </param>
362 /// <returns>The URL encoded text.</returns>
363 public string UrlEncode (string s)
365 return HttpUtility.UrlEncode (s);
369 /// URL encodes a string and sends the resulting output to a TextWriter output stream.
371 /// <param name="s">The text string to encode. </param>
372 /// <param name="output">The TextWriter output stream containing the encoded string. </param>
373 public void UrlEncode (string s, TextWriter output)
376 output.Write (UrlEncode (s));
380 /// URL-encodes the path portion of a URL string and returns the encoded string.
382 /// <param name="s">The text to URL-encode.</param>
383 /// <returns>The URL encoded text.</returns>
384 /// <remarks>Does not do any browser specific adjustments, just encode everything</remarks>
385 public string UrlPathEncode (string s)
390 int idx = s.IndexOf ("?");
393 s2 = s.Substring (0, idx-1);
394 s2 = UrlEncode (s2) + s.Substring (idx);